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