97 lines
3.0 KiB
Bash
97 lines
3.0 KiB
Bash
#!/bin/sh
|
|
|
|
#chkconfig: 2345 99 99
|
|
### BEGIN INIT INFO
|
|
# Provides: v2ray
|
|
# Required-Start: $all
|
|
# Required-Stop: $all
|
|
# Default-Start: 2 3 4 5
|
|
# Default-Stop: 0 1 6
|
|
# Short-Description: Start v2ray daemon at boot time
|
|
# Description: Start v2ray daemon at boot time
|
|
### END INIT INFO
|
|
START=99
|
|
|
|
usage()
|
|
{
|
|
if type systemctl && [ -z "$(systemctl --failed|grep -q 'Host is down')" ]; then
|
|
us="systemctl [start|stop|status|restart] cns.service"
|
|
else
|
|
us="$0 [start|stop|status|restart]"
|
|
fi 2>/dev/null
|
|
echo "Usage:
|
|
$us
|
|
Config file is [v2ray_install_directory]/v2ray.json"
|
|
}
|
|
|
|
status()
|
|
{
|
|
grep -q v2ray /proc/`cat [v2ray_install_directory]/run.pid`/comm && \
|
|
echo "v2ray is running..." || \
|
|
echo "v2ray is stopped..."
|
|
}
|
|
|
|
start()
|
|
{
|
|
status | grep "v2ray is running..." && return 0
|
|
echo -n "Starting v2ray:"
|
|
echo 3 >/proc/sys/net/ipv4/tcp_fastopen 2>/dev/null
|
|
($command >/dev/null & echo $! >[v2ray_install_directory]/run.pid)
|
|
ulimit -n 1048576
|
|
cd "[v2ray_install_directory]"
|
|
(while :; do
|
|
./v2ray run -config v2ray.json &
|
|
echo $! >[v2ray_install_directory]/run.pid
|
|
while grep -q v2ray /proc/`cat [v2ray_install_directory]/run.pid`/comm; do
|
|
sleep 3
|
|
done
|
|
done >/dev/null &
|
|
echo $! >[v2ray_install_directory]/drun.pid)
|
|
count=0
|
|
while [ ! -f "[v2ray_install_directory]/run.pid" ] && [ "$count" -le 5 ]; do
|
|
sleep 1
|
|
count=$((count + 1))
|
|
done
|
|
sleep 1
|
|
for server_port in [v2ray_tcp_port_list]; do
|
|
iptables -I INPUT -p 6 --dport $server_port -m comment --comment "v2ray server port" -j ACCEPT
|
|
ip6tables -I INPUT -p 6 --dport $server_port -m comment --comment "v2ray server port" -j ACCEPT
|
|
done 2>/dev/null
|
|
for server_port in [v2ray_udp_port_list]; do
|
|
iptables -I INPUT -p 17 --dport $server_port -m comment --comment "v2ray server port" -j ACCEPT
|
|
ip6tables -I INPUT -p 17 --dport $server_port -m comment --comment "v2ray server port" -j ACCEPT
|
|
done 2>/dev/null
|
|
grep -q v2ray /proc/`cat [v2ray_install_directory]/run.pid 2>/dev/null`/comm && \
|
|
echo -e "\033[60G[\033[32m OK \033[0m]" || \
|
|
echo -e "\033[60G[\033[31mFAILED\033[0m]"
|
|
}
|
|
|
|
stop()
|
|
{
|
|
for server_port in [v2ray_tcp_port_list]; do
|
|
iptables -D INPUT -p 6 --dport $server_port -m comment --comment "v2ray server port" -j ACCEPT
|
|
ip6tables -D INPUT -p 6 --dport $server_port -m comment --comment "v2ray server port" -j ACCEPT
|
|
done 2>/dev/null
|
|
for server_port in [v2ray_udp_port_list]; do
|
|
iptables -D INPUT -p 17 --dport $server_port -m comment --comment "v2ray server port" -j ACCEPT
|
|
ip6tables -D INPUT -p 17 --dport $server_port -m comment --comment "v2ray server port" -j ACCEPT
|
|
done 2>/dev/null
|
|
status | grep "v2ray is stopped..." && return 0
|
|
echo -n "Stopping v2ray:"
|
|
kill -9 `cat [v2ray_install_directory]/run.pid 2>/dev/null` 2>/dev/null
|
|
sleep 1
|
|
grep -q v2ray /proc/`cat [v2ray_install_directory]/run.pid`/comm && \
|
|
echo -e "\033[60G[\033[31mFAILED\033[0m]" || \
|
|
echo -e "\033[60G[\033[32m OK \033[0m]"
|
|
}
|
|
|
|
restart()
|
|
{
|
|
stop
|
|
start
|
|
}
|
|
|
|
command="[v2ray_install_directory]/v2ray -config [v2ray_install_directory]/v2ray.json"
|
|
${1:-usage} 2>/dev/null
|
|
|