shadowsocksr 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. #!/bin/sh /etc/rc.common
  2. #
  3. # Copyright (C) 2017 openwrt-ssr
  4. # Copyright (C) 2017 yushi studio <[email protected]>
  5. # Copyright (C) 2018 lean <[email protected]>
  6. # Copyright (C) 2020 Mattraks <[email protected]>
  7. #
  8. # This is free software, licensed under the GNU General Public License v3.
  9. # See /LICENSE for more information.
  10. #
  11. START=95
  12. STOP=15
  13. SERVICE_DAEMONIZE=1
  14. NAME=shadowsocksr
  15. LOCK_FILE=/var/lock/ssrplus.lock
  16. LOG_FILE=/var/log/ssrplus.log
  17. TMP_PATH=/var/etc/ssrplus
  18. TMP_BIN_PATH=$TMP_PATH/bin
  19. # 设置 DNSMASQ_CONF_DIR 和 TMP_DNSMASQ_PATH
  20. if [ -f /etc/openwrt_release ]; then
  21. # 获取默认的 DNSMASQ 配置 ID
  22. DEFAULT_DNSMASQ_CFGID="$(uci -q show "dhcp.@dnsmasq[0]" | awk 'NR==1 {split($0, conf, /[.=]/); print conf[2]}')"
  23. # 从 conf-dir 行中提取配置目录路径
  24. if [ -f "/tmp/etc/dnsmasq.conf.$DEFAULT_DNSMASQ_CFGID" ]; then
  25. DNSMASQ_CONF_DIR="$(awk -F '=' '/^conf-dir=/ {print $2}' "/tmp/etc/dnsmasq.conf.$DEFAULT_DNSMASQ_CFGID")"
  26. else
  27. DNSMASQ_CONF_DIR="/tmp/dnsmasq.d"
  28. fi
  29. # 设置 TMP_DNSMASQ_PATH,并去除路径末尾的斜杠
  30. TMP_DNSMASQ_PATH="${DNSMASQ_CONF_DIR%*/}/dnsmasq-ssrplus.d"
  31. fi
  32. chain_config_file= #generate shadowtls chain proxy config file
  33. tcp_config_file=
  34. udp_config_file=
  35. shunt_config_file=
  36. local_config_file=
  37. shunt_dns_config_file=
  38. tmp_local_port=
  39. ARG_UDP=
  40. dns_port="5335" #dns port
  41. china_dns_port="5333" #china_dns_port
  42. tmp_dns_port="300" #dns2socks temporary port
  43. tmp_udp_port="301" #udp temporary port
  44. tmp_udp_local_port="302" #udp socks temporary port
  45. tmp_shunt_port="303" #shunt temporary port
  46. tmp_shunt_local_port="304" #shunt socks temporary port
  47. tmp_shunt_dns_port="305" #shunt dns2socks temporary port
  48. tmp_tcp_local_port="306" #tcp socks temporary port
  49. server_count=0
  50. redir_tcp=0
  51. redir_udp=0
  52. local_enable=0
  53. kcp_enable_flag=0
  54. pdnsd_enable_flag=0
  55. switch_server=$1
  56. CRON_FILE=/etc/crontabs/root
  57. EXTRA_COMMANDS='reset'
  58. EXTRA_HELP=" reset Reset to default settings"
  59. #extra_command "reset" "Reset to default settings"
  60. PS="/bin/busybox ps"
  61. uci_get_by_name() {
  62. local ret=$(uci get $NAME.$1.$2 2>/dev/null)
  63. echo ${ret:=$3}
  64. }
  65. uci_get_by_type() {
  66. local ret=$(uci get $NAME.@$1[0].$2 2>/dev/null)
  67. echo "${ret:=$3}"
  68. }
  69. uci_set_by_name() {
  70. uci set $NAME.$1.$2=$3 2>/dev/null
  71. uci commit $NAME
  72. }
  73. uci_set_by_type() {
  74. uci set $NAME.@$1[0].$2=$3 2>/dev/null
  75. uci commit $NAME
  76. }
  77. uci_get_by_cfgid() {
  78. local ret=$(uci show $NAME.@$1[0].$2 | awk -F '.' '{print $2}' 2>/dev/null)
  79. echo ${ret:=$3}
  80. }
  81. get_host_ip() {
  82. local host=$(uci_get_by_name $1 server)
  83. local ip=$host
  84. if [ -z "$(echo $host | grep -E "([0-9]{1,3}[\.]){3}[0-9]{1,3}")" ]; then
  85. if [ "$host" == "${host#*:[0-9a-fA-F]}" ]; then
  86. ip=$(resolveip -4 -t 3 $host | awk 'NR==1{print}')
  87. [ -z "$ip" ] && ip=$(wget -q -O- http://119.29.29.29/d?dn=$host | awk -F ';' '{print $1}')
  88. fi
  89. fi
  90. [ -z "$ip" ] || uci_set_by_name $1 ip $ip
  91. [ -n "$ip" ] || ip="$(uci_get_by_name $1 ip "ERROR")"
  92. local chinadns="$(uci_get_by_type global chinadns_forward)"
  93. if [ -n "$chinadns" ] && [ "$ip" != "$host" ]; then
  94. grep -q "$host" "$TMP_DNSMASQ_PATH/chinadns_fixed_server.conf" 2>"/dev/null" || \
  95. echo -e "address=/$host/$ip" >> "$TMP_DNSMASQ_PATH/chinadns_fixed_server.conf"
  96. fi
  97. echo $ip
  98. }
  99. clean_log() {
  100. local logsnum=$(cat $LOG_FILE 2>/dev/null | wc -l)
  101. [ "$logsnum" -gt 1000 ] && {
  102. echo "$(date "+%Y-%m-%d %H:%M:%S") 日志文件过长,清空处理!" >$LOG_FILE
  103. }
  104. }
  105. echolog() {
  106. local d="$(date "+%Y-%m-%d %H:%M:%S")"
  107. echo -e "$d: $*" >>$LOG_FILE
  108. }
  109. add_cron() {
  110. touch $CRON_FILE
  111. sed -i '/ssrplus.log/d' $CRON_FILE
  112. [ $(uci_get_by_type server_subscribe auto_update 0) -eq 1 ] && echo "$(uci_get_by_type server_subscribe auto_update_min_time) $(uci_get_by_type server_subscribe auto_update_day_time) * * $(uci_get_by_type server_subscribe auto_update_week_time) /usr/share/shadowsocksr/ssrplusupdate.sh >$LOG_FILE" >>$CRON_FILE
  113. crontab $CRON_FILE
  114. }
  115. del_cron() {
  116. touch $CRON_FILE
  117. sed -i '/ssrplus.log/d' $CRON_FILE
  118. crontab $CRON_FILE
  119. clean_log
  120. }
  121. set_lock() {
  122. exec 1000>"$LOCK_FILE"
  123. flock -xn 1000
  124. }
  125. unset_lock() {
  126. flock -u 1000
  127. rm -rf "$LOCK_FILE"
  128. }
  129. unlock() {
  130. failcount=1
  131. while [ "$failcount" -le 10 ]; do
  132. if [ -f "$LOCK_FILE" ]; then
  133. let "failcount++"
  134. sleep 1s
  135. [ "$failcount" -ge 10 ] && unset_lock
  136. else
  137. break
  138. fi
  139. done
  140. }
  141. _exit() {
  142. local rc=$1
  143. unset_lock
  144. exit ${rc}
  145. }
  146. first_type() {
  147. type -t -p "/bin/${1}" -p "${TMP_BIN_PATH}/${1}" -p "${1}" "$@" | head -n1
  148. }
  149. ln_start_bin() {
  150. local file_func=${1}
  151. local ln_name=${2}
  152. shift 2
  153. if [ "${file_func%%/*}" != "${file_func}" ]; then
  154. [ ! -L "${file_func}" ] && {
  155. ln -s "${file_func}" "${TMP_BIN_PATH}/${ln_name}" >/dev/null 2>&1
  156. file_func="${TMP_BIN_PATH}/${ln_name}"
  157. }
  158. [ -x "${file_func}" ] || echolog "$(readlink ${file_func}) 没有执行权限,无法启动:${file_func} $*"
  159. fi
  160. #echo "${file_func} $*" >&2
  161. [ -x "${file_func}" ] || {
  162. echolog "找不到 ${file_func},无法启动..."
  163. echolog "-----------end------------"
  164. _exit 2
  165. }
  166. ulimit -n 1000000
  167. ${file_func:-echolog " - ${ln_name}"} "$@" >/dev/null 2>&1 &
  168. }
  169. add_dns_into_ipset() {
  170. case "$1" in
  171. gfw) ipset add gfwlist ${2%:*} 2>/dev/null ;;
  172. oversea) ipset add oversea ${2%:*} 2>/dev/null ;;
  173. *) ipset add ss_spec_wan_ac ${2%:*} nomatch 2>/dev/null ;;
  174. esac
  175. }
  176. start_dns() {
  177. local ssrplus_dns="$(uci_get_by_type global pdnsd_enable 0)"
  178. local dnsserver="$(uci_get_by_type global tunnel_forward 8.8.4.4:53)"
  179. local run_mode="$(uci_get_by_type global run_mode)"
  180. if [ "$ssrplus_dns" != "0" ]; then
  181. if [ -n "$dnsserver" ]; then
  182. add_dns_into_ipset $run_mode $dnsserver
  183. fi
  184. case "$ssrplus_dns" in
  185. 1)
  186. ln_start_bin $(first_type dns2tcp) dns2tcp -L 127.0.0.1#$dns_port -R ${dnsserver/:/#}
  187. pdnsd_enable_flag=1
  188. ;;
  189. 2)
  190. ln_start_bin $(first_type microsocks) microsocks -i 127.0.0.1 -p $tmp_dns_port ssrplus-dns
  191. ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_dns_port $dnsserver 127.0.0.1:$dns_port -q
  192. pdnsd_enable_flag=2
  193. ;;
  194. 3)
  195. local mosdns_ipv6="$(uci_get_by_type global mosdns_ipv6)"
  196. local mosdns_dnsserver="$(uci_get_by_type global tunnel_forward_mosdns)"
  197. output=$(for i in $(echo $mosdns_dnsserver | sed "s/,/ /g"); do
  198. dnsserver=${i%:*}
  199. dnsserver=${i##*/}
  200. add_dns_into_ipset $run_mode $dnsserver
  201. echo " - addr: $i"
  202. echo " enable_pipeline: true"
  203. done)
  204. awk -v line=14 -v text="$output" 'NR == line+1 {print text} 1' /etc/ssrplus/mosdns-config.yaml | sed "s/DNS_PORT/$dns_port/g" > $TMP_PATH/mosdns-config.yaml
  205. if [ "$mosdns_ipv6" == "0" ]; then
  206. sed -i "s/DNS_MODE/main_sequence_with_IPv6/g" $TMP_PATH/mosdns-config.yaml
  207. else
  208. sed -i "s/DNS_MODE/main_sequence_disable_IPv6/g" $TMP_PATH/mosdns-config.yaml
  209. fi
  210. ln_start_bin $(first_type mosdns) mosdns start -c $TMP_PATH/mosdns-config.yaml
  211. pdnsd_enable_flag=3
  212. ;;
  213. esac
  214. if [ "$run_mode" = "router" ]; then
  215. local chinadns="$(uci_get_by_type global chinadns_forward)"
  216. if [ -n "$chinadns" ]; then
  217. local wandns="$(ifstatus wan | jsonfilter -e '@["dns-server"][0]' || echo "119.29.29.29")"
  218. case "$chinadns" in
  219. "wan") chinadns="$wandns" ;;
  220. "wan_114") chinadns="$wandns,114.114.114.114" ;;
  221. esac
  222. ln_start_bin $(first_type chinadns-ng) chinadns-ng -l $china_dns_port -4 china -p 3 -c ${chinadns/:/#} -t 127.0.0.1#$dns_port -N -f -r
  223. cat <<-EOF >> "$TMP_DNSMASQ_PATH/chinadns_fixed_server.conf"
  224. no-poll
  225. no-resolv
  226. server=127.0.0.1#$china_dns_port
  227. EOF
  228. fi
  229. fi
  230. fi
  231. if [ "$(uci_get_by_type global apple_optimization 1)" == "1" ]; then
  232. local new_appledns="$(uci_get_by_type global apple_dns)"
  233. if [ -n "$new_appledns" ]; then
  234. sed -i 's/[[:space:]]//g' /etc/ssrplus/applechina.conf #去除所有空白字符
  235. local old_appledns=$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' /etc/ssrplus/applechina.conf | sort -u)
  236. if [ -n "$old_appledns" ] && [ "$old_appledns" != "$new_appledns" ]; then
  237. sed -i "s,$(printf '%s' "$old_appledns"),$(printf '%s' "$new_appledns"),g" /etc/ssrplus/applechina.conf
  238. fi
  239. fi
  240. echolog "Apple 域名中国大陆 CDN 的 优化规则正在加载。"
  241. cp -f /etc/ssrplus/applechina.conf $TMP_DNSMASQ_PATH/
  242. echolog "Apple 域名中国大陆 CDN 的 优化规则加载完毕。"
  243. fi
  244. }
  245. gen_service_file() { #1-server.type 2-cfgname 3-file_path
  246. local fastopen
  247. if [ $(uci_get_by_name $2 fast_open) == "1" ]; then
  248. fastopen="true"
  249. else
  250. fastopen="false"
  251. fi
  252. case $1 in
  253. ssr)
  254. cat <<-EOF >$3
  255. {
  256. "server": "0.0.0.0",
  257. "server_ipv6": "::",
  258. "server_port": $(uci_get_by_name $2 server_port),
  259. "mode": "tcp_and_udp",
  260. "password": "$(uci_get_by_name $2 password)",
  261. "timeout": $(uci_get_by_name $2 timeout 60),
  262. "method": "$(uci_get_by_name $2 encrypt_method)",
  263. "protocol": "$(uci_get_by_name $2 protocol)",
  264. "protocol_param": "$(uci_get_by_name $2 protocol_param)",
  265. "obfs": "$(uci_get_by_name $2 obfs)",
  266. "obfs_param": "$(uci_get_by_name $2 obfs_param)",
  267. "fast_open": $fastopen
  268. }
  269. EOF
  270. ;;
  271. ss)
  272. cat <<-EOF >$3
  273. {
  274. "server": "0.0.0.0",
  275. "server_ipv6": "::",
  276. "server_port": $(uci_get_by_name $2 server_port),
  277. "mode": "tcp_and_udp",
  278. "password": "$(uci_get_by_name $2 password)",
  279. "timeout": $(uci_get_by_name $2 timeout 60),
  280. "method": "$(uci_get_by_name $2 encrypt_method_ss)",
  281. "protocol": "socks",
  282. "fast_open": $fastopen
  283. }
  284. EOF
  285. ;;
  286. esac
  287. }
  288. get_name() {
  289. case "$1" in
  290. ss) echo "Shadowsocks" ;;
  291. ssr) echo "ShadowsocksR" ;;
  292. esac
  293. }
  294. gen_config_file() { #server1 type2 code3 local_port4 socks_port5 chain6 threads5
  295. case "$3" in
  296. 1)
  297. config_file=$tcp_config_file
  298. chain_config_file=$(echo ${config_file}|sed 's/ssrplus\//ssrplus\/chain-/')
  299. ;;
  300. 2)
  301. config_file=$udp_config_file
  302. chain_config_file=$(echo ${config_file}|sed 's/ssrplus\//ssrplus\/chain-/')
  303. ;;
  304. 3)
  305. if [ -n "$tmp_local_port" ]; then
  306. local tmp_port=$tmp_local_port
  307. else
  308. local tmp_port=$tmp_shunt_local_port
  309. fi
  310. config_file=$shunt_config_file
  311. chain_config_file=$(echo ${config_file}|sed 's/ssrplus\//ssrplus\/chain-/')
  312. ;;
  313. 4)
  314. local ss_protocol="socks"
  315. config_file=$local_config_file
  316. chain_config_file=$(echo ${config_file}|sed 's/ssrplus\//ssrplus\/chain-/')
  317. ;;
  318. esac
  319. case "$2" in
  320. ss | ssr)
  321. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 ${ss_protocol:-redir} >$config_file
  322. if [ "$3" == "3" ]; then
  323. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $tmp_port socks >$shunt_dns_config_file
  324. fi
  325. ;;
  326. v2ray)
  327. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 $5 >$config_file
  328. ;;
  329. trojan)
  330. case "$3" in
  331. 1)
  332. lua /usr/share/shadowsocksr/gen_config.lua $1 nat $4 >$config_file
  333. ;;
  334. 2)
  335. lua /usr/share/shadowsocksr/gen_config.lua $1 client $4 >$config_file
  336. ;;
  337. 3)
  338. lua /usr/share/shadowsocksr/gen_config.lua $1 nat $4 >$config_file
  339. lua /usr/share/shadowsocksr/gen_config.lua $1 client $tmp_port >$shunt_dns_config_file
  340. ;;
  341. 4)
  342. lua /usr/share/shadowsocksr/gen_config.lua $1 client $4 >$config_file
  343. ;;
  344. esac
  345. ;;
  346. naiveproxy)
  347. case "$3" in
  348. 1)
  349. lua /usr/share/shadowsocksr/gen_config.lua $1 redir $4 >$config_file
  350. ;;
  351. 3)
  352. lua /usr/share/shadowsocksr/gen_config.lua $1 redir $4 >$config_file
  353. lua /usr/share/shadowsocksr/gen_config.lua $1 socks $tmp_port >$shunt_dns_config_file
  354. ;;
  355. 4)
  356. lua /usr/share/shadowsocksr/gen_config.lua $1 socks $4 >$config_file
  357. ;;
  358. esac
  359. ;;
  360. hysteria)
  361. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 $5 >$config_file
  362. ;;
  363. tuic)
  364. case "$3" in
  365. 1|2|4)
  366. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 >$config_file
  367. ;;
  368. 3)
  369. [ -z "$6" ] && lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 >$shunt_dns_config_file || lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 >$config_file
  370. ;;
  371. esac
  372. ;;
  373. shadowtls)
  374. case "$3" in
  375. 1|2|4)
  376. [ -z "$6" ] && lua /usr/share/shadowsocksr/gen_config.lua $1 $type $4 >$chain_config_file || lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 $5 $6 >$config_file
  377. ;;
  378. 3)
  379. lua /usr/share/shadowsocksr/gen_config.lua $1 $type $4 >$chain_config_file
  380. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 $5 $6 >$config_file
  381. ;;
  382. esac
  383. ;;
  384. socks5)
  385. /usr/share/shadowsocksr/genred2config.sh $config_file $2 $mode $4 \
  386. "$(uci_get_by_name $1 server)" \
  387. "$(uci_get_by_name $1 server_port)" \
  388. "$(uci_get_by_name $1 auth_enable 0)" \
  389. "$(uci_get_by_name $1 username)" \
  390. "$(uci_get_by_name $1 password)"
  391. ;;
  392. tun)
  393. /usr/share/shadowsocksr/genred2config.sh $config_file $2 $(uci_get_by_name $1 iface "br-lan") $4
  394. ;;
  395. esac
  396. sed -i 's/\\//g' $TMP_PATH/*-ssr-*.json #>/dev/null > 2>&1
  397. }
  398. start_udp() {
  399. local type=$(uci_get_by_name $UDP_RELAY_SERVER type)
  400. redir_udp=1
  401. case "$type" in
  402. ss | ssr)
  403. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_port
  404. ss_program="$(first_type ${type}local ${type}-redir)"
  405. ln_start_bin $ss_program ${type}-redir -c $udp_config_file
  406. echolog "UDP TPROXY Relay:$(get_name $type) Started!"
  407. ;;
  408. v2ray)
  409. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_port
  410. ln_start_bin $(first_type xray v2ray) v2ray run -c $udp_config_file
  411. echolog "UDP TPROXY Relay:$($(first_type "xray" "v2ray") version | head -1) Started!"
  412. ;;
  413. trojan) #client
  414. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_local_port
  415. ln_start_bin $(first_type trojan) $type --config $udp_config_file
  416. ln_start_bin $(first_type ipt2socks) ipt2socks -U -b 0.0.0.0 -4 -s 127.0.0.1 -p $tmp_udp_local_port -l $tmp_udp_port
  417. echolog "UDP TPROXY Relay:$($(first_type trojan) --version 2>&1 | head -1) Started!"
  418. ;;
  419. naiveproxy)
  420. echolog "NaïveProxy UDP TPROXY Relay not supported!"
  421. redir_udp=0
  422. ARG_UDP=""
  423. ;;
  424. hysteria)
  425. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_port
  426. ln_start_bin $(first_type hysteria) hysteria client --config $udp_config_file
  427. echolog "UDP TPROXY Relay:$($(first_type "hysteria") version | grep Version | awk '{print "Hysteria2: " $2}') Started!"
  428. ;;
  429. tuic)
  430. # FIXME: ipt2socks cannot handle udp reply from tuic
  431. # 20230726 uncomment following 4 lines
  432. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_local_port
  433. ln_start_bin $(first_type tuic-client) tuic-client --config $udp_config_file
  434. ln_start_bin $(first_type ipt2socks) ipt2socks -U -b 0.0.0.0 -4 -s 127.0.0.1 -p $tmp_udp_local_port -l $tmp_udp_port
  435. echolog "UDP TPROXY Relay:tuic-client $($(first_type tuic-client) --version) Started!"
  436. echolog "TUIC UDP TPROXY Relay not supported!"
  437. #redir_udp=0
  438. #ARG_UDP=""
  439. ;;
  440. shadowtls)
  441. gen_config_file $UDP_RELAY_SERVER $type 2 ${tmp_udp_local_port}
  442. gen_config_file $UDP_RELAY_SERVER $type 2 ${tmp_udp_local_port} 0 chain
  443. ln_start_bin $(first_type shadow-tls) shadow-tls config --config $chain_config_file
  444. local chain_type=$(uci_get_by_name $UDP_RELAY_SERVER chain_type)
  445. case ${chain_type} in
  446. vmess)
  447. ln_start_bin $(first_type xray v2ray) v2ray run -c $udp_config_file
  448. echolog "UDP TPROXY Relay:shadow-tls chain-to $($(first_type xray) --version) Started!"
  449. ;;
  450. sslocal)
  451. ln_start_bin $(first_type sslocal) sslocal -c $udp_config_file
  452. echolog "UDP TPROXY Relay:shadow-tls chain-to $($(first_type sslocal) --version) Started!"
  453. ;;
  454. esac
  455. ;;
  456. socks5)
  457. # if [ "$(uci_get_by_name $UDP_RELAY_SERVER auth_enable 0)" == "1" ]; then
  458. # local auth="-a $(uci_get_by_name $UDP_RELAY_SERVER username) -k $(uci_get_by_name $UDP_RELAY_SERVER password)"
  459. # fi
  460. # ln_start_bin $(first_type ipt2socks) ipt2socks $udp_config_file -U -4 -s $(uci_get_by_name $UDP_RELAY_SERVER server) -p $(uci_get_by_name $UDP_RELAY_SERVER server_port) -l $tmp_udp_port $auth
  461. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_port
  462. ln_start_bin $(first_type redsocks2) redsocks2 -c $udp_config_file
  463. echolog "UDP TPROXY Relay:Socks5 REDIRECT/TPROXY Started!"
  464. ;;
  465. tun)
  466. echolog "Network Tunnel UDP TPROXY Relay not supported!"
  467. redir_udp=0
  468. ARG_UDP=""
  469. ;;
  470. esac
  471. }
  472. shunt_dns_command() {
  473. local shunt_dns_mode="$(uci_get_by_type global shunt_dns_mode)"
  474. local shunt_dnsserver="$(uci_get_by_type global shunt_dnsserver)"
  475. local tmp_port=$1
  476. case "$shunt_dns_mode" in
  477. 1)
  478. ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_port $shunt_dnsserver 127.0.0.1:$tmp_shunt_dns_port -q
  479. ;;
  480. 2)
  481. local shunt_mosdns_ipv6="$(uci_get_by_type global shunt_mosdns_ipv6)"
  482. local shunt_mosdns_dnsserver="$(uci_get_by_type global shunt_mosdns_dnsserver)"
  483. output=$(for i in $(echo $shunt_mosdns_dnsserver | sed "s/,/ /g"); do
  484. echo " - addr: $i"
  485. echo " socks5: \"127.0.0.1:$tmp_port\""
  486. echo " enable_pipeline: true"
  487. done)
  488. awk -v line=14 -v text="$output" 'NR == line+1 {print text} 1' /etc/ssrplus/mosdns-config.yaml | sed "s/DNS_PORT/$tmp_shunt_dns_port/g" > $TMP_PATH/mosdns-config-shunt.yaml
  489. if [ "$shunt_mosdns_ipv6" == "0" ]; then
  490. sed -i "s/DNS_MODE/main_sequence_with_IPv6/g" $TMP_PATH/mosdns-config-shunt.yaml
  491. else
  492. sed -i "s/DNS_MODE/main_sequence_disable_IPv6/g" $TMP_PATH/mosdns-config-shunt.yaml
  493. fi
  494. ln_start_bin $(first_type mosdns) mosdns start -c $TMP_PATH/mosdns-config-shunt.yaml
  495. ;;
  496. esac
  497. }
  498. shunt_dns_config_file_port() {
  499. if [ "$LOCAL_SERVER" == "$SHUNT_SERVER" ]; then
  500. # NetFlix 和 全局socks 节点相同
  501. if [ "$(uci_get_by_type socks5_proxy socks5_auth nil)" != "noauth" ]; then
  502. # 全局socks 有密码,NetFlix 不能使用 auth 验证,需更换为新端口并使用无密码的 socks 配置用于分流
  503. # 新增NetFlix dns 使用端口
  504. local port=$tmp_shunt_local_port
  505. jq --arg port "$port" '.inbounds |= .[0:1] + [{"protocol":"socks","port":($port | tonumber),"settings":{"udp":true,"auth":"noauth"}}] + .[1:]' "$shunt_config_file" > "$shunt_config_file.tmp" && mv "$shunt_config_file.tmp" $shunt_config_file
  506. echo $port # 返回端口号
  507. return 0 # 成功返回
  508. else
  509. sed -i -e '/"mixed"/d' $shunt_config_file
  510. fi
  511. else
  512. # NetFlix 和 全局 socks 节点不相同
  513. if [ "$(uci_get_by_type socks5_proxy socks5_auth nil)" != "noauth" ]; then
  514. # 全局socks 有密码,NetFlix不能使用auth验证,需设置为无密码的socks配置用于分流
  515. # 删除 NetFlix dns 端口密码验证
  516. sed -i \
  517. -e '/"mixed"/d' \
  518. -e 's/"auth"\s*:\s*"password"/\"auth\": \"noauth\"/g' \
  519. -e '/"accounts": \[/,/\]/d' $shunt_config_file
  520. else
  521. sed -i -e '/"mixed"/d' $shunt_config_file
  522. fi
  523. fi
  524. # 使用传入的端口
  525. echo $1 # 返回传入的端口号
  526. return 0 # 成功返回
  527. }
  528. start_shunt() {
  529. local type=$(uci_get_by_name $SHUNT_SERVER type)
  530. case "$type" in
  531. ss | ssr)
  532. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  533. ss_program="$(first_type ${type}local ${type}-redir)"
  534. ln_start_bin $ss_program ${type}-redir -c $shunt_config_file
  535. if [ -n "$tmp_local_port" ]; then
  536. local tmp_port=$tmp_local_port
  537. else
  538. local tmp_port=$tmp_shunt_local_port
  539. ln_start_bin $(first_type ${type}local ${type}-local) ${type}-local -c $shunt_dns_config_file
  540. fi
  541. shunt_dns_command $tmp_port
  542. echolog "shunt:$(get_name $type) Started!"
  543. ;;
  544. v2ray)
  545. local tmp_port=${tmp_local_port:-$tmp_shunt_local_port}
  546. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port $tmp_port
  547. # 处理配置文件中的 NetFlix 端口
  548. tmp_port=$(shunt_dns_config_file_port $tmp_port)
  549. ln_start_bin $(first_type xray v2ray) v2ray run -c $shunt_config_file
  550. shunt_dns_command $tmp_port
  551. echolog "shunt:$($(first_type xray v2ray) version | head -1) Started!"
  552. ;;
  553. trojan)
  554. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  555. ln_start_bin $(first_type trojan) $type --config $shunt_config_file
  556. if [ -n "$tmp_local_port" ]; then
  557. local tmp_port=$tmp_local_port
  558. else
  559. local tmp_port=$tmp_shunt_local_port
  560. ln_start_bin $(first_type trojan) $type --config $shunt_dns_config_file
  561. fi
  562. shunt_dns_command $tmp_port
  563. echolog "shunt:$($(first_type trojan) --version 2>&1 | head -1) Started!"
  564. ;;
  565. naiveproxy)
  566. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  567. ln_start_bin $(first_type naive) naive --config $shunt_config_file
  568. if [ -n "$tmp_local_port" ]; then
  569. local tmp_port=$tmp_local_port
  570. else
  571. local tmp_port=$tmp_shunt_local_port
  572. ln_start_bin $(first_type naive) naive --config $shunt_dns_config_file
  573. fi
  574. shunt_dns_command $tmp_port
  575. echolog "shunt:$($(first_type "naive") --version 2>&1 | head -1) Started!"
  576. redir_udp=0
  577. ;;
  578. hysteria)
  579. if [ -n "$tmp_local_port" ]; then
  580. local tmp_port=$tmp_local_port
  581. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  582. else
  583. local tmp_port=$tmp_shunt_local_port
  584. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port $tmp_port
  585. fi
  586. ln_start_bin $(first_type hysteria) hysteria client --config $shunt_config_file
  587. shunt_dns_command $tmp_port
  588. echolog "shunt:$($(first_type hysteria) version | grep Version | awk '{print "Hysteria2: " $2}') Started!"
  589. ;;
  590. tuic)
  591. local chain_shunt_port="30${tmp_shunt_port}"
  592. gen_config_file $SHUNT_SERVER $type 3 $chain_shunt_port 0 chain #make a tuic socks:30303, make a ipt2socks redir:303
  593. ln_start_bin $(first_type tuic-client) tuic-client --config $shunt_config_file
  594. ln_start_bin $(first_type ipt2socks) ipt2socks -R -b 0.0.0.0 -4 -s 127.0.0.1 -p $chain_shunt_port -l $tmp_shunt_port
  595. [ -n "$tmp_local_port" ] && tmp_port=$tmp_local_port || tmp_port=$tmp_shunt_local_port
  596. gen_config_file $SHUNT_SERVER $type 3 $tmp_port # make a tuic socks :304
  597. ln_start_bin $(first_type tuic-client) tuic-client --config $shunt_dns_config_file
  598. shunt_dns_command $tmp_port
  599. echolog "Netflix Separated Shunt Server:tuic-client $($(first_type tuic-client) --version) Started!"
  600. # FIXME: ipt2socks cannot handle udp reply from tuic
  601. #redir_udp=0
  602. ;;
  603. shadowtls)
  604. [ -n "$tmp_local_port" ] && tmp_port=$tmp_local_port || tmp_port=$tmp_shunt_local_port
  605. gen_config_file $SHUNT_SERVER $type 3 "10${tmp_shunt_port}" $tmp_port chain/$tmp_shunt_port #make a redir:303 and a socks:304
  606. #echo "debug \$tmp_port=$tmp_port, \$tmp_shunt_port=${tmp_shunt_port}, \$tmp_shunt_local_port=$tmp_shunt_local_port"
  607. ln_start_bin $(first_type shadow-tls) shadow-tls config --config $chain_config_file
  608. shunt_dns_command $tmp_port
  609. local chain_type=$(uci_get_by_name $SHUNT_SERVER chain_type)
  610. case ${chain_type} in
  611. vmess)
  612. ln_start_bin $(first_type xray v2ray) v2ray run -c $shunt_config_file
  613. echolog "Netflix Separated Shunt Server:shadow-tls chain-to$($(first_type xray) --version) Started!"
  614. ;;
  615. sslocal)
  616. ln_start_bin $(first_type sslocal) sslocal -c $shunt_config_file
  617. echolog "Netflix Separated Shunt Server:shadow-tls chain-to$($(first_type sslocal) --version) Started!"
  618. ;;
  619. esac
  620. ;;
  621. # socks5)
  622. # if [ "$(uci_get_by_name $SHUNT_SERVER auth_enable 0)" == "1" ]; then
  623. # local auth="-a $(uci_get_by_name $SHUNT_SERVER username) -k $(uci_get_by_name $SHUNT_SERVER password)"
  624. # fi
  625. # ln_start_bin $(first_type ipt2socks) ipt2socks $shunt_config_file -R -4 -s $(uci_get_by_name $SHUNT_SERVER server) -p $(uci_get_by_name $SHUNT_SERVER server_port) -l $tmp_shunt_port $auth
  626. # #gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  627. # #ln_start_bin $(first_type redsocks2) redsocks2 -c $shunt_config_file
  628. # if [ -n "$tmp_local_port" ]; then
  629. # local tmp_port=$tmp_local_port
  630. # else
  631. # local tmp_port=$tmp_shunt_local_port
  632. # ln_start_bin $(first_type microsocks) microsocks -i 127.0.0.1 -p $tmp_port shunt-dns-ssr-plus
  633. # fi
  634. # shunt_dns_command $tmp_port
  635. # echolog "shunt:$type REDIRECT/TPROXY Started!"
  636. # ;;
  637. *)
  638. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  639. ln_start_bin $(first_type redsocks2) redsocks2 -c $shunt_config_file
  640. if [ -n "$tmp_local_port" ]; then
  641. local tmp_port=$tmp_local_port
  642. else
  643. local tmp_port=$tmp_shunt_local_port
  644. ln_start_bin $(first_type microsocks) microsocks -i 127.0.0.1 -p $tmp_port shunt-dns-ssr-plus
  645. fi
  646. shunt_dns_command $tmp_port
  647. echolog "shunt:$type REDIRECT/TPROXY Started!"
  648. ;;
  649. esac
  650. return 0
  651. }
  652. start_local() {
  653. [ "$LOCAL_SERVER" = "nil" ] && return 1
  654. local local_port=$(uci_get_by_type socks5_proxy local_port)
  655. [ "$LOCAL_SERVER" == "$SHUNT_SERVER" ] && tmp_local_port=$local_port
  656. local type=$(uci_get_by_name $LOCAL_SERVER type)
  657. case "$type" in
  658. ss | ssr)
  659. gen_config_file $LOCAL_SERVER $type 4 $local_port
  660. ss_program="$(first_type ${type}local ${type}-local)"
  661. ln_start_bin $ss_program ${type}-local -c $local_config_file
  662. echolog "Global_Socks5:$(get_name $type) Started!"
  663. ;;
  664. v2ray)
  665. if [ "$_local" == "2" ]; then
  666. gen_config_file $LOCAL_SERVER $type 4 0 $local_port
  667. ln_start_bin $(first_type xray v2ray) v2ray run -c $local_config_file
  668. fi
  669. echolog "Global_Socks5:$($(first_type "xray" "v2ray") version | head -1) Started!"
  670. ;;
  671. trojan) #client
  672. gen_config_file $LOCAL_SERVER $type 4 $local_port
  673. ln_start_bin $(first_type trojan) $type --config $local_config_file
  674. echolog "Global_Socks5:$($(first_type trojan) --version 2>&1 | head -1) Started!"
  675. ;;
  676. naiveproxy)
  677. gen_config_file $LOCAL_SERVER $type 4 $local_port
  678. ln_start_bin $(first_type naive) naive --config $local_config_file
  679. echolog "Global_Socks5:$($(first_type naive) --version | head -1) Started!"
  680. ;;
  681. hysteria)
  682. if [ "$_local" == "2" ]; then
  683. gen_config_file $LOCAL_SERVER $type 4 0 $local_port
  684. ln_start_bin $(first_type hysteria) hysteria client --config $local_config_file
  685. echolog "Global_Socks5:$($(first_type hysteria) version | grep Version | awk '{print "Hysteria2: " $2}') Started!"
  686. fi
  687. ;;
  688. tuic)
  689. if [ "$_local" == "2" ]; then
  690. gen_config_file $LOCAL_SERVER $type 4 $local_port
  691. ln_start_bin $(first_type tuic-client) tuic-client --config $local_config_file
  692. echolog "Global Socks5:tuic-client $($(first_type tuic-client) --version) Started!"
  693. fi
  694. ;;
  695. shadowtls)
  696. #respective config for global socks and main node
  697. if [ "$_local" == "2" ]; then
  698. gen_config_file $LOCAL_SERVER $type 4 "10${tmp_tcp_local_port}"
  699. gen_config_file $LOCAL_SERVER $type 4 0 $local_port chain/"10${tmp_tcp_local_port}"
  700. ln_start_bin $(first_type shadow-tls) shadow-tls config --config $chain_local_config_file
  701. local chain_type=$(uci_get_by_name $LOCAL_SERVER chain_type)
  702. case ${chain_type} in
  703. vmess)
  704. ln_start_bin $(first_type xray v2ray) v2ray run -c $local_config_file
  705. echolog "Global Socks5 Proxy:shadow-tls chain-to$($(first_type xray) --version) Started!"
  706. ;;
  707. sslocal)
  708. ln_start_bin $(first_type sslocal) sslocal -c $local_config_file
  709. echolog "Global Socks5 Proxy:shadow-tls chain-to$($(first_type sslocal) --version) Started!"
  710. ;;
  711. esac
  712. fi
  713. ;;
  714. *)
  715. [ -e /proc/sys/net/ipv6 ] && local listenip='-i ::'
  716. ln_start_bin $(first_type microsocks) microsocks $listenip -p $local_port tcp-udp-ssr-local
  717. echolog "Global_Socks5:$type Started!"
  718. ;;
  719. esac
  720. local_enable=1
  721. return 0
  722. }
  723. Start_Run() {
  724. if [ "$(uci_get_by_type global threads 0)" == "0" ]; then
  725. local threads=$(cat /proc/cpuinfo | grep 'processor' | wc -l)
  726. else
  727. local threads=$(uci_get_by_type global threads)
  728. fi
  729. if [ "$(uci_get_by_name $GLOBAL_SERVER kcp_enable 0)" == "1" ]; then
  730. [ ! -f "/usr/bin/kcptun-client" ] && return 1
  731. local kcp_str=$(/usr/bin/kcptun-client -v | grep kcptun | wc -l)
  732. [ "0" == "$kcp_str" ] && return 1
  733. local kcp_server=$(uci_get_by_name $GLOBAL_SERVER server)
  734. local kcp_port=$(uci_get_by_name $GLOBAL_SERVER kcp_port)
  735. local server_port=$(uci_get_by_name $GLOBAL_SERVER server_port)
  736. local password=$(uci_get_by_name $GLOBAL_SERVER kcp_password)
  737. local kcp_param=$(uci_get_by_name $GLOBAL_SERVER kcp_param)
  738. [ "$password" != "" ] && password="--key "$password
  739. service_start /usr/bin/kcptun-client -r $kcp_server:$kcp_port -l :$server_port $password $kcp_param
  740. kcp_enable_flag=1
  741. ARG_UDP=""
  742. fi
  743. if [ "$_local" == "1" ]; then
  744. local socks_port=$(uci_get_by_type socks5_proxy local_port)
  745. tcp_config_file=$TMP_PATH/local-ssr-retcp.json
  746. [ "$mode" == "tcp,udp" ] && tcp_config_file=$TMP_PATH/local-udp-ssr-retcp.json
  747. fi
  748. local tcp_port=$(uci_get_by_name $GLOBAL_SERVER local_port)
  749. local type=$(uci_get_by_name $GLOBAL_SERVER type)
  750. case "$type" in
  751. ss | ssr)
  752. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port
  753. ss_program="$(first_type ${type}local ${type}-redir)"
  754. for i in $(seq 1 $threads); do
  755. ln_start_bin "$ss_program" ${type}-redir -c $tcp_config_file
  756. done
  757. echolog "Main node:$(get_name $type) $threads Threads Started!"
  758. ;;
  759. v2ray)
  760. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port $socks_port
  761. ln_start_bin $(first_type xray v2ray) v2ray run -c $tcp_config_file
  762. echolog "Main node:$($(first_type xray v2ray) version | head -1) Started!"
  763. ;;
  764. trojan)
  765. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port
  766. for i in $(seq 1 $threads); do
  767. ln_start_bin $(first_type $type) $type --config $tcp_config_file
  768. done
  769. echolog "Main node:$($(first_type $type) --version 2>&1 | head -1) , $threads Threads Started!"
  770. ;;
  771. naiveproxy)
  772. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port
  773. ln_start_bin $(first_type naive) naive $tcp_config_file
  774. echolog "Main node:$($(first_type naive) --version 2>&1 | head -1) , $threads Threads Started!"
  775. ;;
  776. hysteria)
  777. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port $socks_port
  778. ln_start_bin $(first_type hysteria) hysteria client --config $tcp_config_file
  779. echolog "Main node:$($(first_type hysteria) version | grep Version | awk '{print "Hysteria2: " $2}') Started!"
  780. ;;
  781. tuic)
  782. local PARAM
  783. [ $mode == "tcp" ] && PARAM="-T" || PARAM=""
  784. gen_config_file $GLOBAL_SERVER $type 1 $tmp_tcp_local_port
  785. ln_start_bin $(first_type tuic-client) tuic-client --config $tcp_config_file
  786. ln_start_bin $(first_type ipt2socks) ipt2socks "$PARAM" -R -b 0.0.0.0 -4 -s 127.0.0.1 -p $tmp_tcp_local_port -l $tcp_port
  787. if [ -n $socks_port ] && [ $GLOBAL_SERVER == $LOCAL_SERVER ]; then #start a new tuic instance
  788. gen_config_file $GLOBAL_SERVER $type 4 $socks_port
  789. ln_start_bin $(first_type tuic-client) tuic-client --config $local_config_file
  790. echolog "Global Socks5:tuic-client $($(first_type tuic-client) --version) Started!"
  791. fi
  792. echolog "Main node:tuic-client $($(first_type tuic-client) --version) Started!"
  793. ;;
  794. shadowtls)
  795. if [ -z "$socks_port" ]; then
  796. gen_config_file $GLOBAL_SERVER $type 1 "10${tmp_tcp_local_port}"
  797. gen_config_file $GLOBAL_SERVER $type 1 "10${tmp_tcp_local_port}" 0 chain
  798. else
  799. gen_config_file $GLOBAL_SERVER $type 1 "10${tmp_tcp_local_port}"
  800. gen_config_file $GLOBAL_SERVER $type 1 "10${tmp_tcp_local_port}" $socks_port chain
  801. fi
  802. local chain_type=$(uci_get_by_name $GLOBAL_SERVER chain_type)
  803. case ${chain_type} in
  804. vmess)
  805. ln_start_bin $(first_type shadow-tls) shadow-tls config --config $chain_config_file
  806. ln_start_bin $(first_type xray v2ray) v2ray run -c $tcp_config_file
  807. echolog "Mian node:shadow-tls chain-to $($(first_type xray) --version) Started!"
  808. ;;
  809. sslocal)
  810. ln_start_bin $(first_type shadow-tls) shadow-tls config --config $chain_config_file
  811. ln_start_bin $(first_type sslocal) sslocal -c $tcp_config_file
  812. echolog "Main node:shadow-tls chain-to $($(first_type sslocal) --version) Started!"
  813. ;;
  814. esac
  815. ;;
  816. socks5)
  817. if [ "$(uci_get_by_name $GLOBAL_SERVER auth_enable 0)" == "1" ]; then
  818. local auth="-a $(uci_get_by_name $GLOBAL_SERVER username) -k $(uci_get_by_name $GLOBAL_SERVER password)"
  819. fi
  820. ln_start_bin $(first_type ipt2socks) ipt2socks $tcp_config_file -R -4 -j $threads -s $(uci_get_by_name $GLOBAL_SERVER server) -p $(uci_get_by_name $GLOBAL_SERVER server_port) -l $tcp_port $auth
  821. #gen_config_file $GLOBAL_SERVER $type 1 $tcp_port
  822. #for i in $(seq 1 $threads); do
  823. # ln_start_bin $(first_type redsocks2) redsocks2 -c $tcp_config_file
  824. #done
  825. echolog "Main node:Socks5 REDIRECT/TPROXY $threads Threads Started!"
  826. ;;
  827. tun)
  828. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port
  829. for i in $(seq 1 $threads); do
  830. ln_start_bin $(first_type redsocks2) redsocks2 -c $tcp_config_file
  831. done
  832. echolog "Main node:Network Tunnel REDIRECT $threads Threads Started!"
  833. ;;
  834. esac
  835. redir_tcp=1
  836. return 0
  837. }
  838. load_config() {
  839. if [ -z "$switch_server" ]; then
  840. GLOBAL_SERVER=$(uci_get_by_type global global_server nil)
  841. else
  842. GLOBAL_SERVER=$switch_server
  843. fi
  844. if [ "$(uci_get_by_type socks5_proxy enabled 0)" == "1" ]; then
  845. # 只有开启 全局socks 才需要取值
  846. LOCAL_SERVER=$(uci_get_by_type socks5_proxy server nil)
  847. else
  848. # 没有开启 设置为 nil
  849. LOCAL_SERVER=nil
  850. fi
  851. if [ "$GLOBAL_SERVER" == "nil" ]; then
  852. mode="tcp,udp"
  853. _local="2"
  854. local_config_file=$TMP_PATH/tcp-udp-ssr-local.json
  855. start_local
  856. return 1
  857. fi
  858. UDP_RELAY_SERVER=$(uci_get_by_type global udp_relay_server nil)
  859. if [ "$(uci_get_by_type global netflix_enable 0)" == "1" ]; then
  860. # 只有开启 NetFlix分流 才需要取值
  861. SHUNT_SERVER=$(uci_get_by_type global netflix_server nil)
  862. else
  863. # 没有开启 设置为 nil
  864. SHUNT_SERVER=nil
  865. fi
  866. tcp_config_file=$TMP_PATH/tcp-only-ssr-retcp.json
  867. case "$UDP_RELAY_SERVER" in
  868. nil)
  869. mode="tcp"
  870. ;;
  871. $GLOBAL_SERVER | same)
  872. mode="tcp,udp"
  873. tcp_config_file=$TMP_PATH/tcp-udp-ssr-retcp.json
  874. ARG_UDP="-u"
  875. UDP_RELAY_SERVER=$GLOBAL_SERVER
  876. ;;
  877. *)
  878. mode="udp"
  879. udp_config_file=$TMP_PATH/udp-only-ssr-reudp.json
  880. ARG_UDP="-U"
  881. start_udp
  882. mode="tcp"
  883. ;;
  884. esac
  885. case "$LOCAL_SERVER" in
  886. nil)
  887. _local="0"
  888. ;;
  889. $GLOBAL_SERVER | same)
  890. _local="1"
  891. LOCAL_SERVER=$GLOBAL_SERVER
  892. local_config_file=$TMP_PATH/tcp-udp-ssr-local.json
  893. start_local
  894. local_enable=0
  895. ;;
  896. $SHUNT_SERVER)
  897. _local="3"
  898. local_config_file=$TMP_PATH/tcp-udp-ssr-local.json
  899. start_local
  900. ;;
  901. *)
  902. _local="2"
  903. local_config_file=$TMP_PATH/tcp-udp-ssr-local.json
  904. start_local
  905. ;;
  906. esac
  907. case "$SHUNT_SERVER" in
  908. nil)
  909. shunt="0"
  910. ;;
  911. $GLOBAL_SERVER | same)
  912. shunt="1"
  913. SHUNT_SERVER=$GLOBAL_SERVER
  914. ;;
  915. $LOCAL_SERVER)
  916. shunt="$tmp_shunt_port"
  917. shunt_config_file=$TMP_PATH/tcp-udp-ssr-local.json
  918. shunt_dns_config_file=$TMP_PATH/shunt-dns-ssr-plus.json
  919. start_shunt
  920. ;;
  921. *)
  922. shunt="$tmp_shunt_port"
  923. shunt_config_file=$TMP_PATH/shunt-ssr-retcp.json
  924. shunt_dns_config_file=$TMP_PATH/shunt-dns-ssr-plus.json
  925. start_shunt
  926. ;;
  927. esac
  928. return 0
  929. }
  930. check_server() {
  931. ENABLE_SERVER=$(uci_get_by_type global global_server nil)
  932. if [ "$ENABLE_SERVER" == "nil" ]; then
  933. return 1
  934. else
  935. local STYPE=$(uci_get_by_name $ENABLE_SERVER type nil)
  936. if [ "$STYPE" == "nil" ]; then
  937. local CFGID=$(uci_get_by_cfgid servers type nil)
  938. if [ "$CFGID" == "nil" ]; then
  939. uci_set_by_type global global_server 'nil'
  940. else
  941. uci_set_by_type global global_server $CFGID
  942. fi
  943. /etc/init.d/shadowsocksr restart
  944. fi
  945. fi
  946. }
  947. start_server() {
  948. [ "$(uci_get_by_type server_global enable_server 0)" == "0" ] && return 0
  949. server_service() {
  950. [ "$(uci_get_by_name $1 enable 0)" == "0" ] && return 1
  951. let server_count=server_count+1
  952. if [ "$server_count" == "1" ]; then
  953. if ! (iptables-save -t filter | grep SSR-SERVER-RULE >/dev/null); then
  954. iptables -N SSR-SERVER-RULE && iptables -t filter -I INPUT -j SSR-SERVER-RULE
  955. fi
  956. fi
  957. local type=$(uci_get_by_name $1 type)
  958. case "$type" in
  959. ss | ssr)
  960. gen_service_file ${type} $1 $TMP_PATH/ssr-server$server_count.json
  961. ln_start_bin $(first_type ${type}server ${type}-server) ${type}-server -c $TMP_PATH/ssr-server$server_count.json
  962. echolog "Server: $(get_name ${type}) Server$server_count Started!"
  963. ;;
  964. socks5)
  965. [ -e /proc/sys/net/ipv6 ] && local listenip='-i ::'
  966. ln_start_bin $(first_type microsocks) microsocks $listenip -p $(uci_get_by_name $1 server_port) -1 -u $(uci_get_by_name $1 username) -P $(uci_get_by_name $1 password) ssr-server$server_count
  967. echolog "Server:Socks5 Server$server_count Started!"
  968. ;;
  969. esac
  970. iptables -t filter -A SSR-SERVER-RULE -p tcp --dport $(uci_get_by_name $1 server_port) -j ACCEPT
  971. iptables -t filter -A SSR-SERVER-RULE -p udp --dport $(uci_get_by_name $1 server_port) -j ACCEPT
  972. return 0
  973. }
  974. gen_serv_include() {
  975. local FWI=$(uci get firewall.shadowsocksr.path 2>/dev/null)
  976. [ -n "$FWI" ] || return 0
  977. if [ ! -f $FWI ]; then
  978. echo '#!/bin/sh' >$FWI
  979. fi
  980. extract_rules() {
  981. echo "*filter"
  982. iptables-save -t filter | grep SSR-SERVER-RULE | sed -e "s/^-A INPUT/-I INPUT/"
  983. echo 'COMMIT'
  984. }
  985. cat <<-EOF >>$FWI
  986. iptables-save -c | grep -v "SSR-SERVER" | iptables-restore -c
  987. iptables-restore -n <<-EOT
  988. $(extract_rules)
  989. EOT
  990. EOF
  991. }
  992. config_load $NAME
  993. config_foreach server_service server_config
  994. gen_serv_include
  995. return 0
  996. }
  997. start_switch() {
  998. if [ "$(uci_get_by_type global enable_switch 0)" == "1" ]; then
  999. if [ -z "$switch_server" ]; then
  1000. local switch_time=$(uci_get_by_type global switch_time)s
  1001. local switch_timeout=$(uci_get_by_type global switch_timeout)
  1002. service_start /usr/bin/ssr-switch start $switch_time $switch_timeout
  1003. fi
  1004. fi
  1005. }
  1006. start_monitor() {
  1007. if [ $(uci_get_by_type global monitor_enable 1) == "1" ]; then
  1008. let total_count=server_count+redir_tcp+redir_udp+kcp_enable_flag+local_enable+pdnsd_enable_flag
  1009. if [ $total_count -gt 0 ]; then
  1010. service_start /usr/bin/ssr-monitor $server_count $redir_tcp $redir_udp $kcp_enable_flag $local_enable $pdnsd_enable_flag
  1011. fi
  1012. fi
  1013. }
  1014. start_rules() {
  1015. local server=$(get_host_ip $GLOBAL_SERVER)
  1016. local local_port=$(uci_get_by_name $GLOBAL_SERVER local_port)
  1017. local lan_ac_ips=$(uci_get_by_type access_control lan_ac_ips)
  1018. local lan_ac_mode=$(uci_get_by_type access_control lan_ac_mode)
  1019. if [ "$kcp_enable_flag" == "0" -a "$redir_udp" == "1" ]; then
  1020. local udp_server=$(get_host_ip $UDP_RELAY_SERVER)
  1021. local udp_local_port=$tmp_udp_port
  1022. fi
  1023. if [ "$shunt" != "0" ]; then
  1024. local shunt_ip=$(get_host_ip $SHUNT_SERVER)
  1025. fi
  1026. if [ -n "$lan_ac_ips" ]; then
  1027. case "$lan_ac_mode" in
  1028. w | W | b | B) local ac_ips="$lan_ac_mode$lan_ac_ips" ;;
  1029. esac
  1030. fi
  1031. gfwmode() {
  1032. case "$(uci_get_by_type global run_mode)" in
  1033. gfw) echo "-g" ;;
  1034. router) echo "-r" ;;
  1035. oversea) echo "-c" ;;
  1036. all) echo "-z" ;;
  1037. esac
  1038. }
  1039. if [ "$(uci_get_by_type global dports)" == "3" ]; then
  1040. local custom_ports=$(uci_get_by_name $GLOBAL_SERVER custom_ports) # custom_ports 存储了用户自定义的端口
  1041. if [ -n "$custom_ports" ]; then
  1042. local proxyport="-m multiport --dports $custom_ports"
  1043. fi
  1044. else
  1045. if [ "$(uci_get_by_type global dports 1)" == "2" ]; then
  1046. local proxyport="-m multiport --dports 22,53,587,465,995,993,143,80,443,853,9418"
  1047. fi
  1048. fi
  1049. get_arg_out() {
  1050. case "$(uci_get_by_type access_control router_proxy 1)" in
  1051. 1) echo "-o" ;;
  1052. 2) echo "-O" ;;
  1053. esac
  1054. }
  1055. /usr/share/shadowsocksr/gfw2ipset.sh
  1056. /usr/bin/ssr-rules \
  1057. -s "$server" \
  1058. -l "$local_port" \
  1059. -S "$udp_server" \
  1060. -L "$udp_local_port" \
  1061. -a "$ac_ips" \
  1062. -i "/etc/ssrplus/china_ssr.txt" \
  1063. -b "$(uci_get_by_type access_control wan_bp_ips)" \
  1064. -w "$(uci_get_by_type access_control wan_fw_ips)" \
  1065. -B "$(uci_get_by_type access_control lan_bp_ips)" \
  1066. -p "$(uci_get_by_type access_control lan_fp_ips)" \
  1067. -G "$(uci_get_by_type access_control lan_gm_ips)" \
  1068. -m "$(uci_get_by_type access_control Interface)" \
  1069. -D "$proxyport" \
  1070. -F "$shunt" \
  1071. -N "$shunt_ip" \
  1072. -M "$(uci_get_by_type global netflix_proxy 0)" \
  1073. -I "/etc/ssrplus/netflixip.list" \
  1074. $(get_arg_out) $(gfwmode) $ARG_UDP
  1075. return $?
  1076. }
  1077. start() {
  1078. set_lock
  1079. echolog "----------start------------"
  1080. mkdir -p /var/run /var/lock /var/log $DNSMASQ_CONF_DIR $TMP_BIN_PATH $TMP_DNSMASQ_PATH
  1081. echo "conf-dir=${TMP_DNSMASQ_PATH}" >"$DNSMASQ_CONF_DIR/dnsmasq-ssrplus.conf"
  1082. if load_config; then
  1083. Start_Run
  1084. start_rules
  1085. start_dns
  1086. add_cron
  1087. start_switch
  1088. else
  1089. echolog "未启动主节点,禁止连接的域名正在加载。"
  1090. cat /etc/ssrplus/deny.list | sed '/^$/d' | sed '/#/d' | sed "/.*/s/.*/address=\/&\//" >$TMP_DNSMASQ_PATH/denylist.conf
  1091. echolog "禁止连接的域名加载完毕。"
  1092. if [ "$(uci_get_by_type global adblock 0)" == "1" ]; then
  1093. echolog "未启动主节点,广告过滤正在加载。"
  1094. cp -f /etc/ssrplus/ad.conf $TMP_DNSMASQ_PATH/
  1095. if [ -f "$TMP_DNSMASQ_PATH/ad.conf" ]; then
  1096. for line in $(cat /etc/ssrplus/black.list); do sed -i "/$line/d" $TMP_DNSMASQ_PATH/ad.conf; done
  1097. for line in $(cat /etc/ssrplus/white.list); do sed -i "/$line/d" $TMP_DNSMASQ_PATH/ad.conf; done
  1098. for line in $(cat /etc/ssrplus/deny.list); do sed -i "/$line/d" $TMP_DNSMASQ_PATH/ad.conf; done
  1099. fi
  1100. echolog "广告过滤加载完毕。"
  1101. fi
  1102. fi
  1103. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  1104. check_server
  1105. start_server
  1106. start_monitor
  1107. clean_log
  1108. echolog "-----------end------------"
  1109. unset_lock
  1110. }
  1111. boot() {
  1112. echolog "boot!"
  1113. mkdir -p /var/run /var/lock /var/log $DNSMASQ_CONF_DIR $TMP_BIN_PATH $TMP_DNSMASQ_PATH
  1114. start
  1115. }
  1116. stop() {
  1117. unlock
  1118. set_lock
  1119. /usr/bin/ssr-rules -f
  1120. local srulecount=$(iptables -L | grep SSR-SERVER-RULE | wc -l)
  1121. if [ $srulecount -gt 0 ]; then
  1122. iptables -F SSR-SERVER-RULE
  1123. iptables -t filter -D INPUT -j SSR-SERVER-RULE
  1124. iptables -X SSR-SERVER-RULE 2>/dev/null
  1125. fi
  1126. if [ -z "$switch_server" ]; then
  1127. $PS -w | grep -v "grep" | grep ssr-switch | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  1128. rm -f /var/lock/ssr-switch.lock
  1129. killall -q -9 kcptun-client
  1130. fi
  1131. $PS -w | grep -v "grep" | grep ssr-monitor | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  1132. $PS -w | grep -v "grep" | grep "sleep 0000" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  1133. ( \
  1134. # Graceful kill first, so programs have the chance to stop its subprocesses
  1135. $PS -w | grep -v "grep" | grep "$TMP_PATH" | awk '{print $1}' | xargs kill >/dev/null 2>&1 ; \
  1136. sleep 3s; \
  1137. # Force kill hanged programs
  1138. $PS -w | grep -v "grep" | grep "$TMP_PATH" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 ; \
  1139. )
  1140. killall -q -9 v2ray-plugin obfs-local xray-plugin
  1141. rm -f /var/lock/ssr-monitor.lock
  1142. if [ "$(uci -q get "dhcp.@dnsmasq[0]._unused_ssrp_changed")" = "1" ]; then
  1143. uci -q del "dhcp.@dnsmasq[0].noresolv"
  1144. uci -q del_list "dhcp.@dnsmasq[0].server"="127.0.0.1#$china_dns_port"
  1145. uci -q rename "dhcp.@dnsmasq[0]._orig_noresolv"="noresolv"
  1146. uci -q rename "dhcp.@dnsmasq[0]._orig_server"="server"
  1147. uci -q del "dhcp.@dnsmasq[0]._unused_ssrp_changed"
  1148. uci -q commit "dhcp"
  1149. fi
  1150. if [ -f "$DNSMASQ_CONF_DIR/dnsmasq-ssrplus.conf" ]; then
  1151. rm -rf $DNSMASQ_CONF_DIR/dnsmasq-ssrplus.conf $TMP_DNSMASQ_PATH $TMP_PATH/*-ssr-*.json $TMP_PATH/ssr-server*.json
  1152. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  1153. fi
  1154. del_cron
  1155. unset_lock
  1156. }
  1157. reset() {
  1158. stop
  1159. set_lock
  1160. rm -rf /etc/config/shadowsocksr $LOG_FILE
  1161. touch /etc/config/shadowsocksr $LOG_FILE
  1162. cp /usr/share/shadowsocksr/shadowsocksr.config /etc/config/shadowsocksr
  1163. unset_lock
  1164. }