def connectionMade(self): if _Debug: lg.out(_DebugLevel, 'tcp_connection.connectionMade %s' % net_misc.pack_address(self.getTransportAddress())) address = self.getAddress() name = 'tcp_connection[%s]' % strng.to_text(net_misc.pack_address(address)) automat.Automat.__init__( self, name, 'AT_STARTUP', debug_level=_DebugLevel, log_events=_Debug, publish_events=False) self.log_transitions = _Debug self.automat('connection-made')
def _send_new_identity(self): """ Send created identity to the identity server to register it. TODO: need to close transport and gateway after that """ lg.out(4, 'id_registrator._send_new_identity ') from transport import gateway from transport import network_transport from transport.tcp import tcp_interface gateway.init() interface = tcp_interface.GateInterface() transport = network_transport.NetworkTransport('tcp', interface) transport.automat('init', gateway.listener()) transport.automat('start') gateway.start() sendfilename = settings.LocalIdentityFilename() + '.new' dlist = [] for idurl in self.new_identity.sources: self.free_idurls.remove(strng.to_bin(idurl)) _, host, _, _ = nameurl.UrlParse(idurl) _, tcpport = known_servers.by_host().get( host, (settings.IdentityWebPort(), settings.IdentityServerPort())) srvhost = net_misc.pack_address(( host, tcpport, )) dlist.append( gateway.send_file_single(idurl, 'tcp', srvhost, sendfilename, 'Identity')) # assert len(self.free_idurls) == 0 return DeferredList(dlist, fireOnOneCallback=True)
def _send_new_identity(self): """ Send created identity to the identity servers to register it. """ if _Debug: lg.out(_DebugLevel, 'id_registrator._send_new_identity') from transport.tcp import tcp_node sendfilename = settings.LocalIdentityFilename() + '.new' dlist = [] for idurl in self.new_identity.getSources(as_originals=True): self.free_idurls.remove(strng.to_bin(idurl)) _, host, _, _ = nameurl.UrlParse(idurl) _, tcpport = known_servers.by_host().get( host, (settings.IdentityWebPort(), settings.IdentityServerPort())) srvhost = net_misc.pack_address(( host, tcpport, )) if _Debug: lg.out(_DebugLevel, ' sending to %r via TCP' % srvhost) dlist.append( tcp_node.send( sendfilename, srvhost, 'Identity', keep_alive=False, )) return DeferredList(dlist, fireOnOneCallback=True)
def _do_send_my_identity(self): """ Send my updated identity to the identity servers to register it. """ from transport.tcp import tcp_node sendfilename = settings.LocalIdentityFilename() my_sources = my_id.getLocalIdentity().getSources(as_originals=True) dlist = [] if _Debug: lg.out( _DebugLevel, 'id_rotator._do_send_my_identity my_sources=%r' % my_sources) for idurl_bin in my_sources: _, host, _, _ = nameurl.UrlParse(idurl_bin) tcpport = None if host in self.preferred_servers: tcpport = int(self.preferred_servers[host][1]) if not tcpport and host in self.known_servers: tcpport = int(self.known_servers[host][1]) if not tcpport: tcpport = settings.IdentityServerPort() srvhost = net_misc.pack_address(( host, tcpport, )) if _Debug: lg.out(_DebugLevel, ' sending to %r via TCP' % srvhost) dlist.append( tcp_node.send( sendfilename, srvhost, 'Identity', keep_alive=False, )) return DeferredList(dlist, fireOnOneCallback=True)
def _send_new_identity(self): """ Send created identity to the identity servers to register it. """ if _Debug: lg.out(_DebugLevel, 'id_registrator._send_new_identity') dlist = [] payload = self.new_identity.serialize(as_text=False) for idurl in self.new_identity.getSources(as_originals=True): self.free_idurls.remove(strng.to_bin(idurl)) _, host, webport, filename = nameurl.UrlParse(idurl) url = net_misc.pack_address(( host, webport, ), proto='http') dlist.append( net_misc.http_post_data( url=url, data=payload, connectTimeout=15, )) if _Debug: lg.args(_DebugLevel, url=url, filename=filename, size=len(payload)) return DeferredList(dlist, fireOnOneCallback=True)
def _do_send_my_identity(self): """ Send my updated identity to the identity servers to register it. """ my_sources = my_id.getLocalIdentity().getSources(as_originals=True) payload = my_id.getLocalIdentity().serialize(as_text=False) dlist = [] if _Debug: lg.out(_DebugLevel, 'id_rotator._do_send_my_identity my_sources=%r' % my_sources) for idurl_bin in my_sources: _, host, _webport, filename = nameurl.UrlParse(idurl_bin) webport = None if host in self.preferred_servers: webport = int(self.preferred_servers[host][0]) if not webport and host in self.known_servers: webport = int(self.known_servers[host][0]) if not webport: webport = _webport url = net_misc.pack_address((host, webport, ), proto='http') dlist.append(net_misc.http_post_data( url=url, data=payload, connectTimeout=15, )) if _Debug: lg.args(_DebugLevel, url=url, filename=filename, size=len(payload)) return DeferredList(dlist, fireOnOneCallback=True)
def interface_receiving_started(host, new_options={}): """ """ if proxy(): return proxy().callRemote('receiving_started', 'http', net_misc.pack_address(host), new_options) lg.warn('transport_http is not ready') return fail(Exception('transport_http is not ready'))
def __init__(self, proto, host, size=0): self.proto = proto self.host = net_misc.pack_address(host) self.time = time.time() self.transfer_id = None self.status = None self.error_message = None self.bytes_sent = 0 self.size = size
def interface_register_file_receiving(host, sender_idurl, filename, size=0): """ """ if proxy(): return proxy().callRemote('register_file_receiving', 'http', net_misc.pack_address(host), sender_idurl, filename, size) lg.warn('transport_http is not ready') return fail(Exception('transport_http is not ready'))
def doSendHello(self, *args, **kwargs): """ Action method. """ from transport.tcp import tcp_node host = strng.to_bin(tcp_node.my_host() or '127.0.0.1:7771') idurl = strng.to_bin(tcp_node.my_idurl() or 'None') payload = host + b' ' + idurl if _Debug: lg.out(_DebugLevel, 'tcp_connection.doSendHello %r to %s' % (payload, net_misc.pack_address(self.getTransportAddress()))) self.sendData(CMD_HELLO, payload)
def to_json(self): j = super().to_json() j.update({ 'proto': self.router_proto_host[0] if self.router_proto_host else '', 'host': net_misc.pack_address(self.router_proto_host[1]) if self.router_proto_host else '', 'idurl': self.router_idurl, 'bytes_received': self.traffic_in, 'bytes_sent': 0, 'connection_info': self.router_connection_info, 'idle_seconds': int(time.time() - self.latest_packet_received), }) return j
def interface_cancelled_file_sending(host, filename, size=0, description=None, error_message=None): """ """ if proxy(): return proxy().callRemote('cancelled_file_sending', 'http', net_misc.pack_address(host), filename, size, description, error_message) lg.warn('transport_http is not ready') return fail(Exception('transport_http is not ready'))
def doSendYourIPPort(self, arg): """ Action method. """ try: datagram, address = arg command, payload = datagram except: return False youripport = net_misc.pack_address((address[0], address[1])) udp.send_command(self.listen_port, udp.CMD_MYIPPORT, youripport, address) lg.out( 4, 'stun_server.doSendYourIPPort [%s] to %s' % (youripport, address))
def interface_cancelled_file_receiving(host, filename, size, error_message=None): """ """ if proxy(): return proxy().callRemote( 'cancelled_file_receiving', 'tcp', net_misc.pack_address(host), filename, size, error_message, ).addErrback(proxy_errback) lg.warn('transport_tcp is not ready') return fail( Exception('transport_tcp is not ready')).addErrback(proxy_errback)
def receive(options): global _MyIDURL global _MyHost global _InternalPort global _Listener from transport.tcp import tcp_interface if _Debug: lg.out(_DebugLevel, 'tcp_node.receive %r' % options) if _Listener: lg.warn('listener already exist') tcp_interface.interface_receiving_started(_MyHost, options) return _Listener try: _MyIDURL = options['idurl'] _InternalPort = int(options['tcp_port']) except: _MyIDURL = None _InternalPort = None lg.exc() return None try: _Listener = reactor.listenTCP( _InternalPort, TCPFactory(None, keep_alive=True)) # @UndefinedVariable _MyHost = net_misc.pack_address( (options['host'].split(b':')[0], int(_InternalPort))) tcp_interface.interface_receiving_started(_MyHost, options) except CannotListenError as ex: lg.warn('port "%d" is busy' % _InternalPort) tcp_interface.interface_receiving_failed('port is busy') return None except Exception as ex: try: e = ex.getErrorMessage() except: e = str(ex) tcp_interface.interface_receiving_failed(e) lg.exc() return None return _Listener
def interface_register_file_sending(host, receiver_idurl, filename, size=0, description=''): """ """ if proxy(): return proxy().callRemote( 'register_file_sending', 'tcp', net_misc.pack_address(host), receiver_idurl, filename, size, description, ).addErrback(proxy_errback) lg.warn('transport_tcp is not ready') return fail( Exception('transport_tcp is not ready')).addErrback(proxy_errback)
def SendServers(): """ My identity file can be stored in different locations, see the "sources" field. So I can use different identity servers to store more secure and reliable. This method will send my identity file to all my identity servers with HTTP POST method. """ LocalIdentity = my_id.getLocalIdentity() payload = LocalIdentity.serialize(as_text=False) dlist = [] for idurl in LocalIdentity.getSources(as_originals=True): _, host, webport, filename = nameurl.UrlParse(idurl) url = net_misc.pack_address((host, webport, ), proto='http') dlist.append(net_misc.http_post_data( url=url, data=payload, connectTimeout=15, )) if _Debug: lg.args(_DebugLevel, url=url, filename=filename, size=len(payload)) dl = DeferredList(dlist, consumeErrors=True) return dl
def connectionLost(self, reason): if _Debug: lg.out( _DebugLevel, 'tcp_connection.connectionLost with %s' % net_misc.pack_address(self.getTransportAddress())) self.automat('connection-lost')