Search in sources :

Example 1 with SocksSocket

use of com.runjva.sourceforge.jsocks.protocol.SocksSocket in project haveno by haveno-dex.

the class SeedPeersSocks5Dns method lookup.

/**
 * Resolves a hostname via remote DNS over socks5 proxy.
 */
@Nullable
public static InetSocketAddress lookup(Socks5Proxy proxy, InetSocketAddress addr) {
    if (!addr.isUnresolved()) {
        return addr;
    }
    try {
        SocksSocket proxySocket = new SocksSocket(proxy, addr.getHostString(), addr.getPort());
        InetAddress addrResolved = proxySocket.getInetAddress();
        proxySocket.close();
        if (addrResolved != null) {
            // log.debug("Resolved " + addr.getHostString() + " to " + addrResolved.getHostAddress());
            return new InetSocketAddress(addrResolved, addr.getPort());
        } else {
            // note: .onion nodes fall in here when proxy is Tor. But they have no IP address.
            // Unfortunately bitcoinj crashes in PeerAddress if it finds an unresolved address.
            log.error("Connected to " + addr.getHostString() + ".  But did not resolve to address.");
        }
    } catch (Exception e) {
        log.warn("Error resolving " + addr.getHostString() + ". Exception:\n" + e.toString());
    }
    return null;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) SocksSocket(com.runjva.sourceforge.jsocks.protocol.SocksSocket) PeerDiscoveryException(org.bitcoinj.net.discovery.PeerDiscoveryException) Nullable(javax.annotation.Nullable)

Example 2 with SocksSocket

use of com.runjva.sourceforge.jsocks.protocol.SocksSocket in project haveno by haveno-dex.

the class PriceNodeStats method execute.

@Override
protected void execute() {
    try {
        // fetch proxy
        Tor tor = Tor.getDefault();
        checkNotNull(tor, "tor must not be null");
        Socks5Proxy proxy = tor.getProxy();
        String[] hosts = configuration.getProperty(HOSTS, "").split(",");
        Collections.shuffle(Arrays.asList(hosts));
        // for each configured host
        for (String current : hosts) {
            Map<String, String> result = new HashMap<>();
            // parse Url
            NodeAddress tmp = OnionParser.getNodeAddress(current);
            // connect
            try {
                SocksSocket socket = new SocksSocket(proxy, tmp.getHostName(), tmp.getPort());
                // prepare to receive data
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // ask for fee data
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
                out.println("GET /getFees/");
                out.println();
                out.flush();
                // sift through the received lines and see if we got something json-like
                String line;
                while ((line = in.readLine()) != null) {
                    Matcher matcher = stringNumberPattern.matcher(line);
                    if (matcher.find())
                        if (!IGNORE.contains(matcher.group(1)))
                            result.put("fees." + matcher.group(1), matcher.group(2));
                }
                in.close();
                out.close();
                socket.close();
                // connect
                socket = new SocksSocket(proxy, tmp.getHostName(), tmp.getPort());
                // prepare to receive data
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // ask for exchange rate data
                out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
                out.println("GET /getAllMarketPrices/");
                out.println();
                out.flush();
                String currencyCode = "";
                while ((line = in.readLine()) != null) {
                    Matcher currencyCodeMatcher = currencyCodePattern.matcher(line);
                    Matcher priceMatcher = pricePattern.matcher(line);
                    if (currencyCodeMatcher.find()) {
                        currencyCode = currencyCodeMatcher.group(1);
                        if (!assets.contains(currencyCode))
                            currencyCode = "";
                    } else if (!"".equals(currencyCode) && priceMatcher.find())
                        result.put("price." + currencyCode, priceMatcher.group(1));
                }
                // close all the things
                in.close();
                out.close();
                socket.close();
                // report
                reporter.report(result, getName());
                // only ask for data as long as we got none
                if (!result.isEmpty())
                    break;
            } catch (IOException e) {
                log.error("{} seems to be down. Trying next configured price node.", tmp.getHostName());
                e.printStackTrace();
            }
        }
    } catch (TorCtlException | IOException e) {
        e.printStackTrace();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Socks5Proxy(com.runjva.sourceforge.jsocks.protocol.Socks5Proxy) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) SocksSocket(com.runjva.sourceforge.jsocks.protocol.SocksSocket) BufferedWriter(java.io.BufferedWriter) TorCtlException(org.berndpruenster.netlayer.tor.TorCtlException) Tor(org.berndpruenster.netlayer.tor.Tor) BufferedReader(java.io.BufferedReader) NodeAddress(bisq.network.p2p.NodeAddress) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Example 3 with SocksSocket

use of com.runjva.sourceforge.jsocks.protocol.SocksSocket in project bitsquare by bitsquare.

the class SeedPeersSocks5Dns method lookup.

/**
     * Resolves a hostname via remote DNS over socks5 proxy.
     */
@Nullable
public static InetSocketAddress lookup(Socks5Proxy proxy, InetSocketAddress addr) {
    if (!addr.isUnresolved()) {
        return addr;
    }
    try {
        SocksSocket proxySocket = new SocksSocket(proxy, addr.getHostString(), addr.getPort());
        InetAddress addrResolved = proxySocket.getInetAddress();
        proxySocket.close();
        if (addrResolved != null) {
            log.debug("Resolved " + addr.getHostString() + " to " + addrResolved.getHostAddress());
            return new InetSocketAddress(addrResolved, addr.getPort());
        } else {
            // note: .onion nodes fall in here when proxy is Tor. But they have no IP address.
            // Unfortunately bitcoinj crashes in PeerAddress if it finds an unresolved address.
            log.error("Connected to " + addr.getHostString() + ".  But did not resolve to address.");
        }
    } catch (Exception e) {
        log.warn("Error resolving " + addr.getHostString() + ". Exception:\n" + e.toString());
    }
    return null;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) SocksSocket(com.runjva.sourceforge.jsocks.protocol.SocksSocket) PeerDiscoveryException(org.bitcoinj.net.discovery.PeerDiscoveryException) UnknownHostException(java.net.UnknownHostException) Nullable(javax.annotation.Nullable)

Example 4 with SocksSocket

use of com.runjva.sourceforge.jsocks.protocol.SocksSocket in project bitsquare by bitsquare.

the class TorNode method connectToHiddenService.

private Socket connectToHiddenService(String onionUrl, int port, int numTries, boolean debug) throws IOException {
    long before = GregorianCalendar.getInstance().getTimeInMillis();
    for (int i = 0; i < numTries; ++i) {
        try {
            SocksSocket ssock = new SocksSocket(proxy, onionUrl, port);
            if (debug)
                log.debug("Took " + (GregorianCalendar.getInstance().getTimeInMillis() - before) + " milliseconds to connect to " + onionUrl + ":" + port);
            ssock.setTcpNoDelay(true);
            return ssock;
        } catch (UnknownHostException exx) {
            try {
                if (debug)
                    log.debug("Try " + (i + 1) + " connecting to " + onionUrl + ":" + port + " failed. retrying...");
                Thread.sleep(RETRY_SLEEP);
                continue;
            } catch (InterruptedException e) {
            }
        } catch (Exception e) {
            throw new IOException("Cannot connect to hidden service");
        }
    }
    throw new IOException("Cannot connect to hidden service");
}
Also used : UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) SocksSocket(com.runjva.sourceforge.jsocks.protocol.SocksSocket) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 5 with SocksSocket

use of com.runjva.sourceforge.jsocks.protocol.SocksSocket in project bisq-core by bisq-network.

the class SeedPeersSocks5Dns method lookup.

/**
 * Resolves a hostname via remote DNS over socks5 proxy.
 */
@Nullable
public static InetSocketAddress lookup(Socks5Proxy proxy, InetSocketAddress addr) {
    if (!addr.isUnresolved()) {
        return addr;
    }
    try {
        SocksSocket proxySocket = new SocksSocket(proxy, addr.getHostString(), addr.getPort());
        InetAddress addrResolved = proxySocket.getInetAddress();
        proxySocket.close();
        if (addrResolved != null) {
            log.debug("Resolved " + addr.getHostString() + " to " + addrResolved.getHostAddress());
            return new InetSocketAddress(addrResolved, addr.getPort());
        } else {
            // note: .onion nodes fall in here when proxy is Tor. But they have no IP address.
            // Unfortunately bitcoinj crashes in PeerAddress if it finds an unresolved address.
            log.error("Connected to " + addr.getHostString() + ".  But did not resolve to address.");
        }
    } catch (Exception e) {
        log.warn("Error resolving " + addr.getHostString() + ". Exception:\n" + e.toString());
    }
    return null;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) SocksSocket(com.runjva.sourceforge.jsocks.protocol.SocksSocket) PeerDiscoveryException(org.bitcoinj.net.discovery.PeerDiscoveryException) Nullable(javax.annotation.Nullable)

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