mirror of https://github.com/dnomd343/ProxyC
dnomd343
3 years ago
6 changed files with 145 additions and 143 deletions
@ -1,2 +1,3 @@ |
|||||
**/__pycache__/ |
**/__pycache__/ |
||||
/.idea/ |
/.idea/ |
||||
|
*.log |
||||
|
@ -1,84 +1,93 @@ |
|||||
#!/usr/bin/env python3 |
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||||
|
|
||||
import json |
import json |
||||
from Basis.Methods import ssMethods |
from Basis.Methods import ssMethods, ssAllMethods |
||||
|
|
||||
|
|
||||
def loadConfig(proxyInfo: dict, socksInfo: dict) -> dict: # load basic config option |
def loadConfig(proxyInfo: dict, socksInfo: dict) -> dict: # load basic config option |
||||
config = { |
config = { |
||||
'server': proxyInfo['server'], |
'server': proxyInfo['server'], |
||||
'server_port': proxyInfo['port'], # type -> int |
'server_port': proxyInfo['port'], # type -> int |
||||
'local_address': socksInfo['addr'], |
'local_address': socksInfo['addr'], |
||||
'local_port': socksInfo['port'], # type -> int |
'local_port': socksInfo['port'], # type -> int |
||||
'method': proxyInfo['method'], |
'method': proxyInfo['method'], |
||||
'password': proxyInfo['passwd'], |
'password': proxyInfo['passwd'], |
||||
} |
} |
||||
if proxyInfo['plugin'] is not None: # with plugin |
if proxyInfo['plugin'] is not None: # with plugin |
||||
config['plugin'] = proxyInfo['plugin']['type'] |
config['plugin'] = proxyInfo['plugin']['type'] |
||||
config['plugin_opts'] = proxyInfo['plugin']['param'] |
config['plugin_opts'] = proxyInfo['plugin']['param'] |
||||
return config |
return config |
||||
|
|
||||
|
|
||||
def pluginUdp(plugin: str, pluginParam: str) -> bool: # whether the plugin uses UDP |
def pluginUdp(plugin: str, pluginParam: str) -> bool: # whether the plugin uses UDP |
||||
if plugin in ['obfs-local', 'simple-tls', 'ck-client', 'gq-client', 'mtt-client', 'rabbit-plugin']: |
if plugin in ['obfs-local', 'simple-tls', 'ck-client', 'gq-client', 'mtt-client', 'rabbit-plugin']: |
||||
return False # UDP is not used |
return False # UDP is not used |
||||
if plugin in ['v2ray-plugin', 'xray-plugin', 'gost-plugin']: |
if plugin in ['v2ray-plugin', 'xray-plugin', 'gost-plugin']: |
||||
if 'mode=quic' not in pluginParam.split(';'): # non-quic mode does not use UDP |
if 'mode=quic' not in pluginParam.split(';'): # non-quic mode does not use UDP |
||||
return False |
return False |
||||
return True # UDP is assumed by default |
return True # UDP is assumed by default |
||||
|
|
||||
|
|
||||
def ssRust(proxyInfo: dict, socksInfo: dict, isUdp: bool, isLegacy: bool = False) -> tuple[dict, list]: |
def ssRust(proxyInfo: dict, socksInfo: dict, isUdp: bool) -> tuple[dict, list]: |
||||
config = loadConfig(proxyInfo, socksInfo) |
config = loadConfig(proxyInfo, socksInfo) |
||||
if isUdp: # Proxy UDP traffic |
if isUdp: # proxy UDP traffic |
||||
config['mode'] = 'tcp_and_udp' |
config['mode'] = 'tcp_and_udp' |
||||
return config, ['ss-rust-local', '-v'] |
return config, ['ss-rust-local', '-v'] |
||||
|
|
||||
|
|
||||
def ssLibev(proxyInfo: dict, socksInfo: dict, isUdp: bool, isLegacy: bool = False) -> tuple[dict, list]: |
def ssLibev(proxyInfo: dict, socksInfo: dict, isUdp: bool) -> tuple[dict, list]: |
||||
config = loadConfig(proxyInfo, socksInfo) |
config = loadConfig(proxyInfo, socksInfo) |
||||
if isUdp: # Proxy UDP traffic |
if isUdp: # proxy UDP traffic |
||||
config['mode'] = 'tcp_and_udp' |
config['mode'] = 'tcp_and_udp' |
||||
return config, ['ss-libev-legacy-local' if isLegacy else 'ss-libev-local', '-v'] |
return config, ['ss-libev-local', '-v'] |
||||
|
|
||||
|
|
||||
def ssPython(proxyInfo: dict, socksInfo: dict, isUdp: bool, isLegacy: bool = False) -> tuple[dict, list]: |
def ssPython(proxyInfo: dict, socksInfo: dict, isUdp: bool) -> tuple[dict, list]: |
||||
config = loadConfig(proxyInfo, socksInfo) |
config = loadConfig(proxyInfo, socksInfo) |
||||
mbedtlsMethods = [ |
mbedtlsMethods = [ |
||||
'aes-128-cfb128', |
'aes-128-cfb128', |
||||
'aes-192-cfb128', |
'aes-192-cfb128', |
||||
'aes-256-cfb128', |
'aes-256-cfb128', |
||||
'camellia-128-cfb128', |
'camellia-128-cfb128', |
||||
'camellia-192-cfb128', |
'camellia-192-cfb128', |
||||
'camellia-256-cfb128', |
'camellia-256-cfb128', |
||||
] |
] |
||||
if not isLegacy: # only for latest version |
if config['method'] in mbedtlsMethods: # mbedtls methods should use prefix `mbedtls:` |
||||
if config['method'] in mbedtlsMethods: # mbedtls methods should use prefix `mbedtls:` |
config['method'] = 'mbedtls:' + config['method'] |
||||
config['method'] = 'mbedtls:' + config['method'] |
if config['method'] in ['idea-cfb', 'seed-cfb']: # only older versions of openssl are supported |
||||
if config['method'] in ['idea-cfb', 'seed-cfb']: # Only older versions of openssl are supported |
config['extra_opts'] = '--libopenssl=libcrypto.so.1.0.0' |
||||
config['extra_opts'] = '--libopenssl=libcrypto.so.1.0.0' |
if not isUdp: |
||||
if not isUdp: |
config['no_udp'] = True # UDP traffic is not proxied |
||||
config['no_udp'] = True # UDP traffic is not proxied |
config['shadowsocks'] = 'ss-python-local' |
||||
config['shadowsocks'] = 'ss-python-legacy-local' if isLegacy else 'ss-python-local' |
return config, ['ss-bootstrap-local', '--debug', '-vv'] |
||||
return config, ['ss-bootstrap-local', '--debug', '-vv'] |
|
||||
|
|
||||
|
def ssPythonLegacy(proxyInfo: dict, socksInfo: dict, isUdp: bool) -> tuple[dict, list]: |
||||
def load(proxyInfo: dict, socksInfo: dict, configFile: str) -> tuple[list, str, None]: |
config = loadConfig(proxyInfo, socksInfo) |
||||
if proxyInfo['plugin'] is None: # UDP is enabled when server without plugin |
if not isUdp: |
||||
isUdp = True |
config['no_udp'] = True # UDP traffic is not proxied |
||||
else: |
config['shadowsocks'] = 'ss-python-legacy-local' |
||||
isUdp = not pluginUdp( # check the UDP conflict status of plugins |
return config, ['ss-bootstrap-local', '--debug', '-vv'] |
||||
proxyInfo['plugin']['type'], proxyInfo['plugin']['param'] |
|
||||
) |
|
||||
if proxyInfo['method'] not in ssMethods['all']: # unknown shadowsocks method |
def load(proxyInfo: dict, socksInfo: dict, configFile: str) -> tuple[list, str, dict]: |
||||
raise RuntimeError('Unknown shadowsocks method') |
if proxyInfo['plugin'] is None: # UDP is enabled when server without plugin |
||||
for client in ssMethods: # traverse all shadowsocks client |
isUdp = True |
||||
if proxyInfo['method'] not in ssMethods[client] or client != 'all': |
else: |
||||
continue |
isUdp = not pluginUdp( # check the UDP conflict status of plugins |
||||
ssLoadConfig = None |
proxyInfo['plugin']['type'], proxyInfo['plugin']['param'] |
||||
if 'rust' in client: ssLoadConfig = ssRust |
) |
||||
if 'libev' in client: ssLoadConfig = ssLibev |
if proxyInfo['method'] not in ssAllMethods: # unknown shadowsocks method |
||||
if 'python' in client: ssLoadConfig = ssPython |
raise RuntimeError('Unknown shadowsocks method') |
||||
ssConfig, ssClient = ssLoadConfig(proxyInfo, socksInfo, isUdp, 'legacy' in client) # generate config file |
for client in ssMethods: # traverse all shadowsocks client |
||||
return ssClient + ['-c', configFile], json.dumps(ssConfig), None # tuple[command, fileContent, envVar] |
if proxyInfo['method'] not in ssMethods[client]: |
||||
|
continue |
||||
|
ssLoadConfig = { |
||||
|
'ss-rust': ssRust, |
||||
|
'ss-libev': ssLibev, |
||||
|
'ss-python': ssPython, |
||||
|
'ss-python-legacy': ssPythonLegacy |
||||
|
}[client] |
||||
|
ssConfig, ssClient = ssLoadConfig(proxyInfo, socksInfo, isUdp) # generate config file |
||||
|
return ssClient + ['-c', configFile], json.dumps(ssConfig), {} # tuple[command, fileContent, envVar] |
||||
|
Loading…
Reference in new issue