|
49 | 49 | from twisted.internet.task import LoopingCall |
50 | 50 | from tor2web import __version__ |
51 | 51 | from tor2web.utils.config import Config |
52 | | -from tor2web.utils.daemon import T2WDaemon, set_pdeathsig, set_proctitle |
| 52 | +from tor2web.utils.daemon import Daemon, set_pdeathsig, set_proctitle |
53 | 53 | from tor2web.utils.hostsmap import HostsMap |
54 | 54 | from tor2web.utils.lists import LimitedSizeDict, List, TorExitNodeList |
55 | 55 | from tor2web.utils.mail import sendmail, sendexceptionmail |
@@ -1216,6 +1216,108 @@ def registerProtocol(self, p): |
1216 | 1216 | except Exception: |
1217 | 1217 | pass |
1218 | 1218 |
|
| 1219 | +def open_listenin_socket(ip, port): |
| 1220 | + try: |
| 1221 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1222 | + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 1223 | + s.setblocking(False) |
| 1224 | + s.bind((ip, port)) |
| 1225 | + s.listen(1024) |
| 1226 | + return s |
| 1227 | + except Exception as e: |
| 1228 | + print "Tor2web Startup Failure: error while binding on %s %s (%s)" % (ip, port, e) |
| 1229 | + exit(1) |
| 1230 | + |
| 1231 | + |
| 1232 | +class T2WDaemon(Daemon): |
| 1233 | + def daemon_init(self): |
| 1234 | + self.quitting = False |
| 1235 | + self.subprocesses = [] |
| 1236 | + |
| 1237 | + self.rpc_server = T2WRPCServer(self.config) |
| 1238 | + |
| 1239 | + self.socket_rpc = open_listenin_socket('127.0.0.1', 8789) |
| 1240 | + |
| 1241 | + self.childFDs = {0: 0, 1: 1, 2: 2} |
| 1242 | + |
| 1243 | + self.fds = [] |
| 1244 | + |
| 1245 | + self.fds_https = self.fds_http = '' |
| 1246 | + |
| 1247 | + i_https = i_http = 0 |
| 1248 | + |
| 1249 | + for ip in [ipv4, ipv6]: |
| 1250 | + if ip is None: |
| 1251 | + continue |
| 1252 | + |
| 1253 | + if self.config.transport in ('HTTPS', 'BOTH'): |
| 1254 | + if i_https: |
| 1255 | + self.fds_https += ',' |
| 1256 | + |
| 1257 | + s = open_listenin_socket(ip, self.config.listen_port_https) |
| 1258 | + self.fds.append(s) |
| 1259 | + self.childFDs[s.fileno()] = s.fileno() |
| 1260 | + self.fds_https += str(s.fileno()) |
| 1261 | + i_https += 1 |
| 1262 | + |
| 1263 | + if self.config.transport in ('HTTP', 'BOTH'): |
| 1264 | + if i_http: |
| 1265 | + self.fds_http += ',' |
| 1266 | + |
| 1267 | + s = open_listenin_socket(ip, self.config.listen_port_http) |
| 1268 | + self.fds.append(s) |
| 1269 | + self.childFDs[s.fileno()] = s.fileno() |
| 1270 | + self.fds_http += str(s.fileno()) |
| 1271 | + i_http += 1 |
| 1272 | + |
| 1273 | + def daemon_main(self): |
| 1274 | + if self.config.logreqs: |
| 1275 | + self.logfile_access = logfile.DailyLogFile.fromFullPath(os.path.join(self.config.datadir, 'logs', 'access.log')) |
| 1276 | + else: |
| 1277 | + self.logfile_access = log.NullFile() |
| 1278 | + |
| 1279 | + if self.config.debugmode: |
| 1280 | + if self.config.debugtostdout and self.config.nodaemon: |
| 1281 | + self.logfile_debug = sys.stdout |
| 1282 | + else: |
| 1283 | + self.logfile_debug = logfile.DailyLogFile.fromFullPath( |
| 1284 | + os.path.join(self.config.datadir, 'logs', 'debug.log')) |
| 1285 | + else: |
| 1286 | + self.logfile_debug = log.NullFile() |
| 1287 | + |
| 1288 | + log.startLogging(self.logfile_debug) |
| 1289 | + |
| 1290 | + reactor.listenTCPonExistingFD = listenTCPonExistingFD |
| 1291 | + |
| 1292 | + reactor.listenUNIX(os.path.join(self.config.rundir, 'rpc.socket'), factory=pb.PBServerFactory(self.rpc_server)) |
| 1293 | + |
| 1294 | + if not self.config.disable_gettor: |
| 1295 | + LoopingCall(getTorTask, self.config).start(3600) |
| 1296 | + |
| 1297 | + for i in range(self.config.processes): |
| 1298 | + subprocess = spawnT2W(self, self.childFDs, self.fds_https, self.fds_http) |
| 1299 | + self.subprocesses.append(subprocess.pid) |
| 1300 | + |
| 1301 | + def MailException(etype, value, tb): |
| 1302 | + sendexceptionmail(self.config, etype, value, tb) |
| 1303 | + |
| 1304 | + if self.config.smtpmailto_exceptions: |
| 1305 | + # if self.config.smtp_mail is configured we change the excepthook |
| 1306 | + sys.excepthook = MailException |
| 1307 | + |
| 1308 | + reactor.run() |
| 1309 | + |
| 1310 | + def daemon_reload(self): |
| 1311 | + self.rpc_server.load_lists() |
| 1312 | + |
| 1313 | + def daemon_shutdown(self): |
| 1314 | + self.quitting = True |
| 1315 | + |
| 1316 | + for pid in self.subprocesses: |
| 1317 | + os.kill(pid, signal.SIGINT) |
| 1318 | + |
| 1319 | + self.subprocesses = [] |
| 1320 | + |
1219 | 1321 |
|
1220 | 1322 | def start_worker(): |
1221 | 1323 | LoopingCall(updateListsTask).start(600) |
@@ -1430,122 +1532,20 @@ def test_file_access(f): |
1430 | 1532 |
|
1431 | 1533 | ports = [] |
1432 | 1534 |
|
| 1535 | +def nullStartedConnecting(self, connector): |
| 1536 | + pass |
| 1537 | + |
1433 | 1538 | pool = client.HTTPConnectionPool(reactor, True) |
1434 | 1539 | pool.maxPersistentPerHost = config.sockmaxpersistentperhost |
1435 | 1540 | pool.cachedConnectionTimeout = config.sockcachedconnectiontimeout |
1436 | 1541 | pool.retryAutomatically = config.sockretryautomatically |
1437 | | -def nullStartedConnecting(self, connector): pass |
1438 | 1542 | pool._factory.startedConnecting = nullStartedConnecting |
1439 | 1543 |
|
| 1544 | + |
1440 | 1545 | if 'T2W_FDS_HTTPS' not in os.environ and 'T2W_FDS_HTTP' not in os.environ: |
1441 | 1546 | set_proctitle("tor2web") |
1442 | 1547 |
|
1443 | | - def open_listenin_socket(ip, port): |
1444 | | - try: |
1445 | | - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
1446 | | - s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
1447 | | - s.setblocking(False) |
1448 | | - s.bind((ip, port)) |
1449 | | - s.listen(1024) |
1450 | | - return s |
1451 | | - except Exception as e: |
1452 | | - print "Tor2web Startup Failure: error while binding on %s %s (%s)" % (ip, port, e) |
1453 | | - exit(1) |
1454 | | - |
1455 | | - def daemon_init(self): |
1456 | | - self.quitting = False |
1457 | | - self.subprocesses = [] |
1458 | | - |
1459 | | - self.socket_rpc = open_listenin_socket('127.0.0.1', 8789) |
1460 | | - |
1461 | | - self.childFDs = {0: 0, 1: 1, 2: 2} |
1462 | | - |
1463 | | - self.fds = [] |
1464 | | - |
1465 | | - self.fds_https = self.fds_http = '' |
1466 | | - |
1467 | | - i_https = i_http = 0 |
1468 | | - |
1469 | | - for ip in [ipv4, ipv6]: |
1470 | | - if ip is None: |
1471 | | - continue |
1472 | | - |
1473 | | - if config.transport in ('HTTPS', 'BOTH'): |
1474 | | - if i_https: |
1475 | | - self.fds_https += ',' |
1476 | | - |
1477 | | - s = open_listenin_socket(ip, config.listen_port_https) |
1478 | | - self.fds.append(s) |
1479 | | - self.childFDs[s.fileno()] = s.fileno() |
1480 | | - self.fds_https += str(s.fileno()) |
1481 | | - i_https += 1 |
1482 | | - |
1483 | | - if config.transport in ('HTTP', 'BOTH'): |
1484 | | - if i_http: |
1485 | | - self.fds_http += ',' |
1486 | | - |
1487 | | - s = open_listenin_socket(ip, config.listen_port_http) |
1488 | | - self.fds.append(s) |
1489 | | - self.childFDs[s.fileno()] = s.fileno() |
1490 | | - self.fds_http += str(s.fileno()) |
1491 | | - i_http += 1 |
1492 | | - |
1493 | | - |
1494 | | - def daemon_main(self): |
1495 | | - if config.logreqs: |
1496 | | - self.logfile_access = logfile.DailyLogFile.fromFullPath(os.path.join(config.datadir, 'logs', 'access.log')) |
1497 | | - else: |
1498 | | - self.logfile_access = log.NullFile() |
1499 | | - |
1500 | | - if config.debugmode: |
1501 | | - if config.debugtostdout and config.nodaemon: |
1502 | | - self.logfile_debug = sys.stdout |
1503 | | - else: |
1504 | | - self.logfile_debug = logfile.DailyLogFile.fromFullPath( |
1505 | | - os.path.join(config.datadir, 'logs', 'debug.log')) |
1506 | | - else: |
1507 | | - self.logfile_debug = log.NullFile() |
1508 | | - |
1509 | | - log.startLogging(self.logfile_debug) |
1510 | | - |
1511 | | - reactor.listenTCPonExistingFD = listenTCPonExistingFD |
1512 | | - |
1513 | | - reactor.listenUNIX(os.path.join(config.rundir, 'rpc.socket'), factory=pb.PBServerFactory(self.rpc_server)) |
1514 | | - |
1515 | | - if not config.disable_gettor: |
1516 | | - gtt = LoopingCall(getTorTask, config) |
1517 | | - gtt.start(3600) |
1518 | | - |
1519 | | - for i in range(config.processes): |
1520 | | - subprocess = spawnT2W(self, self.childFDs, self.fds_https, self.fds_http) |
1521 | | - self.subprocesses.append(subprocess.pid) |
1522 | | - |
1523 | | - def MailException(etype, value, tb): |
1524 | | - sendexceptionmail(config, etype, value, tb) |
1525 | | - |
1526 | | - if config.smtpmailto_exceptions: |
1527 | | - # if config.smtp_mail is configured we change the excepthook |
1528 | | - sys.excepthook = MailException |
1529 | | - |
1530 | | - reactor.run() |
1531 | | - |
1532 | | - def daemon_reload(self): |
1533 | | - self.rpc_server.load_lists() |
1534 | | - |
1535 | | - def daemon_shutdown(self): |
1536 | | - self.quitting = True |
1537 | | - |
1538 | | - for pid in self.subprocesses: |
1539 | | - os.kill(pid, signal.SIGINT) |
1540 | | - |
1541 | | - self.subprocesses = [] |
1542 | | - |
1543 | 1548 | t2w_daemon = T2WDaemon(config) |
1544 | | - t2w_daemon.daemon_init = daemon_init |
1545 | | - t2w_daemon.daemon_main = daemon_main |
1546 | | - t2w_daemon.daemon_reload = daemon_reload |
1547 | | - t2w_daemon.daemon_shutdown = daemon_shutdown |
1548 | | - t2w_daemon.rpc_server = T2WRPCServer(config) |
1549 | 1549 |
|
1550 | 1550 | t2w_daemon.run() |
1551 | 1551 |
|
|
0 commit comments