@@ -187,6 +187,29 @@ function is_installed() {
187
187
return 0
188
188
}
189
189
190
+
191
+ function kill_process_by_port() {
192
+ pids=$( get_pids_by_port $1 )
193
+ if [ -n " $pids " ]; then
194
+ kill -9 $pids
195
+ fi
196
+ }
197
+
198
+ function get_pids_by_port() {
199
+ echo $( netstat -tulpn 2> /dev/null | grep " :$1 " | awk ' {print $7}' | sed ' s|/.*||' )
200
+ }
201
+
202
+ function is_port_open() {
203
+ pids=$( get_pids_by_port $1 )
204
+
205
+ if [ -n " $pids " ]; then
206
+ return 0
207
+ else
208
+ return 1
209
+ fi
210
+ }
211
+
212
+
190
213
function is_running_mtp() {
191
214
if [ -f $pid_file ]; then
192
215
@@ -243,11 +266,35 @@ print_line() {
243
266
echo -e " ========================================="
244
267
}
245
268
269
+ do_kill_process () {
270
+ cd $WORKDIR
271
+ source ./mtp_config
272
+
273
+ if is_port_open $port ; then
274
+ echo " 检测到端口 $port 被占用, 准备杀死进程!"
275
+ kill_process_by_port $port
276
+ fi
277
+
278
+ if is_port_open $web_port ; then
279
+ echo " 检测到端口 $web_port 被占用, 准备杀死进程!"
280
+ kill_process_by_port $web_port
281
+ fi
282
+ }
283
+
284
+ do_check_system_datetime_and_update () {
285
+ offset=$( ntpdate -q time.google.com | grep -oP ' offset \K[\d]+' | tail -n 1)
286
+ tolerance=60
287
+ if [ " $offset " -gt " $tolerance " ] || [ " $offset " -lt " -$tolerance " ]; then
288
+ echo " 检测到系统时间不同步于世界时间, 即将更新"
289
+ ntpdate -u time.google.com
290
+ fi
291
+ }
292
+
246
293
do_install_basic_dep () {
247
294
if check_sys packageManager yum; then
248
- yum install -y iproute curl wget procps-ng.x86_64
295
+ yum install -y iproute curl wget procps-ng.x86_64 net-tools ntp
249
296
elif check_sys packageManager apt; then
250
- apt install -y iproute2 curl wget procps
297
+ apt install -y iproute2 curl wget procps net-tools ntpdate
251
298
fi
252
299
253
300
return 0
@@ -425,7 +472,7 @@ function get_run_command(){
425
472
client_secret=" ee${secret}${domain_hex} "
426
473
local local_ip=$( get_local_ip)
427
474
public_ip=$( get_ip_public)
428
-
475
+
429
476
# ./mtg simple-run -n 1.1.1.1 -t 30s -a 512kib 0.0.0.0:$port $client_secret >/dev/null 2>&1 &
430
477
[[ -f " ./mtg" ]] || (echo -e " 提醒:\033[33m MTProxy 代理程序不存在请重新安装! \033[0m" && exit 1)
431
478
echo " ./mtg run $client_secret $proxy_tag -b 0.0.0.0:$port --multiplex-per-connection 500 --prefer-ip=ipv6 -t $local_ip :$web_port " -4 " $public_ip :$port "
@@ -446,6 +493,9 @@ run_mtp() {
446
493
if is_running_mtp; then
447
494
echo -e " 提醒:\033[33mMTProxy已经运行,请勿重复运行!\033[0m"
448
495
else
496
+ do_kill_process
497
+ do_check_system_datetime_and_update
498
+
449
499
local command=$( get_run_command)
450
500
echo $command
451
501
$command > /dev/null 2>&1 &
@@ -463,6 +513,9 @@ daemon_mtp() {
463
513
if is_running_mtp; then
464
514
echo -e " 提醒:\033[33mMTProxy已经运行,请勿重复运行!\033[0m"
465
515
else
516
+ do_kill_process
517
+ do_check_system_datetime_and_update
518
+
466
519
local command=$( get_run_command)
467
520
echo $command
468
521
while true
@@ -484,6 +537,9 @@ debug_mtp() {
484
537
echo " 当前正在运行调试模式:"
485
538
echo -e " \t你随时可以通过 Ctrl+C 进行取消操作"
486
539
540
+ do_kill_process
541
+ do_check_system_datetime_and_update
542
+
487
543
local command=$( get_run_command)
488
544
echo $command
489
545
$command
0 commit comments