1

I'm working on a Java application that uses websockets to communicate with a hidden service. So far I've managed to get it all working but websocket connection is unstable. Here's what I've done:

First, I've installed Tor and added this to torrc:

HiddenServiceDir /var/lib/tor/hidden_service/
HiddenServicePort 80 127.0.0.1:9001 

I've got a working websocket server on a local machine listening on port 9001.
Next, I need a local HTTP proxy for websockets, not SOCKS5 so I've installed Polipo and added this to it's config file:

proxyAddress = "127.0.0.1"
proxyPort = 8118

socksParentProxy = "localhost:9050"
socksProxyType = socks5

So I've got Tor with a hidden service, working websocket server, Polipo and a websocket client all on a same machine - for a testing purposes. And at last, here's a pseudo code of what my program is doing:

val server = "ws://xe5qeboadaaatzuz.onion:80" // hidden service on a same machine
factory.proxy.setHost("localhost").setPort(8118) // using Polipo HTTP proxy
val socket = factory.createSocket(server)
socket.onDisconnect = wait 5 seconds and reconnect
socket.connect

And here's an output I'm getting:

[ForkJoinPool-1-worker-5] connecting...
[ReadingThread] socket has connected
[ForkJoinPool-1-worker-3] OPEN
[ReadingThread] Got response: ["ok","hello"]
[ReadingThread] Got ping frame
[ReadingThread] Got ping frame
[ReadingThread] Disconnected
[ForkJoinPool-1-worker-5] java.io.IOException: The status code in the response from the proxy server is not '200 Connection established'.The status line is: HTTP/1.1 504 Couldn't connect: Connection refused
...
[ForkJoinPool-1-worker-5] connecting...
[ReadingThread] socket has connected
[ForkJoinPool-1-worker-3] OPEN
[ReadingThread] Got response: ["ok","hello"]
[ReadingThread] Got ping frame
[ReadingThread] Got ping frame
[ReadingThread] Got ping frame
[ReadingThread] Got ping frame
[ReadingThread] Got ping frame
[ReadingThread] Disconnected
[ForkJoinPool-1-worker-5] java.io.IOException: The status code in the response from the proxy server is not '200 Connection established'.The status line is: HTTP/1.1 504 Couldn't connect: Connection refused
[ForkJoinPool-1-worker-5] java.io.IOException: The status code in the response from the proxy server is not '200 Connection established'.The status line is: HTTP/1.1 504 Couldn't connect: Connection refused
...

On average connection lasts for a minute or so. It also happens periodically when I connect to echo.websocket.org. Is this an inherent property of Tor network or am I missing something here? Perhaps some settings in Polipo's or Tor's config could resolve this?

UPDATE
When I test the same setup on Android using OrBot's localhost:8118 as HTTP proxy my websocket connection is stable. But my question about how to achieve the same stability in Linux environment still stands.

1

There is nothing in Tor itself limiting the lifetime of the TCP connection. Although you do get a new circuit every 10 minutes (by default), if there are any TCP connections alive on the existing circuit it is not shut down until they all close as well.

So, since a WebSocket connection is simply a long-lived TCP connection under the hood, one of the other pieces of your system is probably to blame. I would suggest getting it going without Tor at all and then introduce Tor once you've got it working reliably over "normal" networking conditions.

  • well it does work normally under "normal" networking conditions. I suspect it has something to do with the fact that I test everything on a same machine. I'll also try to change Polipo for Privoxy once I get my hands on that part of app again. – src091 Dec 14 '15 at 11:57
  • Do the "normal" conditions also include Polipo? Perhaps it kills connections after some timeout? – meejah Dec 14 '15 at 19:43
  • No, they don't. Polipo is only used when Tor is at play. – src091 Dec 14 '15 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.