def __init__(self, session, port, dest, samaddr=i2p.socket.samaddr, nconnect=-1, timeout=None, **kwargs): """Tunnels localhost:port --> I2P Destination dest. A session named 'session' is created locally, for purposes of routing to 'dest'. nconnect and timeout are the maximum number of connections and maximum time per connection. All other arguments are passed to i2p.socket.socket(). This call blocks until the tunnel is ready.""" S = pysocket.socket(pysocket.AF_INET, pysocket.SOCK_STREAM) S.bind(('', port)) S.listen(4) obj = i2p.socket.socket(session, i2p.socket.SOCK_STREAM, samaddr, **kwargs) self.session = session self.dest = obj.dest def make_send(): C = i2p.socket.socket(session, i2p.socket.SOCK_STREAM, samaddr, **kwargs) C.setblocking(False) try: C.connect(dest) except: pass # Ignore 'would have blocked' error return C Tunnel.__init__(self, S, make_send, nconnect, timeout)
def make_send(): C = pysocket.socket(pysocket.AF_INET, pysocket.SOCK_STREAM) C.setblocking(False) try: C.connect(('127.0.0.1', port)) except: pass # Ignore 'would have blocked' error return C
def stop(dir=None, force=False): """Stop a locally installed I2P router, if it was started by the current Python program. If force is True, stop the router even if it was started by another process. Do nothing if force is False and the router was started by another program. The file 'router.config' is located using the same search process used for find(dir). It is parsed for 'router.shutdownPassword' and 'router.adminPort'. The router is shut down through the admin port. Raises i2p.RouterError if the I2P router is running but cannot be stopped. You must uncomment the 'router.shutdownPassword' line for this command to work. """ if force == False and our_router == False: return config = _parse_config(os.path.join(find(dir), router_config)) password = config.get('router.shutdownPassword', '') if password == '': raise ValueError('router.shutdownPassword not found in ' + router_config) admin_port = config.get('router.adminPort', '') if admin_port == '': raise ValueError('router.adminPort not found in ' + router_config) try: admin_port = int(admin_port) except: raise ValueError('invalid router.adminPort in ' + router_config) try: sock = pysocket.socket(pysocket.AF_INET, pysocket.SOCK_STREAM) sock.connect(('127.0.0.1', admin_port)) sock.send('GET /shutdown?password='******' HTTP/1.0\r\n\r\n') time.sleep(0.01) sock.close() except: raise i2p.RouterError('router shutdown failed')
def check(dir=None): """Checks whether a locally installed router is running. Does nothing if successful, otherwise raises i2p.RouterError. An I2P installation is located by using find(dir). The router.config file is parsed for 'router.adminPort'. This port is queried to determine whether the router is running. """ config = _parse_config(os.path.join(find(dir), router_config)) port = config.get('router.adminPort', '') try: port = int(port) except: raise ValueError('router.adminPort missing or bad in ' + router_config) try: s = pysocket.socket(pysocket.AF_INET, pysocket.SOCK_STREAM) s.connect(('127.0.0.1', port)) s.close() except pysocket.error: raise i2p.RouterError('could not contact 127.0.0.1:' + str(port))