22from pproxy import proto
33
44__title__ = 'pproxy'
5- __version__ = "1.4.1 "
5+ __version__ = "1.4.2 "
66__description__ = "Proxy server that can tunnel among remote servers by regex rules."
77__author__ = "Qian Wenjie"
88__license__ = "MIT License"
@@ -25,9 +25,14 @@ def authed(self):
2525 def set_authed (self ):
2626 self ._auth [self .remote_ip ] = time .time ()
2727
28- async def proxy_handler (reader , writer , protos , rserver , block , cipher , verbose = DUMMY , modstat = lambda r ,h :lambda i :DUMMY , ** kwargs ):
28+ async def proxy_handler (reader , writer , unix , lbind , protos , rserver , block , cipher , verbose = DUMMY , modstat = lambda r ,h :lambda i :DUMMY , ** kwargs ):
2929 try :
30- remote_ip = (writer .get_extra_info ('peername' ) or ['local' ])[0 ]
30+ if not unix :
31+ remote_ip = writer .get_extra_info ('peername' )[0 ]
32+ server_ip = writer .get_extra_info ('sockname' )[0 ]
33+ else :
34+ remote_ip = server_ip = 'local'
35+ local_addr = None if server_ip in ('127.0.0.1' , '::1' , 'local' ) else (server_ip , 0 )
3136 if cipher :
3237 reader .plugin_decrypt = reader .plugin_decrypt2 = writer .plugin_encrypt = writer .plugin_encrypt2 = DUMMY
3338 for plugin in cipher .plugins :
@@ -46,10 +51,10 @@ async def proxy_handler(reader, writer, protos, rserver, block, cipher, verbose=
4651 viaproxy = bool (roption )
4752 if viaproxy :
4853 verbose (f'{ lproto .name } { host_name } :{ port } -> { roption .rproto .name } { roption .bind } ' )
49- wait_connect = roption .connect ()
54+ wait_connect = roption .connect () if roption . unix else roption . connect ( local_addr = local_addr if roption . lbind == 'in' else ( roption . lbind , 0 ) if roption . lbind else None )
5055 else :
5156 verbose (f'{ lproto .name } { host_name } :{ port } ' )
52- wait_connect = asyncio .open_connection (host = host_name , port = port )
57+ wait_connect = asyncio .open_connection (host = host_name , port = port , local_addr = local_addr if lbind == 'in' else ( lbind , 0 ) if lbind else None )
5358 try :
5459 reader_remote , writer_remote = await asyncio .wait_for (wait_connect , timeout = SOCKET_TIMEOUT )
5560 except asyncio .TimeoutError :
@@ -118,15 +123,17 @@ def uri_compile(uri):
118123 else :
119124 sslserver = None
120125 sslclient = None
126+ urlpath , _ , plugins = url .path .partition (',' )
127+ urlpath , _ , lbind = urlpath .partition ('@' )
128+ plugins = plugins .split (',' ) if plugins else None
121129 cipher , _ , loc = url .netloc .rpartition ('@' )
122130 if cipher :
123131 from pproxy .cipher import get_cipher
124132 err_str , cipher = get_cipher (cipher )
125133 if err_str :
126134 raise argparse .ArgumentTypeError (err_str )
127- if url . path :
135+ if plugins :
128136 from pproxy .plugin import get_plugin
129- plugins = url .path .lstrip ('/' ).split (',' )
130137 for name in plugins :
131138 if not name : continue
132139 err_str , plugin = get_plugin (name )
@@ -140,9 +147,9 @@ def uri_compile(uri):
140147 connect = functools .partial (asyncio .open_connection , host = host , port = port , ssl = sslclient )
141148 server = functools .partial (asyncio .start_server , host = host , port = port , ssl = sslserver )
142149 else :
143- connect = functools .partial (asyncio .open_unix_connection , path = url . path , ssl = sslclient , server_hostname = '' if sslclient else None )
144- server = functools .partial (asyncio .start_unix_server , path = url . path , ssl = sslserver )
145- return types .SimpleNamespace (protos = protos , rproto = protos [0 ], cipher = cipher , auth = url .fragment .encode (), match = match , server = server , connect = connect , bind = loc or url . path , sslclient = sslclient , sslserver = sslserver , alive = True )
150+ connect = functools .partial (asyncio .open_unix_connection , path = urlpath , ssl = sslclient , server_hostname = '' if sslclient else None )
151+ server = functools .partial (asyncio .start_unix_server , path = urlpath , ssl = sslserver )
152+ return types .SimpleNamespace (protos = protos , rproto = protos [0 ], cipher = cipher , auth = url .fragment .encode (), match = match , server = server , connect = connect , bind = loc or urlpath , unix = not loc , lbind = lbind , sslclient = sslclient , sslserver = sslserver , alive = True )
146153
147154def main ():
148155 parser = argparse .ArgumentParser (description = __description__ + '\n Supported protocols: http,socks,shadowsocks,redirect' , epilog = 'Online help: <https://github.com/qwj/python-proxy>' )
0 commit comments