Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize print_server_started() to avoid throw an exception when using the QUIC protocol. #198

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
25 changes: 18 additions & 7 deletions pproxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,14 +883,25 @@ async def test_url(url, rserver):
print(f'============ success ============')

def print_server_started(option, server, print_fn):
for s in server.sockets:
sockets=[]
if hasattr(server, 'sockets'): # asyncio.Server or uvloop.loop.Server
sockets.extend(server.sockets)
elif hasattr(server, 'get_extra_info'): # asyncio.Transport or uvloop.loop.UDPTransport
sockets.append(server.get_extra_info('socket'))
elif hasattr(server, '_transport'): # aioquic.asyncio.server.QuicServer
sockets.append(server._transport.get_extra_info('socket'))
else:
print_fn(option, None)
return
for s in 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)
host, port = s.getsockname()[:2] # tuple size varies with protocol family
ipversion = "ipv?"
if s.family == socket.AddressFamily.AF_INET:
ipversion = "ipv4"
elif s.family == socket.AddressFamily.AF_INET6:
ipversion = "ipv6"
bind = ipversion + ' ' + host + ':' + str(port)
print_fn(option, bind)

def main(args = None):
Expand Down