-
Notifications
You must be signed in to change notification settings - Fork 1
/
mengy_getproxy.py
148 lines (120 loc) · 4.2 KB
/
mengy_getproxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
# -*-coding: utf-8 -*-
import urllib.parse
import base64
import logzero
import subprocess
import json
import requests
import time
logger = logzero.setup_logger('get proxy', logfile='/home/Downloads/proxy.log')
socks5Plan = 'socks5://{user}{pass}{server}:{port}'
ssrProxy = 'socks5://127.0.0.1:1080'
ssrProxies = {
'http': ssrProxy,
'https': ssrProxy,
}
ssrJsonPath = '/etc/sslocal.json'
ssrLogPath = '/home/Downloads/ss.log'
ssrCMD = 'python ~/shadowsocksr/shadowsocks/local.py -c {} -d restart'.format(ssrJsonPath)
# #!/bin/bash
# pgrep ss-local | xargs kill -9
# nohup ss-local -c /etc/sslocal.json -v &>>/home/Downloads/ss.log &
ssrLibCMD = '/root/sslocal.sh'
testIP_URL = 'http://httpbin.org/ip'
def startSSR_Server():
p = subprocess.Popen(ssrCMD, stdout=subprocess.PIPE, shell=True)
time.sleep(3)
output, err = p.communicate()
if not err:
logger.info(output.decode('utf-8'))
return True
else:
logger.waring('ssr server start failed')
return False
def startSSR_Server_lib():
p = subprocess.run(ssrLibCMD, shell=True)
time.sleep(3)
return True
def testSProxy(proxies):
try:
req = requests.get(testIP_URL, proxies=proxies, timeout=10)
if req.status_code == 200:
logger.info(req.json())
return True
except requests.exceptions.ReadTimeout:
logger.exception('proxy test failed | timeout | {}'.format(proxies))
return False
except:
logger.exception('proxy test failed | other | {}'.format(proxies))
return False
def getSocksProxyFromTG(tgSocks5Link):
proxies = {}
tgParseResult = urllib.parse.urlparse(tgSocks5Link)
tgQueryList = urllib.parse.parse_qsl(tgParseResult.query)
tgSocks5Info = {i[0]: i[1] for i in tgQueryList}
if tgSocks5Info.get('user', '') == '':
tgSocks5Info['user'] = ''
tgSocks5Info['pass'] = ''
else:
tgSocks5Info['user'] += ':'
tgSocks5Info['pass'] += '@'
socksProxy = socks5Plan.format(**tgSocks5Info)
proxies['http'] = socksProxy
proxies['https'] = socksProxy
if testSProxy(proxies):
logger.debug('tg proxy success | {}'.format(tgSocks5Link))
return proxies
else:
logger.debug('tg proxy failed | {}'.format(tgSocks5Link))
return None
def base64Decode(s):
try:
b = base64.urlsafe_b64decode(s + '=' * 3).decode('utf-8')
except UnicodeDecodeError:
logger.exception('decode failed | {}'.format(s))
return ''
return b
def genSSRJsonFromLink(ssrLink):
ssrParams = {
"local_address": "127.0.0.1",
"local_port": 1080,
"timeout": 300,
"workers": 1,
}
try:
ssrNetloc = urllib.parse.urlparse(ssrLink).netloc
ssrInfo_0 = base64.urlsafe_b64decode(
ssrNetloc + '=' * 3).decode('utf-8').split(':')
ssrParams['server'] = ssrInfo_0[0]
ssrParams['server_port'] = ssrInfo_0[1]
ssrParams['protocol'] = ssrInfo_0[2]
ssrParams['method'] = ssrInfo_0[3]
ssrParams['obfs'] = ssrInfo_0[4]
ssrParseResult = urllib.parse.urlparse(ssrInfo_0[5])
ssrQueryList = urllib.parse.parse_qs(ssrParseResult.query)
if ssrQueryList:
ssrParams['password'] = base64Decode(ssrParseResult.path[:-1])
else:
ssrParams['password'] = base64Decode(ssrParseResult.path)
ssrParams['obfs_param'] = base64Decode(
ssrQueryList.get('obfsparam', [''])[0])
ssrParams['protocol_param'] = base64Decode(
ssrQueryList.get('protoparam', [''])[0])
with open(ssrJsonPath, 'w') as f:
json.dump(ssrParams, f, indent=4, sort_keys=True)
except:
logger.exception('generate ssr config json form ssr link failed | {}'.format(ssrLink))
return False
else:
return True
def getSocksProxyFromSSRLink(ssrLink):
if genSSRJsonFromLink(ssrLink):
# if startSSR_Server():
if startSSR_Server_lib():
if testSProxy(ssrProxies):
logger.debug('ssr proxy success | {}'.format(ssrLink))
return ssrProxies
else:
logger.debug('ssr proxy failed | {}'.format(ssrLink))
return None