2014-04-11 19:13:09 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
2014-04-11 20:49:26 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
2014-04-11 19:13:09 +00:00
|
|
|
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 {
|
2014-04-13 09:32:45 +00:00
|
|
|
|
2014-04-11 19:13:09 +00:00
|
|
|
private JingleConnection jingleConnection;
|
|
|
|
private Socket socket;
|
|
|
|
private String host;
|
2014-04-11 20:49:26 +00:00
|
|
|
private String jid;
|
2014-04-11 19:13:09 +00:00
|
|
|
private int port;
|
2014-04-11 20:49:26 +00:00
|
|
|
private boolean isProxy = false;
|
2014-04-11 19:13:09 +00:00
|
|
|
private String destination;
|
2014-04-11 20:49:26 +00:00
|
|
|
private OutputStream outputStream;
|
2014-04-13 09:32:45 +00:00
|
|
|
|
|
|
|
public SocksConnection(JingleConnection jingleConnection, String host,
|
|
|
|
String jid, int port, String type) {
|
2014-04-11 19:13:09 +00:00
|
|
|
this.jingleConnection = jingleConnection;
|
|
|
|
this.host = host;
|
2014-04-11 20:49:26 +00:00
|
|
|
this.jid = jid;
|
2014-04-11 19:13:09 +00:00
|
|
|
this.port = port;
|
2014-04-11 20:49:26 +00:00
|
|
|
this.isProxy = "proxy".equalsIgnoreCase(type);
|
2014-04-11 19:13:09 +00:00
|
|
|
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();
|
2014-04-13 09:32:45 +00:00
|
|
|
this.destination = CryptoHelper.bytesToHex(mDigest
|
|
|
|
.digest(destBuilder.toString().getBytes()));
|
|
|
|
Log.d("xmppService", "host=" + host + ", port=" + port
|
|
|
|
+ ", destination: " + destination);
|
2014-04-11 19:13:09 +00:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2014-04-13 09:32:45 +00:00
|
|
|
|
2014-04-11 19:13:09 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-13 09:32:45 +00:00
|
|
|
|
2014-04-11 19:13:09 +00:00
|
|
|
public boolean connect() {
|
|
|
|
try {
|
|
|
|
this.socket = new Socket(this.host, this.port);
|
|
|
|
InputStream is = socket.getInputStream();
|
2014-04-11 20:49:26 +00:00
|
|
|
this.outputStream = socket.getOutputStream();
|
2014-04-13 09:32:45 +00:00
|
|
|
byte[] login = { 0x05, 0x01, 0x00 };
|
|
|
|
byte[] expectedReply = { 0x05, 0x00 };
|
2014-04-11 19:13:09 +00:00
|
|
|
byte[] reply = new byte[2];
|
2014-04-11 20:49:26 +00:00
|
|
|
this.outputStream.write(login);
|
2014-04-11 19:13:09 +00:00
|
|
|
is.read(reply);
|
|
|
|
if (Arrays.equals(reply, expectedReply)) {
|
2014-04-13 09:32:45 +00:00
|
|
|
String connect = "" + '\u0005' + '\u0001' + '\u0000' + '\u0003'
|
|
|
|
+ '\u0028' + this.destination + '\u0000' + '\u0000';
|
2014-04-11 20:49:26 +00:00
|
|
|
this.outputStream.write(connect.getBytes());
|
2014-04-11 19:13:09 +00:00
|
|
|
byte[] result = new byte[2];
|
|
|
|
is.read(result);
|
|
|
|
int status = result[0];
|
2014-04-13 09:32:45 +00:00
|
|
|
return (status == 0);
|
2014-04-11 19:13:09 +00:00
|
|
|
} else {
|
|
|
|
socket.close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
return false;
|
|
|
|
} catch (IOException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-04-11 20:49:26 +00:00
|
|
|
|
2014-04-13 09:32:45 +00:00
|
|
|
public void send(final JingleFile file, final OnFileTransmitted callback) {
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
FileInputStream fileInputStream = null;
|
|
|
|
try {
|
|
|
|
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
|
|
|
digest.reset();
|
|
|
|
fileInputStream = new FileInputStream(file);
|
|
|
|
int count;
|
|
|
|
byte[] buffer = new byte[8192];
|
|
|
|
while ((count = fileInputStream.read(buffer)) > 0) {
|
|
|
|
outputStream.write(buffer, 0, count);
|
|
|
|
digest.update(buffer, 0, count);
|
|
|
|
}
|
|
|
|
outputStream.flush();
|
|
|
|
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
|
|
|
|
if (callback!=null) {
|
|
|
|
callback.onFileTransmitted(file);
|
|
|
|
}
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException 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();
|
|
|
|
}
|
2014-04-11 20:49:26 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-13 09:32:45 +00:00
|
|
|
}).start();
|
|
|
|
|
2014-04-11 20:49:26 +00:00
|
|
|
}
|
2014-04-13 09:32:45 +00:00
|
|
|
|
2014-04-11 20:49:26 +00:00
|
|
|
public boolean isProxy() {
|
|
|
|
return this.isProxy;
|
|
|
|
}
|
2014-04-13 09:32:45 +00:00
|
|
|
|
2014-04-11 20:49:26 +00:00
|
|
|
public String getJid() {
|
|
|
|
return this.jid;
|
|
|
|
}
|
2014-04-13 09:32:45 +00:00
|
|
|
|
|
|
|
public void disconnect() {
|
|
|
|
if (this.socket!=null) {
|
|
|
|
try {
|
|
|
|
this.socket.close();
|
|
|
|
Log.d("xmppService","cloesd socket with "+this.host);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.d("xmppService","error closing socket with "+this.host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-11 19:13:09 +00:00
|
|
|
}
|