-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxray.init
133 lines (124 loc) · 3.24 KB
/
xray.init
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
#!/bin/bash
#chkconfig: 2345 99 99
### BEGIN INIT INFO
# Provides: xray
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start xray daemon at boot time
# Description: Start xray daemon at boot time
### END INIT INFO
START=99
EXTRA_COMMANDS="status"
usage()
{
if type systemctl && [ -z "$(systemctl --failed|grep -q 'Host is down')" ]; then
us="systemctl [start|stop|status|restart] xray.service"
else
if [ "$0" = '/etc/rc.common' ]; then
us="/etc/init.d/xray [start|stop|status|restart]"\
else
us="$0 [start|stop|status|restart]"
fi
fi &>/dev/null
echo "Usage:
$us
Config file is /usr/local/xray/xray.json"
} 2>/dev/null
status()
{
grep -q xray /proc/`cat /usr/local/xray/run.pid`/comm && \
echo "xray is running..." || \
echo "xray is stopped..."
} 2>/dev/null
start()
{
status | grep "xray is running..." && return 0
echo -n "Starting xray:"
echo 3 >/proc/sys/net/ipv4/tcp_fastopen
ulimit -n 1048576
cd "/usr/local/xray"
(while :; do
./xray run -config xray.json &
echo $! >/usr/local/xray/run.pid
sleep 3
while grep -q xray /proc/`cat /usr/local/xray/run.pid`/comm; do
sleep 3
done
done >/dev/null &
echo $! >/usr/local/xray/drun.pid)
count=0
while [ ! -f "/usr/local/xray/run.pid" ] && [ "$count" -le 5 ]; do
sleep 1
count=$((count + 1))
done
sleep 1
for server_port in 10002 ; do
iptables -I INPUT -p 6 --dport $server_port -j ACCEPT
ip6tables -I INPUT -p 6 --dport $server_port -j ACCEPT
done
for server_port in ; do
iptables -I INPUT -p 17 --dport $server_port -j ACCEPT
ip6tables -I INPUT -p 17 --dport $server_port -j ACCEPT
done
grep -q xray /proc/`cat /usr/local/xray/run.pid`/comm && \
echo -e "\033[60G[\033[32m OK \033[0m]" || \
echo -e "\033[60G[\033[31mFAILED\033[0m]"
} 2>/dev/null
stop()
{
for server_port in 10002 ; do
iptables -D INPUT -p 6 --dport $server_port -j ACCEPT
ip6tables -D INPUT -p 6 --dport $server_port -j ACCEPT
done
for server_port in ; do
iptables -D INPUT -p 17 --dport $server_port -j ACCEPT
ip6tables -D INPUT -p 17 --dport $server_port -j ACCEPT
done
status | grep "xray is stopped..." && return 0
echo -n "Stopping xray:"
kill -9 `cat /usr/local/xray/drun.pid`
kill -9 `cat /usr/local/xray/run.pid`
sleep 1
grep -q xray /proc/`cat /usr/local/xray/run.pid`/comm && \
echo -e "\033[60G[\033[31mFAILED\033[0m]" || \
echo -e "\033[60G[\033[32m OK \033[0m]"
} 2>/dev/null
restart()
{
stop
start
} 2>/dev/null
systemdStart()
{
status | grep running && return 0
cd "/usr/local/xray"
for server_port in 10002 ; do
iptables -I INPUT -p 6 --dport $server_port -j ACCEPT
ip6tables -I INPUT -p 6 --dport $server_port -j ACCEPT
done
for server_port in ; do
iptables -I INPUT -p 17 --dport $server_port -j ACCEPT
ip6tables -I INPUT -p 17 --dport $server_port -j ACCEPT
done
ulimit -n 1048576
cd "/usr/local/xray"
(while :; do
./xray run -config xray.json &
echo $! >/usr/local/xray/run.pid
sleep 3
while grep -q xray /proc/`cat /usr/local/xray/run.pid`/comm; do
sleep 3
done
done >/dev/null &
echo $! >/usr/local/xray/drun.pid)
} 2>/dev/null
systemdRestart()
{
stop
systemdStart
} 2>/dev/null
if [ -z "$initscript" ]; then
${1:-usage}
fi