-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathServerUpdate.php
More file actions
141 lines (122 loc) · 4.43 KB
/
ServerUpdate.php
File metadata and controls
141 lines (122 loc) · 4.43 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
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
<?php
/**
* FileName: ServerUpdate.php
* Description: fetch http://ishadowsocks.com last password
* Author: Bigpao
* Email: bigpao.luo@gmail.com
* HomePage:
* Version: 0.0.1
* LastChange: 2016-03-08 09:31:15
* History:
*/
$websiteUrl = 'http://www.ishadowsocks.com/'; // 网站地址, 一般不用修改
$typeServer = 'B'; // ['A', 'B', 'C'] 选用服务器地址 A表示美国, B表示香港, C表示日本
$appPath = "D:/Services/shadowsocks/Shadowsocks.exe"; // 安装目录
class ServerUpdate
{
protected $websiteUrl = 'http://www.ishadowsocks.com/';
protected $typeServer = 'A';
protected $appPath = "D:/Services/shadowsocks/Shadowsocks.exe";
protected $serverInfo = array();
const CONFIG_FILE = "gui-config.json";
const KILL_APP = "taskkill /F /IM Shadowsocks.exe /T";
const START_APP = "start %s &";
public function __construct($websiteUrl, $typeServer, $appPath)
{
$this -> websiteUrl = $websiteUrl;
$this -> typeServer = $typeServer;
$this -> appPath = $appPath;
}
public function run()
{
try
{
$serverInfo = $this -> getServerInfo();
$newServerInfo = $this -> mergeConfig($serverInfo);
$this -> dumpConfig($newServerInfo);
$this -> killApp();
sleep(3);
$this -> startApp();
}
catch(Exception $e)
{
$this -> log($e -> getMessage());
}
}
public function getServerInfo()
{
if(!empty($this -> serverInfo)) return $this -> serverInfo;
$serverInfo = array();
$contents = file_get_contents($this -> websiteUrl);
$serverPattern = "/". $this -> typeServer . "服务器地址:(.*)\</";
$serverMatches = array();
$result = preg_match($serverPattern, $contents, $serverMatches);
if($result && isset($serverMatches[1])) // assign server address
$serverInfo['server'] = $serverMatches[1];
$pwdPattern = "/". $this -> typeServer . "密码:(.*)\</";
$pwdMatches = array();
$result = preg_match($pwdPattern, $contents, $pwdMatches);
if($result && isset($pwdMatches[1]))
$serverInfo['password'] = $pwdMatches[1];
$portPattern = "/端口:(\d+)/";
$portMatches = array();
$result = preg_match_all($portPattern, $contents, $portMatches);
if($result && isset($portMatches[1]))
{
switch($this -> typeServer)
{
case 'A':
$serverInfo['server_port'] = $portMatches[1][0];
break;
case 'B':
$serverInfo['server_port'] = $portMatches[1][1];
break;
case 'C':
$serverInfo['server_port'] = $portMatches[1][2];
break;
default:
break;
}
}
return $this -> serverInfo = $serverInfo;
}
protected function mergeConfig($newServerInfo)
{
if(empty($newServerInfo))
{
$this -> log($message = 'Get server infomation from shadowsocks.com error.');
throw new Exception($message);
}
$oldServerInfo = $this -> getConfigure();
$oldServerInfo['configs'][0] = array_merge($oldServerInfo['configs'][0], $newServerInfo);
return $oldServerInfo;
}
protected function getConfigure()
{
$softConfigString = file_get_contents(self::CONFIG_FILE);
$softConfigJson = json_decode($softConfigString, true);
if(is_array($softConfigJson) && isset($softConfigJson['configs']) && isset($softConfigJson['configs'][0]))
return $softConfigJson;
$this -> log($message = 'Application config file[gui-config.json] bad format. please check it.');
throw new Exception($message);
}
protected function dumpConfig($config)
{
file_put_contents(self::CONFIG_FILE, json_encode($config, JSON_UNESCAPED_UNICODE));
}
protected function killApp()
{
echo exec(self::KILL_APP);
}
protected function startApp()
{
$startCommand = sprintf(self::START_APP, $this -> appPath);
echo exec($startCommand);
}
protected function log($msg)
{
$formatMessage = '['. date('Y-m-d H:i:s', time()).']: ' . $msg . "\n";
$fileHandle = file_put_contents('Application.log', $formatMessage, FILE_APPEND);
}
}
(new ServerUpdate($websiteUrl, $typeServer, $appPath)) -> run();