shadowsocksr 26 KB

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