Make AnnounceLocalPlugin and AnnounceZeroPlugin compatible with SiteAnnouncer class
This commit is contained in:
parent
595b2e40d9
commit
ceff73ee5b
2 changed files with 34 additions and 33 deletions
|
@ -7,15 +7,15 @@ from Config import config
|
||||||
import BroadcastServer
|
import BroadcastServer
|
||||||
|
|
||||||
|
|
||||||
@PluginManager.registerTo("Site")
|
@PluginManager.registerTo("SiteAnnouncer")
|
||||||
class SitePlugin(object):
|
class SiteAnnouncerPlugin(object):
|
||||||
def announce(self, force=False, mode="start", *args, **kwargs):
|
def announce(self, force=False, *args, **kwargs):
|
||||||
local_announcer = self.connection_server.local_announcer
|
local_announcer = self.site.connection_server.local_announcer
|
||||||
|
|
||||||
if local_announcer and (force or time.time() - local_announcer.last_discover > 5 * 60):
|
if local_announcer and (force or time.time() - local_announcer.last_discover > 5 * 60):
|
||||||
local_announcer.discover(force=force)
|
local_announcer.discover(force=force)
|
||||||
|
|
||||||
return super(SitePlugin, self).announce(force=force, mode=mode, *args, **kwargs)
|
return super(SiteAnnouncerPlugin, self).announce(force=force, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class LocalAnnouncer(BroadcastServer.BroadcastServer):
|
class LocalAnnouncer(BroadcastServer.BroadcastServer):
|
||||||
|
|
|
@ -3,7 +3,6 @@ import time
|
||||||
from Plugin import PluginManager
|
from Plugin import PluginManager
|
||||||
from util import helper
|
from util import helper
|
||||||
from Crypt import CryptRsa
|
from Crypt import CryptRsa
|
||||||
from Config import config
|
|
||||||
|
|
||||||
allow_reload = False # No source reload supported in this plugin
|
allow_reload = False # No source reload supported in this plugin
|
||||||
time_full_announced = {} # Tracker address: Last announced all site to tracker
|
time_full_announced = {} # Tracker address: Last announced all site to tracker
|
||||||
|
@ -12,9 +11,10 @@ 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 importPeers():
|
def importHostClasses():
|
||||||
global Peer
|
global Peer, AnnounceError
|
||||||
from Peer import Peer
|
from Peer import Peer
|
||||||
|
from Site.SiteAnnounce import AnnounceError
|
||||||
|
|
||||||
|
|
||||||
# Process result got back from tracker
|
# Process result got back from tracker
|
||||||
|
@ -41,38 +41,41 @@ def processPeerRes(tracker_address, site, peers):
|
||||||
return added
|
return added
|
||||||
|
|
||||||
|
|
||||||
@PluginManager.registerTo("Site")
|
@PluginManager.registerTo("SiteAnnouncer")
|
||||||
class SitePlugin(object):
|
class SiteAnnouncerPlugin(object):
|
||||||
def announceTracker(self, tracker_protocol, tracker_address, fileserver_port=0, add_types=[], my_peer_id="", mode="start"):
|
def getTrackerHandler(self, protocol):
|
||||||
if tracker_protocol != "zero":
|
if protocol == "zero":
|
||||||
return super(SitePlugin, self).announceTracker(
|
return self.announceTrackerZero
|
||||||
tracker_protocol, tracker_address, fileserver_port, add_types, my_peer_id, mode
|
else:
|
||||||
)
|
return super(SiteAnnouncerPlugin, self).getTrackerHandler(protocol)
|
||||||
|
|
||||||
|
def announceTrackerZero(self, tracker_address, mode="start", num_want=10):
|
||||||
|
global time_full_announced
|
||||||
s = time.time()
|
s = time.time()
|
||||||
|
|
||||||
need_types = ["ip4"]
|
need_types = ["ip4"]
|
||||||
if self.connection_server and self.connection_server.tor_manager and self.connection_server.tor_manager.enabled:
|
if self.site.connection_server.tor_manager.enabled:
|
||||||
need_types.append("onion")
|
need_types.append("onion")
|
||||||
|
|
||||||
if mode == "start" or mode == "more": # Single: Announce only this site
|
if mode == "start" or mode == "more": # Single: Announce only this site
|
||||||
sites = [self]
|
sites = [self.site]
|
||||||
full_announce = False
|
full_announce = False
|
||||||
else: # Multi: Announce all currently serving site
|
else: # Multi: Announce all currently serving site
|
||||||
full_announce = True
|
full_announce = True
|
||||||
if time.time() - time_full_announced.get(tracker_address, 0) < 60 * 5: # No reannounce all sites within 5 minute
|
if time.time() - time_full_announced.get(tracker_address, 0) < 60 * 5: # No reannounce all sites within 5 minute
|
||||||
return True
|
return []
|
||||||
time_full_announced[tracker_address] = time.time()
|
time_full_announced[tracker_address] = time.time()
|
||||||
from Site import SiteManager
|
from Site import SiteManager
|
||||||
sites = [site for site in SiteManager.site_manager.sites.values() if site.settings["serving"]]
|
sites = [site for site in SiteManager.site_manager.sites.values() if site.settings["serving"]]
|
||||||
|
|
||||||
# Create request
|
# Create request
|
||||||
|
add_types = self.getOpenedServiceTypes()
|
||||||
request = {
|
request = {
|
||||||
"hashes": [], "onions": [], "port": fileserver_port, "need_types": need_types, "need_num": 20, "add": add_types
|
"hashes": [], "onions": [], "port": self.fileserver_port, "need_types": need_types, "need_num": 20, "add": add_types
|
||||||
}
|
}
|
||||||
for site in sites:
|
for site in sites:
|
||||||
if "onion" in add_types:
|
if "onion" in add_types:
|
||||||
onion = self.connection_server.tor_manager.getOnion(site.address)
|
onion = self.site.connection_server.tor_manager.getOnion(site.address)
|
||||||
request["onions"].append(onion)
|
request["onions"].append(onion)
|
||||||
request["hashes"].append(site.address_hash)
|
request["hashes"].append(site.address_hash)
|
||||||
|
|
||||||
|
@ -84,15 +87,14 @@ class SitePlugin(object):
|
||||||
tracker = connection_pool.get(tracker_address) # Re-use tracker connection if possible
|
tracker = connection_pool.get(tracker_address) # Re-use tracker connection if possible
|
||||||
if not tracker:
|
if not tracker:
|
||||||
tracker_ip, tracker_port = tracker_address.split(":")
|
tracker_ip, tracker_port = tracker_address.split(":")
|
||||||
tracker = Peer(tracker_ip, tracker_port, connection_server=self.connection_server)
|
tracker = Peer(tracker_ip, tracker_port, connection_server=self.site.connection_server)
|
||||||
connection_pool[tracker_address] = tracker
|
connection_pool[tracker_address] = tracker
|
||||||
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.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
|
raise AnnounceError("Invalid response: %s" % res)
|
||||||
|
|
||||||
# Add peers from response to site
|
# Add peers from response to site
|
||||||
site_index = 0
|
site_index = 0
|
||||||
|
@ -104,29 +106,28 @@ class SitePlugin(object):
|
||||||
|
|
||||||
# Check if we need to sign prove the onion addresses
|
# Check if we need to sign prove the onion addresses
|
||||||
if "onion_sign_this" in res:
|
if "onion_sign_this" in res:
|
||||||
self.log.debug("Signing %s for %s to add %s onions" % (res["onion_sign_this"], tracker_address, len(sites)))
|
self.site.log.debug("Signing %s for %s to add %s onions" % (res["onion_sign_this"], tracker_address, len(sites)))
|
||||||
request["onion_signs"] = {}
|
request["onion_signs"] = {}
|
||||||
request["onion_sign_this"] = res["onion_sign_this"]
|
request["onion_sign_this"] = res["onion_sign_this"]
|
||||||
request["need_num"] = 0
|
request["need_num"] = 0
|
||||||
for site in sites:
|
for site in sites:
|
||||||
onion = self.connection_server.tor_manager.getOnion(site.address)
|
onion = self.site.connection_server.tor_manager.getOnion(site.address)
|
||||||
publickey = self.connection_server.tor_manager.getPublickey(onion)
|
publickey = self.site.connection_server.tor_manager.getPublickey(onion)
|
||||||
if publickey not in request["onion_signs"]:
|
if publickey not in request["onion_signs"]:
|
||||||
sign = CryptRsa.sign(res["onion_sign_this"], self.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.request("announce", request)
|
||||||
if not res or "onion_sign_this" in res:
|
if not res or "onion_sign_this" in 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
|
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.remove() # Close connection, we don't need it in next 5 minute
|
||||||
|
|
||||||
self.log.debug(
|
self.site.log.debug(
|
||||||
"Tracker result: zero://%s (sites: %s, new: %s)" %
|
"Tracker announce result: zero://%s (sites: %s, new peers: %s) in %.3fs" %
|
||||||
(tracker_address, site_index, peers_added)
|
(tracker_address, site_index, peers_added, time.time() - s)
|
||||||
)
|
)
|
||||||
|
|
||||||
return time.time() - s
|
return None
|
||||||
|
|
Loading…
Reference in a new issue