Skip to content

Commit 7c407d0

Browse files
committed
add support https proxy
1 parent f9ce48a commit 7c407d0

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

x_tunnel/local/proxy_handler.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ def handle(self):
2020
try:
2121
# xlog.debug('Connected from %r', self.client_address)
2222

23-
socks_version = ord(self.read_bytes(1))
24-
if socks_version == 4:
23+
socks_version = self.read_bytes(1)
24+
if socks_version == b'\x04':
2525
self.socks4_handler()
26-
elif socks_version == 5:
26+
elif socks_version == b'\x05':
2727
self.socks5_handler()
28+
elif socks_version == b'C':
29+
self.https_handler()
2830
else:
2931
xlog.warn("socks version:%d not supported", socks_version)
3032
return
@@ -40,12 +42,14 @@ def read_line(self):
4042
try:
4143
while True:
4244
n1 = self.read_buffer.find(b"\x00", self.buffer_start)
45+
if n1 == -1:
46+
n1 = self.read_buffer.find(b"\r", self.buffer_start)
4347
if n1 > -1:
4448
line = self.read_buffer[self.buffer_start:n1]
4549
self.buffer_start = n1 + 1
4650
return line
4751
time.sleep(0.001)
48-
data = sock.recv(256)
52+
data = sock.recv(65535)
4953
self.read_buffer += data
5054
finally:
5155
sock.setblocking(1)
@@ -170,3 +174,35 @@ def socks5_handler(self):
170174
g.session.conn_list[conn_id].transfer_received_data(self.read_buffer[self.buffer_start:])
171175

172176
g.session.conn_list[conn_id].start(block=True)
177+
178+
def https_handler(self):
179+
line = self.read_line()
180+
line = line.decode('iso-8859-1')
181+
words = line.split()
182+
if len(words) == 3:
183+
command, path, version = words
184+
elif len(words) == 2:
185+
command, path = words
186+
version = "HTTP/1.1"
187+
else:
188+
xlog.warn("https req line fail:%s", line)
189+
return
190+
191+
if command != "ONNECT":
192+
xlog.warn("https req line fail:%s", line)
193+
return
194+
195+
host, _, port = path.rpartition(':')
196+
port = int(port)
197+
198+
sock = self.connection
199+
conn_id = g.session.create_conn(sock, host, port)
200+
if not conn_id:
201+
xlog.warn("create conn fail")
202+
sock.send(b'HTTP/1.1 500 Fail\r\n\r\n')
203+
return
204+
205+
xlog.info("https %r connect to %s:%d conn_id:%d", self.client_address, host, port, conn_id)
206+
sock.send(b'HTTP/1.1 200 OK\r\n\r\n')
207+
208+
g.session.conn_list[conn_id].start(block=True)

x_tunnel/start.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
current_path = os.path.dirname(os.path.abspath(__file__))
1313
root_path = os.path.abspath(os.path.join(current_path, os.path.pardir))
1414
sys.path.append(root_path)
15-
#sys.path.insert(0, current_path)
1615
import x_tunnel.local.client as client
1716

1817

0 commit comments

Comments
 (0)