Rev390, Fix sidebar error on non locatable IPs, Configurable bootstrap torrent trackers, Multi-line config file settings, Evenly distributed tracker announce to work better on passive connections, Avoid iframe sandbox escape by using nonces, Better html error messages, Display proper error on invalid startup parameters

This commit is contained in:
HelloZeroNet 2015-09-10 23:25:09 +02:00
parent eec0b22c1f
commit 0de6496f96
11 changed files with 117 additions and 58 deletions

View file

@ -38,6 +38,7 @@ class Site:
self.peers = {} # Key: ip:port, Value: Peer.Peer
self.peer_blacklist = SiteManager.peer_blacklist # Ignore this peers (eg. myself)
self.last_announce = 0 # Last announce time to tracker
self.last_tracker_id = random.randint(0, 10) # Last announced tracker id
self.worker_manager = WorkerManager(self) # Handle site download from other peers
self.bad_files = {} # SHA check failed files, need to redownload {"inner.content": 1} (key: file, value: failed accept)
self.content_updated = None # Content.js update time
@ -510,12 +511,13 @@ class Site:
# Gather peers from tracker
# Return: Complete time or False on error
def announceTracker(self, protocol, ip, port, fileserver_port, address_hash, my_peer_id):
def announceTracker(self, protocol, address, fileserver_port, address_hash, my_peer_id):
s = time.time()
if protocol == "udp": # Udp tracker
if config.disable_udp:
return False # No udp supported
tracker = UdpTrackerClient(ip, port)
ip, port = address.split(":")
tracker = UdpTrackerClient(ip, int(port))
tracker.peer_port = fileserver_port
try:
tracker.connect()
@ -535,7 +537,7 @@ class Site:
}
req = None
try:
url = "http://" + ip + "?" + urllib.urlencode(params)
url = "http://" + address + "?" + urllib.urlencode(params)
# Load url
with gevent.Timeout(10, False): # Make sure of timeout
req = urllib2.urlopen(url, timeout=8)
@ -577,10 +579,17 @@ class Site:
return time.time() - s
# Add myself and get other peers from tracker
def announce(self, force=False):
def announce(self, force=False, num=5, pex=True):
if time.time() < self.last_announce + 30 and not force:
return # No reannouncing within 30 secs
self.last_announce = time.time()
trackers = config.trackers
if num == 1: # Only announce on one tracker, increment the queried tracker id
self.last_tracker_id += 1
self.last_tracker_id = self.last_tracker_id % len(trackers)
trackers = [trackers[self.last_tracker_id]] # We only going to use this one
errors = []
slow = []
address_hash = hashlib.sha1(self.address).hexdigest() # Site address hash
@ -595,39 +604,42 @@ class Site:
announced = 0
threads = []
for protocol, ip, port in SiteManager.TRACKERS: # Start announce threads
thread = gevent.spawn(self.announceTracker, protocol, ip, port, fileserver_port, address_hash, my_peer_id)
for tracker in trackers: # Start announce threads
protocol, address = tracker.split("://")
thread = gevent.spawn(self.announceTracker, protocol, address, fileserver_port, address_hash, my_peer_id)
threads.append(thread)
thread.ip = ip
thread.address = address
thread.protocol = protocol
if len(threads) > num: break # Announce limit
gevent.joinall(threads) # Wait for announce finish
for thread in threads:
if thread.value:
if thread.value > 1:
slow.append("%.2fs %s://%s" % (thread.value, thread.protocol, thread.ip))
slow.append("%.2fs %s://%s" % (thread.value, thread.protocol, thread.address))
announced += 1
else:
errors.append("%s://%s" % (thread.protocol, thread.ip))
errors.append("%s://%s" % (thread.protocol, thread.address))
# Save peers num
self.settings["peers"] = len(self.peers)
self.saveSettings()
if len(errors) < len(SiteManager.TRACKERS): # Less errors than total tracker nums
if len(errors) < min(num, len(trackers)): # Less errors than total tracker nums
self.log.debug(
"Announced port %s to %s trackers in %.3fs, errors: %s, slow: %s" %
(fileserver_port, announced, time.time() - s, errors, slow)
)
else:
self.log.error("Announced to %s trackers in %.3fs, failed" % (announced, time.time() - s))
self.log.error("Announce to %s trackers in %.3fs, failed" % (announced, time.time() - s))
if not [peer for peer in self.peers.values() if peer.connection and peer.connection.connected]:
# If no connected peer yet then wait for connections
gevent.spawn_later(3, self.announcePex, need_num=10) # Spawn 3 secs later
else: # Else announce immediately
self.announcePex()
if pex:
if not [peer for peer in self.peers.values() if peer.connection and peer.connection.connected]:
# If no connected peer yet then wait for connections
gevent.spawn_later(3, self.announcePex, need_num=10) # Spawn 3 secs later
else: # Else announce immediately
self.announcePex()
# Keep connections to get the updates (required for passive clients)
def needConnections(self, num=3):

View file

@ -6,26 +6,6 @@ import os
from Plugin import PluginManager
from Config import config
TRACKERS = [
("udp", "open.demonii.com", 1337),
# ("udp", "sugoi.pomf.se", 2710),
# ("udp", "tracker.coppersurfer.tk", 80),
("udp", "tracker.leechers-paradise.org", 6969),
("udp", "9.rarbg.com", 2710),
# ("udp", "www.eddie4.nl", 6969),
# ("udp", "trackr.sytes.net", 80),
# ("udp", "tracker4.piratux.com", 6969)
# ("http", "exodus.desync.com:80/announce", None), Off
("http", "tracker.aletorrenty.pl:2710/announce", None),
# ("http", "torrent.gresille.org/announce", None), # Slow
# ("http", "announce.torrentsmd.com:6969/announce", None), # Off
# ("http", "i.bandito.org/announce", None), # Off
("http", "retracker.telecom.kz/announce", None),
("http", "torrent.gresille.org/announce", None),
]
@PluginManager.acceptPlugins
class SiteManager(object):