use of com.runjva.sourceforge.jsocks.protocol.Authentication in project bisq2 by bisq-network.
the class Tor method getSocks5Proxy.
public Socks5Proxy getSocks5Proxy(@Nullable String streamId) throws IOException {
checkArgument(state.get() == State.STARTED, "Invalid state at Tor.getSocks5Proxy. state=" + state.get());
checkArgument(proxyPort > -1, "proxyPort must be defined");
Socks5Proxy socks5Proxy = new Socks5Proxy(Constants.LOCALHOST, proxyPort);
socks5Proxy.resolveAddrLocally(false);
if (streamId == null) {
return socks5Proxy;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] digest = messageDigest.digest(streamId.getBytes());
String asBase26 = new BigInteger(digest).toString(26);
byte[] hash = asBase26.getBytes();
// Authentication method ID 2 is User/Password
socks5Proxy.setAuthenticationMethod(2, new Authentication() {
@Override
public Object[] doSocksAuthentication(int i, Socket socket) throws IOException {
// Must not close the streams here, as otherwise we get a socket closed
// exception at SocksSocket
OutputStream outputStream = socket.getOutputStream();
outputStream.write(new byte[] { (byte) 1, (byte) hash.length });
outputStream.write(hash);
outputStream.write(new byte[] { (byte) 1, (byte) 0 });
outputStream.flush();
byte[] status = new byte[2];
InputStream inputStream = socket.getInputStream();
if (inputStream.read(status) == -1) {
throw new IOException("Did not get data");
}
if (status[1] != (byte) 0) {
throw new IOException("Authentication error: " + status[1]);
}
return new Object[] { inputStream, outputStream };
}
});
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return socks5Proxy;
}
Aggregations