use of com.runjva.sourceforge.jsocks.protocol.SocksSocket in project haveno by haveno-dex.
the class TorRoundTripTime method execute.
@Override
protected void execute() {
SocksSocket socket;
try {
// fetch proxy
Tor tor = Tor.getDefault();
checkNotNull(tor, "tor must not be null");
Socks5Proxy proxy = tor.getProxy();
// for each configured host
for (String current : configuration.getProperty(HOSTS, "").split(",")) {
// parse Url
NodeAddress tmp = OnionParser.getNodeAddress(current);
List<Long> samples = new ArrayList<>();
while (samples.size() < Integer.parseInt(configuration.getProperty(SAMPLE_SIZE, "1"))) {
// start timer - we do not need System.nanoTime as we expect our result to be in
// seconds time.
long start = System.currentTimeMillis();
// connect
socket = new SocksSocket(proxy, tmp.getHostName(), tmp.getPort());
// by the time we get here, we are connected
samples.add(System.currentTimeMillis() - start);
// cleanup
socket.close();
}
// report
reporter.report(StatisticsHelper.process(samples), getName());
}
} catch (TorCtlException | IOException e) {
e.printStackTrace();
}
}
use of com.runjva.sourceforge.jsocks.protocol.SocksSocket 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;
}
use of com.runjva.sourceforge.jsocks.protocol.SocksSocket in project bisq2 by bisq-network.
the class Tor method getSocksSocket.
public SocksSocket getSocksSocket(String remoteHost, int remotePort, @Nullable String streamId) throws IOException {
Socks5Proxy socks5Proxy = getSocks5Proxy(streamId);
SocksSocket socksSocket = new SocksSocket(socks5Proxy, remoteHost, remotePort);
socksSocket.setTcpNoDelay(true);
return socksSocket;
}
use of com.runjva.sourceforge.jsocks.protocol.SocksSocket in project bisq2 by bisq-network.
the class TorDemo method sendViaSocksSocket.
private static void sendViaSocksSocket(Tor tor, OnionAddress onionAddress) {
try {
SocksSocket socket = tor.getSocksSocket(onionAddress.getHost(), onionAddress.getPort(), "test_stream_id");
sendOnOutboundConnection(socket, "test via SocksSocket");
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.runjva.sourceforge.jsocks.protocol.SocksSocket in project bisq2 by bisq-network.
the class AbstractTorTest method sendViaSocksSocket.
protected void sendViaSocksSocket(Tor tor, OnionAddress onionAddress) {
try {
startConnectionTs = System.currentTimeMillis();
SocksSocket socket = tor.getSocksSocket(onionAddress.getHost(), onionAddress.getPort(), TEST_STREAM_ID);
sendOnOutboundConnection(socket, "Message via SocksSocket");
} catch (IOException ex) {
fail(ex);
}
}
Aggregations