-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathServerUpdate.py
More file actions
100 lines (86 loc) · 4.11 KB
/
ServerUpdate.py
File metadata and controls
100 lines (86 loc) · 4.11 KB
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
__author__ = "luowen"
"""
Description: Automatic update ishadowsocks's server address and password.
Author: awimj <bigpao.luo@gmail.com>
Time: 2016-02-29
"""
import re, json, os, time, logging, urllib, urllib.request as request
HOSTADDR = "http://www.ishadowsocks.com/"
ISHADOWSCOKS_PROCESS_NAME = "Shadowsocks.exe"
ISHADOWSCOKS_PROCESS_PATH = "D:/Services/shadowsocks/Shadowsocks.exe" # D:\Services\shadowsocks/ shadowsocks.exe文件所在的目录
SERVER_TYPE = "C" # "A" => USA , "B" => HongKong, "C" => Japan # 选择的代理服务器
def updateServer(proxyServerType = "C"):
"""
first: fetch server address and password from ishadowsocks website
second: fetch success then merger ishadowsocks client configure file
"""
serverInfo = fetchIshadowsocks(proxyServerType)
if serverInfo and serverInfo.get("server") and serverInfo.get("password") and serverInfo.get("port"):
# if address or password or port invalid don't merge
if mergeLocalConfig(serverInfo):
killProcess()
time.sleep(2)
startProcess()
else:
logMsg("'{0}' server address or password or port invalid.".format(proxyServerType))
exit(0)
def fetchIshadowsocks(serverType):
"fetch server address and password from ishadowsocks websiet"
serverInfo = {}
breakPointIndex = 0
try:
with request.urlopen(HOSTADDR) as urlHandle:
for line in urlHandle:
lineString = line.decode("utf-8");
reObjMatchUrl = re.search("<h4>{0}服务器地址:(.*)</h4>".format(serverType), lineString)
if reObjMatchUrl:
serverInfo["server"] = reObjMatchUrl.group(1)
reObjMatchPwd = re.search("<h4>{0}密码:(.*)</h4>".format(serverType), lineString)
if reObjMatchPwd:
serverInfo["password"] = reObjMatchPwd.group(1)
reObjMatchPort = re.search("<h4>端口:(\d+)</h4>".format(serverType), lineString)
if reObjMatchPort:
breakPointIndex += 1
if "{0}-{1}".format(serverType, breakPointIndex) == "A-1":
serverInfo["port"] = reObjMatchPort.group(1)
elif "{0}-{1}".format(serverType, breakPointIndex) == "B-2":
serverInfo["port"] = reObjMatchPort.group(1)
elif "{0}-{1}".format(serverType, breakPointIndex) == "C-3":
serverInfo["port"] = reObjMatchPort.group(1)
else:
continue
return serverInfo
except urllib.error.URLError as error:
logMsg(error)
def mergeLocalConfig(serverInfo):
" merge ishadowsocks website server information to local ishadowsocks client configure file"
# TODO get local configure file and merge it
guiConfigFileHandle = open("gui-config.json", encoding="utf-8")
guiConfigJsonObj = json.load(guiConfigFileHandle)
guiOldValue = guiConfigJsonObj['configs'][0]
# assign new server information
guiConfigJsonObj['configs'][0]["server"] = serverInfo['server']
guiConfigJsonObj['configs'][0]["password"] = serverInfo['password']
guiConfigJsonObj['configs'][0]["server_port"] = serverInfo['port']
guiNewConfigFileHandle = open("gui-config.json", encoding="utf-8", mode="w")
json.dump(guiConfigJsonObj, guiNewConfigFileHandle)
logMsg("replace old server value [{0}] to new value [{1}].".format(str(guiOldValue), str(serverInfo)))
return True
def logMsg(msg):
"log process information to application.log"
logging.basicConfig(filename="application.log", level=logging.INFO, format="[%(asctime)s]: %(message)s")
logging.info(msg)
def killProcess():
"update server information need restart, so first kill it."
try:
os.system("taskkill /F /IM {0} /T".format(ISHADOWSCOKS_PROCESS_NAME))
except OSError as error:
logMsg(error)
def startProcess():
"start shadowsocks process"
try:
os.system("start {0} &".format(ISHADOWSCOKS_PROCESS_PATH))
except OSError as error:
logMsg(error)
if __name__ == "__main__":
updateServer(SERVER_TYPE)