Store requested onion address instead of site_lock
This commit is contained in:
parent
75d94aaf06
commit
c5c0df838f
2 changed files with 19 additions and 18 deletions
|
@ -12,20 +12,20 @@ from Crypt import CryptConnection
|
||||||
|
|
||||||
class Connection(object):
|
class Connection(object):
|
||||||
__slots__ = (
|
__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",
|
"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_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"
|
"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.sock = sock
|
||||||
self.ip = ip
|
self.ip = ip
|
||||||
self.port = port
|
self.port = port
|
||||||
self.cert_pin = None
|
self.cert_pin = None
|
||||||
if "#" in ip:
|
if "#" in ip:
|
||||||
self.ip, self.cert_pin = ip.split("#")
|
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
|
self.id = server.last_connection_id
|
||||||
server.last_connection_id += 1
|
server.last_connection_id += 1
|
||||||
self.protocol = "?"
|
self.protocol = "?"
|
||||||
|
@ -76,6 +76,9 @@ class Connection(object):
|
||||||
def log(self, text):
|
def log(self, text):
|
||||||
self.server.log.debug("%s > %s" % (self.name, 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):
|
def badAction(self, weight=1):
|
||||||
self.bad_actions += weight
|
self.bad_actions += weight
|
||||||
if self.bad_actions > 40:
|
if self.bad_actions > 40:
|
||||||
|
@ -83,7 +86,6 @@ class Connection(object):
|
||||||
elif self.bad_actions > 20:
|
elif self.bad_actions > 20:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
def goodAction(self):
|
def goodAction(self):
|
||||||
self.bad_actions = 0
|
self.bad_actions = 0
|
||||||
|
|
||||||
|
@ -181,13 +183,10 @@ class Connection(object):
|
||||||
else:
|
else:
|
||||||
peer_id = self.server.peer_id
|
peer_id = self.server.peer_id
|
||||||
# Setup peer lock from requested onion address
|
# Setup peer lock from requested onion address
|
||||||
if self.handshake and self.handshake.get("target_ip", "").endswith(".onion"):
|
if self.handshake and self.handshake.get("target_ip", "").endswith(".onion") and self.server.tor_manager.start_onions:
|
||||||
target_onion = self.handshake.get("target_ip").replace(".onion", "") # My onion address
|
self.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
|
if not self.server.tor_manager.site_onions.values():
|
||||||
self.site_lock = onion_sites.get(target_onion)
|
self.server.log.warning("Unknown target onion address: %s" % self.target_onion)
|
||||||
if not self.site_lock:
|
|
||||||
self.server.log.warning("Unknown target onion address: %s" % target_onion)
|
|
||||||
self.site_lock = "unknown"
|
|
||||||
|
|
||||||
handshake = {
|
handshake = {
|
||||||
"version": config.version,
|
"version": config.version,
|
||||||
|
@ -200,8 +199,8 @@ class Connection(object):
|
||||||
"crypt_supported": crypt_supported,
|
"crypt_supported": crypt_supported,
|
||||||
"crypt": self.crypt
|
"crypt": self.crypt
|
||||||
}
|
}
|
||||||
if self.site_lock:
|
if self.target_onion:
|
||||||
handshake["onion"] = self.server.tor_manager.getOnion(self.site_lock)
|
handshake["onion"] = self.target_onion
|
||||||
elif self.ip.endswith(".onion"):
|
elif self.ip.endswith(".onion"):
|
||||||
handshake["onion"] = self.server.tor_manager.getOnion("global")
|
handshake["onion"] = self.server.tor_manager.getOnion("global")
|
||||||
|
|
||||||
|
|
|
@ -97,7 +97,8 @@ class ConnectionServer:
|
||||||
|
|
||||||
def getConnection(self, ip=None, port=None, peer_id=None, create=True, site=None):
|
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
|
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:
|
else:
|
||||||
key = ip
|
key = ip
|
||||||
|
|
||||||
|
@ -116,7 +117,7 @@ class ConnectionServer:
|
||||||
if connection.ip == ip:
|
if connection.ip == ip:
|
||||||
if peer_id and connection.handshake.get("peer_id") != peer_id: # Does not match
|
if peer_id and connection.handshake.get("peer_id") != peer_id: # Does not match
|
||||||
continue
|
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
|
# For different site
|
||||||
continue
|
continue
|
||||||
if not connection.connected and create:
|
if not connection.connected and create:
|
||||||
|
@ -131,7 +132,7 @@ class ConnectionServer:
|
||||||
raise Exception("This peer is not connectable")
|
raise Exception("This peer is not connectable")
|
||||||
try:
|
try:
|
||||||
if ip.endswith(".onion") and self.tor_manager.start_onions and site: # Lock connection to site
|
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:
|
else:
|
||||||
connection = Connection(self, ip, port)
|
connection = Connection(self, ip, port)
|
||||||
self.ips[key] = connection
|
self.ips[key] = connection
|
||||||
|
@ -153,8 +154,9 @@ class ConnectionServer:
|
||||||
if self.ips.get(connection.ip) == connection:
|
if self.ips.get(connection.ip) == connection:
|
||||||
del self.ips[connection.ip]
|
del self.ips[connection.ip]
|
||||||
# Site locked connection
|
# Site locked connection
|
||||||
if connection.site_lock and self.ips.get(connection.ip + connection.site_lock) == connection:
|
if connection.target_onion:
|
||||||
del self.ips[connection.ip + connection.site_lock]
|
if self.ips.get(connection.ip + connection.target_onion) == connection:
|
||||||
|
del self.ips[connection.ip + connection.target_onion]
|
||||||
# Cert pinned connection
|
# Cert pinned connection
|
||||||
if connection.cert_pin and self.ips.get(connection.ip + "#" + connection.cert_pin) == connection:
|
if connection.cert_pin and self.ips.get(connection.ip + "#" + connection.cert_pin) == connection:
|
||||||
del self.ips[connection.ip + "#" + connection.cert_pin]
|
del self.ips[connection.ip + "#" + connection.cert_pin]
|
||||||
|
|
Loading…
Reference in a new issue