Skip to content

Commit 09d4752

Browse files
committed
Merge branch 'master' of https://github.com/qwj/python-proxy
python 3.11 compatibility
2 parents 2e18d25 + e03384f commit 09d4752

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

pproxy/proto.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ async def connected(writer):
328328
writer.write(f'{method} {newpath} {ver}\r\n{lines}\r\n\r\n'.encode())
329329
return True
330330
return user, host_name, port, connected
331-
async def connect(self, reader_remote, writer_remote, rauth, host_name, port, myhost, **kw):
332-
writer_remote.write(f'CONNECT {host_name}:{port} HTTP/1.1\r\nHost: {myhost}'.encode() + (b'\r\nProxy-Authorization: Basic '+base64.b64encode(rauth) if rauth else b'') + b'\r\n\r\n')
331+
async def connect(self, reader_remote, writer_remote, rauth, host_name, port, **kw):
332+
writer_remote.write(f'CONNECT {host_name}:{port} HTTP/1.1\r\nHost: {host_name}:{port}'.encode() + (b'\r\nProxy-Authorization: Basic '+base64.b64encode(rauth) if rauth else b'') + b'\r\n\r\n')
333333
await reader_remote.read_until(b'\r\n\r\n')
334334
async def http_channel(self, reader, writer, stat_bytes, stat_conn):
335335
try:
@@ -616,12 +616,21 @@ def abort(self):
616616
self.close()
617617
ssl.connection_made(Transport())
618618
async def channel():
619+
read_size=65536
620+
buffer=None
621+
if hasattr(ssl,'get_buffer'):
622+
buffer=ssl.get_buffer(read_size)
619623
try:
620624
while not reader.at_eof() and not ssl._app_transport._closed:
621-
data = await reader.read(65536)
625+
data = await reader.read(read_size)
622626
if not data:
623627
break
624-
ssl.data_received(data)
628+
if buffer!=None:
629+
data_len=len(data)
630+
buffer[:data_len]=data
631+
ssl.buffer_updated(data_len)
632+
else:
633+
ssl.data_received(data)
625634
except Exception:
626635
pass
627636
finally:

pproxy/server.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,17 @@ async def test_url(url, rserver):
880880
print(body.decode('utf8', 'ignore'))
881881
print(f'============ success ============')
882882

883+
def print_server_started(option, server, print_fn):
884+
for s in server.sockets:
885+
# https://github.com/MagicStack/uvloop/blob/master/uvloop/pseudosock.pyx
886+
laddr = s.getsockname() # tuple size varies with protocol family
887+
h = laddr[0]
888+
p = laddr[1]
889+
f = str(s.family)
890+
ipversion = "ipv4" if f == "AddressFamily.AF_INET" else ("ipv6" if f == "AddressFamily.AF_INET6" else "ipv?") # TODO better
891+
bind = ipversion+' '+h+':'+str(p)
892+
print_fn(option, bind)
893+
883894
def main(args = None):
884895
parser = argparse.ArgumentParser(description=__description__+'\nSupported protocols: http,socks4,socks5,shadowsocks,shadowsocksr,redirect,pf,tunnel', epilog=f'Online help: <{__url__}>')
885896
parser.add_argument('-l', dest='listen', default=[], action='append', type=proxies_by_uri, help='tcp server uri (default: http+socks4+socks5://:8080/)')
@@ -942,27 +953,36 @@ def main(args = None):
942953
from . import verbose
943954
verbose.setup(loop, args)
944955
servers = []
956+
def print_fn(option, bind=None):
957+
print('Serving on', (bind or option.bind), 'by', ",".join(i.name for i in option.protos) + ('(SSL)' if option.sslclient else ''), '({}{})'.format(option.cipher.name, ' '+','.join(i.name() for i in option.cipher.plugins) if option.cipher and option.cipher.plugins else '') if option.cipher else '')
945958
for option in args.listen:
946-
print('Serving on', option.bind, 'by', ",".join(i.name for i in option.protos) + ('(SSL)' if option.sslclient else ''), '({}{})'.format(option.cipher.name, ' '+','.join(i.name() for i in option.cipher.plugins) if option.cipher and option.cipher.plugins else '') if option.cipher else '')
947959
try:
948960
server = loop.run_until_complete(option.start_server(vars(args)))
961+
print_server_started(option, server, print_fn)
949962
servers.append(server)
950963
except Exception as ex:
964+
print_fn(option)
951965
print('Start server failed.\n\t==>', ex)
966+
def print_fn(option, bind=None):
967+
print('Serving on UDP', (bind or option.bind), 'by', ",".join(i.name for i in option.protos), f'({option.cipher.name})' if option.cipher else '')
952968
for option in args.ulisten:
953-
print('Serving on UDP', option.bind, 'by', ",".join(i.name for i in option.protos), f'({option.cipher.name})' if option.cipher else '')
954969
try:
955970
server, protocol = loop.run_until_complete(option.udp_start_server(vars(args)))
971+
print_server_started(option, server, print_fn)
956972
servers.append(server)
957973
except Exception as ex:
974+
print_fn(option)
958975
print('Start server failed.\n\t==>', ex)
976+
def print_fn(option, bind=None):
977+
print('Serving on', (bind or option.bind), 'backward by', ",".join(i.name for i in option.protos) + ('(SSL)' if option.sslclient else ''), '({}{})'.format(option.cipher.name, ' '+','.join(i.name() for i in option.cipher.plugins) if option.cipher and option.cipher.plugins else '') if option.cipher else '')
959978
for option in args.rserver:
960979
if isinstance(option, ProxyBackward):
961-
print('Serving on', option.bind, 'backward by', ",".join(i.name for i in option.protos) + ('(SSL)' if option.sslclient else ''), '({}{})'.format(option.cipher.name, ' '+','.join(i.name() for i in option.cipher.plugins) if option.cipher and option.cipher.plugins else '') if option.cipher else '')
962980
try:
963981
server = loop.run_until_complete(option.start_backward_client(vars(args)))
982+
print_server_started(option, server, print_fn)
964983
servers.append(server)
965984
except Exception as ex:
985+
print_fn(option)
966986
print('Start server failed.\n\t==>', ex)
967987
if servers:
968988
if args.sys:

0 commit comments

Comments
 (0)