某个网站提供免费的SS服务器,不过隔几分钟就会更换密码。本意就是让用户体验下服务,之后来包月。本来就想着探索下怎么用python来写个爬网站,正好试试怎么自动抓到密码,避免每次都手动操作。不是要钻人家空子,只是好玩而已,我还是很乐意付费的。
做法很简单,该网站页面既显示密码,又提供gui-config下载,拿到之后替换本地的config,自动重启下SS即可。
代码:
#update ss with free bw node
import urllib.request, urllib.parse
import os.path
import json
import sched
import time
schedule = sched.scheduler ( time.time, time.sleep )
def getLoginCookies():
cookie = "本地"
return cookie
def getPage(url, headers):
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
html_bytes = response.read()
html_string = html_bytes.decode('utf-8')
return html_string,html_bytes
def getHttpHeaders(cookie):
headers = {}
headers['Cookie'] = cookie
headers['User-Agent']='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
return headers
def getJsonHeaders(cookie):
headers = getHttpHeaders(cookie)
headers['Content-Type']='application/json'
return headers
def getFreeBwNodePage(cookie):
headers = getJsonHeaders(cookie)
url = 'https://www.我不写出来.com/ucenter/?act=free_nodes_export'
content = getPage(url, headers)
return content
def modify_free_config(config,dest):
x0 = json.loads(config)
destfile = os.path.join(dest, 'gui-config.json')
f = open(destfile, 'rt')
x1 = json.loads(f.read())
f.close()
x1['configs'][0]['password'] = x0['configs'][0]['password']
print('NewPassword:{0}'.format(x0['configs'][0]['password']))
f = open(destfile, 'wt')
f.write(json.dumps(x1))
f.close()
def stop_program(procname):
os.system('TASKKILL /F /IM {0}*'.format(procname))
def start_program(progpath):
os.startfile(progpath)
def refreshSS():
ssroot = r'sspath'
ssname = 'ssname'
cookie = getLoginCookies()
print('get cookie:{0}'.format(cookie))
content = getFreeBwNodePage(cookie)
print('get bw node:{0}'.format(content[0]))
stop_program('Shadow')
modify_free_config(content[0], ssroot)
start_program(os.path.join(ssroot,ssname))
print('SS config refreshed')
def scheduledRefreshSS():
M1 = 60
M3 = M1 * 3
schedule.enter(M3,0,refreshSS)
schedule.run()
if __name__ == '__main__':
refreshSS()
scheduledRefreshSS()