shadowsocksr 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. #
  7. # This is free software, licensed under the GNU General Public License v3.
  8. # See /LICENSE for more information.
  9. #
  10. START=95
  11. STOP=15
  12. SERVICE_DAEMONIZE=1
  13. MAXFD=32768
  14. NAME=shadowsocksr
  15. LOCK_FILE=/var/lock/${NAME}.lock
  16. CONFIG_FILE=/var/etc/${NAME}.json
  17. CONFIG_UDP_FILE=/var/etc/${NAME}_u.json
  18. CONFIG_SOCK5_FILE=/var/etc/${NAME}_s.json
  19. CONFIG_NETFLIX_FILE=/var/etc/${NAME}_n.json
  20. server_count=0
  21. redir_tcp=0
  22. redir_udp=0
  23. tunnel_enable=0
  24. local_enable=0
  25. kcp_enable_flag=0
  26. kcp_flag=0
  27. pdnsd_enable_flag=0
  28. switch_enable=0
  29. switch_server=$1
  30. CRON_FILE=/etc/crontabs/root
  31. threads=1
  32. uci_get_by_name() {
  33. local ret=$(uci get $NAME.$1.$2 2>/dev/null)
  34. echo ${ret:=$3}
  35. }
  36. uci_get_by_type() {
  37. local ret=$(uci get $NAME.@$1[0].$2 2>/dev/null)
  38. echo ${ret:=$3}
  39. }
  40. uci_set_by_name() {
  41. uci set $NAME.$1.$2=$3 2>/dev/null
  42. uci commit $NAME
  43. }
  44. uci_set_by_type() {
  45. uci set $NAME.@$1[0].$2=$3 2>/dev/null
  46. uci commit $NAME
  47. }
  48. uci_get_by_cfgid() {
  49. local ret=$(uci show $NAME.@$1[0].$2 | awk -F '.' '{print $2}' 2>/dev/null)
  50. echo ${ret:=$3}
  51. }
  52. get_host_ip() {
  53. local host=$1
  54. local ip=$1
  55. if [ -z "$(echo $1 | grep -E "([0-9]{1,3}[\.]){3}[0-9]{1,3}")" ]; then
  56. if [ "$1" == "${1#*:[0-9a-fA-F]}" ]; then
  57. ip=$(resolveip -4 -t 3 $1 | awk 'NR==1{print}')
  58. [ -z "$ip" ] && ip=$(wget -q -O- http://119.29.29.29/d?dn=$1 | awk -F ';' '{print $1}')
  59. fi
  60. fi
  61. echo ${ip:="ERROR"}
  62. }
  63. clean_log() {
  64. logsnum=$(cat /tmp/ssrplus.log 2>/dev/null | wc -l)
  65. [ "$logsnum" -gt 1000 ] && {
  66. echo "$(date "+%Y-%m-%d %H:%M:%S") 日志文件过长,清空处理!" >/tmp/ssrplus.log
  67. }
  68. }
  69. add_cron() {
  70. touch $CRON_FILE
  71. sed -i '/shadowsocksr/d' $CRON_FILE
  72. [ $(uci_get_by_type server_subscribe auto_update 0) -eq 1 ] && echo "0 $(uci_get_by_type server_subscribe auto_update_time) * * * /usr/share/shadowsocksr/ssrplusupdate.sh >>/tmp/ssrplus.log 2>&1" >>$CRON_FILE
  73. crontab $CRON_FILE
  74. }
  75. del_cron() {
  76. sed -i '/shadowsocksr/d' $CRON_FILE
  77. sed -i '/ssrplus.log/d' $CRON_FILE
  78. /etc/init.d/cron restart
  79. clean_log
  80. }
  81. unlock() {
  82. failcount=1
  83. while [ "$failcount" -le 10 ]; do
  84. if [ -f "$LOCK_FILE" ]; then
  85. let "failcount++"
  86. sleep 1s
  87. [ "$failcount" -ge 10 ] && rm -f "$LOCK_FILE"
  88. else
  89. break
  90. fi
  91. done
  92. }
  93. find_bin() {
  94. case "$1" in
  95. ss) ret="/usr/bin/ss-redir" ;;
  96. ss-local) ret="/usr/bin/ss-local" ;;
  97. ssr) ret="/usr/bin/ssr-redir" ;;
  98. ssr-local) ret="/usr/bin/ssr-local" ;;
  99. ssr-server) ret="/usr/bin/ssr-server" ;;
  100. vmess | vless)
  101. ret="/usr/bin/xray"
  102. [ ! -f "$ret" ] && ret="/usr/bin/xray/xray"
  103. [ ! -f "$ret" ] && ret="/usr/bin/v2ray"
  104. [ ! -f "$ret" ] && ret="/usr/bin/v2ray/v2ray"
  105. ;;
  106. trojan) ret="/usr/sbin/trojan" ;;
  107. naiveproxy) ret="/usr/bin/naive" ;;
  108. socks5 | tun) ret="/usr/sbin/redsocks2" ;;
  109. esac
  110. echo $ret
  111. }
  112. gen_config_file() {
  113. local hostip=$(get_host_ip $(uci_get_by_name $1 server))
  114. if [ "$hostip" == "ERROR" ]; then
  115. hostip=$(uci_get_by_name $1 ip)
  116. else
  117. uci_set_by_name $1 ip $hostip
  118. fi
  119. case "$2" in
  120. 0) config_file=$CONFIG_FILE && [ "$kcp_flag" == "1" ] && hostip="127.0.0.1" ;;
  121. 1) config_file=$CONFIG_UDP_FILE ;;
  122. 2) config_file=$CONFIG_NETFLIX_FILE ;;
  123. *) config_file=$CONFIG_SOCK5_FILE ;;
  124. esac
  125. if [ $(uci_get_by_name $1 fast_open 0) == "1" ]; then
  126. fastopen="true"
  127. else
  128. fastopen="false"
  129. fi
  130. local type=$(uci_get_by_name $1 type)
  131. case "$type" in
  132. ss)
  133. cat <<-EOF >$config_file
  134. {
  135. "server": "$hostip",
  136. "server_port": $(uci_get_by_name $1 server_port),
  137. "local_address": "0.0.0.0",
  138. "local_port": $3,
  139. "password": "$(uci_get_by_name $1 password)",
  140. "timeout": $(uci_get_by_name $1 timeout 60),
  141. "method": "$(uci_get_by_name $1 encrypt_method_ss)",
  142. "reuse_port": true,
  143. "fast_open": $fastopen
  144. }
  145. EOF
  146. local plugin=$(uci_get_by_name $1 plugin)
  147. if [ -n "$plugin" ]; then
  148. [ "$plugin" == "simple-obfs" ] && plugin="obfs-local"
  149. if [ -x "/usr/bin/$plugin" ]; then
  150. sed -i "s@$hostip\",@$hostip\",\n\"plugin\": \"$plugin\",\n\"plugin_opts\": \"$(uci_get_by_name $1 plugin_opts)\",@" $config_file
  151. else
  152. echo "$(date "+%Y-%m-%d %H:%M:%S") Warning!!! SIP003 plugin $plugin not found!!!" >>/tmp/ssrplus.log
  153. fi
  154. fi
  155. ;;
  156. ssr)
  157. cat <<-EOF >$config_file
  158. {
  159. "server": "$hostip",
  160. "server_port": $(uci_get_by_name $1 server_port),
  161. "local_address": "0.0.0.0",
  162. "local_port": $3,
  163. "password": "$(uci_get_by_name $1 password)",
  164. "timeout": $(uci_get_by_name $1 timeout 60),
  165. "method": "$(uci_get_by_name $1 encrypt_method)",
  166. "protocol": "$(uci_get_by_name $1 protocol)",
  167. "protocol_param": "$(uci_get_by_name $1 protocol_param)",
  168. "obfs": "$(uci_get_by_name $1 obfs)",
  169. "obfs_param": "$(uci_get_by_name $1 obfs_param)",
  170. "reuse_port": true,
  171. "fast_open": $fastopen
  172. }
  173. EOF
  174. ;;
  175. vmess | vless)
  176. lua /usr/share/shadowsocksr/genv2config.lua $GLOBAL_SERVER tcp $3 >/var/etc/v2-ssr-retcp.json
  177. sed -i 's/\\//g' /var/etc/v2-ssr-retcp.json
  178. ;;
  179. trojan)
  180. lua /usr/share/shadowsocksr/gentrojanconfig.lua $GLOBAL_SERVER nat $3 >/var/etc/trojan-ssr-retcp.json
  181. sed -i 's/\\//g' /var/etc/trojan-ssr-retcp.json
  182. ;;
  183. naiveproxy)
  184. case "$2" in
  185. 0) PROTO="redir"; FILE_NAME="retcp" ;;
  186. 1) PROTO="redir"; FILE_NAME="netflix" ;;
  187. 2) PROTO="socks"; FILE_NAME="socksdns" ;;
  188. 3) PROTO="socks"; FILE_NAME="local" ;;
  189. esac
  190. cat <<-EOF >/var/etc/naive-ssr-$FILE_NAME.json
  191. {
  192. "listen": "$PROTO://0.0.0.0:$3",
  193. "proxy": "https://$(uci_get_by_name $1 username):$(uci_get_by_name $1 password)@$(uci_get_by_name $1 server):$(uci_get_by_name $1 server_port)",
  194. "concurrency": "${4:-1}"
  195. }
  196. EOF
  197. ;;
  198. esac
  199. }
  200. gen_service_file() {
  201. if [ $(uci_get_by_name $1 fast_open) == "1" ]; then
  202. fastopen="true"
  203. else
  204. fastopen="false"
  205. fi
  206. cat <<-EOF >$2
  207. {
  208. "server": "0.0.0.0",
  209. "server_port": $(uci_get_by_name $1 server_port),
  210. "password": "$(uci_get_by_name $1 password)",
  211. "timeout": $(uci_get_by_name $1 timeout 60),
  212. "method": "$(uci_get_by_name $1 encrypt_method)",
  213. "protocol": "$(uci_get_by_name $1 protocol)",
  214. "protocol_param": "$(uci_get_by_name $1 protocol_param)",
  215. "obfs": "$(uci_get_by_name $1 obfs)",
  216. "obfs_param": "$(uci_get_by_name $1 obfs_param)",
  217. "fast_open": $fastopen
  218. }
  219. EOF
  220. }
  221. gen_serv_include() {
  222. FWI=$(uci get firewall.shadowsocksr.path 2>/dev/null)
  223. [ -n "$FWI" ] || return 0
  224. if [ ! -f $FWI ]; then
  225. echo '#!/bin/sh' >$FWI
  226. fi
  227. extract_rules() {
  228. echo "*filter"
  229. iptables-save -t filter | grep SSR-SERVER-RULE | sed -e "s/^-A INPUT/-I INPUT/"
  230. echo 'COMMIT'
  231. }
  232. cat <<-EOF >>$FWI
  233. iptables-save -c | grep -v "SSR-SERVER" | iptables-restore -c
  234. iptables-restore -n <<-EOT
  235. $(extract_rules)
  236. EOT
  237. EOF
  238. }
  239. start_dns() {
  240. local ssr_dns="$(uci_get_by_type global pdnsd_enable 0)"
  241. local dnsstr="$(uci_get_by_type global tunnel_forward 8.8.4.4:53)"
  242. local dnsserver=$(echo "$dnsstr" | awk -F ':' '{print $1}')
  243. local dnsport=$(echo "$dnsstr" | awk -F ':' '{print $2}')
  244. start_pdnsd() {
  245. local usr_dns="$1"
  246. local usr_port="$2"
  247. if [ ! -f "/var/pdnsd/pdnsd.cache" ]; then
  248. mkdir -p /var/pdnsd
  249. touch /var/pdnsd/pdnsd.cache
  250. chown -R nobody:nogroup /var/pdnsd
  251. fi
  252. cat <<-EOF >/var/etc/pdnsd.conf
  253. global{
  254. perm_cache=1024;
  255. cache_dir="/var/pdnsd";
  256. pid_file="/var/run/pdnsd.pid";
  257. run_as="nobody";
  258. server_ip=127.0.0.1;
  259. server_port=5335;
  260. status_ctl=on;
  261. query_method=tcp_only;
  262. min_ttl=1h;
  263. max_ttl=1w;
  264. timeout=10;
  265. neg_domain_pol=on;
  266. proc_limit=2;
  267. procq_limit=8;
  268. par_queries=1;
  269. }
  270. server{
  271. label="ssr-usrdns";
  272. ip=$usr_dns;
  273. port=$usr_port;
  274. timeout=6;
  275. uptest=none;
  276. interval=10m;
  277. purge_cache=off;
  278. }
  279. EOF
  280. /usr/sbin/pdnsd -c /var/etc/pdnsd.conf >/dev/null 2>&1 &
  281. }
  282. if [ "$ssr_dns" != "0" ]; then
  283. case "$run_mode" in
  284. gfw) ipset add gfwlist $dnsserver 2>/dev/null ;;
  285. oversea) ipset add oversea $dnsserver 2>/dev/null ;;
  286. *) ipset add ss_spec_wan_ac $dnsserver nomatch 2>/dev/null ;;
  287. esac
  288. fi
  289. case "$ssr_dns" in
  290. 1)
  291. start_pdnsd $dnsserver $dnsport
  292. pdnsd_enable_flag=1
  293. ;;
  294. 2)
  295. microsocks -i 127.0.0.1 -p 10802 ssr-dns >/dev/null 2>&1 &
  296. dns2socks 127.0.0.1:10802 $dnsserver:$dnsport 127.0.0.1:5335 -q >/dev/null 2>&1 &
  297. pdnsd_enable_flag=2
  298. ;;
  299. esac
  300. }
  301. start_redir_tcp() {
  302. local type=$(uci_get_by_name $GLOBAL_SERVER type)
  303. local bin=$(find_bin $type)
  304. redir_tcp=1
  305. [ ! -f "$bin" ] && echo "$(date "+%Y-%m-%d %H:%M:%S") Main node:Can't find $bin program, can't start!" >>/tmp/ssrplus.log && redir_tcp=0 && return 1
  306. case "$(uci_get_by_name $GLOBAL_SERVER auth_enable)" in
  307. 1 | on | true | yes | enabled) ARG_OTA="-A" ;;
  308. *) ARG_OTA="" ;;
  309. esac
  310. local kcp_enable=$(uci_get_by_name $GLOBAL_SERVER kcp_enable 0)
  311. if [ "$kcp_enable" == "1" ]; then
  312. [ ! -f "/usr/bin/kcptun-client" ] && return 1
  313. local kcp_str=$(/usr/bin/kcptun-client -v | grep kcptun | wc -l)
  314. [ "0" == "$kcp_str" ] && return 1
  315. local kcp_port=$(uci_get_by_name $GLOBAL_SERVER kcp_port)
  316. local server_port=$(uci_get_by_name $GLOBAL_SERVER server_port)
  317. local password=$(uci_get_by_name $GLOBAL_SERVER kcp_password)
  318. local kcp_param=$(uci_get_by_name $GLOBAL_SERVER kcp_param)
  319. [ "$password" != "" ] && password="--key "$password
  320. service_start /usr/bin/kcptun-client \
  321. -r $kcp_server:$kcp_port \
  322. -l :$server_port $password $kcp_param
  323. kcp_enable_flag=1
  324. fi
  325. if [ "$(uci_get_by_type global threads 0)" == "0" ]; then
  326. threads=$(cat /proc/cpuinfo | grep 'processor' | wc -l)
  327. else
  328. threads=$(uci_get_by_type global threads)
  329. fi
  330. gen_config_file $GLOBAL_SERVER 0 $(uci_get_by_name $GLOBAL_SERVER local_port 1234) $threads
  331. case "$type" in
  332. ss | ssr)
  333. local last_config_file=$CONFIG_FILE
  334. local name="Shadowsocks"
  335. [ "$type" == "ssr" ] && name="ShadowsocksR"
  336. for i in $(seq 1 $threads); do
  337. $bin -c $CONFIG_FILE $ARG_OTA -f /var/run/ssr-retcp_$i.pid >/dev/null 2>&1
  338. done
  339. echo "$(date "+%Y-%m-%d %H:%M:%S") Main node:$name $threads Threads Started!" >>/tmp/ssrplus.log
  340. ;;
  341. vmess | vless)
  342. $bin -config /var/etc/v2-ssr-retcp.json >/dev/null 2>&1 &
  343. echo "$(date "+%Y-%m-%d %H:%M:%S") Main node:$($bin -version | head -1) Started!" >>/tmp/ssrplus.log
  344. ;;
  345. trojan)
  346. for i in $(seq 1 $threads); do
  347. $bin --config /var/etc/trojan-ssr-retcp.json >/dev/null 2>&1 &
  348. done
  349. echo "$(date "+%Y-%m-%d %H:%M:%S") Main node:$($bin --version 2>&1 | head -1) , $threads Threads Started!" >>/tmp/ssrplus.log
  350. ;;
  351. naiveproxy)
  352. $bin /var/etc/naive-ssr-retcp.json >/dev/null 2>&1 &
  353. echo "$(date "+%Y-%m-%d %H:%M:%S") Main node:$($bin --version 2>&1 | head -1) , $threads Threads Started!" >>/tmp/ssrplus.log
  354. ;;
  355. socks5)
  356. /usr/share/shadowsocksr/genred2config.sh "/var/etc/redsocks-ssr-retcp.conf" socks5 tcp $(uci_get_by_name $GLOBAL_SERVER local_port) \
  357. $(get_host_ip $(uci_get_by_name $GLOBAL_SERVER server)) $(uci_get_by_name $GLOBAL_SERVER server_port) \
  358. $(uci_get_by_name $GLOBAL_SERVER auth_enable 0) $(uci_get_by_name $GLOBAL_SERVER username) $(uci_get_by_name $GLOBAL_SERVER password)
  359. for i in $(seq 1 $threads); do
  360. $bin -c /var/etc/redsocks-ssr-retcp.conf >/dev/null 2>&1
  361. done
  362. echo "$(date "+%Y-%m-%d %H:%M:%S") Main node:Socks5 REDIRECT/TPROXY $threads Threads Started!" >>/tmp/ssrplus.log
  363. ;;
  364. tun)
  365. /usr/share/shadowsocksr/genred2config.sh "/var/etc/redsocks-ssr-retcp.conf" vpn $(uci_get_by_name $GLOBAL_SERVER iface "br-lan") $(uci_get_by_name $GLOBAL_SERVER local_port)
  366. for i in $(seq 1 $threads); do
  367. $bin -c /var/etc/redsocks-ssr-retcp.conf >/dev/null 2>&1
  368. done
  369. echo "$(date "+%Y-%m-%d %H:%M:%S") Main node:Network Tunnel REDIRECT $threads Threads Started!" >>/tmp/ssrplus.log
  370. ;;
  371. esac
  372. return 0
  373. }
  374. start_redir_udp() {
  375. if [ -n "$UDP_RELAY_SERVER" ]; then
  376. redir_udp=1
  377. local type=$(uci_get_by_name $UDP_RELAY_SERVER type)
  378. local bin=$(find_bin $type)
  379. local udp_port=1357
  380. [ ! -f "$bin" ] && echo "$(date "+%Y-%m-%d %H:%M:%S") UDP TPROXY Relay:Can't find $bin program, can't start!" >>/tmp/ssrplus.log && redir_udp=0 && return 1
  381. case "$type" in
  382. ss | ssr)
  383. case "$(uci_get_by_name $UDP_RELAY_SERVER auth_enable)" in
  384. 1 | on | true | yes | enabled) ARG_OTA="-A" ;;
  385. *) ARG_OTA="" ;;
  386. esac
  387. local name="Shadowsocks"
  388. [ "$type" == "ssr" ] && name="ShadowsocksR"
  389. gen_config_file $UDP_RELAY_SERVER 1 $udp_port
  390. last_config_file=$CONFIG_UDP_FILE
  391. $bin -c $last_config_file $ARG_OTA -U -f /var/run/ssr-reudp.pid >/dev/null 2>&1
  392. echo "$(date "+%Y-%m-%d %H:%M:%S") UDP TPROXY Relay:$name Started!" >>/tmp/ssrplus.log
  393. ;;
  394. vmess | vless)
  395. lua /usr/share/shadowsocksr/genv2config.lua $UDP_RELAY_SERVER udp $udp_port >/var/etc/v2-ssr-reudp.json
  396. sed -i 's/\\//g' /var/etc/v2-ssr-reudp.json
  397. $bin -config /var/etc/v2-ssr-reudp.json >/dev/null 2>&1 &
  398. echo "$(date "+%Y-%m-%d %H:%M:%S") UDP TPROXY Relay:$($bin -version | head -1) Started!" >>/tmp/ssrplus.log
  399. ;;
  400. trojan)
  401. lua /usr/share/shadowsocksr/gentrojanconfig.lua $GLOBAL_SERVER client 10801 >/var/etc/trojan-ssr-reudp.json
  402. sed -i 's/\\//g' /var/etc/trojan-ssr-reudp.json
  403. $bin --config /var/etc/trojan-ssr-reudp.json >/dev/null 2>&1 &
  404. ipt2socks -U -b 0.0.0.0 -4 -s 127.0.0.1 -p 10801 -l $udp_port >/dev/null 2>&1 &
  405. echo "$(date "+%Y-%m-%d %H:%M:%S") UDP TPROXY Relay:$($bin --version 2>&1 | head -1) Started!" >>/tmp/ssrplus.log
  406. ;;
  407. naiveproxy)
  408. redir_udp=0
  409. echo "$(date "+%Y-%m-%d %H:%M:%S") $($bin --version | head -1) UDP TPROXY Relay not supported!" >>/tmp/ssrplus.log
  410. ;;
  411. socks5)
  412. /usr/share/shadowsocksr/genred2config.sh "/var/etc/redsocks-ssr-reudp.conf" socks5 udp $udp_port \
  413. $(get_host_ip $(uci_get_by_name $UDP_RELAY_SERVER server)) $(uci_get_by_name $UDP_RELAY_SERVER server_port) \
  414. $(uci_get_by_name $UDP_RELAY_SERVER auth_enable 0) $(uci_get_by_name $UDP_RELAY_SERVER username) $(uci_get_by_name $UDP_RELAY_SERVER password)
  415. $bin -c /var/etc/redsocks-ssr-reudp.conf >/dev/null 2>&1
  416. echo "$(date "+%Y-%m-%d %H:%M:%S") UDP TPROXY Relay:Socks5 REDIRECT/TPROXY Started!" >>/tmp/ssrplus.log
  417. ;;
  418. tun)
  419. redir_udp=0
  420. echo "$(date "+%Y-%m-%d %H:%M:%S") Network Tunnel UDP TPROXY Relay not supported!" >>/tmp/ssrplus.log
  421. ;;
  422. esac
  423. fi
  424. return 0
  425. }
  426. start_shunt() {
  427. if [ "$NETFLIX_SERVER" != "nil" ] && [ "$NETFLIX_SERVER" != "$GLOBAL_SERVER" ]; then
  428. local type=$(uci_get_by_name $NETFLIX_SERVER type)
  429. local bin=$(find_bin $type)
  430. [ ! -f "$bin" ] && echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:Can't find $bin program, can't start!" >>/tmp/ssrplus.log && return 1
  431. case "$type" in
  432. ss | ssr)
  433. local bin2=$(find_bin $type-local)
  434. [ ! -f "$bin2" ] && echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:Can't find $bin2 program, can't start!" >>/tmp/ssrplus.log && return 2
  435. local name="Shadowsocks"
  436. [ "$type" == "ssr" ] && name="ShadowsocksR"
  437. gen_config_file $NETFLIX_SERVER 2 4321
  438. gen_config_file $NETFLIX_SERVER 3 1088
  439. $bin -c /var/etc/shadowsocksr_n.json $ARG_OTA -f /var/run/ssr-netflix.pid >/dev/null 2>&1
  440. $bin2 -c /var/etc/shadowsocksr_s.json $ARG_OTA -f /var/run/ssr-socksdns.pid >/dev/null 2>&1
  441. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  442. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:$name Started!" >>/tmp/ssrplus.log
  443. ;;
  444. vmess | vless)
  445. lua /usr/share/shadowsocksr/genv2config.lua $NETFLIX_SERVER tcp 4321 1088 >/var/etc/v2-ssr-netflix.json
  446. sed -i 's/\\//g' /var/etc/v2-ssr-netflix.json
  447. $bin -config /var/etc/v2-ssr-netflix.json >/dev/null 2>&1 &
  448. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  449. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:$($bin -version | head -1) Started!" >>/tmp/ssrplus.log
  450. ;;
  451. trojan)
  452. lua /usr/share/shadowsocksr/gentrojanconfig.lua $NETFLIX_SERVER nat 4321 >/var/etc/trojan-ssr-netflix.json
  453. sed -i 's/\\//g' /var/etc/trojan-ssr-netflix.json
  454. $bin --config /var/etc/trojan-ssr-netflix.json >/dev/null 2>&1 &
  455. lua /usr/share/shadowsocksr/gentrojanconfig.lua $NETFLIX_SERVER client 1088 >/var/etc/trojan-ssr-socksdns.json
  456. sed -i 's/\\//g' /var/etc/trojan-ssr-socksdns.json
  457. $bin --config /var/etc/trojan-ssr-socksdns.json >/dev/null 2>&1 &
  458. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  459. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:$($bin --version 2>&1 | head -1) Started!" >>/tmp/ssrplus.log
  460. ;;
  461. naiveproxy)
  462. gen_config_file $NETFLIX_SERVER 1 4321
  463. $bin /var/etc/naive-ssr-netflix.json >/dev/null 2>&1 &
  464. gen_config_file $NETFLIX_SERVER 2 1088
  465. $bin /var/etc/naive-ssr-socksdns.json >/dev/null 2>&1 &
  466. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  467. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:$($bin --version | head -1) Started!" >>/tmp/ssrplus.log
  468. ;;
  469. socks5)
  470. /usr/share/shadowsocksr/genred2config.sh "/var/etc/redsocks-ssr-netflix.conf" socks5 tcp 4321 \
  471. $(get_host_ip $(uci_get_by_name $NETFLIX_SERVER server)) $(uci_get_by_name $NETFLIX_SERVER server_port) \
  472. $(uci_get_by_name $NETFLIX_SERVER auth_enable 0) $(uci_get_by_name $NETFLIX_SERVER username) $(uci_get_by_name $NETFLIX_SERVER password)
  473. $bin -c /var/etc/redsocks-ssr-netflix.conf >/dev/null 2>&1
  474. microsocks -i 127.0.0.1 -p 1088 ssr-socksdns >/dev/null 2>&1 &
  475. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  476. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:Socks5 Started!" >>/tmp/ssrplus.log
  477. ;;
  478. tun)
  479. /usr/share/shadowsocksr/genred2config.sh "/var/etc/redsocks-ssr-netflix.conf" vpn $(uci_get_by_name $NETFLIX_SERVER iface "br-lan") 4321
  480. $bin -c /var/etc/redsocks-ssr-netflix.conf >/dev/null 2>&1
  481. microsocks -i 127.0.0.1 -p 1088 ssr-socksdns >/dev/null 2>&1 &
  482. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  483. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:Network Tunnel REDIRECT Started!" >>/tmp/ssrplus.log
  484. ;;
  485. esac
  486. fi
  487. return 0
  488. }
  489. start_local() {
  490. local local_server=$(uci_get_by_type socks5_proxy server nil)
  491. [ "$local_server" == "same" ] && local_server=$GLOBAL_SERVER
  492. [ "$local_server" == "nil" ] && return 1
  493. local type=$(uci_get_by_name $local_server type)
  494. local bin=$(find_bin $type)
  495. [ ! -f "$bin" ] && echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:Can't find $bin program, can't start!" >>/tmp/ssrplus.log && local_enable=0 && return 1
  496. case "$type" in
  497. ss | ssr)
  498. local name="Shadowsocks"
  499. local bin=$(find_bin $type-local)
  500. [ ! -f "$bin" ] && echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:Can't find $bin program, can't start!" >>/tmp/ssrplus.log && local_enable=0 && return 1
  501. [ "$type" == "ssr" ] && name="ShadowsocksR"
  502. gen_config_file $local_server 3 $(uci_get_by_type socks5_proxy local_port 1080)
  503. $bin -c $CONFIG_SOCK5_FILE -u -f /var/run/ssr-local.pid >/dev/null 2>&1
  504. echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:$name Started!" >>/tmp/ssrplus.log
  505. ;;
  506. vmess | vless)
  507. lua /usr/share/shadowsocksr/genv2config.lua $local_server tcp 0 $(uci_get_by_type socks5_proxy local_port 1080) >/var/etc/v2-ssr-local.json
  508. sed -i 's/\\//g' /var/etc/v2-ssr-local.json
  509. $bin -config /var/etc/v2-ssr-local.json >/dev/null 2>&1 &
  510. echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:$($bin -version | head -1) Started!" >>/tmp/ssrplus.log
  511. ;;
  512. trojan)
  513. lua /usr/share/shadowsocksr/gentrojanconfig.lua $local_server client $(uci_get_by_type socks5_proxy local_port 1080) >/var/etc/trojan-ssr-local.json
  514. sed -i 's/\\//g' /var/etc/trojan-ssr-local.json
  515. $bin --config /var/etc/trojan-ssr-local.json >/dev/null 2>&1 &
  516. echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:$($bin --version 2>&1 | head -1) Started!" >>/tmp/ssrplus.log
  517. ;;
  518. naiveproxy)
  519. gen_config_file $local_server 3 $(uci_get_by_type socks5_proxy local_port 1080)
  520. $bin /var/etc/naive-ssr-local.json >/dev/null 2>&1 &
  521. echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:$($bin --version | head -1) Started!" >>/tmp/ssrplus.log
  522. ;;
  523. *)
  524. [ -e /proc/sys/net/ipv6 ] && local listenip='-i ::'
  525. microsocks $listenip -p $(uci_get_by_type socks5_proxy local_port 1080) ssr-local >/dev/null 2>&1 &
  526. echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:$type Started!" >>/tmp/ssrplus.log
  527. ;;
  528. esac
  529. local_enable=1
  530. return 0
  531. }
  532. start_server() {
  533. SERVER_ENABLE=$(uci_get_by_type server_global enable_server 0)
  534. [ "$SERVER_ENABLE" == "0" ] && return 0
  535. server_service() {
  536. [ $(uci_get_by_name $1 enable 0) == "0" ] && return 1
  537. let server_count=server_count+1
  538. if [ "$server_count" == "1" ]; then
  539. if ! (iptables-save -t filter | grep SSR-SERVER-RULE >/dev/null); then
  540. iptables -N SSR-SERVER-RULE && \
  541. iptables -t filter -I INPUT -j SSR-SERVER-RULE
  542. fi
  543. fi
  544. if [ "$(uci_get_by_name $1 type ssr)" == "ssr" ]; then
  545. local bin=$(find_bin ssr-server)
  546. [ ! -f "$bin" ] && echo "$(date "+%Y-%m-%d %H:%M:%S") Server:Can't find $bin program, can't start!" >>/tmp/ssrplus.log && return 1
  547. gen_service_file $1 /var/etc/${NAME}_$server_count.json
  548. $(find_bin ssr-server) -c /var/etc/${NAME}_$server_count.json -u -f /var/run/ssr-server$server_count.pid >/dev/null 2>&1
  549. echo "$(date "+%Y-%m-%d %H:%M:%S") Server:ShadowsocksR Server$server_count Started!" >>/tmp/ssrplus.log
  550. else
  551. [ -e /proc/sys/net/ipv6 ] && local listenip='-i ::'
  552. 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 >/dev/null 2>&1 &
  553. echo "$(date "+%Y-%m-%d %H:%M:%S") Server:Socks5 Server$server_count Started!" >>/tmp/ssrplus.log
  554. fi
  555. iptables -t filter -A SSR-SERVER-RULE -p tcp --dport $(uci_get_by_name $1 server_port) -j ACCEPT
  556. iptables -t filter -A SSR-SERVER-RULE -p udp --dport $(uci_get_by_name $1 server_port) -j ACCEPT
  557. return 0
  558. }
  559. config_load $NAME
  560. config_foreach server_service server_config
  561. gen_serv_include
  562. return 0
  563. }
  564. start_switch() {
  565. if [ "$(uci_get_by_type global enable_switch 0)" == "1" ]; then
  566. if [ -z "$switch_server" ]; then
  567. local switch_time=$(uci_get_by_type global switch_time)s
  568. local switch_timeout=$(uci_get_by_type global switch_timeout)
  569. service_start /usr/bin/ssr-switch start $switch_time $switch_timeout
  570. switch_enable=1
  571. fi
  572. fi
  573. }
  574. start_monitor() {
  575. if [ $(uci_get_by_type global monitor_enable 1) == "1" ]; then
  576. let total_count=server_count+redir_tcp+redir_udp+tunnel_enable+kcp_enable_flag+local_enable+pdnsd_enable_flag+switch_enable
  577. if [ $total_count -gt 0 ]; then
  578. service_start /usr/bin/ssr-monitor $server_count $redir_tcp $redir_udp $tunnel_enable $kcp_enable_flag $local_enable $pdnsd_enable_flag $switch_enable
  579. fi
  580. fi
  581. }
  582. check_server() {
  583. ENABLE_SERVER=$(uci_get_by_type global global_server nil)
  584. if [ "$ENABLE_SERVER" == "nil" ]; then
  585. return 1
  586. else
  587. local STYPE=$(uci_get_by_name $ENABLE_SERVER type nil)
  588. if [ "$STYPE" == "nil" ]; then
  589. local CFGID=$(uci_get_by_cfgid servers type nil)
  590. if [ "$CFGID" == "nil" ]; then
  591. uci_set_by_type global global_server 'nil'
  592. else
  593. uci_set_by_type global global_server $CFGID
  594. fi
  595. /etc/init.d/shadowsocksr restart
  596. fi
  597. fi
  598. }
  599. start_rules() {
  600. if [ -z "$switch_server" ]; then
  601. GLOBAL_SERVER=$(uci_get_by_type global global_server nil)
  602. else
  603. GLOBAL_SERVER=$switch_server
  604. switch_enable=1
  605. fi
  606. if [ "$GLOBAL_SERVER" == "nil" ]; then
  607. return 1
  608. fi
  609. NETFLIX_SERVER=$(uci_get_by_type global netflix_server nil)
  610. if [ "$NETFLIX_SERVER" == "same" ]; then
  611. NETFLIX_SERVER=$GLOBAL_SERVER
  612. fi
  613. run_mode=$(uci_get_by_type global run_mode)
  614. UDP_RELAY_SERVER=$(uci_get_by_type global udp_relay_server)
  615. if [ "$UDP_RELAY_SERVER" == "same" ] ; then
  616. UDP_RELAY_SERVER=$GLOBAL_SERVER
  617. fi
  618. local server=$(get_host_ip $(uci_get_by_name $GLOBAL_SERVER server))
  619. if [ "$server" == "ERROR" ]; then
  620. server=$(uci_get_by_name $GLOBAL_SERVER ip)
  621. fi
  622. local kcp_enable=$(uci_get_by_name $GLOBAL_SERVER kcp_enable 0)
  623. if [ $kcp_enable == "1" ]; then
  624. kcp_server=$server
  625. kcp_flag=1
  626. fi
  627. local local_port=$(uci_get_by_name $GLOBAL_SERVER local_port 1234)
  628. local lan_ac_ips=$(uci_get_by_type access_control lan_ac_ips)
  629. local lan_ac_mode=$(uci_get_by_type access_control lan_ac_mode)
  630. local router_proxy=$(uci_get_by_type access_control router_proxy)
  631. if [ "$GLOBAL_SERVER" == "$UDP_RELAY_SERVER" -a "$kcp_flag" == "0" ]; then
  632. ARG_UDP="-u"
  633. elif [ -n "$UDP_RELAY_SERVER" ]; then
  634. if [ "$kcp_flag" == "0" ]; then
  635. local type=$(uci_get_by_name $UDP_RELAY_SERVER type)
  636. case "$type" in
  637. tun | naiveproxy) ;;
  638. *) ARG_UDP="-U";;
  639. esac
  640. local udp_server=$(uci_get_by_name $UDP_RELAY_SERVER server)
  641. if [ "$udp_server" == "ERROR" ]; then
  642. udp_server=$(uci_get_by_name $UDP_RELAY_SERVER ip)
  643. fi
  644. local udp_local_port=1357
  645. fi
  646. fi
  647. if [ -n "$lan_ac_ips" ]; then
  648. case "$lan_ac_mode" in
  649. w | W | b | B) local ac_ips="$lan_ac_mode$lan_ac_ips" ;;
  650. esac
  651. fi
  652. #deal gfw firewall rule
  653. local gfwmode=""
  654. case "$run_mode" in
  655. gfw) gfwmode="-g" ;;
  656. router) gfwmode="-r" ;;
  657. oversea) gfwmode="-c" ;;
  658. all) gfwmode="-z" ;;
  659. esac
  660. local dports=$(uci_get_by_type global dports 1)
  661. if [ $dports == "1" ]; then
  662. proxyport=" "
  663. else
  664. proxyport="-m multiport --dports 22,53,587,465,995,993,143,80,443,853"
  665. fi
  666. if [ "$NETFLIX_SERVER" != "nil" ]; then
  667. if [ "$NETFLIX_SERVER" != "$GLOBAL_SERVER" ]; then
  668. netflix="1"
  669. else
  670. netflix="2"
  671. fi
  672. else
  673. netflix="0"
  674. fi
  675. get_arg_out() {
  676. case "$(uci_get_by_type access_control router_proxy 1)" in
  677. 1) echo "-o" ;;
  678. 2) echo "-O" ;;
  679. esac
  680. }
  681. netflix_ip=$(get_host_ip $(uci_get_by_name $NETFLIX_SERVER server))
  682. if [ "$netflix_ip" == "ERROR" ]; then
  683. netflix_ip=$(uci_get_by_name $NETFLIX_SERVER ip)
  684. fi
  685. /usr/bin/ssr-rules \
  686. -s "$server" \
  687. -l "$local_port" \
  688. -S "$udp_server" \
  689. -L "$udp_local_port" \
  690. -a "$ac_ips" \
  691. -i "/etc/ssr/china_ssr.txt" \
  692. -b "$(uci_get_by_type access_control wan_bp_ips)" \
  693. -w "$(uci_get_by_type access_control wan_fw_ips)" \
  694. -B "$(uci_get_by_type access_control lan_bp_ips)" \
  695. -p "$(uci_get_by_type access_control lan_fp_ips)" \
  696. -G "$(uci_get_by_type access_control lan_gm_ips)" \
  697. -m "$(uci_get_by_type access_control Interface)" \
  698. -D "$proxyport" \
  699. -F "$netflix" \
  700. -N "$netflix_ip" \
  701. -M "$(uci_get_by_type global netflix_proxy 0)" \
  702. -I "/etc/ssr/netflixip.list" \
  703. $(get_arg_out) $gfwmode $ARG_UDP
  704. return $?
  705. }
  706. start() {
  707. [ -f "$LOCK_FILE" ] && exit 2
  708. touch "$LOCK_FILE"
  709. echo "----------start------------" >>/tmp/ssrplus.log
  710. mkdir -p /tmp/dnsmasq.d /tmp/dnsmasq.ssr /var/run /var/etc
  711. cat <<-EOF >/tmp/dnsmasq.d/dnsmasq-ssr.conf
  712. conf-dir=/tmp/dnsmasq.ssr
  713. EOF
  714. if start_rules; then
  715. if start_redir_tcp; then
  716. start_redir_udp
  717. start_shunt
  718. start_switch
  719. start_dns
  720. add_cron
  721. /usr/share/shadowsocksr/gfw2ipset.sh $switch_server
  722. else
  723. /usr/bin/ssr-rules -f
  724. fi
  725. else
  726. echo "$(date "+%Y-%m-%d %H:%M:%S") 未启动主节点,禁止连接的域名正在加载。" >>/tmp/ssrplus.log
  727. sed "/.*/s/.*/address=\/&\//" /etc/ssr/deny.list >/tmp/dnsmasq.ssr/denylist.conf
  728. echo "$(date "+%Y-%m-%d %H:%M:%S") 禁止连接的域名加载完毕。" >>/tmp/ssrplus.log
  729. if [ "$(uci_get_by_type global adblock 0)" == "1" ]; then
  730. echo "$(date "+%Y-%m-%d %H:%M:%S") 未启动主节点,广告过滤正在加载。" >>/tmp/ssrplus.log
  731. [ "$1" == "" ] && cp -f /etc/ssr/ad.conf /tmp/dnsmasq.ssr/
  732. if [ -f "/tmp/dnsmasq.ssr/ad.conf" ]; then
  733. for line in $(cat /etc/ssr/black.list); do sed -i "/$line/d" /tmp/dnsmasq.ssr/ad.conf; done
  734. for line in $(cat /etc/ssr/white.list); do sed -i "/$line/d" /tmp/dnsmasq.ssr/ad.conf; done
  735. for line in $(cat /etc/ssr/deny.list); do sed -i "/$line/d" /tmp/dnsmasq.ssr/ad.conf; done
  736. fi
  737. echo "$(date "+%Y-%m-%d %H:%M:%S") 广告过滤加载完毕。" >>/tmp/ssrplus.log
  738. fi
  739. fi
  740. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  741. start_server
  742. start_local
  743. start_monitor
  744. check_server
  745. clean_log
  746. echo "-----------end------------" >>/tmp/ssrplus.log
  747. rm -f $LOCK_FILE
  748. }
  749. boot() {
  750. echo "$(date "+%Y-%m-%d %H:%M:%S") boot!" >/tmp/ssrplus.log
  751. mkdir -p /var/lock /tmp/dnsmasq.d /tmp/dnsmasq.ssr /var/run /var/etc
  752. ulimit -n 65535
  753. start
  754. }
  755. stop() {
  756. unlock
  757. /usr/bin/ssr-rules -f
  758. local srulecount=$(iptables -L | grep SSR-SERVER-RULE | wc -l)
  759. if [ $srulecount -gt 0 ]; then
  760. iptables -F SSR-SERVER-RULE
  761. iptables -t filter -D INPUT -j SSR-SERVER-RULE
  762. iptables -X SSR-SERVER-RULE 2>/dev/null
  763. fi
  764. if [ -z "$switch_server" ]; then
  765. ps -w | grep -v "grep" | grep ssr-switch | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  766. rm -f /var/lock/ssr-switch.lock
  767. killall -q -9 kcptun-client
  768. fi
  769. ps -w | grep -v "grep" | grep ssr-monitor | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  770. ps -w | grep -v "grep" | grep "sleep 0000" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  771. killall -q -9 ss-redir ss-local obfs-local ssr-redir ssr-local ssr-server v2ray v2ray-plugin xray trojan naive microsocks ipt2socks dns2socks redsocks2 pdnsd
  772. rm -f /var/lock/ssr-monitor.lock
  773. if [ -f "/tmp/dnsmasq.d/dnsmasq-ssr.conf" ]; then
  774. rm -rf /tmp/dnsmasq.d/dnsmasq-ssr.conf /tmp/dnsmasq.ssr /tmp/dnsmasq.oversea
  775. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  776. fi
  777. del_cron
  778. }