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
|
package eu.siacs.conversations.xmpp.jingle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import eu.siacs.conversations.utils.CryptoHelper;
import android.util.Log;
public class SocksConnection {
private JingleConnection jingleConnection;
private Socket socket;
private String host;
private String jid;
private int port;
private boolean isProxy = false;
private String destination;
private OutputStream outputStream;
public SocksConnection(JingleConnection jingleConnection, String host, String jid, int port, String type) {
this.jingleConnection = jingleConnection;
this.host = host;
this.jid = jid;
this.port = port;
this.isProxy = "proxy".equalsIgnoreCase(type);
try {
MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
StringBuilder destBuilder = new StringBuilder();
destBuilder.append(jingleConnection.getSessionId());
destBuilder.append(jingleConnection.getInitiator());
destBuilder.append(jingleConnection.getResponder());
mDigest.reset();
this.destination = CryptoHelper.bytesToHex(mDigest.digest(destBuilder.toString().getBytes()));
Log.d("xmppService","host="+host+", port="+port+", destination: "+destination);
} catch (NoSuchAlgorithmException e) {
}
}
public boolean connect() {
try {
this.socket = new Socket(this.host, this.port);
InputStream is = socket.getInputStream();
this.outputStream = socket.getOutputStream();
byte[] login = {0x05, 0x01, 0x00};
byte[] expectedReply = {0x05,0x00};
byte[] reply = new byte[2];
this.outputStream.write(login);
is.read(reply);
if (Arrays.equals(reply, expectedReply)) {
String connect = ""+'\u0005'+'\u0001'+'\u0000'+'\u0003'+'\u0028'+this.destination+'\u0000'+'\u0000';
this.outputStream.write(connect.getBytes());
byte[] result = new byte[2];
is.read(result);
int status = result[0];
return (status==0);
} else {
socket.close();
return false;
}
} catch (UnknownHostException e) {
return false;
} catch (IOException e) {
return false;
}
}
public void send(File file) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
int count;
byte[] buffer = new byte[8192];
while ((count = fileInputStream.read(buffer)) > 0) {
this.outputStream.write(buffer, 0, count);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (fileInputStream!=null) {
fileInputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean isProxy() {
return this.isProxy;
}
public String getJid() {
return this.jid;
}
}
|