diff --git a/src/Connection/Connection.py b/src/Connection/Connection.py index 38d58035..21089449 100644 --- a/src/Connection/Connection.py +++ b/src/Connection/Connection.py @@ -12,20 +12,20 @@ from Crypt import CryptConnection class Connection(object): __slots__ = ( - "sock", "sock_wrapped", "ip", "port", "cert_pin", "site_lock", "id", "protocol", "type", "server", "unpacker", "req_id", + "sock", "sock_wrapped", "ip", "port", "cert_pin", "target_onion", "id", "protocol", "type", "server", "unpacker", "req_id", "handshake", "crypt", "connected", "event_connected", "closed", "start_time", "last_recv_time", "last_message_time", "last_send_time", "last_sent_time", "incomplete_buff_recv", "bytes_recv", "bytes_sent", "cpu_time", "last_ping_delay", "last_req_time", "last_cmd", "bad_actions", "sites", "name", "updateName", "waiting_requests", "waiting_streams" ) - def __init__(self, server, ip, port, sock=None, site_lock=None): + def __init__(self, server, ip, port, sock=None, target_onion=None): self.sock = sock self.ip = ip self.port = port self.cert_pin = None if "#" in ip: self.ip, self.cert_pin = ip.split("#") - self.site_lock = site_lock # Only this site requests allowed (for Tor) + self.target_onion = target_onion # Requested onion adress self.id = server.last_connection_id server.last_connection_id += 1 self.protocol = "?" @@ -76,6 +76,9 @@ class Connection(object): def log(self, text): self.server.log.debug("%s > %s" % (self.name, text)) + def getValidSites(self): + return [key for key, val in self.server.tor_manager.site_onions.items() if val == self.target_onion] + def badAction(self, weight=1): self.bad_actions += weight if self.bad_actions > 40: @@ -83,7 +86,6 @@ class Connection(object): elif self.bad_actions > 20: time.sleep(5) - def goodAction(self): self.bad_actions = 0 @@ -181,13 +183,10 @@ class Connection(object): else: peer_id = self.server.peer_id # Setup peer lock from requested onion address - if self.handshake and self.handshake.get("target_ip", "").endswith(".onion"): - target_onion = self.handshake.get("target_ip").replace(".onion", "") # My onion address - onion_sites = {v: k for k, v in self.server.tor_manager.site_onions.items()} # Inverse, Onion: Site address - self.site_lock = onion_sites.get(target_onion) - if not self.site_lock: - self.server.log.warning("Unknown target onion address: %s" % target_onion) - self.site_lock = "unknown" + if self.handshake and self.handshake.get("target_ip", "").endswith(".onion") and self.server.tor_manager.start_onions: + self.target_onion = self.handshake.get("target_ip").replace(".onion", "") # My onion address + if not self.server.tor_manager.site_onions.values(): + self.server.log.warning("Unknown target onion address: %s" % self.target_onion) handshake = { "version": config.version, @@ -200,8 +199,8 @@ class Connection(object): "crypt_supported": crypt_supported, "crypt": self.crypt } - if self.site_lock: - handshake["onion"] = self.server.tor_manager.getOnion(self.site_lock) + if self.target_onion: + handshake["onion"] = self.target_onion elif self.ip.endswith(".onion"): handshake["onion"] = self.server.tor_manager.getOnion("global") diff --git a/src/Connection/ConnectionServer.py b/src/Connection/ConnectionServer.py index ee42aba7..0fd68bee 100644 --- a/src/Connection/ConnectionServer.py +++ b/src/Connection/ConnectionServer.py @@ -97,7 +97,8 @@ class ConnectionServer: def getConnection(self, ip=None, port=None, peer_id=None, create=True, site=None): if ip.endswith(".onion") and self.tor_manager.start_onions and site: # Site-unique connection for Tor - key = ip + site.address + site_onion = self.tor_manager.getOnion(site.address) + key = ip + site_onion else: key = ip @@ -116,7 +117,7 @@ class ConnectionServer: if connection.ip == ip: if peer_id and connection.handshake.get("peer_id") != peer_id: # Does not match continue - if ip.endswith(".onion") and self.tor_manager.start_onions and connection.site_lock != site.address: + if ip.endswith(".onion") and self.tor_manager.start_onions and ip.replace(".onion", "") != connection.target_onion: # For different site continue if not connection.connected and create: @@ -131,7 +132,7 @@ class ConnectionServer: raise Exception("This peer is not connectable") try: if ip.endswith(".onion") and self.tor_manager.start_onions and site: # Lock connection to site - connection = Connection(self, ip, port, site_lock=site.address) + connection = Connection(self, ip, port, target_onion=ip.replace(".onion", "")) else: connection = Connection(self, ip, port) self.ips[key] = connection @@ -153,8 +154,9 @@ class ConnectionServer: if self.ips.get(connection.ip) == connection: del self.ips[connection.ip] # Site locked connection - if connection.site_lock and self.ips.get(connection.ip + connection.site_lock) == connection: - del self.ips[connection.ip + connection.site_lock] + if connection.target_onion: + if self.ips.get(connection.ip + connection.target_onion) == connection: + del self.ips[connection.ip + connection.target_onion] # Cert pinned connection if connection.cert_pin and self.ips.get(connection.ip + "#" + connection.cert_pin) == connection: del self.ips[connection.ip + "#" + connection.cert_pin]