Search in sources :

Example 6 with SocksSocket

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();
    }
}
Also used : TorCtlException(org.berndpruenster.netlayer.tor.TorCtlException) Socks5Proxy(com.runjva.sourceforge.jsocks.protocol.Socks5Proxy) Tor(org.berndpruenster.netlayer.tor.Tor) ArrayList(java.util.ArrayList) NodeAddress(bisq.network.p2p.NodeAddress) IOException(java.io.IOException) SocksSocket(com.runjva.sourceforge.jsocks.protocol.SocksSocket)

Example 7 with SocksSocket

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;
}
Also used : Socks5Proxy(com.runjva.sourceforge.jsocks.protocol.Socks5Proxy) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Authentication(com.runjva.sourceforge.jsocks.protocol.Authentication) BigInteger(java.math.BigInteger) MessageDigest(java.security.MessageDigest) Socket(java.net.Socket) SocksSocket(com.runjva.sourceforge.jsocks.protocol.SocksSocket)

Example 8 with SocksSocket

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;
}
Also used : Socks5Proxy(com.runjva.sourceforge.jsocks.protocol.Socks5Proxy) SocksSocket(com.runjva.sourceforge.jsocks.protocol.SocksSocket)

Example 9 with 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();
    }
}
Also used : IOException(java.io.IOException) SocksSocket(com.runjva.sourceforge.jsocks.protocol.SocksSocket)

Example 10 with SocksSocket

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);
    }
}
Also used : SocksSocket(com.runjva.sourceforge.jsocks.protocol.SocksSocket)

Aggregations

SocksSocket (com.runjva.sourceforge.jsocks.protocol.SocksSocket)10 IOException (java.io.IOException)5 Socks5Proxy (com.runjva.sourceforge.jsocks.protocol.Socks5Proxy)4 InetAddress (java.net.InetAddress)3 InetSocketAddress (java.net.InetSocketAddress)3 Nullable (javax.annotation.Nullable)3 PeerDiscoveryException (org.bitcoinj.net.discovery.PeerDiscoveryException)3 NodeAddress (bisq.network.p2p.NodeAddress)2 UnknownHostException (java.net.UnknownHostException)2 Tor (org.berndpruenster.netlayer.tor.Tor)2 TorCtlException (org.berndpruenster.netlayer.tor.TorCtlException)2 Authentication (com.runjva.sourceforge.jsocks.protocol.Authentication)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 BigInteger (java.math.BigInteger)1