Java Code Examples for com.runjva.sourceforge.jsocks.protocol.Socks5Proxy#getPort()

The following examples show how to use com.runjva.sourceforge.jsocks.protocol.Socks5Proxy#getPort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpClient.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Make an HTTP Get request routed over socks5 proxy.
 */
private String requestWithGETProxy(String param, Socks5Proxy socks5Proxy, @Nullable String headerKey, @Nullable String headerValue) throws IOException {
    log.debug("requestWithGETProxy param=" + param);
    // This code is adapted from:
    //  http://stackoverflow.com/a/25203021/5616248

    // Register our own SocketFactories to override createSocket() and connectSocket().
    // connectSocket does NOT resolve hostname before passing it to proxy.
    Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", new SocksConnectionSocketFactory())
            .register("https", new SocksSSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build();

    // Use FakeDNSResolver if not resolving DNS locally.
    // This prevents a local DNS lookup (which would be ignored anyway)
    PoolingHttpClientConnectionManager cm = socks5Proxy.resolveAddrLocally() ?
            new PoolingHttpClientConnectionManager(reg) :
            new PoolingHttpClientConnectionManager(reg, new FakeDnsResolver());
    try (CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build()) {
        InetSocketAddress socksAddress = new InetSocketAddress(socks5Proxy.getInetAddress(), socks5Proxy.getPort());

        // remove me: Use this to test with system-wide Tor proxy, or change port for another proxy.
        // InetSocketAddress socksAddress = new InetSocketAddress("127.0.0.1", 9050);

        HttpClientContext context = HttpClientContext.create();
        context.setAttribute("socks.address", socksAddress);

        HttpGet request = new HttpGet(baseUrl + param);
        if (headerKey != null && headerValue != null)
            request.setHeader(headerKey, headerValue);

        log.debug("Executing request " + request + " proxy: " + socksAddress);
        try (CloseableHttpResponse response = httpclient.execute(request, context)) {
            return convertInputStreamToString(response.getEntity().getContent());
        }
    } catch (Throwable t) {
        throw new IOException("Error at requestWithGETProxy with URL: " + (baseUrl + param) + ". Throwable=" + t.getMessage());
    }
}
 
Example 2
Source File: DnsLookupTor.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Performs DNS lookup and returns a single InetAddress
 */
public static InetAddress lookup(Socks5Proxy proxy, String host) throws DnsLookupException {
    try {
        // note:  This is creating a new connection to our proxy, without any authentication.
        //        This works fine when connecting to bisq's internal Tor proxy, but
        //        would fail if user has configured an external proxy that requires auth.
        //        It would be much better to use the already connected proxy socket, but when I
        //        tried that I get weird errors and the lookup fails.
        //
        //        So this is an area for future improvement.
        Socket proxySocket = new Socket(proxy.getInetAddress(), proxy.getPort());

        proxySocket.getOutputStream().write(new byte[]{b('\u0005'), b('\u0001'), b('\u0000')});
        byte[] buf = new byte[2];
        //noinspection ResultOfMethodCallIgnored
        proxySocket.getInputStream().read(buf);

        if (buf[0] != b('\u0005')) {
            throw new DnsLookupException("Invalid Proxy Response");
        }
        if (buf[1] != b('\u0000')) {
            throw new DnsLookupException("Unrecognized Tor Auth Method");
        }

        byte[] hostBytes = host.getBytes(Charsets.UTF_8);
        buf = new byte[7 + hostBytes.length];
        buf[0] = b('\u0005');               // version SOCKS5
        buf[1] = b('\u00f0');               // CMD_RESOLVE
        buf[2] = b('\u0000');               // (reserved)
        buf[3] = b('\u0003');               // SOCKS5_ATYPE_HOSTNAME
        buf[4] = (byte) hostBytes.length;
        System.arraycopy(hostBytes, 0, buf, 5, hostBytes.length);
        buf[5 + hostBytes.length] = 0;

        proxySocket.getOutputStream().write(buf);

        buf = new byte[4];
        //noinspection UnusedAssignment
        int bytesRead = proxySocket.getInputStream().read(buf);

        // TODO: Should not be a length check here as well?
       /* if (bytesRead != 4)
            throw new DnsLookupException("Invalid Tor Address Response");*/


        if (buf[0] != b('\u0005'))
            throw new DnsLookupException("Invalid Tor Proxy Response");

        if (buf[1] != b('\u0000')) {
            if (!torStatusErrors.containsKey(buf[1])) {
                throw new DnsLookupException("Invalid Tor Proxy Response");
            }
            throw new DnsLookupException(torStatusErrors.get(buf[1]) + "(host=" + host + ")");
        }

        final char SOCKS5_ATYPE_IPV4 = '\u0001';
        final char SOCKS5_ATYPE_IPV6 = '\u0004';
        final byte atype = buf[3];
        if (atype != b(SOCKS5_ATYPE_IPV4) && atype != b(SOCKS5_ATYPE_IPV6))
            throw new DnsLookupException(torStatusErrors.get(b('\u0001')) + "(host=" + host + ")");

        final int octets = (atype == SOCKS5_ATYPE_IPV4 ? 4 : 16);
        buf = new byte[octets];
        bytesRead = proxySocket.getInputStream().read(buf);
        if (bytesRead != octets)
            throw new DnsLookupException("Invalid Tor Address Response");

        return InetAddress.getByAddress(buf);
    } catch (IOException | DnsLookupException e) {
        log.warn("Error resolving " + host + ". Exception:\n" + e.toString());
        throw new DnsLookupException(e);
    }
}