Store if it's tracker connection on connect

This commit is contained in:
shortcutme 2018-08-26 02:52:23 +02:00
parent 53a8c2d574
commit e05c432d14
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE
4 changed files with 23 additions and 18 deletions

View file

@ -84,12 +84,14 @@ class SiteAnnouncerPlugin(object):
request["delete"] = True request["delete"] = True
# Sent request to tracker # Sent request to tracker
tracker = connection_pool.get(tracker_address) # Re-use tracker connection if possible tracker_peer = connection_pool.get(tracker_address) # Re-use tracker connection if possible
if not tracker: if not tracker_peer:
tracker_ip, tracker_port = tracker_address.split(":") tracker_ip, tracker_port = tracker_address.split(":")
tracker = Peer(tracker_ip, tracker_port, connection_server=self.site.connection_server) tracker_peer = Peer(tracker_ip, tracker_port, connection_server=self.site.connection_server)
connection_pool[tracker_address] = tracker tracker_peer.is_tracker_connection = True
res = tracker.request("announce", request) connection_pool[tracker_address] = tracker_peer
res = tracker_peer.request("announce", request)
if not res or "peers" not in res: if not res or "peers" not in res:
if full_announce: if full_announce:
@ -116,14 +118,14 @@ class SiteAnnouncerPlugin(object):
if publickey not in request["onion_signs"]: if publickey not in request["onion_signs"]:
sign = CryptRsa.sign(res["onion_sign_this"], self.site.connection_server.tor_manager.getPrivatekey(onion)) sign = CryptRsa.sign(res["onion_sign_this"], self.site.connection_server.tor_manager.getPrivatekey(onion))
request["onion_signs"][publickey] = sign request["onion_signs"][publickey] = sign
res = tracker.request("announce", request) res = tracker_peer.request("announce", request)
if not res or "onion_sign_this" in res: if not res or "onion_sign_this" in res:
if full_announce: if full_announce:
time_full_announced[tracker_address] = 0 time_full_announced[tracker_address] = 0
raise AnnounceError("Announce onion address to failed: %s" % res) raise AnnounceError("Announce onion address to failed: %s" % res)
if full_announce: if full_announce:
tracker.remove() # Close connection, we don't need it in next 5 minute tracker_peer.remove() # Close connection, we don't need it in next 5 minute
self.site.log.debug( self.site.log.debug(
"Tracker announce result: zero://%s (sites: %s, new peers: %s) in %.3fs" % "Tracker announce result: zero://%s (sites: %s, new peers: %s) in %.3fs" %

View file

@ -19,12 +19,12 @@ from util import helper
class Connection(object): class Connection(object):
__slots__ = ( __slots__ = (
"sock", "sock_wrapped", "ip", "port", "cert_pin", "target_onion", "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", "is_private_ip", "handshake", "crypt", "connected", "event_connected", "closed", "start_time", "last_recv_time", "is_private_ip", "is_tracker_connection",
"last_message_time", "last_send_time", "last_sent_time", "incomplete_buff_recv", "bytes_recv", "bytes_sent", "cpu_time", "send_lock", "last_message_time", "last_send_time", "last_sent_time", "incomplete_buff_recv", "bytes_recv", "bytes_sent", "cpu_time", "send_lock",
"last_ping_delay", "last_req_time", "last_cmd_sent", "last_cmd_recv", "bad_actions", "sites", "name", "updateName", "waiting_requests", "waiting_streams" "last_ping_delay", "last_req_time", "last_cmd_sent", "last_cmd_recv", "bad_actions", "sites", "name", "updateName", "waiting_requests", "waiting_streams"
) )
def __init__(self, server, ip, port, sock=None, target_onion=None): def __init__(self, server, ip, port, sock=None, target_onion=None, is_tracker_connection=False):
self.sock = sock self.sock = sock
self.ip = ip self.ip = ip
self.port = port self.port = port
@ -41,6 +41,7 @@ class Connection(object):
self.is_private_ip = True self.is_private_ip = True
else: else:
self.is_private_ip = False self.is_private_ip = False
self.is_tracker_connection = is_tracker_connection
self.server = server self.server = server
self.unpacker = None # Stream incoming socket messages here self.unpacker = None # Stream incoming socket messages here
@ -112,7 +113,7 @@ class Connection(object):
self.sock = self.server.tor_manager.createSocket(self.ip, self.port) self.sock = self.server.tor_manager.createSocket(self.ip, self.port)
elif config.tor == "always" and helper.isPrivateIp(self.ip) and self.ip not in config.ip_local: elif config.tor == "always" and helper.isPrivateIp(self.ip) and self.ip not in config.ip_local:
raise Exception("Can't connect to local IPs in Tor: always mode") raise Exception("Can't connect to local IPs in Tor: always mode")
elif config.trackers_proxy != "disable" and self.cert_pin and "zero://%s#%s:%s" % (self.ip, self.cert_pin, self.port) in config.trackers: elif config.trackers_proxy != "disable" and self.is_tracker_connection:
if config.trackers_proxy == "tor": if config.trackers_proxy == "tor":
self.sock = self.server.tor_manager.createSocket(self.ip, self.port) self.sock = self.server.tor_manager.createSocket(self.ip, self.port)
else: else:

View file

@ -117,7 +117,7 @@ class ConnectionServer(object):
def handleMessage(self, *args, **kwargs): def handleMessage(self, *args, **kwargs):
pass pass
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, is_tracker_connection=False):
if (ip.endswith(".onion") or self.port_opened == False) and self.tor_manager.start_onions and site: # Site-unique connection for Tor if (ip.endswith(".onion") or self.port_opened == False) and self.tor_manager.start_onions and site: # Site-unique connection for Tor
if ip.endswith(".onion"): if ip.endswith(".onion"):
site_onion = self.tor_manager.getOnion(site.address) site_onion = self.tor_manager.getOnion(site.address)
@ -161,9 +161,9 @@ class ConnectionServer(object):
try: try:
if (ip.endswith(".onion") or self.port_opened == False) and self.tor_manager.start_onions and site: # Lock connection to site if (ip.endswith(".onion") or self.port_opened == False) and self.tor_manager.start_onions and site: # Lock connection to site
connection = Connection(self, ip, port, target_onion=site_onion) connection = Connection(self, ip, port, target_onion=site_onion, is_tracker_connection=is_tracker_connection)
else: else:
connection = Connection(self, ip, port) connection = Connection(self, ip, port, is_tracker_connection=is_tracker_connection)
self.ips[key] = connection self.ips[key] = connection
self.connections.append(connection) self.connections.append(connection)
succ = connection.connect() succ = connection.connect()

View file

@ -19,7 +19,7 @@ if config.use_tempfiles:
@PluginManager.acceptPlugins @PluginManager.acceptPlugins
class Peer(object): class Peer(object):
__slots__ = ( __slots__ = (
"ip", "port", "site", "key", "connection", "connection_server", "time_found", "time_response", "time_hashfield", "time_added", "has_hashfield", "ip", "port", "site", "key", "connection", "connection_server", "time_found", "time_response", "time_hashfield", "time_added", "has_hashfield", "is_tracker_connection",
"time_my_hashfield_sent", "last_ping", "reputation", "last_content_json_update", "hashfield", "connection_error", "hash_failed", "download_bytes", "download_time" "time_my_hashfield_sent", "last_ping", "reputation", "last_content_json_update", "hashfield", "connection_error", "hash_failed", "download_bytes", "download_time"
) )
@ -38,6 +38,7 @@ class Peer(object):
self.time_response = None # Time of last successful response from peer self.time_response = None # Time of last successful response from peer
self.time_added = time.time() self.time_added = time.time()
self.last_ping = None # Last response time for ping self.last_ping = None # Last response time for ping
self.is_tracker_connection = False # Tracker connection instead of normal peer
self.reputation = 0 # More likely to connect if larger self.reputation = 0 # More likely to connect if larger
self.last_content_json_update = 0.0 # Modify date of last received content.json self.last_content_json_update = 0.0 # Modify date of last received content.json
@ -79,18 +80,19 @@ class Peer(object):
try: try:
if self.connection_server: if self.connection_server:
self.connection = self.connection_server.getConnection(self.ip, self.port, site=self.site) connection_server = self.connection_server
elif self.site: elif self.site:
self.connection = self.site.connection_server.getConnection(self.ip, self.port, site=self.site) connection_server = self.site.connection_server
else: else:
self.connection = sys.modules["main"].file_server.getConnection(self.ip, self.port, site=self.site) connection_server = sys.modules["main"].file_server
self.connection = connection_server.getConnection(self.ip, self.port, site=self.site, is_tracker_connection=self.is_tracker_connection)
self.connection.sites += 1 self.connection.sites += 1
except Exception, err: except Exception, err:
self.onConnectionError("Getting connection error") self.onConnectionError("Getting connection error")
self.log("Getting connection error: %s (connection_error: %s, hash_failed: %s)" % self.log("Getting connection error: %s (connection_error: %s, hash_failed: %s)" %
(Debug.formatException(err), self.connection_error, self.hash_failed)) (Debug.formatException(err), self.connection_error, self.hash_failed))
self.connection = None self.connection = None
return self.connection
# Check if we have connection to peer # Check if we have connection to peer
def findConnection(self): def findConnection(self):