Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/qwj/python-proxy
Browse files Browse the repository at this point in the history
python 3.11 compatibility
  • Loading branch information
qwj committed Jan 16, 2024
2 parents 2e18d25 + e03384f commit 09d4752
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
17 changes: 13 additions & 4 deletions pproxy/proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ async def connected(writer):
writer.write(f'{method} {newpath} {ver}\r\n{lines}\r\n\r\n'.encode())
return True
return user, host_name, port, connected
async def connect(self, reader_remote, writer_remote, rauth, host_name, port, myhost, **kw):
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')
async def connect(self, reader_remote, writer_remote, rauth, host_name, port, **kw):
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')
await reader_remote.read_until(b'\r\n\r\n')
async def http_channel(self, reader, writer, stat_bytes, stat_conn):
try:
Expand Down Expand Up @@ -616,12 +616,21 @@ def abort(self):
self.close()
ssl.connection_made(Transport())
async def channel():
read_size=65536
buffer=None
if hasattr(ssl,'get_buffer'):
buffer=ssl.get_buffer(read_size)
try:
while not reader.at_eof() and not ssl._app_transport._closed:
data = await reader.read(65536)
data = await reader.read(read_size)
if not data:
break
ssl.data_received(data)
if buffer!=None:
data_len=len(data)
buffer[:data_len]=data
ssl.buffer_updated(data_len)
else:
ssl.data_received(data)
except Exception:
pass
finally:
Expand Down
26 changes: 23 additions & 3 deletions pproxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,17 @@ async def test_url(url, rserver):
print(body.decode('utf8', 'ignore'))
print(f'============ success ============')

def print_server_started(option, server, print_fn):
for s in server.sockets:
# https://github.com/MagicStack/uvloop/blob/master/uvloop/pseudosock.pyx
laddr = s.getsockname() # tuple size varies with protocol family
h = laddr[0]
p = laddr[1]
f = str(s.family)
ipversion = "ipv4" if f == "AddressFamily.AF_INET" else ("ipv6" if f == "AddressFamily.AF_INET6" else "ipv?") # TODO better
bind = ipversion+' '+h+':'+str(p)
print_fn(option, bind)

def main(args = None):
parser = argparse.ArgumentParser(description=__description__+'\nSupported protocols: http,socks4,socks5,shadowsocks,shadowsocksr,redirect,pf,tunnel', epilog=f'Online help: <{__url__}>')
parser.add_argument('-l', dest='listen', default=[], action='append', type=proxies_by_uri, help='tcp server uri (default: http+socks4+socks5://:8080/)')
Expand Down Expand Up @@ -942,27 +953,36 @@ def main(args = None):
from . import verbose
verbose.setup(loop, args)
servers = []
def print_fn(option, bind=None):
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 '')
for option in args.listen:
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 '')
try:
server = loop.run_until_complete(option.start_server(vars(args)))
print_server_started(option, server, print_fn)
servers.append(server)
except Exception as ex:
print_fn(option)
print('Start server failed.\n\t==>', ex)
def print_fn(option, bind=None):
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 '')
for option in args.ulisten:
print('Serving on UDP', option.bind, 'by', ",".join(i.name for i in option.protos), f'({option.cipher.name})' if option.cipher else '')
try:
server, protocol = loop.run_until_complete(option.udp_start_server(vars(args)))
print_server_started(option, server, print_fn)
servers.append(server)
except Exception as ex:
print_fn(option)
print('Start server failed.\n\t==>', ex)
def print_fn(option, bind=None):
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 '')
for option in args.rserver:
if isinstance(option, ProxyBackward):
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 '')
try:
server = loop.run_until_complete(option.start_backward_client(vars(args)))
print_server_started(option, server, print_fn)
servers.append(server)
except Exception as ex:
print_fn(option)
print('Start server failed.\n\t==>', ex)
if servers:
if args.sys:
Expand Down

0 comments on commit 09d4752

Please sign in to comment.