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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.java.sip.communicator.impl.protocol.ssh;
import java.io.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.Logger;
import com.jcraft.jsch.*;
/**
* @author Shobhit Jindal
*/
public class SSHFileTransferDaemon
extends Thread
{
private static final Logger logger =
Logger.getLogger(SSHFileTransferDaemon .class);
/**
* The contact of the remote machine
*/
private ContactSSH sshContact;
/**
* The currently valid ssh protocol provider
*/
private ProtocolProviderServiceSSHImpl ppService;
/**
* JSch Channel to be used for file transfer
*/
private Channel fileTransferChannel;
/**
* The identifier for the Input Stream associated with SCP Channel
*/
private InputStream scpInputStream = null;
/**
* The identifier for the Output Stream associated with SCP Channel
*/
private OutputStream scpOutputStream = null;
/**
* Identifier of local file
*/
private String localPath;
/**
* Identifier of remote file
*/
private String remotePath;
/**
* File to be uploaded or saved
*/
private File file;
/**
* The file input stream associated with the file to be uploaded
*/
private FileInputStream fileInputStream;
/**
* The file output stream associated with the file to be uploaded
*/
private FileOutputStream fileOutputStream;
/**
* The boolean which determines whether we are uploading or downloading
* files
*/
private boolean uploadFile;
/**
* The currently valid ssh persistent presence operation set
*/
private OperationSetPersistentPresenceSSHImpl opSetPersPresence = null;
/**
* The currently valid ssh instant messaging operation set
*/
private OperationSetBasicInstantMessagingSSHImpl instantMessaging = null;
/**
* Creates a new instance of SSHFileTransferDaemon
*
*
* @param sshContact The contact of the remote machine
* @param ppService The current ssh protocol provider
*/
public SSHFileTransferDaemon(
ContactSSH sshContact,
ProtocolProviderServiceSSHImpl ppService)
{
super();
this.sshContact = sshContact;
this.opSetPersPresence = (OperationSetPersistentPresenceSSHImpl)
ppService.getOperationSet(OperationSetPersistentPresence.class);
this.instantMessaging = (OperationSetBasicInstantMessagingSSHImpl)
ppService.getOperationSet(
OperationSetBasicInstantMessaging.class);
this.ppService = ppService;
}
/**
* This method is called when file is to be transfered from local machine
* to remote machine
*
* @param remotePath - the identifier for the remote file
* @param localPath - the identifier for the local file
*/
public void uploadFile(
String remotePath,
String localPath)
{
this.uploadFile = true;
this.remotePath = remotePath;
this.localPath = localPath;
file = new File(localPath);
start();
}
/**
* This method is called when a file is to be downloaded from remote machine
* to local machine
*
* @param remotePath - the identifier for the remote file
* @param localPath - the identifier for the local file
*/
public void downloadFile(
String remotePath,
String localPath)
{
this.uploadFile = false;
this.remotePath = remotePath;
this.localPath = localPath;
file = new File(localPath);
start();
}
/**
* Background thread for the file transfer
*/
@Override
public void run()
{
//oldStatus to be resumed earlier
PresenceStatus oldStatus = sshContact.getPresenceStatus();
opSetPersPresence.changeContactPresenceStatus(
sshContact,
SSHStatusEnum.CONNECTING);
try
{
//create a new JSch session if current is invalid
if( !ppService.isSessionValid(sshContact))
ppService.createSSHSessionAndLogin(sshContact);
fileTransferChannel = sshContact.getSSHSession()
.openChannel("exec");
String command;
// -p = Preserves modification times, access times, and modes from
// the original file
if(uploadFile)
command = "scp -p -t " + remotePath;
else
command = "scp -f " + remotePath;
//the command to be executed on the remote terminal
((ChannelExec)fileTransferChannel).setCommand(command);
scpInputStream = fileTransferChannel.getInputStream();
scpOutputStream = fileTransferChannel.getOutputStream();
fileTransferChannel.connect();
//file transfer is setup
opSetPersPresence.changeContactPresenceStatus(
sshContact,
SSHStatusEnum.FILE_TRANSFER);
if(uploadFile)
{
instantMessaging.deliverMessage(
instantMessaging.createMessage(
"Uploading " + file.getName() + " to server"),
sshContact);
upload();
}
else
{
instantMessaging.deliverMessage(
instantMessaging.createMessage(
"Downloading " + file.getName() + " from server"),
sshContact);
download();
}
}
catch(Exception ex)
{
//presently errors(any type) are directly logged directly in chat
instantMessaging.deliverMessage(
instantMessaging.createMessage(ex.getMessage()),
sshContact);
logger.error(ex.getMessage());
try
{
if(fileInputStream!=null)
{
fileInputStream.close();
}
if(fileOutputStream!=null)
{
fileOutputStream.close();
}
}
catch(Exception e)
{}
}
// restore old status
opSetPersPresence.changeContactPresenceStatus(
sshContact,
oldStatus);
}
/**
* Check for error in reading stream of remote machine
*
* @return 0 for success, 1 for error, 2 for fatal error, -1 otherwise
* @throws IOException when the network goes down
*/
private int checkAck(InputStream inputStream)
throws IOException
{
int result = inputStream.read();
// read error message
if(result==1 || result==2)
{
StringBuffer buffer = new StringBuffer();
int ch;
do
{
//read a line of message
ch = inputStream.read();
buffer.append((char)ch);
}while(ch != '\n');
ProtocolProviderServiceSSHImpl
.getUIService()
.getPopupDialog()
.showMessagePopupDialog(
buffer.toString(),
"File Transfer Error: "
+ sshContact.getDisplayName(),
PopupDialog.ERROR_MESSAGE);
logger.error(buffer.toString());
}
return result;
}
/**
* Uploads the file to the remote server
*
* @throws IOException when the network goes down
* @throws OperationFailedException when server behaves unexpectedly
*/
private void upload()
throws IOException,
OperationFailedException
{
fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int result, bytesRead;
if( (result = checkAck(scpInputStream)) !=0)
throw new OperationFailedException("Error in Ack", result);
// send "C0644 filesize filename", where filename should not include '/'
long filesize= file.length();
String command = "C0644 " + filesize + " ";
// if(lfile.lastIndexOf('/')>0)
// {
// command+=lfile.substring(lfile.lastIndexOf('/')+1);
// }
// else
// {
// command+=lfile;
// }
command += file.getName() + "\n";
if (logger.isTraceEnabled())
logger.trace(command);
scpOutputStream.write(command.getBytes());
scpOutputStream.flush();
if( (result = checkAck(scpInputStream)) !=0)
throw new OperationFailedException("Error in Ack", result);
while(true)
{
bytesRead = fileInputStream.read(buffer, 0, buffer.length);
if(bytesRead <= 0)
break;
scpOutputStream.write(buffer, 0, bytesRead); //out.flush();
}
fileInputStream.close();
fileInputStream = null;
// send '\0'
buffer[0]=0; scpOutputStream.write(buffer, 0, 1);
scpOutputStream.flush();
if( (result = checkAck(scpInputStream)) !=0)
throw new OperationFailedException("Error in Ack", result);
scpInputStream.close();
scpOutputStream.close();
fileTransferChannel.disconnect();
instantMessaging.deliverMessage(
instantMessaging.createMessage(file.getName()
+ " uploaded to Server"),
sshContact);
}
/**
* Downloads a file from the remote machine
*
* @throws IOException when the network goes down
* @throws OperationFailedException when server behaves unexpectedly
*/
private void download()
throws IOException,
OperationFailedException
{
fileOutputStream = new FileOutputStream(file);
int result;
byte[] buffer = new byte[1024];
// send '\0'
buffer[0]=0;
scpOutputStream.write(buffer, 0, 1);
scpOutputStream.flush();
int ch = checkAck(scpInputStream);
if(ch!='C')
{
throw new OperationFailedException("Invalid reply from server", 12);
}
// read '0644 '
scpInputStream.read(buffer, 0, 5);
long filesize=0L;
while(true)
{
if(scpInputStream.read(buffer, 0, 1) < 0)
{
// error
break;
}
if(buffer[0]==' ')break;
filesize=filesize*10L+buffer[0]-'0';
}
String file=null;
for(int i=0;true;i++)
{
scpInputStream.read(buffer, i, 1);
if(buffer[i]==(byte)0x0a)
{
file=new String(buffer, 0, i);
break;
}
}
//System.out.println("filesize="+filesize+", file="+file);
// send '\0'
buffer[0]=0;
scpOutputStream.write(buffer, 0, 1);
scpOutputStream.flush();
// read a content of lfile
int foo;
while(true)
{
if(buffer.length<filesize)
foo=buffer.length;
else
foo=(int)filesize;
foo = scpInputStream.read(buffer, 0, foo);
if(foo<0)
break;
fileOutputStream.write(buffer, 0, foo);
filesize-=foo;
if(filesize==0L) break;
}
fileOutputStream.close();
fileOutputStream=null;
if( (result = checkAck(scpInputStream)) !=0)
throw new OperationFailedException("Error in Ack", result);
// send '\0'
buffer[0]=0;
scpOutputStream.write(buffer, 0, 1);
scpOutputStream.flush();
scpInputStream.close();
scpOutputStream.close();
fileTransferChannel.disconnect();
instantMessaging.deliverMessage(
instantMessaging.createMessage(
this.file.getName() + " downloaded from Server"),
sshContact);
}
}
|