Skip to content

Commit 2cd2dec

Browse files
committed
netloc parse
1 parent 290a40b commit 2cd2dec

2 files changed

Lines changed: 11 additions & 16 deletions

File tree

pproxy/proto.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
HTTP_LINE = re.compile('([^ ]+) +(.+?) +(HTTP/[^ ]+)$')
44
packstr = lambda s, n=1: len(s).to_bytes(n, 'big') + s
55

6-
def netloc_split(loc, default_port):
6+
def netloc_split(loc, default_host=None, default_port=None):
77
ipv6 = re.fullmatch('\[([0-9a-fA-F:]*)\](?::(\d+)?)?', loc)
88
if ipv6:
99
host_name, port = ipv6.groups()
10+
elif ':' in loc:
11+
host_name, port = loc.rsplit(':', 1)
1012
else:
11-
host_name, _, port = loc.partition(':')
12-
return host_name, int(port) if port else default_port
13+
host_name, port = loc, None
14+
return host_name or default_host, int(port) if port else default_port
1315

1416
async def socks_address_stream(reader, n):
1517
if n in (1, 17):
@@ -311,12 +313,11 @@ async def accept(self, reader, user, writer, users, authtable, httpget=None, **k
311313
raise Exception('Unauthorized HTTP')
312314
authtable.set_authed(user)
313315
if method == 'CONNECT':
314-
host_name, port = path.rsplit(':', 1)
315-
port = int(port)
316+
host_name, port = netloc_split(path)
316317
return user, host_name, port, f'{ver} 200 OK\r\nConnection: close\r\n\r\n'.encode()
317318
else:
318319
url = urllib.parse.urlparse(path)
319-
host_name, port = netloc_split(url.netloc or headers.get("Host"), 80)
320+
host_name, port = netloc_split(url.netloc or headers.get("Host"), default_port=80)
320321
newpath = url._replace(netloc='', scheme='').geturl()
321322
return user, host_name, port, b'', f'{method} {newpath} {ver}\r\n{lines}\r\n\r\n'.encode()
322323
async def connect(self, reader_remote, writer_remote, rauth, host_name, port, myhost, **kw):
@@ -421,11 +422,8 @@ class Tunnel(Transparent):
421422
def query_remote(self, sock):
422423
if not self.param:
423424
return 'tunnel', 0
424-
host, _, port = self.param.partition(':')
425425
dst = sock.getsockname()
426-
host = host or dst[0]
427-
port = int(port) if port else dst[1]
428-
return host, port
426+
return netloc_split(self.param, dst[0], dst[1])
429427
async def connect(self, reader_remote, writer_remote, rauth, host_name, port, **kw):
430428
pass
431429
def udp_connect(self, rauth, host_name, port, data, **kw):
@@ -502,10 +500,8 @@ async def accept(self, reader, user, writer, users, authtable, sock, **kw):
502500
self.patch_ws_stream(reader, writer, False)
503501
if not self.param:
504502
return 'tunnel', 0
505-
host, _, port = self.param.partition(':')
506503
dst = sock.getsockname()
507-
host = host or dst[0]
508-
port = int(port) if port else dst[1]
504+
host, port = netloc_split(self.param, dst[0], dst[1])
509505
return user, host, port
510506
async def connect(self, reader_remote, writer_remote, rauth, host_name, port, myhost, **kw):
511507
seckey = base64.b64encode(os.urandom(16)).decode()

pproxy/server.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ def compile(cls, uri, relay=None):
493493
cipher.plugins.append(plugin)
494494
match = cls.compile_rule(url.query) if url.query else None
495495
if loc:
496-
host_name, port = proto.netloc_split(loc, 22 if 'ssh' in rawprotos else 8080)
496+
host_name, port = proto.netloc_split(loc, default_port=22 if 'ssh' in rawprotos else 8080)
497497
else:
498498
host_name = port = None
499499
if url.fragment.startswith('#'):
@@ -514,8 +514,7 @@ def compile(cls, uri, relay=None):
514514
async def test_url(url, rserver):
515515
url = urllib.parse.urlparse(url)
516516
assert url.scheme in ('http', 'https'), f'Unknown scheme {url.scheme}'
517-
host_name, _, port = url.netloc.partition(':')
518-
port = int(port) if port else 80 if url.scheme == 'http' else 443
517+
host_name, port = proto.netloc_split(url.netloc, default_port = 80 if url.scheme=='http' else 443)
519518
initbuf = f'GET {url.path or "/"} HTTP/1.1\r\nHost: {host_name}\r\nUser-Agent: pproxy-{__version__}\r\nAccept: */*\r\nConnection: close\r\n\r\n'.encode()
520519
for roption in rserver:
521520
print(f'============ {roption.bind} ============')

0 commit comments

Comments
 (0)