forked from whunt1/onekeymakemtg
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mtproxy_go_centos
120 lines (116 loc) · 4.09 KB
/
mtproxy_go_centos
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
#!/bin/bash
# chkconfig: 2345 90 10
# description: MTProxy Golang
### BEGIN INIT INFO
# Provides: MTProxy Golang
# Required-Start: $network $syslog
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Simple MT-Proto-go proxy
# Description: Start or stop the MTProxy-go
### END INIT INFO
NAME="MTProxy"
NAME_BIN="./mtg "
FILE="/usr/local/mtproxy-go"
CONF="/usr/local/mtproxy-go/mtproxy.conf"
LOG="/usr/local/mtproxy-go/mtproxy.log"
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"
Info="${Green_font_prefix}[信息]${Font_color_suffix}"
Error="${Red_font_prefix}[错误]${Font_color_suffix}"
RETVAL=0
check_running(){
PID=$(ps -ef |grep "${NAME_BIN}" |grep -v "grep" |grep -v "init.d" |grep -v "service" |awk '{print $2}')
if [[ ! -z ${PID} ]]; then
return 0
else
return 1
fi
}
read_config(){
[[ ! -e ${CONF} ]] && echo -e "${Error} $NAME 配置文件不存在 !" && exit 1
port=$(cat ${CONF}|grep 'PORT = '|awk -F 'PORT = ' '{print $NF}')
password=$(cat ${CONF}|grep 'PASSWORD = '|awk -F 'PASSWORD = ' '{print $NF}')
fake_tls=$(cat ${CONF}|grep 'FAKE-TLS = '|awk -F 'FAKE-TLS = ' '{print $NF}')
tag=$(cat ${CONF}|grep 'TAG = '|awk -F 'TAG = ' '{print $NF}')
nat_ipv4=$(cat ${CONF}|grep 'NAT-IPv4 = '|awk -F 'NAT-IPv4 = ' '{print $NF}')
nat_ipv6=$(cat ${CONF}|grep 'NAT-IPv6 = '|awk -F 'NAT-IPv6 = ' '{print $NF}')
secure=$(cat ${CONF}|grep 'SECURE = '|awk -F 'SECURE = ' '{print $NF}')
buffer_write=$(cat ${CONF}|grep 'BUFFER-WRITE = '|awk -F 'BUFFER-WRITE = ' '{print $NF}')
buffer_read=$(cat ${CONF}|grep 'BUFFER-READ = '|awk -F 'BUFFER-READ = ' '{print $NF}')
stats_bind=$(cat ${CONF}|grep 'STATS-BIND = '|awk -F 'STATS-BIND = ' '{print $NF}')
anti_replay_max_size=$(cat ${CONF}|grep 'ANTI-REPLAY-MAX-SIZE = '|awk -F 'ANTI-REPLAY-MAX-SIZE = ' '{print $NF}')
multiplex_per_connection=$(cat ${CONF}|grep 'MULTIPLEX-PER-CONNECTION = '|awk -F 'MULTIPLEX-PER-CONNECTION = ' '{print $NF}')
[[ ! -z "${nat_ipv4}" ]] && nat_ipv4="-4 \"${nat_ipv4}:${port}\""
[[ ! -z "${nat_ipv6}" ]] && nat_ipv6="-6 \"[${nat_ipv6}]:${port}\""
[[ ! -z "${buffer_write}" ]] && buffer_write="-w ${buffer_write}"
[[ ! -z "${buffer_read}" ]] && buffer_read="-r ${buffer_read}"
[[ ! -z "${stats_bind}" ]] && stats_bind="--stats-bind=\"${stats_bind}\""
[[ ! -z "${anti_replay_max_size}" ]] && anti_replay_max_size="--anti-replay-max-size=${anti_replay_max_size}"
[[ ! -z "${multiplex_per_connection}" ]] && multiplex_per_connection="--multiplex-per-connection=${multiplex_per_connection}"
if [[ "${secure}" == "YES" && "${fake_tls}" == "NO" ]]; then
password="dd${password}"
fi
if [[ "${fake_tls}" == "YES" ]]; then
tls_port="--cloak-port=${port}"
else
tls_port=""
fi
}
do_start(){
check_running
if [[ $? -eq 0 ]]; then
echo -e "${Info} $NAME (PID ${PID}) 正在运行..." && exit 0
else
read_config
cd ${FILE}
echo -e "${Info} $NAME 启动中..."
ulimit -n 51200
eval nohup ./mtg run -b 0.0.0.0:${port} $(echo ${tls_port}) $(echo ${nat_ipv4}) $(echo ${nat_ipv6}) $(echo ${buffer_write}) $(echo ${buffer_read}) $(echo ${stats_bind}) $(echo ${anti_replay_max_size}) $(echo ${multiplex_per_connection}) "${password}" "${tag}" >> "${LOG}" 2>&1 &
sleep 2s
check_running
if [[ $? -eq 0 ]]; then
echo -e "${Info} $NAME 启动成功 !"
else
echo -e "${Error} $NAME 启动失败 !请查看日志文件检查问题所在。"
fi
fi
}
do_stop(){
check_running
if [[ $? -eq 0 ]]; then
kill -9 ${PID}
RETVAL=$?
if [[ $RETVAL -eq 0 ]]; then
echo -e "${Info} $NAME 停止成功 !"
else
echo -e "${Error} $NAME 停止失败 !"
fi
else
echo -e "${Info} $NAME 未运行"
RETVAL=1
fi
}
do_status(){
check_running
if [[ $? -eq 0 ]]; then
echo -e "${Info} $NAME (PID ${PID}) 正在运行..."
else
echo -e "${Info} $NAME 未运行 !"
RETVAL=1
fi
}
do_restart(){
do_stop
do_start
}
case "$1" in
start|stop|restart|status)
do_$1
;;
*)
echo -e "使用方法: $0 { start | stop | restart | status }"
RETVAL=1
;;
esac
exit $RETVAL