shadowsocksr 28 KB

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