def testStreamFile(self, file_server, site): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) connection = client.getConnection("127.0.0.1", 1544) file_server.sites[site.address] = site buff = StringIO.StringIO() response = connection.request("streamFile", {"site": site.address, "inner_path": "content.json", "location": 0}, buff) assert "stream_bytes" in response assert "sign" in buff.getvalue() # Invalid file buff = StringIO.StringIO() response = connection.request("streamFile", {"site": site.address, "inner_path": "invalid.file", "location": 0}, buff) assert "File read error" in response["error"] # Location over size buff = StringIO.StringIO() response = connection.request( "streamFile", {"site": site.address, "inner_path": "content.json", "location": 1024 * 1024}, buff ) assert "File read error" in response["error"] # Stream from parent dir buff = StringIO.StringIO() response = connection.request("streamFile", {"site": site.address, "inner_path": "../users.json", "location": 0}, buff) assert "File read error" in response["error"] connection.close() client.stop()
def testStreamFile(self, file_server, site): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer(file_server.ip, 1545) connection = client.getConnection(file_server.ip, 1544) file_server.sites[site.address] = site buff = io.BytesIO() response = connection.request("streamFile", {"site": site.address, "inner_path": "content.json", "location": 0}, buff) assert "stream_bytes" in response assert b"sign" in buff.getvalue() # Invalid file buff = io.BytesIO() response = connection.request("streamFile", {"site": site.address, "inner_path": "invalid.file", "location": 0}, buff) assert "File read error" in response["error"] # Location over size buff = io.BytesIO() response = connection.request( "streamFile", {"site": site.address, "inner_path": "content.json", "location": 1024 * 1024}, buff ) assert "File read error" in response["error"] # Stream from parent dir buff = io.BytesIO() response = connection.request("streamFile", {"site": site.address, "inner_path": "../users.json", "location": 0}, buff) assert "File read exception" in response["error"] connection.close() client.stop()
def testPing(self, file_server, site): client = ConnectionServer(file_server.ip, 1545) connection = client.getConnection(file_server.ip, 1544) assert connection.ping() connection.close() client.stop()
def testPing(self, file_server, site): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) connection = client.getConnection("127.0.0.1", 1544) assert connection.ping() connection.close() client.stop()
def testGetFile(self, file_server, site): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) connection = client.getConnection("127.0.0.1", 1544) file_server.sites[site.address] = site response = connection.request("getFile", { "site": site.address, "inner_path": "content.json", "location": 0 }) assert "sign" in response["body"] # Invalid file response = connection.request("getFile", { "site": site.address, "inner_path": "invalid.file", "location": 0 }) assert "File read error" in response["error"] # Location over size response = connection.request( "getFile", { "site": site.address, "inner_path": "content.json", "location": 1024 * 1024 }) assert "File read error" in response["error"] # Stream from parent dir response = connection.request("getFile", { "site": site.address, "inner_path": "../users.json", "location": 0 }) assert "File read error" in response["error"] # Invalid site response = connection.request("getFile", { "site": "", "inner_path": "users.json", "location": 0 }) assert "Unknown site" in response["error"] response = connection.request("getFile", { "site": ".", "inner_path": "users.json", "location": 0 }) assert "Unknown site" in response["error"] connection.close() client.stop()
def testGetConnection(self, file_server): client = ConnectionServer(file_server.ip, 1545) connection = client.getConnection(file_server.ip, 1544) # Get connection by ip/port connection2 = client.getConnection(file_server.ip, 1544) assert connection == connection2 # Get connection by peerid assert not client.getConnection(file_server.ip, 1544, peer_id="notexists", create=False) connection2 = client.getConnection(file_server.ip, 1544, peer_id=connection.handshake["peer_id"], create=False) assert connection2 == connection connection.close() client.stop()
def testGetConnection(self, file_server): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) connection = client.getConnection("127.0.0.1", 1544) # Get connection by ip/port connection2 = client.getConnection("127.0.0.1", 1544) assert connection == connection2 # Get connection by peerid assert not client.getConnection("127.0.0.1", 1544, peer_id="notexists", create=False) connection2 = client.getConnection("127.0.0.1", 1544, peer_id=connection.handshake["peer_id"], create=False) assert connection2 == connection connection.close() client.stop()
def testSslConnection(self, file_server): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) assert file_server != client # Connect to myself connection = client.getConnection("127.0.0.1", 1544) assert len(file_server.connections) == 1 assert connection.handshake assert connection.crypt # Close connection connection.close() client.stop() time.sleep(0.01) assert len(file_server.connections) == 0
def testGetFile(self, file_server, site): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer(file_server.ip, 1545) connection = client.getConnection(file_server.ip, 1544) file_server.sites[site.address] = site # Normal request response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0}) assert b"sign" in response["body"] response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0, "file_size": site.storage.getSize("content.json")}) assert b"sign" in response["body"] # Invalid file response = connection.request("getFile", {"site": site.address, "inner_path": "invalid.file", "location": 0}) assert "File read error" in response["error"] # Location over size response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 1024 * 1024}) assert "File read error" in response["error"] # Stream from parent dir response = connection.request("getFile", {"site": site.address, "inner_path": "../users.json", "location": 0}) assert "File read exception" in response["error"] # Invalid site response = connection.request("getFile", {"site": "", "inner_path": "users.json", "location": 0}) assert "Unknown site" in response["error"] response = connection.request("getFile", {"site": ".", "inner_path": "users.json", "location": 0}) assert "Unknown site" in response["error"] # Invalid size response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0, "file_size": 1234}) assert "File size does not match" in response["error"] # Invalid path for path in ["../users.json", "./../users.json", "data/../content.json", ".../users.json"]: for sep in ["/", "\\"]: response = connection.request("getFile", {"site": site.address, "inner_path": path.replace("/", sep), "location": 0}) assert response["error"] == 'File read exception' connection.close() client.stop()
def testIpv6(self, file_server6): assert ":" in file_server6.ip client = ConnectionServer(file_server6.ip, 1545) connection = client.getConnection(file_server6.ip, 1544) assert connection.ping() # Close connection connection.close() client.stop() time.sleep(0.01) assert len(file_server6.connections) == 0 # Should not able to reach on ipv4 ip with pytest.raises(socket.error) as err: client = ConnectionServer("127.0.0.1", 1545) connection = client.getConnection("127.0.0.1", 1544)
def testSslConnection(self, file_server): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) assert file_server != client # Connect to myself with mock.patch('Config.config.ip_local', return_value=[]): # SSL not used for local ips connection = client.getConnection("127.0.0.1", 1544) assert len(file_server.connections) == 1 assert connection.handshake assert connection.crypt # Close connection connection.close() client.stop() time.sleep(0.01) assert len(file_server.connections) == 0
def testSslConnection(self, file_server): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) assert file_server != client # Connect to myself connection = client.getConnection("127.0.0.1", 1544) assert len(file_server.connections) == 1 assert len(file_server.ips) == 1 assert connection.handshake assert connection.crypt # Close connection connection.close() client.stop() time.sleep(0.01) assert len(file_server.connections) == 0 assert len(file_server.ips) == 0
def testSslConnection(self, file_server): client = ConnectionServer(file_server.ip, 1545) assert file_server != client # Connect to myself with mock.patch('Config.config.ip_local', return_value=[]): # SSL not used for local ips connection = client.getConnection(file_server.ip, 1544) assert len(file_server.connections) == 1 assert connection.handshake assert connection.crypt # Close connection connection.close() client.stop() time.sleep(0.01) assert len(file_server.connections) == 0
def stop(self): if self.running and self.portchecker.upnp_port_opened: self.log.debug('Closing port %d' % self.port) try: self.portchecker.portClose(self.port) self.log.info('Closed port via upnp.') except Exception as err: self.log.info("Failed at attempt to use upnp to close port: %s" % err) return ConnectionServer.stop(self)
def testRawConnection(self, file_server): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) assert file_server != client # Remove all supported crypto crypt_supported_bk = CryptConnection.manager.crypt_supported CryptConnection.manager.crypt_supported = [] connection = client.getConnection("127.0.0.1", 1544) assert len(file_server.connections) == 1 assert not connection.crypt # Close connection connection.close() client.stop() time.sleep(0.01) assert len(file_server.connections) == 0 # Reset supported crypts CryptConnection.manager.crypt_supported = crypt_supported_bk
def testRawConnection(self, file_server): client = ConnectionServer(file_server.ip, 1545) assert file_server != client # Remove all supported crypto crypt_supported_bk = CryptConnection.manager.crypt_supported CryptConnection.manager.crypt_supported = [] with mock.patch('Config.config.ip_local', return_value=[]): # SSL not used for local ips connection = client.getConnection(file_server.ip, 1544) assert len(file_server.connections) == 1 assert not connection.crypt # Close connection connection.close() client.stop() time.sleep(0.01) assert len(file_server.connections) == 0 # Reset supported crypts CryptConnection.manager.crypt_supported = crypt_supported_bk
def testGetFile(self, file_server, site): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) connection = client.getConnection("127.0.0.1", 1544) file_server.sites[site.address] = site # Normal request response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0}) assert "sign" in response["body"] response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0, "file_size": site.storage.getSize("content.json")}) assert "sign" in response["body"] # Invalid file response = connection.request("getFile", {"site": site.address, "inner_path": "invalid.file", "location": 0}) assert "File read error" in response["error"] # Location over size response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 1024 * 1024}) assert "File read error" in response["error"] # Stream from parent dir response = connection.request("getFile", {"site": site.address, "inner_path": "../users.json", "location": 0}) assert "File read error" in response["error"] # Invalid site response = connection.request("getFile", {"site": "", "inner_path": "users.json", "location": 0}) assert "Unknown site" in response["error"] response = connection.request("getFile", {"site": ".", "inner_path": "users.json", "location": 0}) assert "Unknown site" in response["error"] # Invalid size response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0, "file_size": 1234}) assert "File size does not match" in response["error"] connection.close() client.stop()
def testRawConnection(self, file_server): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) assert file_server != client # Remove all supported crypto crypt_supported_bk = CryptConnection.manager.crypt_supported CryptConnection.manager.crypt_supported = [] print "---" with mock.patch('Config.config.ip_local', return_value=[]): # SSL not used for local ips connection = client.getConnection("127.0.0.1", 1544) assert len(file_server.connections) == 1 assert not connection.crypt # Close connection connection.close() client.stop() time.sleep(0.01) assert len(file_server.connections) == 0 # Reset supported crypts CryptConnection.manager.crypt_supported = crypt_supported_bk
def testGetFile(self, file_server, site): file_server.ip_incoming = {} # Reset flood protection client = ConnectionServer("127.0.0.1", 1545) connection = client.getConnection("127.0.0.1", 1544) file_server.sites[site.address] = site response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 0}) assert "sign" in response["body"] # Invalid file response = connection.request("getFile", {"site": site.address, "inner_path": "invalid.file", "location": 0}) assert "No such file or directory" in response["error"] # Location over size response = connection.request("getFile", {"site": site.address, "inner_path": "content.json", "location": 1024 * 1024}) assert "File read error" in response["error"] # Stream from parent dir response = connection.request("getFile", {"site": site.address, "inner_path": "../users.json", "location": 0}) assert "File not allowed" in response["error"] connection.close() client.stop()
class FileServer(ConnectionServer): def __init__(self, ip=config.fileserver_ip, port=config.fileserver_port): self.log = logging.getLogger("FileServer") # ConnectionServer.__init__(self, ip, port, self.handleRequest) if config.ip_external: # Ip external defined in arguments 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 self.upnp_port_opened = False self.sites = {} self.last_request = time.time() self.files_parsing = {} # Check site file integrity def checkSite(self, site, check_files=False): if site.settings["serving"]: site.announce(mode="startup") # Announce site to tracker site.update(check_files=check_files) # Update site's content.json and download changed files site.sendMyHashfield() site.updateHashfield() if len(site.peers) > 5: # Keep active connections if site having 5 or more peers site.needConnections() # Bind and start serving sites def start(self, check_sites=True): self.sites = SiteManager.site_manager.list() self.log = logging.getLogger("FileServer") if config.debug: # Auto reload FileRequest on change from Debug import DebugReloader DebugReloader(self.reload) self.log.debug("Stopped.") def stop(self): if self.running and self.upnp_port_opened: try: UpnpPunch.ask_to_close_port(self.port, protos=["TCP"]) # self.log.info('Closed port via upnp.') except (UpnpPunch.UpnpError, UpnpPunch.IGDError), err: pass # self.log.info("Failed at attempt to use upnp to close port: %s" % err) ConnectionServer.stop(self)
def start(self, check_sites=True): self.sites = SiteManager.site_manager.list() 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_cleanup_sites = gevent.spawn(self.cleanupSites) thread_wakeup_watcher = gevent.spawn(self.wakeupWatcher) ConnectionServer.start(self) self.log.debug("Stopped.") def stop(self): if self.running and self.upnp_port_opened: self.log.debug('Closing port %d' % self.port) try: UpnpPunch.ask_to_close_port(self.port, protos=["TCP"]) 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)
# Bind and start serving sites def start(self, check_sites=True): self.sites = self.site_manager.list() 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_cleanup_sites = gevent.spawn(self.cleanupSites) thread_wakeup_watcher = gevent.spawn(self.wakeupWatcher) ConnectionServer.start(self) self.log.debug("Stopped.") def stop(self): if self.running and self.upnp_port_opened: self.log.debug('Closing port %d' % self.port) try: UpnpPunch.ask_to_close_port(self.port, protos=["TCP"]) 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) return ConnectionServer.stop(self)
def start(self, check_sites=True): ConnectionServer.start(self) self.sites = self.site_manager.list() 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_cleanup_sites = gevent.spawn(self.cleanupSites) thread_wakeup_watcher = gevent.spawn(self.wakeupWatcher) ConnectionServer.listen(self) self.log.debug("Stopped.") def stop(self): if self.running and self.upnp_port_opened: self.log.debug('Closing port %d' % self.port) try: UpnpPunch.ask_to_close_port(self.port, protos=["TCP"]) 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) return ConnectionServer.stop(self)
# Bind and start serving sites def start(self, check_sites=True): self.sites = SiteManager.site_manager.list() 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.") def stop(self): if self.running and self.upnp_port_opened: self.log.debug('Closing port %d' % self.port) try: UpnpPunch.ask_to_close_port(self.port, protos=["TCP"]) 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)