Docstrings in network from #1368

This commit is contained in:
Dmitri Bogomolov 2019-07-08 17:20:29 +03:00
parent 2998599442
commit 391b5ded87
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
2 changed files with 32 additions and 0 deletions

View File

@ -21,6 +21,7 @@ from udp import UDPSocket
@Singleton @Singleton
class BMConnectionPool(object): class BMConnectionPool(object):
"""Pool of all existing connections"""
def __init__(self): def __init__(self):
asyncore.set_rates( asyncore.set_rates(
BMConfigParser().safeGetInt( BMConfigParser().safeGetInt(
@ -38,9 +39,14 @@ class BMConnectionPool(object):
self.bootstrapped = False self.bootstrapped = False
def connectToStream(self, streamNumber): def connectToStream(self, streamNumber):
"""Connect to a bitmessage stream"""
self.streams.append(streamNumber) self.streams.append(streamNumber)
def getConnectionByAddr(self, addr): def getConnectionByAddr(self, addr):
"""
Return an (existing) connection object based on a `Peer` object
(IP and port)
"""
try: try:
return self.inboundConnections[addr] return self.inboundConnections[addr]
except KeyError: except KeyError:
@ -60,6 +66,7 @@ class BMConnectionPool(object):
raise KeyError raise KeyError
def isAlreadyConnected(self, nodeid): def isAlreadyConnected(self, nodeid):
"""Check if we're already connected to this peer"""
for i in ( for i in (
self.inboundConnections.values() + self.inboundConnections.values() +
self.outboundConnections.values() self.outboundConnections.values()
@ -72,6 +79,7 @@ class BMConnectionPool(object):
return False return False
def addConnection(self, connection): def addConnection(self, connection):
"""Add a connection object to our internal dict"""
if isinstance(connection, UDPSocket): if isinstance(connection, UDPSocket):
return return
if connection.isOutbound: if connection.isOutbound:
@ -84,6 +92,7 @@ class BMConnectionPool(object):
connection connection
def removeConnection(self, connection): def removeConnection(self, connection):
"""Remove a connection from our internal dict"""
if isinstance(connection, UDPSocket): if isinstance(connection, UDPSocket):
del self.udpSockets[connection.listening.host] del self.udpSockets[connection.listening.host]
elif isinstance(connection, TCPServer): elif isinstance(connection, TCPServer):
@ -105,6 +114,7 @@ class BMConnectionPool(object):
connection.handle_close() connection.handle_close()
def getListeningIP(self): def getListeningIP(self):
"""What IP are we supposed to be listening on?"""
if BMConfigParser().safeGet( if BMConfigParser().safeGet(
"bitmessagesettings", "onionhostname").endswith(".onion"): "bitmessagesettings", "onionhostname").endswith(".onion"):
host = BMConfigParser().safeGet( host = BMConfigParser().safeGet(
@ -121,6 +131,7 @@ class BMConnectionPool(object):
return host return host
def startListening(self, bind=None): def startListening(self, bind=None):
"""Open a listening socket and start accepting connections on it"""
if bind is None: if bind is None:
bind = self.getListeningIP() bind = self.getListeningIP()
port = BMConfigParser().safeGetInt("bitmessagesettings", "port") port = BMConfigParser().safeGetInt("bitmessagesettings", "port")
@ -129,6 +140,10 @@ class BMConnectionPool(object):
self.listeningSockets[ls.destination] = ls self.listeningSockets[ls.destination] = ls
def startUDPSocket(self, bind=None): def startUDPSocket(self, bind=None):
"""
Open an UDP socket. Depending on settings, it can either only
accept incoming UDP packets, or also be able to send them.
"""
if bind is None: if bind is None:
host = self.getListeningIP() host = self.getListeningIP()
udpSocket = UDPSocket(host=host, announcing=True) udpSocket = UDPSocket(host=host, announcing=True)
@ -140,6 +155,7 @@ class BMConnectionPool(object):
self.udpSockets[udpSocket.listening.host] = udpSocket self.udpSockets[udpSocket.listening.host] = udpSocket
def loop(self): def loop(self):
"""Main Connectionpool's loop"""
# defaults to empty loop if outbound connections are maxed # defaults to empty loop if outbound connections are maxed
spawnConnections = False spawnConnections = False
acceptConnections = True acceptConnections = True

View File

@ -9,6 +9,7 @@ from debug import logger
class ProxyError(Exception): class ProxyError(Exception):
"""Base proxy exception class"""
errorCodes = ("Unknown error",) errorCodes = ("Unknown error",)
def __init__(self, code=-1): def __init__(self, code=-1):
@ -21,6 +22,7 @@ class ProxyError(Exception):
class GeneralProxyError(ProxyError): class GeneralProxyError(ProxyError):
"""General proxy error class (not specfic to an implementation)"""
errorCodes = ( errorCodes = (
"Success", "Success",
"Invalid data", "Invalid data",
@ -36,6 +38,7 @@ class GeneralProxyError(ProxyError):
class Proxy(AdvancedDispatcher): class Proxy(AdvancedDispatcher):
"""Base proxy class"""
# these are global, and if you change config during runtime, # these are global, and if you change config during runtime,
# all active/new instances should change too # all active/new instances should change too
_proxy = ("127.0.0.1", 9050) _proxy = ("127.0.0.1", 9050)
@ -46,10 +49,12 @@ class Proxy(AdvancedDispatcher):
@property @property
def proxy(self): def proxy(self):
"""Return proxy IP and port"""
return self.__class__._proxy return self.__class__._proxy
@proxy.setter @proxy.setter
def proxy(self, address): def proxy(self, address):
"""Set proxy IP and port"""
if (not isinstance(address, tuple) or len(address) < 2 or if (not isinstance(address, tuple) or len(address) < 2 or
not isinstance(address[0], str) or not isinstance(address[0], str) or
not isinstance(address[1], int)): not isinstance(address[1], int)):
@ -58,18 +63,25 @@ class Proxy(AdvancedDispatcher):
@property @property
def auth(self): def auth(self):
"""Return proxy authentication settings"""
return self.__class__._auth return self.__class__._auth
@auth.setter @auth.setter
def auth(self, authTuple): def auth(self, authTuple):
"""Set proxy authentication (username and password)"""
self.__class__._auth = authTuple self.__class__._auth = authTuple
@property @property
def onion_proxy(self): def onion_proxy(self):
"""
Return separate proxy IP and port for use only with onion
addresses. Untested.
"""
return self.__class__._onion_proxy return self.__class__._onion_proxy
@onion_proxy.setter @onion_proxy.setter
def onion_proxy(self, address): def onion_proxy(self, address):
"""Set onion proxy address"""
if address is not None and ( if address is not None and (
not isinstance(address, tuple) or len(address) < 2 or not isinstance(address, tuple) or len(address) < 2 or
not isinstance(address[0], str) or not isinstance(address[0], str) or
@ -79,10 +91,12 @@ class Proxy(AdvancedDispatcher):
@property @property
def onion_auth(self): def onion_auth(self):
"""Return proxy authentication settings for onion hosts only"""
return self.__class__._onion_auth return self.__class__._onion_auth
@onion_auth.setter @onion_auth.setter
def onion_auth(self, authTuple): def onion_auth(self, authTuple):
"""Set proxy authentication for onion hosts only. Untested."""
self.__class__._onion_auth = authTuple self.__class__._onion_auth = authTuple
def __init__(self, address): def __init__(self, address):
@ -110,6 +124,7 @@ class Proxy(AdvancedDispatcher):
) )
def handle_connect(self): def handle_connect(self):
"""Handle connection event (to the proxy)"""
self.set_state("init") self.set_state("init")
try: try:
AdvancedDispatcher.handle_connect(self) AdvancedDispatcher.handle_connect(self)
@ -122,5 +137,6 @@ class Proxy(AdvancedDispatcher):
self.state_init() self.state_init()
def state_proxy_handshake_done(self): def state_proxy_handshake_done(self):
"""Handshake is complete at this point"""
self.connectedAt = time.time() self.connectedAt = time.time()
return False return False