Generalize tracker logging
This commit is contained in:
parent
13e651c822
commit
64b4789bc1
2 changed files with 29 additions and 15 deletions
|
@ -1,4 +1,4 @@
|
||||||
import hashlib
|
from Config import config
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from Plugin import PluginManager
|
from Plugin import PluginManager
|
||||||
|
@ -12,13 +12,13 @@ connection_pool = {} # Tracker address: Peer object
|
||||||
|
|
||||||
# We can only import plugin host clases after the plugins are loaded
|
# We can only import plugin host clases after the plugins are loaded
|
||||||
@PluginManager.afterLoad
|
@PluginManager.afterLoad
|
||||||
def importErrors():
|
def importPeers():
|
||||||
global Peer
|
global Peer
|
||||||
from Peer import Peer
|
from Peer import Peer
|
||||||
|
|
||||||
|
|
||||||
# Process result got back from tracker
|
# Process result got back from tracker
|
||||||
def processPeerRes(site, peers):
|
def processPeerRes(tracker_address, site, peers):
|
||||||
added = 0
|
added = 0
|
||||||
# Ip4
|
# Ip4
|
||||||
found_ip4 = 0
|
found_ip4 = 0
|
||||||
|
@ -38,7 +38,7 @@ def processPeerRes(site, peers):
|
||||||
if added:
|
if added:
|
||||||
site.worker_manager.onPeers()
|
site.worker_manager.onPeers()
|
||||||
site.updateWebsocket(peers_added=added)
|
site.updateWebsocket(peers_added=added)
|
||||||
site.log.debug("Found %s ip4, %s onion peers, new: %s" % (found_ip4, found_onion, added))
|
return added
|
||||||
|
|
||||||
|
|
||||||
@PluginManager.registerTo("Site")
|
@PluginManager.registerTo("Site")
|
||||||
|
@ -89,16 +89,17 @@ class SitePlugin(object):
|
||||||
res = tracker.request("announce", request)
|
res = tracker.request("announce", request)
|
||||||
|
|
||||||
if not res or "peers" not in res:
|
if not res or "peers" not in res:
|
||||||
self.log.debug("Announce to %s failed: %s" % (tracker_address, res))
|
self.log.warning("Tracker error: zero://%s (%s)" % (tracker_address, res))
|
||||||
if full_announce:
|
if full_announce:
|
||||||
time_full_announced[tracker_address] = 0
|
time_full_announced[tracker_address] = 0
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Add peers from response to site
|
# Add peers from response to site
|
||||||
site_index = 0
|
site_index = 0
|
||||||
|
peers_added = 0
|
||||||
for site_res in res["peers"]:
|
for site_res in res["peers"]:
|
||||||
site = sites[site_index]
|
site = sites[site_index]
|
||||||
processPeerRes(site, site_res)
|
peers_added += processPeerRes(tracker_address, site, site_res)
|
||||||
site_index += 1
|
site_index += 1
|
||||||
|
|
||||||
# Check if we need to sign prove the onion addresses
|
# Check if we need to sign prove the onion addresses
|
||||||
|
@ -115,7 +116,7 @@ class SitePlugin(object):
|
||||||
request["onion_signs"][publickey] = sign
|
request["onion_signs"][publickey] = sign
|
||||||
res = tracker.request("announce", request)
|
res = tracker.request("announce", request)
|
||||||
if not res or "onion_sign_this" in res:
|
if not res or "onion_sign_this" in res:
|
||||||
self.log.debug("Announce onion address to %s failed: %s" % (tracker_address, res))
|
self.log.warning("Tracker error: %s (Announce onion address to failed: %s)" % (tracker_address, res))
|
||||||
if full_announce:
|
if full_announce:
|
||||||
time_full_announced[tracker_address] = 0
|
time_full_announced[tracker_address] = 0
|
||||||
return False
|
return False
|
||||||
|
@ -123,4 +124,9 @@ class SitePlugin(object):
|
||||||
if full_announce:
|
if full_announce:
|
||||||
tracker.remove() # Close connection, we don't need it in next 5 minute
|
tracker.remove() # Close connection, we don't need it in next 5 minute
|
||||||
|
|
||||||
|
self.log.debug(
|
||||||
|
"Tracker result: zero://%s (sites: %s, new: %s)" %
|
||||||
|
(tracker_address, site_index, peers_added)
|
||||||
|
)
|
||||||
|
|
||||||
return time.time() - s
|
return time.time() - s
|
||||||
|
|
|
@ -832,8 +832,12 @@ class Site(object):
|
||||||
tracker.poll_once()
|
tracker.poll_once()
|
||||||
tracker.announce(info_hash=hashlib.sha1(self.address).hexdigest(), num_want=50)
|
tracker.announce(info_hash=hashlib.sha1(self.address).hexdigest(), num_want=50)
|
||||||
back = tracker.poll_once()
|
back = tracker.poll_once()
|
||||||
peers = back["response"]["peers"]
|
if back:
|
||||||
|
peers = back["response"]["peers"]
|
||||||
|
else:
|
||||||
|
raise Exception("No response")
|
||||||
except Exception, err:
|
except Exception, err:
|
||||||
|
self.log.warning("Tracker error: udp://%s:%s (%s)" % (ip, port, err))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
elif tracker_protocol == "http": # Http tracker
|
elif tracker_protocol == "http": # Http tracker
|
||||||
|
@ -854,7 +858,7 @@ class Site(object):
|
||||||
req.close()
|
req.close()
|
||||||
req = None
|
req = None
|
||||||
if not response:
|
if not response:
|
||||||
self.log.debug("Http tracker %s response error" % tracker_address)
|
self.log.warning("Tracker error: http://%s (No response)" % tracker_address)
|
||||||
return False
|
return False
|
||||||
# Decode peers
|
# Decode peers
|
||||||
peer_data = bencode.decode(response)["peers"]
|
peer_data = bencode.decode(response)["peers"]
|
||||||
|
@ -867,7 +871,7 @@ class Site(object):
|
||||||
addr, port = struct.unpack('!LH', peer)
|
addr, port = struct.unpack('!LH', peer)
|
||||||
peers.append({"addr": socket.inet_ntoa(struct.pack('!L', addr)), "port": port})
|
peers.append({"addr": socket.inet_ntoa(struct.pack('!L', addr)), "port": port})
|
||||||
except Exception, err:
|
except Exception, err:
|
||||||
self.log.debug("Http tracker %s error: %s" % (tracker_address, err))
|
self.log.warning("Tracker error: http://%s (%s)" % (tracker_address, err))
|
||||||
if req:
|
if req:
|
||||||
req.close()
|
req.close()
|
||||||
req = None
|
req = None
|
||||||
|
@ -885,7 +889,10 @@ class Site(object):
|
||||||
if added:
|
if added:
|
||||||
self.worker_manager.onPeers()
|
self.worker_manager.onPeers()
|
||||||
self.updateWebsocket(peers_added=added)
|
self.updateWebsocket(peers_added=added)
|
||||||
self.log.debug("%s: Found %s peers, new: %s, total: %s" % (tracker_address, len(peers), added, len(self.peers)))
|
self.log.debug(
|
||||||
|
"Tracker result: %s://%s (found %s peers, new: %s, total: %s)" %
|
||||||
|
(tracker_protocol, tracker_address, len(peers), added, len(self.peers))
|
||||||
|
)
|
||||||
return time.time() - s
|
return time.time() - s
|
||||||
|
|
||||||
# Add myself and get other peers from tracker
|
# Add myself and get other peers from tracker
|
||||||
|
@ -955,10 +962,11 @@ class Site(object):
|
||||||
announced_to = trackers[0]
|
announced_to = trackers[0]
|
||||||
else:
|
else:
|
||||||
announced_to = "%s trackers" % announced
|
announced_to = "%s trackers" % announced
|
||||||
self.log.debug(
|
if config.verbose:
|
||||||
"Announced types %s in mode %s to %s in %.3fs, errors: %s, slow: %s" %
|
self.log.debug(
|
||||||
(add_types, mode, announced_to, time.time() - s, errors, slow)
|
"Announced types %s in mode %s to %s in %.3fs, errors: %s, slow: %s" %
|
||||||
)
|
(add_types, mode, announced_to, time.time() - s, errors, slow)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
if mode != "update":
|
if mode != "update":
|
||||||
self.log.error("Announce to %s trackers in %.3fs, failed" % (announced, time.time() - s))
|
self.log.error("Announce to %s trackers in %.3fs, failed" % (announced, time.time() - s))
|
||||||
|
|
Loading…
Reference in a new issue