|
| 1 | +import os |
1 | 2 |
|
2 | | -from zope.interface import implementer |
| 3 | +import txtorcon |
| 4 | +from twisted.internet import defer, reactor, task |
| 5 | +from twisted.internet.endpoints import ( |
| 6 | + TCP4ClientEndpoint, |
| 7 | + UNIXClientEndpoint, |
| 8 | + serverFromString, |
| 9 | +) |
3 | 10 | from twisted.internet.error import ReactorNotRunning |
4 | | -from twisted.internet import reactor, defer |
5 | | -from twisted.internet.endpoints import (TCP4ClientEndpoint, |
6 | | - UNIXClientEndpoint, serverFromString) |
7 | 11 | from twisted.web.client import Agent, BrowserLikePolicyForHTTPS |
8 | | -import txtorcon |
| 12 | +from txtorcon import TorConfig, TorControlProtocol |
9 | 13 | from txtorcon.web import tor_agent |
10 | | -from txtorcon import TorControlProtocol, TorConfig |
| 14 | +from zope.interface import implementer |
11 | 15 |
|
12 | 16 | _custom_stop_reactor_is_set = False |
13 | 17 | custom_stop_reactor = None |
@@ -174,44 +178,87 @@ def start_tor(self): |
174 | 178 | """ This function executes the workflow |
175 | 179 | of starting the hidden service and returning its hostname |
176 | 180 | """ |
177 | | - self.info_callback("Attempting to start onion service on port: {} " |
178 | | - "...".format(self.virtual_port)) |
| 181 | + self.info_callback( |
| 182 | + f"Attempting to start onion service on port: {self.virtual_port} ..." |
| 183 | + ) |
| 184 | + |
| 185 | + # Check if using Tor-managed mode (via torrc, not control port) |
| 186 | + if self.hidden_service_dir.startswith("tor-managed:"): |
| 187 | + self.start_tor_managed_onion() |
| 188 | + return |
| 189 | + |
| 190 | + # Ephemeral or txtorcon-managed hidden service (via control port) |
| 191 | + if str(self.tor_control_host).startswith("unix:"): |
| 192 | + control_endpoint = UNIXClientEndpoint(reactor, self.tor_control_host[5:]) |
| 193 | + else: |
| 194 | + control_endpoint = TCP4ClientEndpoint( |
| 195 | + reactor, self.tor_control_host, self.tor_control_port |
| 196 | + ) |
| 197 | + d = txtorcon.connect(reactor, control_endpoint) |
| 198 | + |
179 | 199 | if self.hidden_service_dir == "": |
180 | | - if str(self.tor_control_host).startswith('unix:'): |
181 | | - control_endpoint = UNIXClientEndpoint(reactor, |
182 | | - self.tor_control_host[5:]) |
183 | | - else: |
184 | | - control_endpoint = TCP4ClientEndpoint(reactor, |
185 | | - self.tor_control_host, self.tor_control_port) |
186 | | - d = txtorcon.connect(reactor, control_endpoint) |
| 200 | + # Ephemeral hidden service (no persistence) |
187 | 201 | d.addCallback(self.create_onion_ep) |
188 | 202 | d.addErrback(self.setup_failed) |
189 | | - # TODO: add errbacks to the next two calls in |
190 | | - # the chain: |
191 | 203 | d.addCallback(self.onion_listen) |
192 | 204 | d.addCallback(self.print_host) |
193 | 205 | else: |
194 | | - ep = "onion:" + str(self.virtual_port) + ":localPort=" |
195 | | - ep += str(self.serving_port) |
196 | | - # endpoints.TCPHiddenServiceEndpoint creates version 2 by |
197 | | - # default for backwards compat (err, txtorcon needs to update that ...) |
198 | | - ep += ":version=3" |
199 | | - ep += ":hiddenServiceDir="+self.hidden_service_dir |
200 | | - onion_endpoint = serverFromString(reactor, ep) |
201 | | - d = onion_endpoint.listen(self.proto_factory) |
| 206 | + # txtorcon-managed filesystem hidden service |
| 207 | + d.addCallback(self.create_filesystem_onion_ep) |
| 208 | + d.addErrback(self.setup_failed) |
202 | 209 | d.addCallback(self.print_host_filesystem) |
203 | 210 |
|
204 | | - |
205 | 211 | def setup_failed(self, arg): |
206 | 212 | # Note that actions based on this failure are deferred to callers: |
207 | 213 | self.error_callback("Setup failed: " + str(arg)) |
208 | 214 |
|
209 | 215 | def create_onion_ep(self, t): |
210 | 216 | self.tor_connection = t |
211 | | - portmap_string = config_to_hs_ports(self.virtual_port, |
212 | | - self.serving_host, self.serving_port) |
| 217 | + portmap_string = config_to_hs_ports( |
| 218 | + self.virtual_port, self.serving_host, self.serving_port |
| 219 | + ) |
213 | 220 | return t.create_onion_service( |
214 | | - ports=[portmap_string], private_key=txtorcon.DISCARD) |
| 221 | + ports=[portmap_string], private_key=txtorcon.DISCARD |
| 222 | + ) |
| 223 | + |
| 224 | + def create_filesystem_onion_ep(self, t): |
| 225 | + """Create a persistent hidden service using txtorcon's filesystem support. |
| 226 | + Requires local Tor control port access. |
| 227 | + """ |
| 228 | + self.tor_connection = t |
| 229 | + ep = "onion:" + str(self.virtual_port) + ":localPort=" |
| 230 | + ep += str(self.serving_port) |
| 231 | + ep += ":version=3" |
| 232 | + ep += ":hiddenServiceDir=" + self.hidden_service_dir |
| 233 | + onion_endpoint = serverFromString(reactor, ep) |
| 234 | + return onion_endpoint.listen(self.proto_factory) |
| 235 | + |
| 236 | + def start_tor_managed_onion(self): |
| 237 | + """ |
| 238 | + For Tor-managed hidden services: read hostname, start listening. |
| 239 | + No control port connection needed. |
| 240 | + """ |
| 241 | + hs_dir = self.hidden_service_dir.removeprefix("tor-managed:") |
| 242 | + hostname_file = os.path.join(hs_dir, "hostname") |
| 243 | + |
| 244 | + def check_and_start(): |
| 245 | + if not os.path.exists(hostname_file): |
| 246 | + return |
| 247 | + |
| 248 | + try: |
| 249 | + with open(hostname_file, "r") as f: |
| 250 | + hostname = f.read().strip() |
| 251 | + except Exception as e: |
| 252 | + self.error_callback(f"Failed to read {hostname_file}: {e}") |
| 253 | + poll_loop.stop() |
| 254 | + return |
| 255 | + |
| 256 | + poll_loop.stop() |
| 257 | + self.info_callback(f"Using Tor-managed hidden service: {hostname}") |
| 258 | + self.onion_hostname_callback(hostname) |
| 259 | + |
| 260 | + poll_loop = task.LoopingCall(check_and_start) |
| 261 | + poll_loop.start(0.5) |
215 | 262 |
|
216 | 263 | def onion_listen(self, onion): |
217 | 264 | # 'onion' arg is the created EphemeralOnionService object; |
|
0 commit comments