txtorcon icon indicating copy to clipboard operation
txtorcon copied to clipboard

TCPHiddenServiceEndpoint.listen not respecting local_port

Open AdamISZ opened this issue 3 years ago • 1 comments

Hi, I have previously used this codebase to start up ephemeral onion services with no issues, however I encountered an issue when using it to start a persistent onion service. In my torrc I have configured HiddenServicePort in the usual way; 80 for the public port, and a specific chosen port is entered (say 8080) for the local port.

When I ran a test using the example code, but setting my endpoint string like this:

"onion:80:controlPort=9051:localPort=8080:hiddenServiceDir=/my/hidserv/dir"

... I found that the local port is generated randomly, rather than being 8080. In the code for TCPHiddenServiceEndpoint.listen() it's clear why:

https://github.com/meejah/txtorcon/blob/0c416cc8fe18b913cd0c7422935885a1bfecf4c0/txtorcon/endpoints.py#L562-L569

The variable self.local_port was set in the constructor as intended (here, to 8080) but the string used in the serverFromString call just puts 0 so we get a fresh port. I verified that this could be fixed with something like:

        if self.local_port is None:
            serverstring = 'tcp:0:interface=127.0.0.1'
        else:
            serverstring = 'tcp:{}:interface=127.0.0.1'.format(self.local_port)
        self.tcp_endpoint = serverFromString(
            self._reactor,
            serverstring,
        )

... not that that is more than a testing patch, but just to concretize the point. It works fine after that (i.e. the hidden service is accessible).

AdamISZ avatar Dec 29 '20 16:12 AdamISZ

Thanks for the clear report! This does indeed sound like a bug.

(Sorry for the delay responding. I believe I now have a better way to pay attention to github notifications...)

If you're up for trying a PR, that'd be great!

meejah avatar Apr 01 '21 23:04 meejah