def https_handler(self): line = self.read_crlf_line() line = line.decode('iso-8859-1') words = line.split() if len(words) == 3: command, path, version = words elif len(words) == 2: command, path = words version = "HTTP/1.1" else: xlog.warn("https req line fail:%s", line) return if command != "CONNECT": xlog.warn("https req line fail:%s", line) return host, _, port = path.rpartition(':') host = host.encode() port = int(port) header_block = self.read_headers() sock = self.conn # xlog.debug("https %r connect to %s:%d", self.client_address, host, port) sock.send(b'HTTP/1.1 200 OK\r\n\r\n') handle_domain_proxy(sock, host, port, self.client_address)
def socks5_handler(self): sock = self.conn socks_version = ord(self.read_bytes(1)) auth_mode_num = ord(self.read_bytes(1)) data = self.read_bytes(auth_mode_num) sock.send(b"\x05\x00") # socks version 5, no auth needed. try: data = self.read_bytes(4) except Exception as e: xlog.debug("socks5 auth num:%d, list:%s", auth_mode_num, utils.str2hex(data)) xlog.warn("socks5 protocol error:%r", e) return socks_version = ord(data[0]) if socks_version != 5: xlog.warn("request version:%d error", socks_version) return command = ord(data[1]) if command != 1: # 1. Tcp connect xlog.warn("request not supported command mode:%d", command) sock.send(b"\x05\x07\x00\x01") # Command not supported return addrtype_pack = data[3] addrtype = ord(addrtype_pack) if addrtype == 1: # IPv4 addr_pack = self.read_bytes(4) addr = socket.inet_ntoa(addr_pack) elif addrtype == 3: # Domain name domain_len_pack = self.read_bytes(1)[0] domain_len = ord(domain_len_pack) domain = self.read_bytes(domain_len) addr_pack = domain_len_pack + domain addr = domain elif addrtype == 4: # IPv6 addr_pack = self.read_bytes(16) addr = socket.inet_ntop(socket.AF_INET6, addr_pack) else: xlog.warn("request address type unknown:%d", addrtype) sock.send(b"\x05\x07\x00\x01") # Command not supported return port = struct.unpack('>H', self.rfile.read(2))[0] # xlog.debug("socks5 %r connect to %s:%d", self.client_address, addr, port) reply = b"\x05\x00\x00" + addrtype_pack + addr_pack + struct.pack( ">H", port) sock.send(reply) if addrtype in [1, 4]: handle_ip_proxy(sock, addr, port, self.client_address) else: handle_domain_proxy(sock, addr, port, self.client_address)
def socks5_handler(self): sock = self.conn socks_version = ord(self.read_bytes(1)) auth_mode_num = ord(self.read_bytes(1)) data = self.read_bytes(auth_mode_num) sock.send(b"\x05\x00") # socks version 5, no auth needed. try: data = self.read_bytes(4) except Exception as e: xlog.debug("socks5 auth num:%d, list:%s", auth_mode_num, utils.str2hex(data)) xlog.warn("socks5 protocol error:%r", e) return socks_version = ord(data[0]) if socks_version != 5: xlog.warn("request version:%d error", socks_version) return command = ord(data[1]) if command != 1: # 1. Tcp connect xlog.warn("request not supported command mode:%d", command) sock.send(b"\x05\x07\x00\x01") # Command not supported return addrtype_pack = data[3] addrtype = ord(addrtype_pack) if addrtype == 1: # IPv4 addr_pack = self.read_bytes(4) addr = socket.inet_ntoa(addr_pack) elif addrtype == 3: # Domain name domain_len_pack = self.read_bytes(1)[0] domain_len = ord(domain_len_pack) domain = self.read_bytes(domain_len) addr_pack = domain_len_pack + domain addr = domain elif addrtype == 4: # IPv6 addr_pack = self.read_bytes(16) addr = socket.inet_ntop(socket.AF_INET6, addr_pack) else: xlog.warn("request address type unknown:%d", addrtype) sock.send(b"\x05\x07\x00\x01") # Command not supported return port = struct.unpack('>H', self.rfile.read(2))[0] # xlog.debug("socks5 %r connect to %s:%d", self.client_address, addr, port) reply = b"\x05\x00\x00" + addrtype_pack + addr_pack + struct.pack(">H", port) sock.send(reply) if addrtype in [1, 4]: handle_ip_proxy(sock, addr, port, self.client_address) else: handle_domain_proxy(sock, addr, port, self.client_address)
def http_handler(self): req_data = self.conn.recv(65537, socket.MSG_PEEK) if "\r\n" not in req_data: xlog.warn("http req:%s", req_data) return rp = req_data.split("\r\n") req_line = rp[0] words = req_line.split() if len(words) == 3: method, url, http_version = words elif len(words) == 2: method, url = words http_version = "HTTP/1.1" else: xlog.warn("http req line fail:%s", req_line) return if url.lower().startswith("http://"): o = urlparse.urlparse(url) host, port = netloc_to_host_port(o.netloc) p = url[7:].find("/") if p >= 0: path = url[7+p:] else: path = "/" else: # not proxy request, should be PAC xlog.debug("PAC %s %s from:%s", method, url, self.client_address) handler = pac_server.PacHandler(self.conn, self.client_address, None, xlog) return handler.handle() # xlog.debug("http %r connect to %s:%d", self.client_address, host, port) l = self.conn.recv(len(req_line)) if len(l) != len(req_line): xlog.error("req:%s l:%d", req_line, len(l)) return new_req_line = "%s %s %s" % (method, path, http_version) handle_domain_proxy(self.conn, host, port, self.client_address, new_req_line)
def http_handler(self): req_data = self.conn.recv(65537, socket.MSG_PEEK) rp = req_data.split("\r\n") req_line = rp[0] words = req_line.split() if len(words) == 3: method, url, http_version = words elif len(words) == 2: method, url = words http_version = "HTTP/1.1" else: xlog.warn("http req line fail:%s", req_line) return if url.lower().startswith("http://"): o = urlparse.urlparse(url) host, port = netloc_to_host_port(o.netloc) url_prex_len = url[7:].find("/") if url_prex_len >= 0: url_prex_len += 7 path = url[url_prex_len:] else: url_prex_len = len(url) path = "/" else: # not proxy request, should be PAC xlog.debug("PAC %s %s from:%s", method, url, self.client_address) handler = pac_server.PacHandler(self.conn, self.client_address, None, xlog) return handler.handle() #req_d = self.conn.recv(len(req_line)) #req_d = req_d.replace(url, path) sock = SocketWrap(self.conn, self.client_address[0], self.client_address[1]) sock.replace_pattern = [url[:url_prex_len], ""] xlog.debug("http %r connect to %s:%d %s %s", self.client_address, host, port, method, path) handle_domain_proxy(sock, host, port, self.client_address)