aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/jingle/JingleSocks5Transport.java
blob: 6480251fdce34a32933b2b79d8075e94c0ec3ecd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package de.thedevstack.conversationsplus.xmpp.jingle;

import android.os.PowerManager;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import de.thedevstack.android.logcat.Logging;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.utils.StreamUtil;
import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.utils.CryptoHelper;
import de.thedevstack.conversationsplus.utils.SocksSocketFactory;

public class JingleSocks5Transport extends JingleTransport {
	private JingleCandidate candidate;
	private JingleConnection connection;
	private String destination;
	private OutputStream outputStream;
	private InputStream inputStream;
	private boolean isEstablished = false;
	private boolean activated = false;
	protected Socket socket;

	public JingleSocks5Transport(JingleConnection jingleConnection,
			JingleCandidate candidate) {
		this.candidate = candidate;
		this.connection = jingleConnection;
		try {
			MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
			StringBuilder destBuilder = new StringBuilder();
			destBuilder.append(jingleConnection.getSessionId());
			if (candidate.isOurs()) {
				destBuilder.append(jingleConnection.getAccount().getJid());
				destBuilder.append(jingleConnection.getCounterPart());
			} else {
				destBuilder.append(jingleConnection.getCounterPart());
				destBuilder.append(jingleConnection.getAccount().getJid());
			}
			mDigest.reset();
			this.destination = CryptoHelper.bytesToHex(mDigest
					.digest(destBuilder.toString().getBytes()));
		} catch (NoSuchAlgorithmException e) {

		}
	}

	public void connect(final OnTransportConnected callback) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					socket = new Socket();
					SocketAddress address = new InetSocketAddress(candidate.getHost(),candidate.getPort());
					socket.connect(address,Config.SOCKET_TIMEOUT * 1000);

					inputStream = socket.getInputStream();
					outputStream = socket.getOutputStream();
					SocksSocketFactory.createSocksConnection(socket,destination,0);
					isEstablished = true;
					callback.established();
				} catch (UnknownHostException e) {
					callback.failed();
				} catch (IOException e) {
					callback.failed();
				}
			}
		}).start();

	}

	public void send(final DownloadableFile file, final OnFileTransmissionStatusChanged callback) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				InputStream fileInputStream = null;
				final PowerManager.WakeLock wakeLock = ConversationsPlusApplication.createPartialWakeLock("jingle_send_"+connection.getSessionId());
				try {
					wakeLock.acquire();
					MessageDigest digest = MessageDigest.getInstance("SHA-1");
					digest.reset();
					fileInputStream = connection.getFileInputStream();
					if (fileInputStream == null) {
						Logging.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": could not create input stream");
						callback.onFileTransferAborted();
						return;
					}
					long size = file.getExpectedSize();
					long transmitted = 0;
					int count;
					byte[] buffer = new byte[8192];
					while ((count = fileInputStream.read(buffer)) > 0) {
						outputStream.write(buffer, 0, count);
						digest.update(buffer, 0, count);
						transmitted += count;
						connection.updateProgress((int) ((((double) transmitted) / size) * 100));
					}
					outputStream.flush();
					file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
					if (callback != null) {
						callback.onFileTransmitted(file);
					}
				} catch (FileNotFoundException e) {
					Logging.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": "+e.getMessage());
					callback.onFileTransferAborted();
				} catch (IOException e) {
					Logging.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": "+e.getMessage());
					callback.onFileTransferAborted();
				} catch (NoSuchAlgorithmException e) {
					Logging.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": "+e.getMessage());
					callback.onFileTransferAborted();
				} finally {
					StreamUtil.close(fileInputStream);
					wakeLock.release();
				}
			}
		}).start();

	}

	public void receive(final DownloadableFile file, final OnFileTransmissionStatusChanged callback) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				OutputStream fileOutputStream = null;
				final PowerManager.WakeLock wakeLock = ConversationsPlusApplication.createPartialWakeLock("jingle_receive_"+connection.getSessionId());
				try {
					wakeLock.acquire();
					MessageDigest digest = MessageDigest.getInstance("SHA-1");
					digest.reset();
					//inputStream.skip(45);
					socket.setSoTimeout(30000);
					file.getParentFile().mkdirs();
					file.createNewFile();
					fileOutputStream = connection.getFileOutputStream();
					if (fileOutputStream == null) {
						callback.onFileTransferAborted();
						Logging.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": could not create output stream");
						return;
					}
					double size = file.getExpectedSize();
					long remainingSize = file.getExpectedSize();
					byte[] buffer = new byte[8192];
					int count;
					while (remainingSize > 0) {
						count = inputStream.read(buffer);
						if (count == -1) {
							callback.onFileTransferAborted();
							Logging.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": file ended prematurely with "+remainingSize+" bytes remaining");
							return;
						} else {
							fileOutputStream.write(buffer, 0, count);
							digest.update(buffer, 0, count);
							remainingSize -= count;
						}
						connection.updateProgress((int) (((size - remainingSize) / size) * 100));
					}
					fileOutputStream.flush();
					fileOutputStream.close();
					file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
					callback.onFileTransmitted(file);
				} catch (FileNotFoundException e) {
					Logging.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": "+e.getMessage());
					callback.onFileTransferAborted();
				} catch (IOException e) {
					Logging.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": "+e.getMessage());
					callback.onFileTransferAborted();
				} catch (NoSuchAlgorithmException e) {
					Logging.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": "+e.getMessage());
					callback.onFileTransferAborted();
				} finally {
					wakeLock.release();
					StreamUtil.close(fileOutputStream);
					StreamUtil.close(inputStream);
				}
			}
		}).start();
	}

	public boolean isProxy() {
		return this.candidate.getType() == JingleCandidate.TYPE_PROXY;
	}

	public boolean needsActivation() {
		return (this.isProxy() && !this.activated);
	}

	public void disconnect() {
		StreamUtil.close(inputStream);
		StreamUtil.close(outputStream);
		StreamUtil.close(socket);
	}

	public boolean isEstablished() {
		return this.isEstablished;
	}

	public JingleCandidate getCandidate() {
		return this.candidate;
	}

	public void setActivated(boolean activated) {
		this.activated = activated;
	}
}