summaryrefslogtreecommitdiffstats
blob: 8452e36d5dda39572cb4cf68997795e421e2b056 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.filetransfer;

import com.isode.stroke.network.Connection;
import com.isode.stroke.network.HostAddressPort;
import com.isode.stroke.base.ByteArray;
import com.isode.stroke.base.SafeByteArray;
import com.isode.stroke.signals.SignalConnection;
import com.isode.stroke.signals.Signal1;
import com.isode.stroke.signals.Signal;
import com.isode.stroke.signals.Slot;
import com.isode.stroke.signals.Slot1;

import java.util.logging.Logger;

public class SOCKS5BytestreamServerSession  extends SOCKS5AbstractBytestreamSession  {

	private Connection connection;
	private SOCKS5BytestreamRegistry bytestreams;
	private ByteArray unprocessedData;
	private State state;
	private int chunkSize;
	private String streamID = "";
	private ReadBytestream readBytestream;
	private WriteBytestream writeBytestream;
	private boolean waitingForData;

	private SignalConnection disconnectedConnection;
	private SignalConnection dataReadConnection;
	private SignalConnection dataWrittenConnection;
	private SignalConnection dataAvailableConnection;
	private Logger logger_ = Logger.getLogger(this.getClass().getName());

	public enum State {
		Initial,
		WaitingForAuthentication,
		WaitingForRequest,
		ReadyForTransfer,
		ReadingData,
		WritingData,
		Finished
	};

	public SOCKS5BytestreamServerSession(Connection connection, SOCKS5BytestreamRegistry bytestreams) {
		this.connection = connection;
		this.bytestreams = bytestreams;
		this.state = State.Initial;
		this.chunkSize = 131072;
		this.waitingForData = false;
		disconnectedConnection = connection.onDisconnected.connect(new Slot1<Connection.Error>() {
			@Override
			public void call(Connection.Error e) {
				handleDisconnected(e);
			}
		});
	}

	public void setChunkSize(int chunkSize) {
		this.chunkSize = chunkSize;
	}

	public void start() {
		logger_.fine("\n");
		dataReadConnection = connection.onDataRead.connect(new Slot1<SafeByteArray>() {
			@Override
			public void call(SafeByteArray s) {
				handleDataRead(s);
			}
		});
		state = State.WaitingForAuthentication;
	}

	public void stop() {
		finish(false);
	}

	public void startSending(ReadBytestream stream) {
		if (!State.ReadyForTransfer.equals(state)) { logger_.fine("Not ready for transfer!\n"); return; }
		readBytestream = stream;
		state = State.WritingData;
		dataAvailableConnection = readBytestream.onDataAvailable.connect(new Slot() {
			@Override
			public void call() {
				handleDataAvailable();
			}
		});
		dataWrittenConnection = connection.onDataWritten.connect(new Slot() {
			@Override
			public void call() {
				sendData();
			}
		});
		sendData();
	}

	public void startReceiving(WriteBytestream stream) {
		if (!State.ReadyForTransfer.equals(state)) { logger_.fine("Not ready for transfer!\n"); return; }

		writeBytestream = stream;
		state = State.ReadingData;
		writeBytestream.write(unprocessedData);
		// onBytesReceived(unprocessedData.getSize());
		unprocessedData.clear();
	}

	public HostAddressPort getAddressPort() {
		return connection.getLocalAddress();
	}

	

	public String getStreamID() {
		return streamID;
	}

	private void finish(boolean error) {
		logger_.fine(error + " " + state + "\n");
		if (State.Finished.equals(state)) {
			return;
		}

		disconnectedConnection.disconnect();
		dataReadConnection.disconnect();
		dataWrittenConnection.disconnect();
		dataAvailableConnection.disconnect();
		readBytestream = null;
		state = State.Finished;
		if (error) {
			onFinished.emit(new FileTransferError(FileTransferError.Type.PeerError));
		} else {
			onFinished.emit(null);
		}
	}

	private void process() {
		if (State.WaitingForAuthentication.equals(state)) {
			if (unprocessedData.getSize() >= 2) {
				int authCount = unprocessedData.getData()[1];
				int i = 2;
				while (i < 2 + authCount && i < unprocessedData.getSize()) {
					// Skip authentication mechanism
					++i;
				}
				if (i == 2 + authCount) {
					// Authentication message is complete
					if (i != unprocessedData.getSize()) {
						logger_.fine("Junk after authentication mechanism\n");
					}
					unprocessedData.clear();
					connection.write(new SafeByteArray(new byte[]{0x05, 0x00}));
					state = State.WaitingForRequest;
				}
			}
		}
		else if (State.WaitingForRequest.equals(state)) {
			if (unprocessedData.getSize() >= 5) {
				ByteArray requestID = new ByteArray();
				int i = 5;
				int hostnameSize = unprocessedData.getData()[4];
				while (i < 5 + hostnameSize && i < unprocessedData.getSize()) {
					requestID.append(unprocessedData.getData()[i]);
					++i;
				}
				// Skip the port: 2 byte large, one already skipped. Add one for comparison with size
				i += 2;
				if (i <= unprocessedData.getSize()) {
					if (i != unprocessedData.getSize()) {
						logger_.fine("Junk after authentication mechanism\n");
					}
					unprocessedData.clear();
					streamID = requestID.toString();
					boolean hasBytestream = bytestreams.hasBytestream(streamID);
					SafeByteArray result = new SafeByteArray("0x05");
					result.append(hasBytestream ? (byte)0x0 : (byte)0x4);
					result.append(new ByteArray(new byte[]{0x00, 0x03}));
					result.append(Integer.toString(requestID.getSize()));
					result.append(requestID.append(new ByteArray(new byte[]{0x00, 0x00})));
					if (!hasBytestream) {
						logger_.fine("Readstream or Wrtiestream with ID " + streamID + " not found!\n");
						connection.write(result);
						finish(true);
					}
					else {
						logger_.fine("Found stream. Sent OK.\n");
						connection.write(result);
						state = State.ReadyForTransfer;
					}
				}
			}
		}
	}

	private void handleDataRead(SafeByteArray data) {
		if (!State.ReadingData.equals(state)) {
			unprocessedData.append(data);
			process();
		} else {
			writeBytestream.write(new ByteArray(data));
			// onBytesReceived(data.size());
		}
	}

	private void handleDisconnected(final Connection.Error error) {
		logger_.fine((error != null ? (error.equals(Connection.Error.ReadError) ? "Read Error" : "Write Error") : "No Error") + "\n");
		finish(error != null ? true : false);
	}

	private void handleDataAvailable() {
		if (waitingForData) {
			sendData();
		}
	}

	private void sendData() {
		if (!readBytestream.isFinished()) {
			//try {
				SafeByteArray dataToSend = new SafeByteArray(readBytestream.read((chunkSize)));
				if (!dataToSend.isEmpty()) {
					connection.write(dataToSend);
					onBytesSent.emit(dataToSend.getSize());
					waitingForData = false;
				}
				else {
					waitingForData = true;
				}
			//}
			//catch (BytestreamException e) {
			//	finish(true);
			//}
		}
		else {
			finish(false);
		}
	}
}