1package eu.siacs.conversations.xmpp.jingle;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.OutputStream;
9import java.net.Socket;
10import java.net.UnknownHostException;
11import java.security.MessageDigest;
12import java.security.NoSuchAlgorithmException;
13import java.util.Arrays;
14
15import eu.siacs.conversations.utils.CryptoHelper;
16
17import android.util.Log;
18
19public class SocksConnection {
20
21 private JingleConnection jingleConnection;
22 private Socket socket;
23 private String host;
24 private String jid;
25 private int port;
26 private boolean isProxy = false;
27 private String destination;
28 private OutputStream outputStream;
29
30 public SocksConnection(JingleConnection jingleConnection, String host, String jid, int port, String type) {
31 this.jingleConnection = jingleConnection;
32 this.host = host;
33 this.jid = jid;
34 this.port = port;
35 this.isProxy = "proxy".equalsIgnoreCase(type);
36 try {
37 MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
38 StringBuilder destBuilder = new StringBuilder();
39 destBuilder.append(jingleConnection.getSessionId());
40 destBuilder.append(jingleConnection.getInitiator());
41 destBuilder.append(jingleConnection.getResponder());
42 mDigest.reset();
43 this.destination = CryptoHelper.bytesToHex(mDigest.digest(destBuilder.toString().getBytes()));
44 Log.d("xmppService","host="+host+", port="+port+", destination: "+destination);
45 } catch (NoSuchAlgorithmException e) {
46
47 }
48 }
49
50 public boolean connect() {
51 try {
52 this.socket = new Socket(this.host, this.port);
53 InputStream is = socket.getInputStream();
54 this.outputStream = socket.getOutputStream();
55 byte[] login = {0x05, 0x01, 0x00};
56 byte[] expectedReply = {0x05,0x00};
57 byte[] reply = new byte[2];
58 this.outputStream.write(login);
59 is.read(reply);
60 if (Arrays.equals(reply, expectedReply)) {
61 String connect = ""+'\u0005'+'\u0001'+'\u0000'+'\u0003'+'\u0028'+this.destination+'\u0000'+'\u0000';
62 this.outputStream.write(connect.getBytes());
63 byte[] result = new byte[2];
64 is.read(result);
65 int status = result[0];
66 return (status==0);
67 } else {
68 socket.close();
69 return false;
70 }
71 } catch (UnknownHostException e) {
72 return false;
73 } catch (IOException e) {
74 return false;
75 }
76 }
77
78 public void send(File file) {
79 FileInputStream fileInputStream = null;
80 try {
81 fileInputStream = new FileInputStream(file);
82 int count;
83 byte[] buffer = new byte[8192];
84 while ((count = fileInputStream.read(buffer)) > 0) {
85 this.outputStream.write(buffer, 0, count);
86 }
87
88 } catch (FileNotFoundException e) {
89 // TODO Auto-generated catch block
90 e.printStackTrace();
91 } catch (IOException e) {
92 // TODO Auto-generated catch block
93 e.printStackTrace();
94 } finally {
95 try {
96 if (fileInputStream!=null) {
97 fileInputStream.close();
98 }
99 } catch (IOException e) {
100 // TODO Auto-generated catch block
101 e.printStackTrace();
102 }
103 }
104 }
105
106 public boolean isProxy() {
107 return this.isProxy;
108 }
109
110 public String getJid() {
111 return this.jid;
112 }
113}