-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyunti
executable file
·386 lines (280 loc) · 9.74 KB
/
yunti
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env php
<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use League\CLImate\CLImate;
use Sunra\PhpSimple\HtmlDomParser;
class YunTi {
const SITE_URL = 'https://www.yuntiweb.com';
const LOGIN_PATH = '/users/sign_in';
const SERVERS_PATH = '/admin/servers';
/**
* @var mixed
*/
private $parser;
/**
* @var mixed
*/
private $client;
/**
* @var mixed
*/
private $token;
/**
* @var mixed
*/
private $servers;
public function __construct() {
$this->parser = new HtmlDomParser();
$options = [
'base_uri' => self::SITE_URL,
'timeout' => 15,
'cookies' => true,
'debug' => false,
'headers' => [
'User-Agent' => 'AlphaGo'
]
];
$this->client = new Client($options);
$this->cli = new CLImate;
date_default_timezone_set('Asia/Shanghai');
}
public function run() {
if (is_file(__DIR__ . '/servers.txt')) {
$text = file_get_contents(__DIR__ . '/servers.txt', 'r') or die('Unable to open file!');
$this->servers = json_decode($text, true);
} else {
if (is_file(__DIR__ . '/auth.txt')) {
$text = file_get_contents(__DIR__ . '/auth.txt', 'r') or die('Unable to open file!');
$auth = json_decode($text, true);
} else {
$auth = $this->inputAuth();
}
$login_status = $this->login($auth['username'], $auth['password']);
if ($login_status) {
$this->saveAuth($auth);
} else {
$this->run();
}
$this->servers = $this->getServers();
$this->storageServers($this->servers);
}
$this->pingTest();
$this->connection();
}
/**
* @return mixed
*/
public function getCsrfToken() {
$result = $this->client->get(self::LOGIN_PATH);
$html = $result->getBody();
$html_dom = $this->parser->str_get_html($html);
$token = $html_dom->find('meta[name=csrf-token]', 0)->getAttribute('content');
if (empty($token)) {
throw new \Exception('Get csrf token error');
}
$this->token = $token;
return $token;
}
/**
* @param $username
* @param $password
*/
private function login($username, $password) {
$this->cli->br()->out('正在登陆…');
$this->getCsrfToken();
$result = $this->client->post(self::LOGIN_PATH, [
'form_params' => [
'user[login]' => $username,
'user[password]' => $password,
'user[remember_me]' => 1,
'authenticity_token' => $this->token,
'commit' => '登录',
'utf8' => '✓',
],
]);
$html = $result->getBody();
$html_dom = $this->parser->str_get_html($html);
$login_info = $html_dom->find('div.alert-success, div.alert-danger', 0)->plaintext;
# cut utf-8 chinese
preg_match_all('/[\x{4e00}-\x{9fa5}]+/u', $login_info, $matches);
$login_info = $matches[0][0];
if ($login_info == '登录成功') {
$this->cli->br()->info('登录成功');
return true;
} else {
$this->cli->br()->error($login_info);
return false;
}
}
/**
* @return mixed
*/
private function getServers() {
$this->cli->br()->out('获取服务器列表…');
$result = $this->client->get(self::SERVERS_PATH);
$html = $result->getBody();
$html_dom = $this->parser->str_get_html($html);
$servers = [];
foreach ($html_dom->find('table', 0)->find('tr') as $key => $tr) {
# ignore th
if ($key != 0) {
if ($tr->find('td[rowspan=3]')) {
$name = trim($tr->find('td[rowspan=3]', -1)->plaintext);
}
$ip = trim($tr->find('td', -4)->plaintext);
$type = trim($tr->find('td', -2)->plaintext);
$servers[] = compact('name', 'type', 'ip');
}
}
$this->cli->br()->ingo('服务器列表获取成功,开始测速…');
return $servers;
}
/**
* @param array $servers
*/
public function storageServers(array $servers) {
$file = fopen(__DIR__ . '/servers.txt', 'w') or die('Unable to write file!' . PHP_EOL);
fwrite($file, json_encode($servers));
fclose($file);
}
/**
* @return mixed
*/
public function inputAuth() {
$input = $this->cli->br()->input('请输入您的云梯登陆账号:');
$input->accept(function($response) {
return ($response !== '');
});
$username = $input->prompt();
$input = $this->cli->password('请输入您的云梯登陆密码:');
$password = $input->prompt();
$auth = compact('username', 'password');
return $auth;
}
/**
* @param array $auth
*/
public function saveAuth(array $auth) {
$file = fopen(__DIR__ . '/auth.txt', 'w') or die('Unable to write file!' . PHP_EOL);
fwrite($file, json_encode($auth));
fclose($file);
}
/**
* @return mixed
*/
public function pingTest() {
$dir = __DIR__ . '/logs/';
if (!is_dir($dir)) {
mkdir($dir);
} else {
# 清理上次的纪录
$files = scandir($dir);
foreach ($files as $key => $file) {
$file_path = $dir . $file;
if (is_file($file_path)) {
unlink($file_path);
}
}
}
$this->cli->br()->out('ping 啊 ping,ping 个大气球…');
foreach ($this->servers as $key => $server) {
exec('ping -c 5 ' . $server['ip'] . ' > ' . $dir . $key . '.log &');
}
$avgs = [];
$progress = $this->cli->br()->progress()->total(count($this->servers));
$servers = $this->servers;
while (true) {
foreach ($servers as $key => $server) {
$file_name = $key . '.log';
$data = file($dir . $file_name);
$last_line = array_pop($data);
if (strpos($last_line, 'avg')) {
# Mac用户
preg_match_all('/[1-9]\d*\.\d{3}/', $last_line, $matches);
$avg = $matches[0][1];
$avgs[$key] = $avg;
$this->servers[$key]['avg'] = $avg;
} elseif (strpos($last_line, '100.0% packet loss')) {
$avgs[$key] = 'down';
$this->servers[$key]['avg'] = 'down';
} else {
usleep(5000);
continue;
}
unset($servers[$key]);
$progress->current(count($avgs));
}
if (count($avgs) == count($this->servers)) {
$table[] = ['name', 'type', 'ip', 'avg'];
$this->servers = $this->sortArray($this->servers, 'avg');
foreach ($this->servers as $key => $server) {
$table[] = array_values($server);
}
$this->cli->clear();
$this->cli->br()->columns($table);
break;
}
}
return $avgs;
}
public function sortArray($array, $sort_key)
{
$sort_keys = [];
foreach ($array as $key => $value) {
$sort_keys[] = $value[$sort_key];
}
array_multisort($sort_keys, SORT_DESC, $array);
return $array;
}
public function checkConnectionStatus()
{
exec('ifconfig |grep ppp0', $result, $result_code);
if (!empty($result) && $result_code === 0) {
return true;
} else {
return false;
}
}
public function connection() {
// unsupported PPTP and IkEV2
$array_key_count = count($this->servers) - 1;
for ($i=$array_key_count; $i >= 0; $i--) {
if ($this->servers[$i]['type'] != 'L2TP') {
continue;
} else {
$fast_server_key = $i;
break;
}
}
$fastest_vpn_name = '云梯 ' . $this->servers[$fast_server_key]['name'] . ' ' . $this->servers[$fast_server_key]['type'];
$fastest_vpn_info = '延迟最低 L2TP 的线路为:' . $fastest_vpn_name . ' AVG: ' . $this->servers[$fast_server_key]['avg'];
$this->cli->br()->lightBlue($fastest_vpn_info);
$scpt_code_templete = [
'tell application "System Events"',
'tell current location of network preferences',
'set VPNservice to service "' . $fastest_vpn_name . '" -- name of the VPN service',
'if exists VPNservice then connect VPNservice',
'end tell',
'end tell',
];
$scpt_code = 'osascript';
foreach ($scpt_code_templete as $key => $value) {
$scpt_code .= ' -e \'' . $value . '\'';
}
$this->cli->br()->out('正在连接' . $fastest_vpn_name . '…');
exec($scpt_code, $result);
for ($i=0; $i < 10; $i++) {
sleep(1);
$status = $this->checkConnectionStatus();
if ($status) {
break;
} else if ($i == 9) {
$this->cli->br()->error('检测连接状态超时,请亲自确认 VPN 是否已连接。');
}
}
$this->cli->br()->info('完成');
}
}
$yunti = new YunTi();
$yunti->run();