def process(self): parsed = urllib_parse.urlparse(self.uri) protocol = parsed[0] host = parsed[1].decode('ascii') if protocol in self.ports: port = self.ports[protocol] else: # handle pass if ':' in host: host, port = host.split(':') port = int(port) rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' if protocol in self.protocols: factory = self.protocols[protocol] headers = self.getAllHeaders().copy() if b'host' not in headers: headers[b'host'] = host.encode('ascii') headers.pop(b'user-agent', None) headers[b'user-agent'] = b'I2P' self.content.seek(0, 0) s = self.content.read() client = factory(self.method, rest, self.clientproto, headers, s, self) ep = self.endpointFactory(host, port) connectProtocol(ep, client.buildProtocol(ep))
def process(self): parsed = urllib_parse.urlparse(self.uri) protocol = parsed[0] host = parsed[1].decode('ascii') port = self.ports[protocol] if ':' in host: host, port = host.split(':') port = int(port) rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' class_ = self.protocols[protocol] headers = self.getAllHeaders().copy() if b'host' not in headers: headers[b'host'] = host.encode('ascii') self.content.seek(0, 0) s = self.content.read() clientFactory = class_(self.method, rest, self.clientproto, headers, s, self) self.reactor.connectTCP(host, port, clientFactory)
def process_prepare(self): # print self.method, self.uri, self.path, self.args, self.requestHeaders, self.responseHeaders, self.received_cookies, self.protocols, self.host, self.channel, self.content, self.cookies parsed = urllib_parse.urlparse(self.uri) protocol = parsed[0] or 'http' host = parsed[1].decode('ascii') port = self.ports[protocol] if ':' in host: host, port = host.split(':') port = int(port) rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' class_ = self.protocols[protocol] headers = self.getAllHeaders().copy() if b'host' not in headers: headers[b'host'] = host.encode('ascii') self.content.seek(0, 0) s = self.content.read() clientFactory = class_(self.method, rest, self.clientproto, headers, s, self) return host, port, clientFactory
def process(self): try: parsed = urllib_parse.urlparse(self.uri) protocol = parsed[0] host = parsed[1].decode('ascii') port = self.ports[protocol] if ':' in host: host, port = host.split(':') port = int(port) rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' class_ = self.protocols[protocol] headers = self.getAllHeaders().copy() if b'host' not in headers: headers[b'host'] = host.encode('ascii') self.content.seek(0, 0) s = self.content.read() clientFactory = class_(self.method, rest, self.clientproto, headers, s, self) log.msg(self.uri) m = hashlib.md5() if self.method == 'POST': self.content.seek(0) post_params = self.content.read() else: post_params = '' m.update(self.uri + post_params) self.cache_filename = os.path.join('cached_files', m.hexdigest() + ".cached") log.msg('Search for {}'.format(self.cache_filename)) if os.path.exists(self.cache_filename) and time.time() - os.path.getmtime(self.cache_filename) < 60: log.msg("Cache hit") data = open(self.cache_filename).read() self.write(data) log.msg('Cache is go to client') else: log.msg("Cache miss") self.reactor.connectTCP(host, port, clientFactory) except KeyError: log.msg("HTTPS is not supported at the moment!")
def process(self): log.msg('Processing: %s' % self.uri) parsed = urllib_parse.urlparse(self.uri) protocol = parsed[0] if protocol == b'': protocol = b'http' elif protocol == b'https': raise ConnectionRefusedError( 'The https protocol is not supported.') host = parsed[1].decode('ascii') if host is b'': raise ConnectionRefusedError('Host is empty.') port = self.ports[protocol] if ':' in host: host, port = host.split(':') port = int(port) rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' headers = self.getAllHeaders().copy() if b'host' not in headers: headers[b'host'] = host.encode('ascii') self.content.seek(0, 0) content = self.content.read() new_c_factory = self.factories[protocol] c_factory = new_c_factory(self.method, rest, self.clientproto, headers, content, self) self.client_endpoint = TCP4ClientEndpoint(self.reactor, host, port) d = self.client_endpoint.connect(c_factory) d.addCallback(lambda: self.got_protocol)
def process_prepare(self): parsed = urllib_parse.urlparse(self.uri) protocol = parsed[0] host = parsed[1].decode('ascii') port = self.ports[protocol] if ':' in host: host, port = host.split(':') port = int(port) rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' class_ = self.protocols[protocol] headers = self.getAllHeaders().copy() if b'host' not in headers: headers[b'host'] = host.encode('ascii') self.content.seek(0, 0) s = self.content.read() clientFactory = class_(self.method, rest, self.clientproto, headers, s, self) # if self.uri == 'http://music.163.com/eapi/song/like': # print rest, headers, s # if self.uri == 'http://music.163.com/eapi/v1/playlist/manipulate/tracks': # print rest, headers, s return host, port, clientFactory
def process(self): log.msg('Processing: %s' % self.uri) parsed = urllib_parse.urlparse(self.uri) protocol = parsed[0] if protocol == b'': protocol = b'http' elif protocol == b'https': raise ConnectionRefusedError('The https protocol is not supported.') host = parsed[1].decode('ascii') if host is b'': raise ConnectionRefusedError('Host is empty.') port = self.ports[protocol] if ':' in host: host, port = host.split(':') port = int(port) rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' headers = self.getAllHeaders().copy() if b'host' not in headers: headers[b'host'] = host.encode('ascii') self.content.seek(0, 0) content = self.content.read() new_c_factory = self.factories[protocol] c_factory = new_c_factory(self.method, rest, self.clientproto, headers, content, self) self.client_endpoint = TCP4ClientEndpoint(self.reactor, host, port) d = self.client_endpoint.connect(c_factory) d.addCallback(lambda: self.got_protocol)
def process(self): method = self.method.decode('ascii') uri = self.uri.decode('ascii') if method == 'CONNECT': host, port = split_host_port(uri) port = int(port) else: parsed = urllib_parse.urlparse(self.uri) decoded = parsed[1].decode('ascii') host, port = split_host_port(decoded) if port is None: port = 80 rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' headers = self.getAllHeaders().copy() self.content.seek(0, 0) s = self.content.read() proxy_suggestion = (self.force_proxy or self.force_direct or pacparser.find_proxy('http://{}'.format(host))) proxy_suggestions = proxy_suggestion.split(";") parsed_proxy_suggestion = self.proxy_suggestion_parser.match( proxy_suggestions[0]) if parsed_proxy_suggestion: connect_method, destination = parsed_proxy_suggestion.groups() if connect_method == 'PROXY': proxy_host, proxy_port = destination.split(":") proxy_port = int(proxy_port) if method != 'CONNECT': clientFactory = proxy.ProxyClientFactory( self.method, self.uri, self.clientproto, headers, s, self, ) logger.info('%s %s; forwarding request to %s:%s', method, uri, proxy_host, proxy_port) else: self.transport.unregisterProducer() self.transport.pauseProducing() rawConnectionProtocol = portforward.Proxy() rawConnectionProtocol.transport = self.transport self.transport.protocol = rawConnectionProtocol clientFactory = CONNECTProtocolForwardFactory(host, port) clientFactory.setServer(rawConnectionProtocol) logger.info('%s %s; establishing tunnel through %s:%s', method, uri, proxy_host, proxy_port) self.reactor.connectTCP(proxy_host, proxy_port, clientFactory) return else: # can this be anything else? Let's fall back to the DIRECT # codepath. pass if method != 'CONNECT': if b'host' not in headers: headers[b'host'] = host.encode('ascii') clientFactory = proxy.ProxyClientFactory( self.method, rest, self.clientproto, headers, s, self, ) logger.info('%s %s; forwarding request', method, uri) self.reactor.connectTCP(host, port, clientFactory) else: # hack/trick to move responsibility for this connection # away from a HTTP protocol class hierarchy and to a # port forward hierarchy self.transport.unregisterProducer() self.transport.pauseProducing() rawConnectionProtocol = portforward.Proxy() rawConnectionProtocol.transport = self.transport self.transport.protocol = rawConnectionProtocol clientFactory = portforward.ProxyClientFactory() clientFactory.setServer(rawConnectionProtocol) clientFactory.protocol = CONNECTProtocolClient # we don't do connectSSL, as the handshake is taken # care of by the client, and we only forward it logger.info('%s %s; establishing tunnel to %s:%s', method, uri, host, port) self.reactor.connectTCP(host, port, clientFactory)
def process(self, args=None): if not self.has_valid_creds(): self.request_creds() return None parsed = urllib_parse.urlparse(self.uri) protocol = parsed[0] host = parsed[1].decode('ascii') port = 80 if protocol != b'': port = self.ports[protocol] if ':' in host: host, port = host.split(':') port = int(port) doSSL = False if self.method.upper() == b"CONNECT": # TODO: finish HTTPS support #self.setResponseCode(200) self.transport.write( "HTTP/1.1 200 Connection established\r\nConnection: close\n\n". encode("ascii")) self.transport.startTLS(MySSLContext2()) protocol = b"https" self.host = parsed.scheme #self.transport.write("HTTPS unsupported\r\n".encode("ascii")) self.finish() return else: if self.isSecure(): headers = self.getAllHeaders().copy() if "host" not in headers: self.setResponseCode(400) self.write("Malformed request\r\n".encode("ascii")) self.finish() return host = headers["host"] protocol = b"https" doSSL = True if protocol not in self.ports: self.setResponseCode(400) self.write("Unsupported protocol\r\n".encode("ascii")) self.finish() return None rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' if protocol not in self.protocols: self.setResponseCode(400) self.write("Unsupported protocol\r\n".encode("ascii")) self.finish() return None class_ = self.protocols[protocol] headers = self.getAllHeaders().copy() if b'host' not in headers: headers[b'host'] = host.encode('ascii') # Add x-forwarded-for headers[b'X-Forwarded-For'] = self.getClientIP().encode('ascii') self.content.seek(0, 0) s = self.content.read() clientFactory = class_(self.method, rest, self.clientproto, headers, s, self) #print("About to connect upstream to {}:{}".format(host, port)) #print("Headers for upstream: {}".format(headers)) dec_path = self.path.decode("ascii", "ignore") if is_internal_page(dec_path): # 1st priority: internal pages self.serve_internal(dec_path, doSSL) elif should_block(dec_path): # 2nd priority: blocked page self.serve_blocked(doSSL) else: # 3rd priority: regular page if doSSL: print("Trying connect ssl to {} on {}".format(host, port)) self.reactor.connectSSL(host, port, clientFactory, ssl.ClientContextFactory()) else: self.reactor.connectTCP(host, port, clientFactory)