#!/bin/sh
#
# Copyright (C) 2019 vssr
# Copyright (C) 2019 jerrykuku <jerrykuku@qq.com>
#
# This is free software, licensed under the GNU General Public License v3.
# See /LICENSE for more information.
#

NAME=vssr

#定义配置文件名称
CONFIG_FILE=/var/etc/${NAME}_t.json
CONFIG_UDP_FILE=/var/etc/${NAME}_u.json
CONFIG_SOCK5_FILE=/var/etc/${NAME}_s.json

switch_server=$1

uci_get_by_name() {
    local ret=$(uci get $NAME.$1.$2 2>/dev/null)
    echo ${ret:=$3}
}

uci_get_by_type() {
    local ret=$(uci get $NAME.@$1[0].$2 2>/dev/null)
    echo ${ret:=$3}
}

gen_config_file() {
    local host=$(uci_get_by_name $1 server)
    if echo $host | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
        hostip=${host}
    elif [ "$host" != "${host#*:[0-9a-fA-F]}" ]; then
        hostip=${host}
    else
        hostip=$(ping ${host} -s 1 -c 1 | grep PING | cut -d'(' -f 2 | cut -d')' -f1)
        if echo $hostip | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
            hostip=${hostip}
        else
            hostip=$(cat /etc/ssr_ip)
        fi
    fi
    [ $2 = "0" -a $kcp_flag = "1" ] && hostip="127.0.0.1"

    if [ $2 = "0" ]; then
        re_type="tcp"
        config_file=$CONFIG_FILE
        server_obj=$GLOBAL_SERVER
    elif [ $2 = "1" ]; then
        re_type="udp"
        config_file=$CONFIG_UDP_FILE
        server_obj=$UDP_RELAY_SERVER
    fi
    if [ $(uci_get_by_name $1 fast_open 0) = "1" ]; then
        fastopen="true"
    else
        fastopen="false"
    fi

    local stype=$(uci_get_by_name $1 type)
    local port=$(uci_get_by_name $1 local_port)
    if [ "$stype" == "trojan" ]; then
        if [ "$re_type" == "udp" ]; then
            re_type="client"
            port="10801"
        else
            re_type="nat"
        fi
    fi
    lua /usr/share/vssr/genconfig_${stype}.lua ${server_obj} ${re_type} ${port} ${hostip} >${config_file}
    sed -i 's/\\//g' $config_file

}
start_local() {
    local local_server=$(uci_get_by_type socks5_proxy enable_server)
    local local_port=$(uci_get_by_type socks5_proxy local_port)
    local Socks_user=$(uci_get_by_type socks5_proxy Socks_user)
    local Socks_pass=$(uci_get_by_type socks5_proxy Socks_pass)
    [ "$local_server" = "0" ] && return 1
    mkdir -p /var/run /var/etc

    lua /usr/share/vssr/genconfig_v2ray_s.lua "socks" ${local_port} ${Socks_user} ${Socks_pass} >$CONFIG_SOCK5_FILE
    sed -i 's/\\//g' $config_file

    /usr/bin/xray -config $CONFIG_SOCK5_FILE >/dev/null 2>&1 &
}

killall -q -9 ssr-redir
killall -q -9 v2ray
killall -q -9 trojan
killall -q -9 ipt2socks

case "$(uci_get_by_name $GLOBAL_SERVER auth_enable)" in
1 | on | true | yes | enabled) ARG_OTA="-A" ;;
*) ARG_OTA="" ;;
esac

if [ -z "$switch_server" ]; then
    GLOBAL_SERVER=$(uci_get_by_type global global_server)
else
    GLOBAL_SERVER=$switch_server
fi

gen_config_file $GLOBAL_SERVER 0
stype=$(uci_get_by_name $GLOBAL_SERVER type)
local stype=$(uci_get_by_name $GLOBAL_SERVER type)
if [ "$stype" == "ss" -o "$stype" == "v2ray" ]; then
    sscmd="/usr/bin/xray"
    [ ! -f "$sscmd" ] && sscmd="/usr/bin/xray"
elif [ "$stype" == "ssr" ]; then
    sscmd="/usr/bin/ssr-redir"
elif [ "$stype" == "trojan" ]; then
    sscmd="/usr/sbin/trojan"
fi

local utype=$(uci_get_by_name $UDP_RELAY_SERVER type)
if [ "$utype" == "ss" -o "$utype" == "v2ray" ]; then
    ucmd="/usr/bin/xray"
    [ ! -f "$ucmd" ] && ucmd="/usr/bin/xray"
elif [ "$utype" == "ssr" ]; then
    ucmd="/usr/bin/ssr-redir"
elif [ "$utype" == "trojan" ]; then
    ucmd="/usr/sbin/trojan"
fi

if [ "$(uci_get_by_type global threads 0)" = "0" ]; then
    threads=$(cat /proc/cpuinfo | grep 'processor' | wc -l)
else
    threads=$(uci_get_by_type global threads)
fi

#转发TCP
if [ "$(uci_get_by_type global threads 0)" = "0" ]; then
    threads=$(cat /proc/cpuinfo | grep 'processor' | wc -l)
else
    threads=$(uci_get_by_type global threads)
fi
#转发TCP
redir_tcp=1
local last_config_file=$CONFIG_FILE
if [ "$stype" == "ssr" ]; then
    local pid_file="/var/run/ssr-retcp.pid"
    for i in $(seq 1 $threads); do
        $sscmd -c $last_config_file $ARG_OTA -f /var/run/ssr-retcp_$i.pid >/dev/null 2>&1
    done
    echo "$(date "+%Y-%m-%d %H:%M:%S") SSR $threads 线程 已启动!" >>/tmp/vssr.log
elif [ "$stype" == "v2ray" -o "$stype" == "ss" ]; then
    $sscmd -config $last_config_file >/dev/null 2>&1 &

    echo "$(date "+%Y-%m-%d %H:%M:%S") $($sscmd -version | head -1) 已启动!" >>/tmp/vssr.log

elif [ "$stype" == "trojan" ]; then
    $sscmd -c $last_config_file >/dev/null 2>&1 &
    echo "$(date "+%Y-%m-%d %H:%M:%S") $($sscmd -V 2>&1 | head -1)  已启动!" >>/tmp/vssr.log
fi

#转发UDP
if [ -n "$UDP_RELAY_SERVER" ]; then
    redir_udp=1
    gen_config_file $UDP_RELAY_SERVER 1
    last_config_file=$CONFIG_UDP_FILE
    echo $utype
    if [ "$utype" == "ssr" ]; then

        case "$(uci_get_by_name $UDP_RELAY_SERVER auth_enable)" in
        1 | on | true | yes | enabled) ARG_OTA="-A" ;;
        *) ARG_OTA="" ;;
        esac
        pid_file="/var/run/ssr-reudp.pid"
        #echo  $ucmd >> /tmp/vssr.log
        $ucmd -c $last_config_file $ARG_OTA -U -f /var/run/ssr-reudp.pid >/tmp/vssr.log 2>&1
        #echo "$(date "+%Y-%m-%d %H:%M:%S") $($sscmd -version | head -1) UDP已启动!" >> /tmp/vssr.log
    elif [ "$utype" == "ss" -o "$utype" == "v2ray" ]; then
        $ucmd -config $last_config_file >/dev/null 2>&1 &
    elif [ "$stype" == "trojan" ]; then
        $ucmd --config $last_config_file >/dev/null 2>&1 &
        ipt2socks -U -4 -b 0.0.0.0 -s 127.0.0.1 -p 10801 -l $(uci_get_by_name $UDP_RELAY_SERVER local_port) >/dev/null 2>&1 &
    fi
fi
start_local
