def forward_local(self): host = self.headers.get('Host', '') host_ip, _, port = host.rpartition(':') http_client = simple_http_client.HTTP_client((host_ip, int(port))) request_headers = dict((k.title(), v) for k, v in self.headers.items()) payload = b'' if 'Content-Length' in request_headers: try: payload_len = int(request_headers.get('Content-Length', 0)) payload = self.rfile.read(payload_len) except Exception as e: xlog.warn('forward_local read payload failed:%s', e) return self.parsed_url = urlparse.urlparse(self.path) if len(self.parsed_url[4]): path = '?'.join([self.parsed_url[2], self.parsed_url[4]]) else: path = self.parsed_url[2] content, status, response = http_client.request( self.command, path, request_headers, payload) if not status: xlog.warn("forward_local fail") return out_list = [] out_list.append("HTTP/1.1 %d\r\n" % status) for key, value in response.getheaders(): key = key.title() out_list.append("%s: %s\r\n" % (key, value)) out_list.append("\r\n") out_list.append(content) self.wfile.write("".join(out_list))
def update_front_domains(): next_update_time = time.time() while connect_control.keep_running: if time.time() < next_update_time: time.sleep(4) continue try: client = simple_http_client.HTTP_client( "raw.githubusercontent.com", use_https=True) path = "/XX-net/XX-Net/master/code/default/x_tunnel/local/cloudflare_front/front_domains.json" content, status, response = client.request("GET", path) if status != 200: xlog.warn("update front domains fail:%d", status) raise Exception("status:%r", status) front_domains_fn = os.path.join(config.DATA_PATH, "front_domains.json") if os.path.exists(front_domains_fn): with open(front_domains_fn, "r") as fd: old_content = fd.read() if content != old_content: with open(front_domains_fn, "w") as fd: fd.write(content) check_ip.update_front_domains() next_update_time = time.time() + (4 * 3600) xlog.info("updated cloudflare front domains from github.") except Exception as e: next_update_time = time.time() + (1800) xlog.debug( "updated cloudflare front domains from github fail:%r", e)
def update_front_domains(self): client = simple_http_client.HTTP_client("raw.githubusercontent.com", use_https=True) path = "/XX-net/XX-Net/master/code/default/x_tunnel/local/cloudflare_front/front_domains.json" content, status, response = client.request("GET", path) if status != 200: xlog.warn("update front domains fail:%d", status) return front_domains_fn = os.path.join(config.DATA_PATH, "front_domains.json") with open(front_domains_fn, "w") as fd: fd.write(content) check_ip.update_front_domains()
def forward_local(self): """ If browser send localhost:xxx request to GAE_proxy, we forward it to localhost. """ host = self.headers.get('Host', '') host_ip = None port = None host_ip, _, port = host.rpartition(':') self.parsed_url = urlparse.urlparse(self.path) if not host_ip: host_ip = host try: port = int(port) except: if self.parsed_url[0] == 'https': port = 443 else: port = 80 http_client = simple_http_client.HTTP_client((host_ip, port)) request_headers = dict((k.title(), v) for k, v in self.headers.items()) payload = b'' if 'Content-Length' in request_headers: try: payload_len = int(request_headers.get('Content-Length', 0)) payload = self.rfile.read(payload_len) except Exception as e: xlog.warn('forward_local read payload failed:%s', e) return if len(self.parsed_url[4]): path = '?'.join([self.parsed_url[2], self.parsed_url[4]]) else: path = self.parsed_url[2] content, status, response = http_client.request(self.command, path, request_headers, payload) if not status: xlog.warn("forward_local fail: %d, host: %s, command: %s, path: %s, headers: %s, payload: %s, response: %s", status, host, self.command, path, request_headers, payload, response) return out_list = [] out_list.append("HTTP/1.1 %d\r\n" % status) for key, value in response.getheaders(): key = key.title() out_list.append("%s: %s\r\n" % (key, value)) out_list.append("\r\n") out_list.append(content) self.wfile.write("".join(out_list))
def forward_local(self): """ If browser send localhost:xxx request to GAE_proxy, we forward it to localhost. """ http_client = simple_http_client.HTTP_client( (self.host, int(self.port))) content, status, response = http_client.request( self.command, self.path, self.headers, self.payload) if not status: xlog.warn("forward_local fail") return out_list = [] out_list.append("HTTP/1.1 %d\r\n" % status) for key, value in response.getheaders(): key = key.title() out_list.append("%s: %s\r\n" % (key, value)) out_list.append("\r\n") out_list.append(content) self.wfile.write("".join(out_list))