#!/bin/sh # # Copyright (C) 2019 vssr # Copyright (C) 2019 jerrykuku # # 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 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) lua /usr/share/vssr/genconfig_${stype}.lua ${server_obj} ${re_type} $(uci_get_by_name $1 local_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/v2ray/v2ray -config $CONFIG_SOCK5_FILE >/dev/null 2>&1 & } killall -q -9 ssr-redir killall -q -9 v2ray 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) if [ "$stype" == "ss" -o "$stype" == "v2ray" ]; then sscmd="/usr/bin/v2ray/v2ray" elif [ "$stype" == "ssr" ]; then sscmd="/usr/bin/ssr-redir" 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 redir_tcp=1 last_config_file=$CONFIG_FILE if [ "$stype" == "ssr" ]; then 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 fi start_local