@@ -187,6 +187,29 @@ function is_installed() {
187187 return 0
188188}
189189
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+
190213function is_running_mtp() {
191214 if [ -f $pid_file ]; then
192215
@@ -243,11 +266,35 @@ print_line() {
243266 echo -e " ========================================="
244267}
245268
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+
246293do_install_basic_dep () {
247294 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
249296 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
251298 fi
252299
253300 return 0
@@ -425,7 +472,7 @@ function get_run_command(){
425472 client_secret=" ee${secret}${domain_hex} "
426473 local local_ip=$( get_local_ip)
427474 public_ip=$( get_ip_public)
428-
475+
429476 # ./mtg simple-run -n 1.1.1.1 -t 30s -a 512kib 0.0.0.0:$port $client_secret >/dev/null 2>&1 &
430477 [[ -f " ./mtg" ]] || (echo -e " 提醒:\033[33m MTProxy 代理程序不存在请重新安装! \033[0m" && exit 1)
431478 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() {
446493 if is_running_mtp; then
447494 echo -e " 提醒:\033[33mMTProxy已经运行,请勿重复运行!\033[0m"
448495 else
496+ do_kill_process
497+ do_check_system_datetime_and_update
498+
449499 local command=$( get_run_command)
450500 echo $command
451501 $command > /dev/null 2>&1 &
@@ -463,6 +513,9 @@ daemon_mtp() {
463513 if is_running_mtp; then
464514 echo -e " 提醒:\033[33mMTProxy已经运行,请勿重复运行!\033[0m"
465515 else
516+ do_kill_process
517+ do_check_system_datetime_and_update
518+
466519 local command=$( get_run_command)
467520 echo $command
468521 while true
@@ -484,6 +537,9 @@ debug_mtp() {
484537 echo " 当前正在运行调试模式:"
485538 echo -e " \t你随时可以通过 Ctrl+C 进行取消操作"
486539
540+ do_kill_process
541+ do_check_system_datetime_and_update
542+
487543 local command=$( get_run_command)
488544 echo $command
489545 $command
0 commit comments