Search in sources :

Example 1 with Authentication

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;
}
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)

Aggregations

Authentication (com.runjva.sourceforge.jsocks.protocol.Authentication)1 Socks5Proxy (com.runjva.sourceforge.jsocks.protocol.Socks5Proxy)1 SocksSocket (com.runjva.sourceforge.jsocks.protocol.SocksSocket)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 BigInteger (java.math.BigInteger)1 Socket (java.net.Socket)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1