OSDN Git Service

luci-app-v2ray-server: fix compatibility for v2ray-core v5
[immortalwrt/luci.git] / applications / luci-app-v2ray-server / root / etc / init.d / v2ray_server
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2018-2020 Lienol <lawlienol@gmail.com>
3
4 START=99
5
6 CONFIG=v2ray_server
7 CONFIG_PATH=/var/etc/$CONFIG
8 LOG_PATH=/var/log/$CONFIG
9 LOG_APP_FILE=$LOG_PATH/app.log
10
11 echolog() {
12         echo -e "$(date "+%Y-%m-%d %H:%M:%S"): $1" >> $LOG_APP_FILE
13 }
14
15 gen_v2ray_config_file() {
16         config_get enable $1 enable
17         [ "$enable" = "0" ] && return 0
18         config_get remarks $1 remarks
19         config_get port $1 port
20         config_get transport $1 transport
21         lua /usr/lib/lua/luci/model/cbi/v2ray_server/api/genv2rayconfig.lua $1 > $CONFIG_PATH/$1.json
22         echolog "$remarks $port 生成并运行 V2ray 配置文件 - $CONFIG_PATH/$1.json" 
23         if [ ! -f /var/v2server ]; then
24                 local ret="/usr/bin/xray"
25                 [ ! -f "$ret" ] && ret="/usr/bin/v2ray/v2ray"
26                 [ ! -f "$ret" ] && ret="/usr/bin/v2ray"
27                 cp -a $ret /var/v2server
28         fi
29         /var/v2server run -config $CONFIG_PATH/$1.json >/dev/null 2>&1 &
30 }
31
32 start_v2ray_server() {
33         mkdir -p $CONFIG_PATH $LOG_PATH
34         touch $LOG_APP_FILE
35         config_foreach gen_v2ray_config_file "user"
36         fw3 reload > /dev/null 2>&1
37 }
38
39 stop_v2ray_server() {
40         fw3 reload > /dev/null 2>&1
41         ps -w | grep "$CONFIG_PATH/" | grep -v "grep" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
42         rm -rf $CONFIG_PATH
43         rm -rf $LOG_PATH
44 }
45
46 start() {
47         config_load $CONFIG
48         enable=$(uci get $CONFIG.@global[0].enable)
49         if [ "$enable" = "0" ];then
50                 stop_v2ray_server
51         else
52                 start_v2ray_server
53         fi
54 }
55
56 stop() {
57         stop_v2ray_server
58 }
59
60 restart() {
61         stop
62         sleep 1
63         start
64 }