Skip to content

Commit 7c96730

Browse files
authored
Merge pull request #143 from milahu/print-effective-port-when-port-is-automatic
print effective port when port is automatic (zero)
2 parents 4965c8d + 697635c commit 7c96730

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

pproxy/server.py

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

877+
def print_server_started(option, server, print_fn):
878+
for s in server.sockets:
879+
# https://github.com/MagicStack/uvloop/blob/master/uvloop/pseudosock.pyx
880+
laddr = s.getsockname() # tuple size varies with protocol family
881+
h = laddr[0]
882+
p = laddr[1]
883+
f = str(s.family)
884+
ipversion = "ipv4" if f == "AddressFamily.AF_INET" else ("ipv6" if f == "AddressFamily.AF_INET6" else "ipv?") # TODO better
885+
bind = ipversion+' '+h+':'+str(p)
886+
print_fn(option, bind)
887+
877888
def main(args = None):
878889
parser = argparse.ArgumentParser(description=__description__+'\nSupported protocols: http,socks4,socks5,shadowsocks,shadowsocksr,redirect,pf,tunnel', epilog=f'Online help: <{__url__}>')
879890
parser.add_argument('-l', dest='listen', default=[], action='append', type=proxies_by_uri, help='tcp server uri (default: http+socks4+socks5://:8080/)')
@@ -936,27 +947,36 @@ def main(args = None):
936947
from . import verbose
937948
verbose.setup(loop, args)
938949
servers = []
950+
def print_fn(option, bind=None):
951+
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 '')
939952
for option in args.listen:
940-
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 '')
941953
try:
942954
server = loop.run_until_complete(option.start_server(vars(args)))
955+
print_server_started(option, server, print_fn)
943956
servers.append(server)
944957
except Exception as ex:
958+
print_fn(option)
945959
print('Start server failed.\n\t==>', ex)
960+
def print_fn(option, bind=None):
961+
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 '')
946962
for option in args.ulisten:
947-
print('Serving on UDP', option.bind, 'by', ",".join(i.name for i in option.protos), f'({option.cipher.name})' if option.cipher else '')
948963
try:
949964
server, protocol = loop.run_until_complete(option.udp_start_server(vars(args)))
965+
print_server_started(option, server, print_fn)
950966
servers.append(server)
951967
except Exception as ex:
968+
print_fn(option)
952969
print('Start server failed.\n\t==>', ex)
970+
def print_fn(option, bind=None):
971+
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 '')
953972
for option in args.rserver:
954973
if isinstance(option, ProxyBackward):
955-
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 '')
956974
try:
957975
server = loop.run_until_complete(option.start_backward_client(vars(args)))
976+
print_server_started(option, server, print_fn)
958977
servers.append(server)
959978
except Exception as ex:
979+
print_fn(option)
960980
print('Start server failed.\n\t==>', ex)
961981
if servers:
962982
if args.sys:

0 commit comments

Comments
 (0)