meejah / txtorcon

Twisted-based asynchronous Tor control protocol implementation. Includes unit-tests, examples, state-tracking code and configuration abstraction.
http://fjblvrw2jrxnhtg67qpbzi45r7ofojaoo3orzykesly2j3c2m3htapid.onion/
MIT License
250 stars 72 forks source link

TCPHiddenServiceEndpoint.listen not respecting local_port #347

Open AdamISZ opened 3 years ago

AdamISZ commented 3 years ago

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).

meejah commented 3 years ago

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!