-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathSessionThread.java
More file actions
440 lines (388 loc) · 13.3 KB
/
SessionThread.java
File metadata and controls
440 lines (388 loc) · 13.3 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
Copyright 2009 David Revell
This file is part of SwiFTP.
SwiFTP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SwiFTP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SwiFTP. If not, see <http://www.gnu.org/licenses/>.
*/
package org.swiftp;
import net.micode.fileexplorer.FTPServerService;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import android.util.Log;
public class SessionThread extends Thread {
protected boolean shouldExit = false;
protected Socket cmdSocket;
protected MyLog myLog = new MyLog(getClass().getName());
protected ByteBuffer buffer = ByteBuffer.allocate(Defaults
.getInputBufferSize());
protected boolean pasvMode = false;
protected boolean binaryMode = false;
protected Account account = new Account();
protected boolean authenticated = false;
protected File workingDir = Globals.getPrimaryDir();
// protected ServerSocket dataServerSocket = null;
protected Socket dataSocket = null;
// protected FTPServerService service;
protected File renameFrom = null;
// protected InetAddress outDataDest = null;
// protected int outDataPort = 20; // 20 is the default ftp-data port
protected DataSocketFactory dataSocketFactory;
OutputStream dataOutputStream = null;
private boolean sendWelcomeBanner;
protected String encoding = Defaults.SESSION_ENCODING;
protected Source source;
int authFails = 0;
public enum Source {LOCAL, PROXY}; // where did this connection come from?
public static int MAX_AUTH_FAILS = 3;
/**
* Used when we get a PORT command to open up an outgoing socket.
*
* @return
*/
// public void setPortSocket(InetAddress dest, int port) {
// myLog.l(Log.DEBUG, "Setting PORT dest to " +
// dest.getHostAddress() + " port " + port);
// outDataDest = dest;
// outDataPort = port;
// }
/**
* Sends a string over the already-established data socket
*
* @param string
* @return Whether the send completed successfully
*/
public boolean sendViaDataSocket(String string) {
try {
byte[] bytes = string.getBytes(encoding);
myLog.d("Using data connection encoding: " + encoding);
return sendViaDataSocket(bytes, bytes.length);
} catch (UnsupportedEncodingException e) {
myLog.l(Log.ERROR, "Unsupported encoding for data socket send");
return false;
}
}
public boolean sendViaDataSocket(byte[] bytes, int len) {
return sendViaDataSocket(bytes, 0, len);
}
/**
* Sends a byte array over the already-established data socket
*
* @param bytes
* @param len
* @return
*/
public boolean sendViaDataSocket(byte[] bytes, int start, int len) {
if (dataOutputStream == null) {
myLog.l(Log.INFO, "Can't send via null dataOutputStream");
return false;
}
if (len == 0) {
return true; // this isn't an "error"
}
try {
dataOutputStream.write(bytes, start, len);
} catch (IOException e) {
myLog.l(Log.INFO, "Couldn't write output stream for data socket");
myLog.l(Log.INFO, e.toString());
return false;
}
dataSocketFactory.reportTraffic(len);
return true;
}
/**
* Received some bytes from the data socket, which is assumed to already be
* connected. The bytes are placed in the given array, and the number of
* bytes successfully read is returned.
*
* @param bytes
* Where to place the input bytes
* @return >0 if successful which is the number of bytes read, -1 if no
* bytes remain to be read, -2 if the data socket was not connected,
* 0 if there was a read error
*/
public int receiveFromDataSocket(byte[] buf) {
int bytesRead;
if (dataSocket == null) {
myLog.l(Log.INFO, "Can't receive from null dataSocket");
return -2;
}
if (!dataSocket.isConnected()) {
myLog.l(Log.INFO, "Can't receive from unconnected socket");
return -2;
}
InputStream in;
try {
in = dataSocket.getInputStream();
// If the read returns 0 bytes, the stream is not yet
// closed, but we just want to read again.
while ((bytesRead = in.read(buf, 0, buf.length)) == 0) {
}
if (bytesRead == -1) {
// If InputStream.read returns -1, there are no bytes
// remaining, so we return 0.
return -1;
}
} catch (IOException e) {
myLog.l(Log.INFO, "Error reading data socket");
return 0;
}
dataSocketFactory.reportTraffic(bytesRead);
return bytesRead;
}
/**
* Called when we receive a PASV command.
*
* @return Whether the necessary initialization was successful.
*/
public int onPasv() {
return dataSocketFactory.onPasv();
}
/**
* Called when we receive a PORT command.
*
* @return Whether the necessary initialization was successful.
*/
public boolean onPort(InetAddress dest, int port) {
return dataSocketFactory.onPort(dest, port);
}
public InetAddress getDataSocketPasvIp() {
// When the client sends PASV, our reply will contain the address and port
// of the data connection that the client should connect to. For this purpose
// we always use the same IP address that the command socket is using.
return cmdSocket.getLocalAddress();
// The old code, not totally correct.
// return dataSocketFactory.getPasvIp();
}
// public int getDataSocketPort() {
// return dataSocketFactory.getPortNumber();
// }
/**
* Will be called by (e.g.) CmdSTOR, CmdRETR, CmdLIST, etc. when they are
* about to start actually doing IO over the data socket.
*
* @return
*/
public boolean startUsingDataSocket() {
try {
dataSocket = dataSocketFactory.onTransfer();
if (dataSocket == null) {
myLog.l(Log.INFO,
"dataSocketFactory.onTransfer() returned null");
return false;
}
dataOutputStream = dataSocket.getOutputStream();
return true;
} catch (IOException e) {
myLog.l(Log.INFO,
"IOException getting OutputStream for data socket");
dataSocket = null;
return false;
}
}
public void quit() {
myLog.d("SessionThread told to quit");
closeSocket();
}
public void closeDataSocket() {
myLog.l(Log.DEBUG, "Closing data socket");
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
}
dataOutputStream = null;
}
if (dataSocket != null) {
try {
dataSocket.close();
} catch (IOException e) {
}
}
dataSocket = null;
}
protected InetAddress getLocalAddress() {
return cmdSocket.getLocalAddress();
}
static int numNulls = 0;
public void run() {
myLog.l(Log.INFO, "SessionThread started. root = " + Globals.getChrootDir() + ", primary dir = " + Globals.getPrimaryDir());
if(sendWelcomeBanner) {
writeString("220 SwiFTP " + Util.getVersion() + " ready\r\n");
}
// Main loop: read an incoming line and process it
try {
BufferedReader in = new BufferedReader(new InputStreamReader(cmdSocket
.getInputStream()), 8192); // use 8k buffer
while (true) {
String line;
line = in.readLine(); // will accept \r\n or \n for terminator
if (line != null) {
FTPServerService.writeMonitor(true, line);
myLog.l(Log.DEBUG, "Received line from client: " + line);
FtpCmd.dispatchCommand(this, line);
} else {
myLog.i("readLine gave null, quitting");
break;
}
}
} catch (IOException e) {
myLog.l(Log.INFO, "Connection was dropped");
}
closeSocket();
}
/**
* A static method to check the equality of two byte arrays, but only up to
* a given length.
*/
public static boolean compareLen(byte[] array1, byte[] array2, int len) {
for (int i = 0; i < len; i++) {
if (array1[i] != array2[i]) {
return false;
}
}
return true;
}
public void closeSocket() {
if (cmdSocket == null) {
return;
}
try {
cmdSocket.close();
} catch (IOException e) {}
}
public void writeBytes(byte[] bytes) {
try {
// TODO: do we really want to do all of this on each write? Why?
BufferedOutputStream out = new BufferedOutputStream(cmdSocket
.getOutputStream(), Defaults.dataChunkSize);
out.write(bytes);
out.flush();
dataSocketFactory.reportTraffic(bytes.length);
} catch (IOException e) {
myLog.l(Log.INFO, "Exception writing socket");
closeSocket();
return;
}
}
public void writeString(String str) {
FTPServerService.writeMonitor(false, str);
byte[] strBytes;
try {
strBytes = str.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
myLog.e("Unsupported encoding: " + encoding);
strBytes = str.getBytes();
}
writeBytes(strBytes);
}
protected Socket getSocket() {
return cmdSocket;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public boolean isPasvMode() {
return pasvMode;
}
public SessionThread(Socket socket, DataSocketFactory dataSocketFactory,
Source source) {
this.cmdSocket = socket;
this.source = source;
this.dataSocketFactory = dataSocketFactory;
if(source == Source.LOCAL) {
this.sendWelcomeBanner = true;
} else {
this.sendWelcomeBanner = false;
}
}
static public ByteBuffer stringToBB(String s) {
return ByteBuffer.wrap(s.getBytes());
}
public boolean isBinaryMode() {
return binaryMode;
}
public void setBinaryMode(boolean binaryMode) {
this.binaryMode = binaryMode;
}
public boolean isAuthenticated() {
return authenticated;
}
public void authAttempt(boolean authenticated) {
if (authenticated) {
myLog.l(Log.INFO, "Authentication complete");
this.authenticated = true;
} else {
// There was a failed auth attempt. If the connection came
// via the proxy, then drop it now. The client can't try again
// successfully because it doesn't know its real username. What
// it knows is prefix_username.
if(source == Source.PROXY) {
quit();
} else {
authFails++;
myLog.i("Auth failed: " + authFails + "/" + MAX_AUTH_FAILS);
}
if(authFails > MAX_AUTH_FAILS) {
myLog.i("Too many auth fails, quitting session");
quit();
}
}
}
public File getWorkingDir() {
return workingDir;
}
public void setWorkingDir(File workingDir) {
try {
this.workingDir = workingDir.getCanonicalFile().getAbsoluteFile();
} catch (IOException e) {
myLog.l(Log.INFO, "SessionThread canonical error");
}
}
/*
* public FTPServerService getService() { return service; }
*
* public void setService(FTPServerService service) { this.service =
* service; }
*/
public Socket getDataSocket() {
return dataSocket;
}
public void setDataSocket(Socket dataSocket) {
this.dataSocket = dataSocket;
}
// public ServerSocket getServerSocket() {
// return dataServerSocket;
// }
public File getRenameFrom() {
return renameFrom;
}
public void setRenameFrom(File renameFrom) {
this.renameFrom = renameFrom;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
}