def test_REPORT_SERVICES(): setup_base_env() e = Env() assert e.report_services == [] # This has a blank entry between commas os.environ['REPORT_SERVICES'] = 'tcp://foo.bar:1234,,ws://1.2.3.4:567' e = Env() assert e.report_services == [ Service('tcp', NetAddress('foo.bar', 1234)), Service('ws', NetAddress('1.2.3.4', 567)), ]
def test_SERVICES(): setup_base_env() e = Env() assert e.services == [] # This has a blank entry between commas os.environ['SERVICES'] = 'tcp://foo.bar:1234,,ws://1.2.3.4:567,rpc://[::1]:700' e = Env() assert e.services == [ Service('tcp', NetAddress('foo.bar', 1234)), Service('ws', NetAddress('1.2.3.4', 567)), Service('rpc', NetAddress('::1', 700)), ]
def get_netaddress(self, key: str) -> Optional[NetAddress]: text = self.get(key) if text: try: return NetAddress.from_string(text) except: pass
def add_node_announcements(self, msg_payloads): # note: signatures have already been verified. if type(msg_payloads) is dict: msg_payloads = [msg_payloads] new_nodes = {} for msg_payload in msg_payloads: try: node_info, node_addresses = NodeInfo.from_msg(msg_payload) except IncompatibleOrInsaneFeatures: continue node_id = node_info.node_id # Ignore node if it has no associated channel (DoS protection) if node_id not in self._channels_for_node: #self.logger.info('ignoring orphan node_announcement') continue node = self._nodes.get(node_id) if node and node.timestamp >= node_info.timestamp: continue node = new_nodes.get(node_id) if node and node.timestamp >= node_info.timestamp: continue # save with self.lock: self._nodes[node_id] = node_info if 'raw' in msg_payload: self._db_save_node_info(node_id, msg_payload['raw']) with self.lock: for addr in node_addresses: net_addr = NetAddress(addr.host, addr.port) self._addresses[node_id][net_addr] = self._addresses[node_id].get(net_addr) or 0 self._db_save_node_addresses(node_addresses) self.logger.debug("on_node_announcement: %d/%d"%(len(new_nodes), len(msg_payloads))) self.update_counts()
def get_peer_addresses(self) -> Iterator[LNPeerAddr]: # sort by timestamp: most recent first addrs = sorted(self.storage.get('peer_network_addresses', {}).items(), key=lambda x: x[1], reverse=True) for net_addr_str, ts in addrs: net_addr = NetAddress.from_string(net_addr_str) yield LNPeerAddr(host=str(net_addr.host), port=net_addr.port, pubkey=self.node_id)
def get_netaddress(self, key: str) -> Optional[NetAddress]: text = self.get(key) if text: try: host, port = text.split(':') return NetAddress(host, port) except: pass
def deserialize_server(server_str: str) -> Tuple[str, str, str]: # host might be IPv6 address, hence do rsplit: host, port, protocol = str(server_str).rsplit(':', 2) if not host: raise ValueError('host must not be empty') if host[0] == '[' and host[-1] == ']': # IPv6 host = host[1:-1] if protocol not in ('s', 't'): raise ValueError('invalid network protocol: {}'.format(protocol)) net_addr = NetAddress(host, port) # this validates host and port host = str(net_addr.host) # canonical form (if e.g. IPv6 address) return host, port, protocol
def load_data(self): if self.data_loaded.is_set(): return # Note: this method takes several seconds... mostly due to lnmsg.decode_msg being slow. c = self.conn.cursor() c.execute("""SELECT * FROM address""") for x in c: node_id, host, port, timestamp = x try: net_addr = NetAddress(host, port) except Exception: continue self._addresses[node_id][net_addr] = int(timestamp or 0) def newest_ts_for_node_id(node_id): newest_ts = 0 for addr, ts in self._addresses[node_id].items(): newest_ts = max(newest_ts, ts) return newest_ts sorted_node_ids = sorted(self._addresses.keys(), key=newest_ts_for_node_id, reverse=True) self._recent_peers = sorted_node_ids[:self.NUM_MAX_RECENT_PEERS] c.execute("""SELECT * FROM channel_info""") for short_channel_id, msg in c: try: ci = ChannelInfo.from_raw_msg(msg) except IncompatibleOrInsaneFeatures: continue self._channels[ShortChannelID.normalize(short_channel_id)] = ci c.execute("""SELECT * FROM node_info""") for node_id, msg in c: try: node_info, node_addresses = NodeInfo.from_raw_msg(msg) except IncompatibleOrInsaneFeatures: continue # don't load node_addresses because they dont have timestamps self._nodes[node_id] = node_info c.execute("""SELECT * FROM policy""") for key, msg in c: p = Policy.from_raw_msg(key, msg) self._policies[(p.start_node, p.short_channel_id)] = p for channel_info in self._channels.values(): self._channels_for_node[channel_info.node1_id].add(channel_info.short_channel_id) self._channels_for_node[channel_info.node2_id].add(channel_info.short_channel_id) self._update_num_policies_for_chan(channel_info.short_channel_id) self.logger.info(f'data loaded. {len(self._channels)} chans. {len(self._policies)} policies. ' f'{len(self._channels_for_node)} nodes.') self.update_counts() (nchans_with_0p, nchans_with_1p, nchans_with_2p) = self.get_num_channels_partitioned_by_policy_count() self.logger.info(f'num_channels_partitioned_by_policy_count. ' f'0p: {nchans_with_0p}, 1p: {nchans_with_1p}, 2p: {nchans_with_2p}') self.asyncio_loop.call_soon_threadsafe(self.data_loaded.set) util.trigger_callback('gossip_db_loaded')
def __init__(self, host: str, port: int, pubkey: bytes): assert isinstance(host, str), repr(host) assert isinstance(port, int), repr(port) assert isinstance(pubkey, bytes), repr(pubkey) try: net_addr = NetAddress(host, port) # this validates host and port except Exception as e: raise ValueError(f"cannot construct LNPeerAddr: invalid host or port (host={host}, port={port})") from e # note: not validating pubkey as it would be too expensive: # if not ECPubkey.is_pubkey_bytes(pubkey): raise ValueError() self.host = host self.port = port self.pubkey = pubkey self._net_addr_str = str(net_addr)
def _set_proxy(self, proxy: dict): if proxy: username, pw = proxy.get('user'), proxy.get('password') if not username or not pw: auth = None else: auth = aiorpcx.socks.SOCKSUserAuth(username, pw) addr = NetAddress(proxy['host'], proxy['port']) if proxy['mode'] == "socks4": self.proxy = aiorpcx.socks.SOCKSProxy(addr, aiorpcx.socks.SOCKS4a, auth) elif proxy['mode'] == "socks5": self.proxy = aiorpcx.socks.SOCKSProxy(addr, aiorpcx.socks.SOCKS5, auth) else: raise NotImplementedError # http proxy not available with aiorpcx else: self.proxy = None
def __init__(self, host: str, port: Union[int, str], *, protocol: str = None): assert isinstance(host, str), repr(host) if protocol is None: protocol = 's' if not host: raise ValueError('host must not be empty') if host[0] == '[' and host[-1] == ']': # IPv6 host = host[1:-1] try: net_addr = NetAddress(host, port) # this validates host and port except Exception as e: raise ValueError(f"cannot construct ServerAddr: invalid host or port (host={host}, port={port})") from e if protocol not in _KNOWN_NETWORK_PROTOCOLS: raise ValueError(f"invalid network protocol: {protocol}") self.host = str(net_addr.host) # canonical form (if e.g. IPv6 address) self.port = int(net_addr.port) self.protocol = protocol self._net_addr_str = str(net_addr)
def set_proxy(self): if self.filling_in: return proxy = None if self.proxy_cb.isChecked(): try: address = NetAddress(self.proxy_host.text(), self.proxy_port.text()) if self.proxy_username.text(): auth = SVUserAuth(self.proxy_username.text(), self.proxy_password.text()) else: auth = None proxy = SVProxy(address, self.proxy_mode.currentText(), auth) except Exception: logger.exception('error setting proxy') if not proxy: self.tor_cb.setChecked(False) self.network.set_proxy(proxy)
def diagnostic_name(self): return str(NetAddress(self.host, self.port))