2015-07-12 20:36:46 +02:00
|
|
|
import logging
|
|
|
|
import urllib2
|
|
|
|
import re
|
|
|
|
import time
|
2016-10-03 19:26:37 +02:00
|
|
|
import socket
|
2015-07-12 20:36:46 +02:00
|
|
|
|
|
|
|
import gevent
|
|
|
|
|
2016-03-18 20:03:38 +01:00
|
|
|
import util
|
2015-01-12 02:03:45 +01:00
|
|
|
from Config import config
|
|
|
|
from FileRequest import FileRequest
|
|
|
|
from Site import SiteManager
|
2015-01-17 18:50:56 +01:00
|
|
|
from Debug import Debug
|
version 0.2.4, peerPing and peerGetFile commands, old content update bugfix, new network code and protocol, connection share between sites, connection reuse, dont retry bad file more than 3 times in 20 min, multi threaded include file download, shuffle peers before publish, simple internal stats page, dont retry on failed peers, more than 10 peers publish bugfix
2015-02-23 23:33:31 +01:00
|
|
|
from Connection import ConnectionServer
|
2015-02-24 14:02:05 +01:00
|
|
|
from util import UpnpPunch
|
2015-01-12 02:03:45 +01:00
|
|
|
|
|
|
|
|
version 0.2.4, peerPing and peerGetFile commands, old content update bugfix, new network code and protocol, connection share between sites, connection reuse, dont retry bad file more than 3 times in 20 min, multi threaded include file download, shuffle peers before publish, simple internal stats page, dont retry on failed peers, more than 10 peers publish bugfix
2015-02-23 23:33:31 +01:00
|
|
|
class FileServer(ConnectionServer):
|
2015-07-12 20:36:46 +02:00
|
|
|
|
2015-09-24 22:08:08 +02:00
|
|
|
def __init__(self, ip=config.fileserver_ip, port=config.fileserver_port):
|
|
|
|
ConnectionServer.__init__(self, ip, port, self.handleRequest)
|
2016-03-21 09:43:53 +01:00
|
|
|
if config.ip_external: # Ip external defined in arguments
|
2015-07-12 20:36:46 +02:00
|
|
|
self.port_opened = True
|
|
|
|
SiteManager.peer_blacklist.append((config.ip_external, self.port)) # Add myself to peer blacklist
|
|
|
|
else:
|
|
|
|
self.port_opened = None # Is file server opened on router
|
2015-09-24 22:08:08 +02:00
|
|
|
self.sites = {}
|
2016-03-19 01:36:01 +01:00
|
|
|
self.last_request = time.time()
|
2016-04-20 23:38:22 +02:00
|
|
|
self.files_parsing = {}
|
2015-07-12 20:36:46 +02:00
|
|
|
|
|
|
|
# Handle request to fileserver
|
|
|
|
def handleRequest(self, connection, message):
|
2016-03-06 00:55:50 +01:00
|
|
|
if config.verbose:
|
|
|
|
if "params" in message:
|
|
|
|
self.log.debug(
|
|
|
|
"FileRequest: %s %s %s %s" %
|
|
|
|
(str(connection), message["cmd"], message["params"].get("site"), message["params"].get("inner_path"))
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.log.debug("FileRequest: %s %s" % (str(connection), message["cmd"]))
|
2015-07-12 20:36:46 +02:00
|
|
|
req = FileRequest(self, connection)
|
|
|
|
req.route(message["cmd"], message.get("req_id"), message.get("params"))
|
2016-03-19 18:14:09 +01:00
|
|
|
if not self.has_internet:
|
|
|
|
self.has_internet = True
|
|
|
|
self.onInternetOnline()
|
|
|
|
|
|
|
|
def onInternetOnline(self):
|
|
|
|
self.log.info("Internet online")
|
|
|
|
gevent.spawn(self.checkSites, check_files=False, force_port_check=True)
|
2015-07-12 20:36:46 +02:00
|
|
|
|
|
|
|
# Reload the FileRequest class to prevent restarts in debug mode
|
|
|
|
def reload(self):
|
|
|
|
global FileRequest
|
|
|
|
import imp
|
|
|
|
FileRequest = imp.load_source("FileRequest", "src/File/FileRequest.py").FileRequest
|
|
|
|
|
|
|
|
# Try to open the port using upnp
|
|
|
|
def openport(self, port=None, check=True):
|
|
|
|
if not port:
|
|
|
|
port = self.port
|
|
|
|
if self.port_opened:
|
|
|
|
return True # Port already opened
|
|
|
|
if check: # Check first if its already opened
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
time.sleep(1) # Wait for port open
|
|
|
|
if self.testOpenport(port, use_alternative=False)["result"] is True:
|
2015-07-12 20:36:46 +02:00
|
|
|
return True # Port already opened
|
|
|
|
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
if config.tor == "always": # Port opening won't work in Tor mode
|
|
|
|
return False
|
|
|
|
|
2015-07-12 20:36:46 +02:00
|
|
|
self.log.info("Trying to open port using UpnpPunch...")
|
|
|
|
try:
|
2016-10-02 14:39:03 +02:00
|
|
|
UpnpPunch.ask_to_open_port(self.port, 'ZeroNet', retries=3, protos=["TCP"])
|
2016-10-03 19:26:37 +02:00
|
|
|
except (UpnpPunch.UpnpError, UpnpPunch.IGDError, socket.error) as err:
|
2016-08-10 17:03:58 +02:00
|
|
|
self.log.error("UpnpPunch run error: %s" %
|
|
|
|
Debug.formatException(err))
|
|
|
|
return False
|
2015-07-12 20:36:46 +02:00
|
|
|
|
2016-08-10 17:03:58 +02:00
|
|
|
if self.testOpenport(port)["result"] is True:
|
2015-07-12 20:36:46 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
self.log.info("Upnp mapping failed :( Please forward port %s on your router to your ipaddress" % port)
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Test if the port is open
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
def testOpenport(self, port=None, use_alternative=True):
|
2015-07-12 20:36:46 +02:00
|
|
|
if not port:
|
|
|
|
port = self.port
|
|
|
|
back = self.testOpenportPortchecker(port)
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
if back["result"] is not True and use_alternative: # If no success try alternative checker
|
2015-07-12 20:36:46 +02:00
|
|
|
return self.testOpenportCanyouseeme(port)
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
else:
|
|
|
|
return back
|
2015-07-12 20:36:46 +02:00
|
|
|
|
|
|
|
def testOpenportPortchecker(self, port=None):
|
|
|
|
self.log.info("Checking port %s using portchecker.co..." % port)
|
|
|
|
try:
|
|
|
|
data = urllib2.urlopen("http://portchecker.co/check", "port=%s" % port, timeout=20.0).read()
|
|
|
|
message = re.match('.*<div id="results-wrapper">(.*?)</div>', data, re.DOTALL).group(1)
|
|
|
|
message = re.sub("<.*?>", "", message.replace("<br>", " ").replace(" ", " ").strip()) # Strip http tags
|
|
|
|
except Exception, err:
|
|
|
|
message = "Error: %s" % Debug.formatException(err)
|
|
|
|
data = ""
|
|
|
|
|
|
|
|
if "closed" in message or "Error" in message:
|
2016-01-19 20:42:00 +01:00
|
|
|
if config.tor != "always":
|
|
|
|
self.log.info("[BAD :(] Port closed: %s" % message)
|
2015-07-12 20:36:46 +02:00
|
|
|
if port == self.port:
|
|
|
|
self.port_opened = False # Self port, update port_opened status
|
|
|
|
match = re.match(".*targetIP.*?value=\"(.*?)\"", data, re.DOTALL) # Try find my external ip in message
|
|
|
|
if match: # Found my ip in message
|
|
|
|
config.ip_external = match.group(1)
|
|
|
|
SiteManager.peer_blacklist.append((config.ip_external, self.port)) # Add myself to peer blacklist
|
|
|
|
else:
|
|
|
|
config.ip_external = False
|
|
|
|
return {"result": False, "message": message}
|
|
|
|
else:
|
|
|
|
self.log.info("[OK :)] Port open: %s" % message)
|
|
|
|
if port == self.port: # Self port, update port_opened status
|
|
|
|
self.port_opened = True
|
|
|
|
match = re.match(".*targetIP.*?value=\"(.*?)\"", data, re.DOTALL) # Try find my external ip in message
|
|
|
|
if match: # Found my ip in message
|
|
|
|
config.ip_external = match.group(1)
|
|
|
|
SiteManager.peer_blacklist.append((config.ip_external, self.port)) # Add myself to peer blacklist
|
|
|
|
else:
|
|
|
|
config.ip_external = False
|
|
|
|
return {"result": True, "message": message}
|
|
|
|
|
|
|
|
def testOpenportCanyouseeme(self, port=None):
|
|
|
|
self.log.info("Checking port %s using canyouseeme.org..." % port)
|
|
|
|
try:
|
|
|
|
data = urllib2.urlopen("http://www.canyouseeme.org/", "port=%s" % port, timeout=20.0).read()
|
|
|
|
message = re.match('.*<p style="padding-left:15px">(.*?)</p>', data, re.DOTALL).group(1)
|
|
|
|
message = re.sub("<.*?>", "", message.replace("<br>", " ").replace(" ", " ")) # Strip http tags
|
|
|
|
except Exception, err:
|
|
|
|
message = "Error: %s" % Debug.formatException(err)
|
|
|
|
|
|
|
|
if "Error" in message:
|
2016-01-19 20:42:00 +01:00
|
|
|
if config.tor != "always":
|
|
|
|
self.log.info("[BAD :(] Port closed: %s" % message)
|
2015-07-12 20:36:46 +02:00
|
|
|
if port == self.port:
|
|
|
|
self.port_opened = False # Self port, update port_opened status
|
|
|
|
match = re.match(".*?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", message) # Try find my external ip in message
|
|
|
|
if match: # Found my ip in message
|
|
|
|
config.ip_external = match.group(1)
|
|
|
|
SiteManager.peer_blacklist.append((config.ip_external, self.port)) # Add myself to peer blacklist
|
|
|
|
else:
|
|
|
|
config.ip_external = False
|
|
|
|
return {"result": False, "message": message}
|
|
|
|
else:
|
|
|
|
self.log.info("[OK :)] Port open: %s" % message)
|
|
|
|
if port == self.port: # Self port, update port_opened status
|
|
|
|
self.port_opened = True
|
|
|
|
match = re.match(".*?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", message) # Try find my external ip in message
|
|
|
|
if match: # Found my ip in message
|
|
|
|
config.ip_external = match.group(1)
|
|
|
|
SiteManager.peer_blacklist.append((config.ip_external, self.port)) # Add myself to peer blacklist
|
|
|
|
else:
|
|
|
|
config.ip_external = False
|
|
|
|
return {"result": True, "message": message}
|
|
|
|
|
|
|
|
# Set external ip without testing
|
|
|
|
def setIpExternal(self, ip_external):
|
|
|
|
logging.info("Setting external ip without testing: %s..." % ip_external)
|
|
|
|
config.ip_external = ip_external
|
|
|
|
self.port_opened = True
|
|
|
|
|
|
|
|
# Check site file integrity
|
2016-09-04 17:51:00 +02:00
|
|
|
def checkSite(self, site, check_files=False):
|
2015-07-12 20:36:46 +02:00
|
|
|
if site.settings["serving"]:
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
site.announce(mode="startup") # Announce site to tracker
|
2016-03-18 20:03:19 +01:00
|
|
|
site.update(check_files=check_files) # Update site's content.json and download changed files
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
site.sendMyHashfield()
|
|
|
|
site.updateHashfield()
|
2016-03-16 23:24:57 +01:00
|
|
|
if len(site.peers) > 5: # Keep active connections if site having 5 or more peers
|
2015-07-12 20:36:46 +02:00
|
|
|
site.needConnections()
|
|
|
|
|
|
|
|
# Check sites integrity
|
2016-03-18 20:03:38 +01:00
|
|
|
@util.Noparallel()
|
2016-09-04 17:51:00 +02:00
|
|
|
def checkSites(self, check_files=False, force_port_check=False):
|
2016-03-19 01:36:01 +01:00
|
|
|
self.log.debug("Checking sites...")
|
2016-03-18 20:03:19 +01:00
|
|
|
sites_checking = False
|
2016-03-19 12:23:29 +01:00
|
|
|
if self.port_opened is None or force_port_check: # Test and open port if not tested yet
|
2016-03-18 20:03:19 +01:00
|
|
|
if len(self.sites) <= 2: # Don't wait port opening on first startup
|
|
|
|
sites_checking = True
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
for address, site in self.sites.items():
|
2016-03-18 20:03:19 +01:00
|
|
|
gevent.spawn(self.checkSite, site, check_files)
|
2015-07-12 20:36:46 +02:00
|
|
|
|
2016-08-15 13:53:20 +02:00
|
|
|
if force_port_check:
|
|
|
|
self.port_opened = None
|
2016-03-18 20:03:19 +01:00
|
|
|
self.openport()
|
2016-03-30 23:19:46 +02:00
|
|
|
if self.port_opened is False:
|
|
|
|
self.tor_manager.startOnions()
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
|
2016-03-18 20:03:19 +01:00
|
|
|
if not sites_checking:
|
|
|
|
for address, site in self.sites.items(): # Check sites integrity
|
|
|
|
gevent.spawn(self.checkSite, site, check_files) # Check in new thread
|
|
|
|
time.sleep(2) # Prevent too quick request
|
2015-07-12 20:36:46 +02:00
|
|
|
|
2016-01-19 20:42:00 +01:00
|
|
|
def trackersFileReloader(self):
|
|
|
|
while 1:
|
|
|
|
config.loadTrackersFile()
|
|
|
|
time.sleep(60)
|
|
|
|
|
2015-07-12 20:36:46 +02:00
|
|
|
# Announce sites every 20 min
|
|
|
|
def announceSites(self):
|
|
|
|
import gc
|
2016-01-19 20:42:00 +01:00
|
|
|
if config.trackers_file:
|
|
|
|
gevent.spawn(self.trackersFileReloader)
|
2015-07-12 20:36:46 +02:00
|
|
|
while 1:
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
# Sites health care every 20 min
|
2015-07-12 20:36:46 +02:00
|
|
|
for address, site in self.sites.items():
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
if not site.settings["serving"]:
|
|
|
|
continue
|
|
|
|
if site.peers:
|
|
|
|
site.announcePex()
|
2015-07-12 20:36:46 +02:00
|
|
|
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
# Retry failed files
|
|
|
|
if site.bad_files:
|
|
|
|
site.retryBadFiles()
|
2015-07-25 13:38:58 +02:00
|
|
|
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
site.cleanupPeers()
|
2015-07-12 20:36:46 +02:00
|
|
|
|
2016-08-26 11:41:45 +02:00
|
|
|
site.needConnections() # Keep 5 active peer connection to get the updates
|
Rev536, Fix stats page, Support ranged http requests for better video browser compatibility, setHashfield command, One by one send hashfield to connected peers if changed, Keep count hashfield changetime, PeerHashfield optimalizations, Wait for peers on checkmodification, Give more time to query trackers, Do not count udp trackers as error if udp disabled, Test hashfield push
2015-10-30 02:08:02 +01:00
|
|
|
|
2015-07-12 20:36:46 +02:00
|
|
|
time.sleep(2) # Prevent too quick request
|
|
|
|
|
|
|
|
site = None
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
gc.collect() # Implicit garbage collection
|
2015-07-12 20:36:46 +02:00
|
|
|
|
2015-09-10 23:25:09 +02:00
|
|
|
# Find new peers
|
|
|
|
for tracker_i in range(len(config.trackers)):
|
|
|
|
time.sleep(60 * 20 / len(config.trackers)) # Query all trackers one-by-one in 20 minutes evenly distributed
|
|
|
|
for address, site in self.sites.items():
|
Version 0.3.5, Rev830, Full Tor mode support with hidden services, Onion stats in Sidebar, GeoDB download fix using Tor, Gray out disabled sites in Stats page, Tor hidden service status in stat page, Benchmark sha256, Skyts tracker out expodie in, 2 new tracker using ZeroNet protocol, Keep SSL cert option between restarts, SSL Certificate pinning support for connections, Site lock support for connections, Certificate pinned connections using implicit SSL, Flood protection whitelist support, Foreign keys support for DB layer, Not support for SQL query helper, 0 length file get bugfix, Pex onion address support, Faster port testing, Faster uPnP port opening, Need connections more often on owned sites, Delay ZeroHello startup message if port check or Tor manager not ready yet, Use lockfiles to avoid double start, Save original socket on proxy monkey patching to get ability to connect localhost directly, Handle atomic write errors, Broken gevent https workaround helper, Rsa crypt functions, Plugin to Bootstrap using ZeroNet protocol
2016-01-05 00:20:52 +01:00
|
|
|
if not site.settings["serving"]:
|
|
|
|
continue
|
|
|
|
site.announce(mode="update", pex=False)
|
|
|
|
if site.settings["own"]: # Check connections more frequently on own sites to speed-up first connections
|
|
|
|
site.needConnections()
|
2015-11-12 02:04:45 +01:00
|
|
|
site.sendMyHashfield(3)
|
|
|
|
site.updateHashfield(1)
|
2015-09-10 23:25:09 +02:00
|
|
|
time.sleep(2)
|
|
|
|
|
2015-07-12 20:36:46 +02:00
|
|
|
# Detects if computer back from wakeup
|
|
|
|
def wakeupWatcher(self):
|
|
|
|
last_time = time.time()
|
|
|
|
while 1:
|
|
|
|
time.sleep(30)
|
2016-10-02 14:22:01 +02:00
|
|
|
if time.time() - max(self.last_request, last_time) > 60 * 3:
|
|
|
|
# If taken more than 3 minute then the computer was in sleep mode
|
2015-07-12 20:36:46 +02:00
|
|
|
self.log.info(
|
2016-04-03 10:32:13 +02:00
|
|
|
"Wakeup detected: time warp from %s to %s (%s sleep seconds), acting like startup..." %
|
2015-07-12 20:36:46 +02:00
|
|
|
(last_time, time.time(), time.time() - last_time)
|
|
|
|
)
|
2016-03-19 12:23:29 +01:00
|
|
|
self.checkSites(check_files=False, force_port_check=True)
|
2015-07-12 20:36:46 +02:00
|
|
|
last_time = time.time()
|
|
|
|
|
|
|
|
# Bind and start serving sites
|
|
|
|
def start(self, check_sites=True):
|
2015-09-24 22:08:08 +02:00
|
|
|
self.sites = SiteManager.site_manager.list()
|
2015-07-12 20:36:46 +02:00
|
|
|
self.log = logging.getLogger("FileServer")
|
|
|
|
|
|
|
|
if config.debug:
|
|
|
|
# Auto reload FileRequest on change
|
|
|
|
from Debug import DebugReloader
|
|
|
|
DebugReloader(self.reload)
|
|
|
|
|
|
|
|
if check_sites: # Open port, Update sites, Check files integrity
|
|
|
|
gevent.spawn(self.checkSites)
|
|
|
|
|
|
|
|
thread_announce_sites = gevent.spawn(self.announceSites)
|
|
|
|
thread_wakeup_watcher = gevent.spawn(self.wakeupWatcher)
|
|
|
|
|
|
|
|
ConnectionServer.start(self)
|
|
|
|
|
|
|
|
self.log.debug("Stopped.")
|
2016-10-02 14:22:01 +02:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
if self.running and self.port_opened:
|
|
|
|
self.log.debug('Closing port %d' % self.port)
|
|
|
|
try:
|
2016-10-02 14:39:03 +02:00
|
|
|
UpnpPunch.ask_to_close_port(self.port, protos=["TCP"])
|
2016-10-02 14:22:01 +02:00
|
|
|
self.log.info('Closed port via upnp.')
|
|
|
|
except (UpnpPunch.UpnpError, UpnpPunch.IGDError), err:
|
|
|
|
self.log.info("Failed at attempt to use upnp to close port: %s" % err)
|
|
|
|
ConnectionServer.stop(self)
|