def buildProtocol(self, addr): try: endpoint, factory = self.endpoints.pop(0)[1] except IndexError: factory = ProxyFactory(None, None) else: self.endpoints.append((endpoint, factory)) return factory.buildProtocol(addr)
def listen(): for i, key in enumerate(_get_service_keys(os.environ)): host = os.environ[key] port = int(os.environ[key[:-4] + "PORT"]) service = endpoints.TCP4ServerEndpoint(reactor, 2000 + i) service.listen(ProxyFactory(host, port)) print("Connecting port {} to {}:{} ({})".format( 2000 + i, host, port, key))
def getWebServer(cls, config_path): """ Return a service suitable for creating an application object. """ if not config_path.startswith("/"): raise ValueError, "config_path must be an absolute path" config = ConfigParser() config.read(config_path) bind = config.get("webserver", "bind") port = int(config.get("webserver", "port")) root_path = config.get("webserver", "root") if not root_path.startswith("/"): root_path = os.path.join(os.path.dirname(config_path), root_path) # create a resource to serve static files root = static.File(root_path) web_server = cls(root, port, bind) # setup proxies if config.has_section('url-maps'): for url, dest in config.items('url-maps'): tcp_host, tcp_port = dest.split(':') service = "generic" if ',' in tcp_port: tcp_port, service = tcp_port.split(',') proxy = ProxyFactory(tcp_host, int(tcp_port)) ws = WebSocketWrapperFactory(proxy) ws.protocol = services_wrappers.get(service, services_wrappers["generic"]) web_server.addHandler(url, ws.buildHandler) return web_server
def alter_do_connections(self,tunnel_route,port,torused): linking_list = [None]*(len(tunnel_route)+1) for idx in range(len(linking_list)): if idx==0: if torused: linking_list[idx] = clientFromString(reactor, "unix:/var/run/tor/control") else: linking_list[idx] = TCP4ClientEndpoint(reactor, '127.0.0.1', 8080) else: proxy_data = tunnel_route[idx-1] linking_list[idx] = SOCKS5ClientEndpoint( proxy_data.ip_address, proxy_data.port, linking_list[idx-1], methods={'login': (str(proxy_data.user), str(proxy_data.password))}) #SOCKS4ClientEndpoint(proxy_data.ip_address, proxy_data.port, linking_list[idx-1]) if torused: tor = yield txtorcon.connect(reactor,linking_list[-1:][0]) config = yield tor.get_config() config.SOCKSPort = [str(port)] yield config.save() else: linking_list[-1:][0].connect(ProxyFactory('127.0.0.1',port))
def main(): from twisted.internet import reactor pfactory = ProxyFactory('173.37.183.72',993) pfactory.protocol = PS reactor.listenTCP(9999, pfactory) reactor.run()
def __init__(self, hostports): self.factories = [] for (host, port) in hostports: self.factories.append(ProxyFactory(host, port))
def main(): from twisted.internet import reactor pfactory = ProxyFactory('173.37.183.72', 993) pfactory.protocol = PS reactor.listenTCP(9999, pfactory) reactor.run()
def add_endpoint(self, host, port): endpoint = tuple(host, port) if endpoint not in dict(self.endpoints): self.endpoints.append((endpoint, ProxyFactory(host, port))) else: raise ObjectExists('endpoint already exists in load balancer')