Skip to content

Commit 4101bda

Browse files
committed
Add automatically correct time and kill process for occupied port
1 parent f156301 commit 4101bda

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ RUN set -ex \
2222
# build mtproxy and install php
2323
RUN set -ex \
2424
&& apt-get update \
25-
&& apt-get install -y --no-install-recommends git wget curl build-essential libssl-dev zlib1g-dev iproute2 php7.4-fpm vim-common \
25+
&& apt-get install -y --no-install-recommends git wget curl build-essential libssl-dev zlib1g-dev iproute2 php7.4-fpm vim-common net-tools ntpdate procps \
2626
&& bash mtproxy.sh build \
2727
&& sed -i 's/^user\s*=[^\r]\+/user = root/' /etc/php/7.4/fpm/pool.d/www.conf \
2828
&& sed -i 's/^group\s*=[^\r]\+/group = root/' /etc/php/7.4/fpm/pool.d/www.conf \

mtproxy.sh

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
190213
function 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+
246293
do_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

Comments
 (0)