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;
}
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();
}
}
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;
}
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");
}
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;
}
Aggregations