shadowsocksr 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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 $(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. 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. [ ! -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
  380. case "$type" in
  381. ss | ssr)
  382. case "$(uci_get_by_name $UDP_RELAY_SERVER auth_enable)" in
  383. 1 | on | true | yes | enabled) ARG_OTA="-A" ;;
  384. *) ARG_OTA="" ;;
  385. esac
  386. local name="Shadowsocks"
  387. [ "$type" == "ssr" ] && name="ShadowsocksR"
  388. gen_config_file $UDP_RELAY_SERVER 1 $(uci_get_by_name $UDP_RELAY_SERVER local_port 1234)
  389. last_config_file=$CONFIG_UDP_FILE
  390. $bin -c $last_config_file $ARG_OTA -U -f /var/run/ssr-reudp.pid >/dev/null 2>&1
  391. echo "$(date "+%Y-%m-%d %H:%M:%S") UDP TPROXY Relay:$name Started!" >>/tmp/ssrplus.log
  392. ;;
  393. vmess | vless)
  394. 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
  395. sed -i 's/\\//g' /var/etc/v2-ssr-reudp.json
  396. $bin -config /var/etc/v2-ssr-reudp.json >/dev/null 2>&1 &
  397. echo "$(date "+%Y-%m-%d %H:%M:%S") UDP TPROXY Relay:$($bin -version | head -1) Started!" >>/tmp/ssrplus.log
  398. ;;
  399. trojan)
  400. lua /usr/share/shadowsocksr/gentrojanconfig.lua $UDP_RELAY_SERVER client 10801 >/var/etc/trojan-ssr-reudp.json
  401. sed -i 's/\\//g' /var/etc/trojan-ssr-reudp.json
  402. $bin --config /var/etc/trojan-ssr-reudp.json >/dev/null 2>&1 &
  403. 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 &
  404. echo "$(date "+%Y-%m-%d %H:%M:%S") UDP TPROXY Relay:$($bin --version 2>&1 | head -1) Started!" >>/tmp/ssrplus.log
  405. ;;
  406. naiveproxy)
  407. redir_udp=0
  408. echo "$(date "+%Y-%m-%d %H:%M:%S") $($bin --version | head -1) UDP TPROXY Relay not supported!" >>/tmp/ssrplus.log
  409. ;;
  410. socks5)
  411. /usr/share/shadowsocksr/genred2config.sh "/var/etc/redsocks-ssr-reudp.conf" socks5 udp $(uci_get_by_name $UDP_RELAY_SERVER local_port) \
  412. $(get_host_ip $(uci_get_by_name $UDP_RELAY_SERVER server)) $(uci_get_by_name $UDP_RELAY_SERVER server_port) \
  413. $(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)
  414. $bin -c /var/etc/redsocks-ssr-reudp.conf >/dev/null 2>&1
  415. echo "$(date "+%Y-%m-%d %H:%M:%S") UDP TPROXY Relay:Socks5 REDIRECT/TPROXY Started!" >>/tmp/ssrplus.log
  416. ;;
  417. tun)
  418. redir_udp=0
  419. echo "$(date "+%Y-%m-%d %H:%M:%S") Network Tunnel UDP TPROXY Relay not supported!" >>/tmp/ssrplus.log
  420. ;;
  421. esac
  422. fi
  423. return 0
  424. }
  425. start_shunt() {
  426. if [ "$NETFLIX_SERVER" != "nil" ] && [ "$NETFLIX_SERVER" != "$GLOBAL_SERVER" ]; then
  427. local type=$(uci_get_by_name $NETFLIX_SERVER type)
  428. local bin=$(find_bin $type)
  429. [ ! -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
  430. case "$type" in
  431. ss | ssr)
  432. local bin2=$(find_bin $type-local)
  433. [ ! -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
  434. local name="Shadowsocks"
  435. [ "$type" == "ssr" ] && name="ShadowsocksR"
  436. gen_config_file $NETFLIX_SERVER 2 4321
  437. gen_config_file $NETFLIX_SERVER 3 1088
  438. $bin -c /var/etc/shadowsocksr_n.json $ARG_OTA -f /var/run/ssr-netflix.pid >/dev/null 2>&1
  439. $bin2 -c /var/etc/shadowsocksr_s.json $ARG_OTA -f /var/run/ssr-socksdns.pid >/dev/null 2>&1
  440. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  441. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:$name Started!" >>/tmp/ssrplus.log
  442. ;;
  443. vmess | vless)
  444. lua /usr/share/shadowsocksr/genv2config.lua $NETFLIX_SERVER tcp 4321 1088 >/var/etc/v2-ssr-netflix.json
  445. sed -i 's/\\//g' /var/etc/v2-ssr-netflix.json
  446. $bin -config /var/etc/v2-ssr-netflix.json >/dev/null 2>&1 &
  447. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  448. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:$($bin -version | head -1) Started!" >>/tmp/ssrplus.log
  449. ;;
  450. trojan)
  451. lua /usr/share/shadowsocksr/gentrojanconfig.lua $NETFLIX_SERVER nat 4321 >/var/etc/trojan-ssr-netflix.json
  452. sed -i 's/\\//g' /var/etc/trojan-ssr-netflix.json
  453. $bin --config /var/etc/trojan-ssr-netflix.json >/dev/null 2>&1 &
  454. lua /usr/share/shadowsocksr/gentrojanconfig.lua $NETFLIX_SERVER client 1088 >/var/etc/trojan-ssr-socksdns.json
  455. sed -i 's/\\//g' /var/etc/trojan-ssr-socksdns.json
  456. $bin --config /var/etc/trojan-ssr-socksdns.json >/dev/null 2>&1 &
  457. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  458. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:$($bin --version 2>&1 | head -1) Started!" >>/tmp/ssrplus.log
  459. ;;
  460. naiveproxy)
  461. gen_config_file $NETFLIX_SERVER 1 4321
  462. $bin /var/etc/naive-ssr-netflix.json >/dev/null 2>&1 &
  463. gen_config_file $NETFLIX_SERVER 2 1088
  464. $bin /var/etc/naive-ssr-socksdns.json >/dev/null 2>&1 &
  465. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  466. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:$($bin --version | head -1) Started!" >>/tmp/ssrplus.log
  467. ;;
  468. socks5)
  469. /usr/share/shadowsocksr/genred2config.sh "/var/etc/redsocks-ssr-netflix.conf" socks5 tcp 4321 \
  470. $(get_host_ip $(uci_get_by_name $NETFLIX_SERVER server)) $(uci_get_by_name $NETFLIX_SERVER server_port) \
  471. $(uci_get_by_name $NETFLIX_SERVER auth_enable 0) $(uci_get_by_name $NETFLIX_SERVER username) $(uci_get_by_name $NETFLIX_SERVER password)
  472. $bin -c /var/etc/redsocks-ssr-netflix.conf >/dev/null 2>&1
  473. microsocks -i 127.0.0.1 -p 1088 ssr-socksdns >/dev/null 2>&1 &
  474. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  475. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:Socks5 Started!" >>/tmp/ssrplus.log
  476. ;;
  477. tun)
  478. /usr/share/shadowsocksr/genred2config.sh "/var/etc/redsocks-ssr-netflix.conf" vpn $(uci_get_by_name $NETFLIX_SERVER iface "br-lan") 4321
  479. $bin -c /var/etc/redsocks-ssr-netflix.conf >/dev/null 2>&1
  480. microsocks -i 127.0.0.1 -p 1088 ssr-socksdns >/dev/null 2>&1 &
  481. dns2socks 127.0.0.1:1088 8.8.8.8:53 127.0.0.1:5555 -q >/dev/null 2>&1 &
  482. echo "$(date "+%Y-%m-%d %H:%M:%S") Netflix shunt:Network Tunnel REDIRECT Started!" >>/tmp/ssrplus.log
  483. ;;
  484. esac
  485. fi
  486. return 0
  487. }
  488. start_local() {
  489. local local_server=$(uci_get_by_type socks5_proxy server nil)
  490. [ "$local_server" == "same" ] && local_server=$GLOBAL_SERVER
  491. [ "$local_server" == "nil" ] && return 1
  492. local type=$(uci_get_by_name $local_server type)
  493. local bin=$(find_bin $type)
  494. [ ! -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
  495. case "$type" in
  496. ss | ssr)
  497. local name="Shadowsocks"
  498. local bin=$(find_bin $type-local)
  499. [ ! -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
  500. [ "$type" == "ssr" ] && name="ShadowsocksR"
  501. gen_config_file $local_server 3 $(uci_get_by_type socks5_proxy local_port 1080)
  502. $bin -c $CONFIG_SOCK5_FILE -u -f /var/run/ssr-local.pid >/dev/null 2>&1
  503. echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:$name Started!" >>/tmp/ssrplus.log
  504. ;;
  505. vmess | vless)
  506. 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
  507. sed -i 's/\\//g' /var/etc/v2-ssr-local.json
  508. $bin -config /var/etc/v2-ssr-local.json >/dev/null 2>&1 &
  509. echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:$($bin -version | head -1) Started!" >>/tmp/ssrplus.log
  510. ;;
  511. trojan)
  512. lua /usr/share/shadowsocksr/gentrojanconfig.lua $local_server client $(uci_get_by_type socks5_proxy local_port 1080) >/var/etc/trojan-ssr-local.json
  513. sed -i 's/\\//g' /var/etc/trojan-ssr-local.json
  514. $bin --config /var/etc/trojan-ssr-local.json >/dev/null 2>&1 &
  515. echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:$($bin --version 2>&1 | head -1) Started!" >>/tmp/ssrplus.log
  516. ;;
  517. naiveproxy)
  518. gen_config_file $local_server 3 $(uci_get_by_type socks5_proxy local_port 1080)
  519. $bin /var/etc/naive-ssr-local.json >/dev/null 2>&1 &
  520. echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:$($bin --version | head -1) Started!" >>/tmp/ssrplus.log
  521. ;;
  522. *)
  523. [ -e /proc/sys/net/ipv6 ] && local listenip='-i ::'
  524. microsocks $listenip -p $(uci_get_by_type socks5_proxy local_port 1080) ssr-local >/dev/null 2>&1 &
  525. echo "$(date "+%Y-%m-%d %H:%M:%S") Global_Socks5:$type Started!" >>/tmp/ssrplus.log
  526. ;;
  527. esac
  528. local_enable=1
  529. return 0
  530. }
  531. start_server() {
  532. SERVER_ENABLE=$(uci_get_by_type server_global enable_server 0)
  533. [ "$SERVER_ENABLE" == "0" ] && return 0
  534. server_service() {
  535. [ $(uci_get_by_name $1 enable 0) == "0" ] && return 1
  536. let server_count=server_count+1
  537. if [ "$server_count" == "1" ]; then
  538. if ! (iptables-save -t filter | grep SSR-SERVER-RULE >/dev/null); then
  539. iptables -N SSR-SERVER-RULE && \
  540. iptables -t filter -I INPUT -j SSR-SERVER-RULE
  541. fi
  542. fi
  543. if [ "$(uci_get_by_name $1 type ssr)" == "ssr" ]; then
  544. local bin=$(find_bin ssr-server)
  545. [ ! -f "$bin" ] && echo "$(date "+%Y-%m-%d %H:%M:%S") Server:Can't find $bin program, can't start!" >>/tmp/ssrplus.log && return 1
  546. gen_service_file $1 /var/etc/${NAME}_$server_count.json
  547. $(find_bin ssr-server) -c /var/etc/${NAME}_$server_count.json -u -f /var/run/ssr-server$server_count.pid >/dev/null 2>&1
  548. echo "$(date "+%Y-%m-%d %H:%M:%S") Server:ShadowsocksR Server$server_count Started!" >>/tmp/ssrplus.log
  549. else
  550. [ -e /proc/sys/net/ipv6 ] && local listenip='-i ::'
  551. 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 &
  552. echo "$(date "+%Y-%m-%d %H:%M:%S") Server:Socks5 Server$server_count Started!" >>/tmp/ssrplus.log
  553. fi
  554. iptables -t filter -A SSR-SERVER-RULE -p tcp --dport $(uci_get_by_name $1 server_port) -j ACCEPT
  555. iptables -t filter -A SSR-SERVER-RULE -p udp --dport $(uci_get_by_name $1 server_port) -j ACCEPT
  556. return 0
  557. }
  558. config_load $NAME
  559. config_foreach server_service server_config
  560. gen_serv_include
  561. return 0
  562. }
  563. start_switch() {
  564. if [ "$(uci_get_by_type global enable_switch 0)" == "1" ]; then
  565. if [ -z "$switch_server" ]; then
  566. local switch_time=$(uci_get_by_type global switch_time)s
  567. local switch_timeout=$(uci_get_by_type global switch_timeout)
  568. service_start /usr/bin/ssr-switch start $switch_time $switch_timeout
  569. switch_enable=1
  570. fi
  571. fi
  572. }
  573. start_monitor() {
  574. if [ $(uci_get_by_type global monitor_enable 1) == "1" ]; then
  575. let total_count=server_count+redir_tcp+redir_udp+tunnel_enable+kcp_enable_flag+local_enable+pdnsd_enable_flag+switch_enable
  576. if [ $total_count -gt 0 ]; then
  577. service_start /usr/bin/ssr-monitor $server_count $redir_tcp $redir_udp $tunnel_enable $kcp_enable_flag $local_enable $pdnsd_enable_flag $switch_enable
  578. fi
  579. fi
  580. }
  581. check_server() {
  582. ENABLE_SERVER=$(uci_get_by_type global global_server nil)
  583. if [ "$ENABLE_SERVER" == "nil" ]; then
  584. return 1
  585. else
  586. local STYPE=$(uci_get_by_name $ENABLE_SERVER type nil)
  587. if [ "$STYPE" == "nil" ]; then
  588. local CFGID=$(uci_get_by_cfgid servers type nil)
  589. if [ "$CFGID" == "nil" ]; then
  590. uci_set_by_type global global_server 'nil'
  591. else
  592. uci_set_by_type global global_server $CFGID
  593. fi
  594. /etc/init.d/shadowsocksr restart
  595. fi
  596. fi
  597. }
  598. start_rules() {
  599. if [ -z "$switch_server" ]; then
  600. GLOBAL_SERVER=$(uci_get_by_type global global_server nil)
  601. else
  602. GLOBAL_SERVER=$switch_server
  603. switch_enable=1
  604. fi
  605. if [ "$GLOBAL_SERVER" == "nil" ]; then
  606. return 1
  607. fi
  608. NETFLIX_SERVER=$(uci_get_by_type global netflix_server nil)
  609. if [ "$NETFLIX_SERVER" == "same" ]; then
  610. NETFLIX_SERVER=$GLOBAL_SERVER
  611. fi
  612. run_mode=$(uci_get_by_type global run_mode)
  613. UDP_RELAY_SERVER=$(uci_get_by_type global udp_relay_server)
  614. if [ "$UDP_RELAY_SERVER" == "same" ] ; then
  615. UDP_RELAY_SERVER=$GLOBAL_SERVER
  616. fi
  617. local server=$(get_host_ip $(uci_get_by_name $GLOBAL_SERVER server))
  618. if [ "$server" == "ERROR" ]; then
  619. server=$(uci_get_by_name $GLOBAL_SERVER ip)
  620. fi
  621. local kcp_enable=$(uci_get_by_name $GLOBAL_SERVER kcp_enable 0)
  622. if [ $kcp_enable == "1" ]; then
  623. kcp_server=$server
  624. kcp_flag=1
  625. fi
  626. local local_port=$(uci_get_by_name $GLOBAL_SERVER local_port)
  627. local lan_ac_ips=$(uci_get_by_type access_control lan_ac_ips)
  628. local lan_ac_mode=$(uci_get_by_type access_control lan_ac_mode)
  629. local router_proxy=$(uci_get_by_type access_control router_proxy)
  630. if [ "$GLOBAL_SERVER" == "$UDP_RELAY_SERVER" -a "$kcp_flag" == "0" ]; then
  631. ARG_UDP="-u"
  632. elif [ -n "$UDP_RELAY_SERVER" ]; then
  633. ARG_UDP="-U"
  634. local udp_server=$(uci_get_by_name $UDP_RELAY_SERVER server)
  635. local udp_local_port=$(uci_get_by_name $UDP_RELAY_SERVER local_port)
  636. fi
  637. if [ -n "$lan_ac_ips" ]; then
  638. case "$lan_ac_mode" in
  639. w | W | b | B) local ac_ips="$lan_ac_mode$lan_ac_ips" ;;
  640. esac
  641. fi
  642. #deal gfw firewall rule
  643. local gfwmode=""
  644. case "$run_mode" in
  645. gfw) gfwmode="-g" ;;
  646. router) gfwmode="-r" ;;
  647. oversea) gfwmode="-c" ;;
  648. all) gfwmode="-z" ;;
  649. esac
  650. local dports=$(uci_get_by_type global dports 1)
  651. if [ $dports == "1" ]; then
  652. proxyport=" "
  653. else
  654. proxyport="-m multiport --dports 22,53,587,465,995,993,143,80,443,853,9418"
  655. fi
  656. if [ "$NETFLIX_SERVER" != "nil" ]; then
  657. if [ "$NETFLIX_SERVER" != "$GLOBAL_SERVER" ]; then
  658. netflix="1"
  659. else
  660. netflix="2"
  661. fi
  662. else
  663. netflix="0"
  664. fi
  665. get_arg_out() {
  666. case "$(uci_get_by_type access_control router_proxy 1)" in
  667. 1) echo "-o" ;;
  668. 2) echo "-O" ;;
  669. esac
  670. }
  671. netflix_ip=$(get_host_ip $(uci_get_by_name $NETFLIX_SERVER server))
  672. if [ "$netflix_ip" == "ERROR" ]; then
  673. netflix_ip=$(uci_get_by_name $NETFLIX_SERVER ip)
  674. fi
  675. /usr/bin/ssr-rules \
  676. -s "$server" \
  677. -l "$local_port" \
  678. -S "$udp_server" \
  679. -L "$udp_local_port" \
  680. -a "$ac_ips" \
  681. -i "/etc/ssr/china_ssr.txt" \
  682. -b "$(uci_get_by_type access_control wan_bp_ips)" \
  683. -w "$(uci_get_by_type access_control wan_fw_ips)" \
  684. -B "$(uci_get_by_type access_control lan_bp_ips)" \
  685. -p "$(uci_get_by_type access_control lan_fp_ips)" \
  686. -G "$(uci_get_by_type access_control lan_gm_ips)" \
  687. -m "$(uci_get_by_type access_control Interface)" \
  688. -D "$proxyport" \
  689. -F "$netflix" \
  690. -N "$netflix_ip" \
  691. -M "$(uci_get_by_type global netflix_proxy 0)" \
  692. -I "/etc/ssr/netflixip.list" \
  693. $(get_arg_out) $gfwmode $ARG_UDP
  694. return $?
  695. }
  696. start() {
  697. [ -f "$LOCK_FILE" ] && exit 2
  698. touch "$LOCK_FILE"
  699. echo "----------start------------" >>/tmp/ssrplus.log
  700. mkdir -p /tmp/dnsmasq.d /tmp/dnsmasq.ssr /var/run /var/etc
  701. cat <<-EOF >/tmp/dnsmasq.d/dnsmasq-ssr.conf
  702. conf-dir=/tmp/dnsmasq.ssr
  703. EOF
  704. if start_rules; then
  705. if start_redir_tcp; then
  706. start_redir_udp
  707. start_shunt
  708. start_switch
  709. start_dns
  710. add_cron
  711. /usr/share/shadowsocksr/gfw2ipset.sh $switch_server
  712. else
  713. /usr/bin/ssr-rules -f
  714. fi
  715. else
  716. echo "$(date "+%Y-%m-%d %H:%M:%S") 未启动主节点,禁止连接的域名正在加载。" >>/tmp/ssrplus.log
  717. sed "/.*/s/.*/address=\/&\//" /etc/ssr/deny.list >/tmp/dnsmasq.ssr/denylist.conf
  718. echo "$(date "+%Y-%m-%d %H:%M:%S") 禁止连接的域名加载完毕。" >>/tmp/ssrplus.log
  719. if [ "$(uci_get_by_type global adblock 0)" == "1" ]; then
  720. echo "$(date "+%Y-%m-%d %H:%M:%S") 未启动主节点,广告过滤正在加载。" >>/tmp/ssrplus.log
  721. [ "$1" == "" ] && cp -f /etc/ssr/ad.conf /tmp/dnsmasq.ssr/
  722. if [ -f "/tmp/dnsmasq.ssr/ad.conf" ]; then
  723. for line in $(cat /etc/ssr/black.list); do sed -i "/$line/d" /tmp/dnsmasq.ssr/ad.conf; done
  724. for line in $(cat /etc/ssr/white.list); do sed -i "/$line/d" /tmp/dnsmasq.ssr/ad.conf; done
  725. for line in $(cat /etc/ssr/deny.list); do sed -i "/$line/d" /tmp/dnsmasq.ssr/ad.conf; done
  726. fi
  727. echo "$(date "+%Y-%m-%d %H:%M:%S") 广告过滤加载完毕。" >>/tmp/ssrplus.log
  728. fi
  729. fi
  730. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  731. start_server
  732. start_local
  733. start_monitor
  734. check_server
  735. clean_log
  736. echo "-----------end------------" >>/tmp/ssrplus.log
  737. rm -f $LOCK_FILE
  738. }
  739. boot() {
  740. echo "$(date "+%Y-%m-%d %H:%M:%S") boot!" >/tmp/ssrplus.log
  741. mkdir -p /var/lock /tmp/dnsmasq.d /tmp/dnsmasq.ssr /var/run /var/etc
  742. ulimit -n 65535
  743. start
  744. }
  745. stop() {
  746. unlock
  747. /usr/bin/ssr-rules -f
  748. local srulecount=$(iptables -L | grep SSR-SERVER-RULE | wc -l)
  749. if [ $srulecount -gt 0 ]; then
  750. iptables -F SSR-SERVER-RULE
  751. iptables -t filter -D INPUT -j SSR-SERVER-RULE
  752. iptables -X SSR-SERVER-RULE 2>/dev/null
  753. fi
  754. if [ -z "$switch_server" ]; then
  755. ps -w | grep -v "grep" | grep ssr-switch | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  756. rm -f /var/lock/ssr-switch.lock
  757. killall -q -9 kcptun-client
  758. fi
  759. ps -w | grep -v "grep" | grep ssr-monitor | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  760. ps -w | grep -v "grep" | grep "sleep 0000" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  761. 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
  762. rm -f /var/lock/ssr-monitor.lock
  763. if [ -f "/tmp/dnsmasq.d/dnsmasq-ssr.conf" ]; then
  764. rm -rf /tmp/dnsmasq.d/dnsmasq-ssr.conf /tmp/dnsmasq.ssr /tmp/dnsmasq.oversea
  765. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  766. fi
  767. del_cron
  768. }