Skip to content

Commit

Permalink
Merge pull request #7707 from egbertbouman/fix_tunnel_helper
Browse files Browse the repository at this point in the history
Fixed exit node script
  • Loading branch information
egbertbouman committed Nov 21, 2023
2 parents e44679b + 43b3f0e commit e1b843f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
48 changes: 17 additions & 31 deletions scripts/exit_node/run_exit_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@
import logging
import os
import re
import signal
import sys
import time
from asyncio import ensure_future, get_event_loop
from asyncio import get_running_loop, run
from ipaddress import AddressValueError, IPv4Address

from ipv8.messaging.anonymization.tunnel import Circuit
from ipv8.taskmanager import TaskManager
from ipv8.util import run_forever

from tribler.core import notifications
from tribler.core.components.bandwidth_accounting.bandwidth_accounting_component import BandwidthAccountingComponent
from tribler.core.components.ipv8.ipv8_component import Ipv8Component
from tribler.core.components.key.key_component import KeyComponent
from tribler.core.components.resource_monitor.resource_monitor_component import ResourceMonitorComponent
from tribler.core.components.restapi.restapi_component import RESTComponent
from tribler.core.components.session import Session
from tribler.core.components.socks_servers.socks_servers_component import SocksServersComponent
from tribler.core.components.tunnel.tunnel_component import TunnelsComponent
from tribler.core.config.tribler_config import TriblerConfig
from tribler.core.utilities.osutils import get_root_state_directory
Expand All @@ -35,9 +33,7 @@ def components_gen():
yield KeyComponent()
yield RESTComponent()
yield Ipv8Component()
yield ResourceMonitorComponent()
yield BandwidthAccountingComponent()
yield SocksServersComponent()
yield TunnelsComponent()


Expand Down Expand Up @@ -98,20 +94,14 @@ def __init__(self):
self.log_circuits = False
self.session = None
self.community = None
self.ipv8 = None

def on_circuit_reject(self, reject_time, balance):
with open(os.path.join(self.session.config.state_dir, "circuit_rejects.log"), 'a') as out_file:
time_millis = int(round(reject_time * 1000))
out_file.write("%d,%d\n" % (time_millis, balance))

def tribler_started(self):
async def signal_handler(sig):
print(f"Received shut down signal {sig}") # noqa: T001
await self.stop()

signal.signal(signal.SIGINT, lambda sig, _: ensure_future(signal_handler(sig)))
signal.signal(signal.SIGTERM, lambda sig, _: ensure_future(signal_handler(sig)))

component = self.session.get_instance(TunnelsComponent)
tunnel_community = component.community
self.register_task("bootstrap", tunnel_community.bootstrap, interval=30)
Expand All @@ -122,20 +112,19 @@ async def signal_handler(sig):
for handler in handlers:
root_logger.removeHandler(handler)
logging.getLogger().setLevel(logging.ERROR)
component = self.session.get_instance(Ipv8Component)
ipv8 = component.ipv8

self.ipv8 = self.session.get_instance(Ipv8Component).ipv8
new_strategies = []
with ipv8.overlay_lock:
for strategy, target_peers in ipv8.strategies:
with self.ipv8.overlay_lock:
for strategy, target_peers in self.ipv8.strategies:
if strategy.overlay == tunnel_community:
new_strategies.append((strategy, -1))
else:
new_strategies.append((strategy, target_peers))
ipv8.strategies = new_strategies
self.ipv8.strategies = new_strategies

def circuit_removed(self, circuit: Circuit, additional_info: str):
ipv8 = Ipv8Component.instance().ipv8
ipv8.network.remove_by_address(circuit.peer.address)
self.ipv8.network.remove_by_address(circuit.peer.address)
if self.log_circuits:
with open(os.path.join(self.session.config.state_dir, "circuits.log"), 'a') as out_file:
duration = time.time() - circuit.creation_time
Expand Down Expand Up @@ -163,7 +152,6 @@ async def stop(self):
self._stopping = True
await self.shutdown_task_manager()
await self.session.shutdown()
get_event_loop().stop()


class PortAction(argparse.Action):
Expand Down Expand Up @@ -199,9 +187,9 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, values)


def main():
async def main():
parser = argparse.ArgumentParser(add_help=False,
description=('Tunnel helper script, starts a (hidden) tunnel as a service'))
description='Tunnel helper script, starts a (hidden) tunnel as a service')
parser.add_argument('--help', '-h', action='help', default=argparse.SUPPRESS,
help='Show this help message and exit')
parser.add_argument('--ipv8_port', '-d', default=-1, type=int, help='IPv8 port', action=PortAction,
Expand All @@ -227,16 +215,14 @@ def main():
parser.add_argument('--fragile', '-f', help='Fail at the first error', action='store_true')

args = parser.parse_args(sys.argv[1:])
service = TunnelHelperService()
loop = get_event_loop()
if args.fragile:
make_async_loop_fragile(loop)

coro = service.start(args)
ensure_future(coro)
make_async_loop_fragile(get_running_loop())

loop.run_forever()
service = TunnelHelperService()
await service.start(args)
await run_forever()
await service.stop()


if __name__ == "__main__":
main()
run(main())
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ async def test_hidden_services(proxy_factory: ProxyFactory, hidden_seeder_comm:
"""
leecher_community = await proxy_factory.get(exitnode=False, start_lt=True)
# We don't want libtorrent peers interfering with the download. This is merely to avoid
# getting "unregistered address" warnings in the logs and should affect the outcome.
# getting "unregistered address" warnings in the logs and should not affect the outcome.
leecher_community.readd_bittorrent_peers = MagicMock() # type: ignore

hidden_seeder_comm.build_tunnels(hops=1)
Expand Down

0 comments on commit e1b843f

Please sign in to comment.