shadowsocksr 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. #!/bin/sh /etc/rc.common
  2. #
  3. # Copyright (C) 2017 openwrt-ssr
  4. # Copyright (C) 2017 yushi studio <[email protected]>
  5. # Copyright (C) 2018 lean <[email protected]>
  6. # Copyright (C) 2020 Mattraks <[email protected]>
  7. #
  8. # This is free software, licensed under the GNU General Public License v3.
  9. # See /LICENSE for more information.
  10. #
  11. START=95
  12. STOP=15
  13. SERVICE_DAEMONIZE=1
  14. NAME=shadowsocksr
  15. LOCK_FILE=/var/lock/ssrplus.lock
  16. LOG_FILE=/var/log/ssrplus.log
  17. TMP_PATH=/var/etc/ssrplus
  18. TMP_BIN_PATH=$TMP_PATH/bin
  19. TMP_DNSMASQ_PATH=/tmp/dnsmasq.d/dnsmasq-ssrplus.d
  20. tcp_config_file=
  21. udp_config_file=
  22. shunt_config_file=
  23. local_config_file=
  24. shunt_dns_config_file=
  25. tmp_local_port=
  26. ARG_UDP=
  27. dns_port="5335" #dns port
  28. china_dns_port="5333" #china_dns_port
  29. tmp_dns_port="300" #dns2socks temporary port
  30. tmp_udp_port="301" #udp temporary port
  31. tmp_udp_local_port="302" #udp socks temporary port
  32. tmp_shunt_port="303" #shunt temporary port
  33. tmp_shunt_local_port="304" #shunt socks temporary port
  34. tmp_shunt_dns_port="305" #shunt dns2socks temporary port
  35. tmp_tcp_local_port="306" #tcp socks temporary port
  36. server_count=0
  37. redir_tcp=0
  38. redir_udp=0
  39. local_enable=0
  40. kcp_enable_flag=0
  41. pdnsd_enable_flag=0
  42. switch_server=$1
  43. CRON_FILE=/etc/crontabs/root
  44. EXTRA_COMMANDS='reset'
  45. EXTRA_HELP=" reset Reset to default settings"
  46. #extra_command "reset" "Reset to default settings"
  47. PS="/bin/busybox ps"
  48. uci_get_by_name() {
  49. local ret=$(uci get $NAME.$1.$2 2>/dev/null)
  50. echo ${ret:=$3}
  51. }
  52. uci_get_by_type() {
  53. local ret=$(uci get $NAME.@$1[0].$2 2>/dev/null)
  54. echo ${ret:=$3}
  55. }
  56. uci_set_by_name() {
  57. uci set $NAME.$1.$2=$3 2>/dev/null
  58. uci commit $NAME
  59. }
  60. uci_set_by_type() {
  61. uci set $NAME.@$1[0].$2=$3 2>/dev/null
  62. uci commit $NAME
  63. }
  64. uci_get_by_cfgid() {
  65. local ret=$(uci show $NAME.@$1[0].$2 | awk -F '.' '{print $2}' 2>/dev/null)
  66. echo ${ret:=$3}
  67. }
  68. get_host_ip() {
  69. local host=$(uci_get_by_name $1 server)
  70. local ip=$host
  71. if [ -z "$(echo $host | grep -E "([0-9]{1,3}[\.]){3}[0-9]{1,3}")" ]; then
  72. if [ "$host" == "${host#*:[0-9a-fA-F]}" ]; then
  73. ip=$(resolveip -4 -t 3 $host | awk 'NR==1{print}')
  74. [ -z "$ip" ] && ip=$(wget -q -O- http://119.29.29.29/d?dn=$host | awk -F ';' '{print $1}')
  75. fi
  76. fi
  77. [ -z "$ip" ] || uci_set_by_name $1 ip $ip
  78. [ -n "$ip" ] || ip="$(uci_get_by_name $1 ip "ERROR")"
  79. local chinadns="$(uci_get_by_type global chinadns_forward)"
  80. if [ -n "$chinadns" ] && [ "$ip" != "$host" ]; then
  81. grep -q "$host" "$TMP_DNSMASQ_PATH/chinadns_fixed_server.conf" 2>"/dev/null" || \
  82. echo -e "address=/$host/$ip" >> "$TMP_DNSMASQ_PATH/chinadns_fixed_server.conf"
  83. fi
  84. echo $ip
  85. }
  86. clean_log() {
  87. local logsnum=$(cat $LOG_FILE 2>/dev/null | wc -l)
  88. [ "$logsnum" -gt 1000 ] && {
  89. echo "$(date "+%Y-%m-%d %H:%M:%S") 日志文件过长,清空处理!" >$LOG_FILE
  90. }
  91. }
  92. echolog() {
  93. local d="$(date "+%Y-%m-%d %H:%M:%S")"
  94. echo -e "$d: $*" >>$LOG_FILE
  95. }
  96. add_cron() {
  97. touch $CRON_FILE
  98. sed -i '/ssrplus.log/d' $CRON_FILE
  99. [ $(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 >$LOG_FILE" >>$CRON_FILE
  100. crontab $CRON_FILE
  101. }
  102. del_cron() {
  103. touch $CRON_FILE
  104. sed -i '/ssrplus.log/d' $CRON_FILE
  105. crontab $CRON_FILE
  106. clean_log
  107. }
  108. set_lock() {
  109. exec 1000>"$LOCK_FILE"
  110. flock -xn 1000
  111. }
  112. unset_lock() {
  113. flock -u 1000
  114. rm -rf "$LOCK_FILE"
  115. }
  116. unlock() {
  117. failcount=1
  118. while [ "$failcount" -le 10 ]; do
  119. if [ -f "$LOCK_FILE" ]; then
  120. let "failcount++"
  121. sleep 1s
  122. [ "$failcount" -ge 10 ] && unset_lock
  123. else
  124. break
  125. fi
  126. done
  127. }
  128. _exit() {
  129. local rc=$1
  130. unset_lock
  131. exit ${rc}
  132. }
  133. first_type() {
  134. type -t -p "/bin/${1}" -p "${TMP_BIN_PATH}/${1}" -p "${1}" "$@" | head -n1
  135. }
  136. ln_start_bin() {
  137. local file_func=${1}
  138. local ln_name=${2}
  139. shift 2
  140. if [ "${file_func%%/*}" != "${file_func}" ]; then
  141. [ ! -L "${file_func}" ] && {
  142. ln -s "${file_func}" "${TMP_BIN_PATH}/${ln_name}" >/dev/null 2>&1
  143. file_func="${TMP_BIN_PATH}/${ln_name}"
  144. }
  145. [ -x "${file_func}" ] || echolog "$(readlink ${file_func}) 没有执行权限,无法启动:${file_func} $*"
  146. fi
  147. #echo "${file_func} $*" >&2
  148. [ -x "${file_func}" ] || {
  149. echolog "找不到 ${file_func},无法启动..."
  150. echolog "-----------end------------"
  151. _exit 2
  152. }
  153. ulimit -n 1000000
  154. ${file_func:-echolog " - ${ln_name}"} "$@" >/dev/null 2>&1 &
  155. }
  156. start_dns() {
  157. local ssrplus_dns="$(uci_get_by_type global pdnsd_enable 0)"
  158. local dnsserver="$(uci_get_by_type global tunnel_forward 8.8.4.4:53)"
  159. local run_mode="$(uci_get_by_type global run_mode)"
  160. if [ "$ssrplus_dns" != "0" ]; then
  161. case "$run_mode" in
  162. gfw) ipset add gfwlist ${dnsserver%:*} 2>/dev/null ;;
  163. oversea) ipset add oversea ${dnsserver%:*} 2>/dev/null ;;
  164. *) ipset add ss_spec_wan_ac ${dnsserver%:*} nomatch 2>/dev/null ;;
  165. esac
  166. case "$ssrplus_dns" in
  167. 1)
  168. ln_start_bin $(first_type dns2tcp) dns2tcp -L 127.0.0.1#$dns_port -R ${dnsserver/:/#}
  169. pdnsd_enable_flag=1
  170. ;;
  171. 2)
  172. ln_start_bin $(first_type microsocks) microsocks -i 127.0.0.1 -p $tmp_dns_port ssrplus-dns
  173. ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_dns_port $dnsserver 127.0.0.1:$dns_port -q
  174. pdnsd_enable_flag=2
  175. ;;
  176. esac
  177. if [ "$run_mode" = "router" ]; then
  178. local chinadns="$(uci_get_by_type global chinadns_forward)"
  179. if [ -n "$chinadns" ]; then
  180. local wandns="$(ifstatus wan | jsonfilter -e '@["dns-server"][0]' || echo "119.29.29.29")"
  181. case "$chinadns" in
  182. "wan") chinadns="$wandns" ;;
  183. "wan_114") chinadns="$wandns,114.114.114.114" ;;
  184. esac
  185. ln_start_bin $(first_type chinadns-ng) chinadns-ng -l $china_dns_port -4 china -p 3 -c ${chinadns/:/#} -t 127.0.0.1#$dns_port -N -f -r
  186. cat <<-EOF > "$TMP_DNSMASQ_PATH/chinadns_fixed_server.conf"
  187. no-poll
  188. no-resolv
  189. server=127.0.0.1#$china_dns_port
  190. EOF
  191. fi
  192. fi
  193. fi
  194. }
  195. gen_service_file() {
  196. if [ $(uci_get_by_name $2 fast_open) == "1" ]; then
  197. local fastopen="true"
  198. else
  199. local fastopen="false"
  200. fi
  201. if [ $1 == "ssr" ]; then
  202. cat <<-EOF >$3
  203. {
  204. "server": "0.0.0.0",
  205. "server_ipv6": "::",
  206. "server_port": $(uci_get_by_name $2 server_port),
  207. "mode": "tcp_and_udp",
  208. "password": "$(uci_get_by_name $2 password)",
  209. "timeout": $(uci_get_by_name $2 timeout 60),
  210. "method": "$(uci_get_by_name $2 encrypt_method)",
  211. "protocol": "$(uci_get_by_name $2 protocol)",
  212. "protocol_param": "$(uci_get_by_name $2 protocol_param)",
  213. "obfs": "$(uci_get_by_name $2 obfs)",
  214. "obfs_param": "$(uci_get_by_name $2 obfs_param)",
  215. "fast_open": $fastopen
  216. }
  217. EOF
  218. else
  219. cat <<-EOF >$3
  220. {
  221. "server": "0.0.0.0",
  222. "server_ipv6": "::",
  223. "server_port": $(uci_get_by_name $2 server_port),
  224. "mode": "tcp_and_udp",
  225. "password": "$(uci_get_by_name $2 password)",
  226. "timeout": $(uci_get_by_name $2 timeout 60),
  227. "method": "$(uci_get_by_name $2 encrypt_method_ss)",
  228. "protocol": "socks",
  229. "fast_open": $fastopen
  230. }
  231. EOF
  232. fi
  233. }
  234. get_name() {
  235. case "$1" in
  236. ss) echo "Shadowsocks" ;;
  237. ssr) echo "ShadowsocksR" ;;
  238. esac
  239. }
  240. gen_config_file() { #server1 type2 code3 local_port4 socks_port5 threads5
  241. case "$3" in
  242. 1)
  243. config_file=$tcp_config_file
  244. ;;
  245. 2)
  246. config_file=$udp_config_file
  247. ;;
  248. 3)
  249. if [ -n "$tmp_local_port" ]; then
  250. local tmp_port=$tmp_local_port
  251. else
  252. local tmp_port=$tmp_shunt_local_port
  253. fi
  254. config_file=$shunt_config_file
  255. ;;
  256. 4)
  257. local ss_protocol="socks"
  258. config_file=$local_config_file
  259. ;;
  260. esac
  261. case "$2" in
  262. ss | ssr)
  263. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 ${ss_protocol:-redir} >$config_file
  264. if [ "$3" == "3" ]; then
  265. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $tmp_port socks >$shunt_dns_config_file
  266. fi
  267. ;;
  268. v2ray)
  269. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 $5 >$config_file
  270. ;;
  271. trojan)
  272. case "$3" in
  273. 1)
  274. lua /usr/share/shadowsocksr/gen_config.lua $1 nat $4 >$config_file
  275. ;;
  276. 2)
  277. lua /usr/share/shadowsocksr/gen_config.lua $1 client $4 >$config_file
  278. ;;
  279. 3)
  280. lua /usr/share/shadowsocksr/gen_config.lua $1 nat $4 >$config_file
  281. lua /usr/share/shadowsocksr/gen_config.lua $1 client $tmp_port >$shunt_dns_config_file
  282. ;;
  283. 4)
  284. lua /usr/share/shadowsocksr/gen_config.lua $1 client $4 >$config_file
  285. ;;
  286. esac
  287. ;;
  288. naiveproxy)
  289. case "$3" in
  290. 1)
  291. lua /usr/share/shadowsocksr/gen_config.lua $1 redir $4 >$config_file
  292. ;;
  293. 3)
  294. lua /usr/share/shadowsocksr/gen_config.lua $1 redir $4 >$config_file
  295. lua /usr/share/shadowsocksr/gen_config.lua $1 socks $tmp_port >$shunt_dns_config_file
  296. ;;
  297. 4)
  298. lua /usr/share/shadowsocksr/gen_config.lua $1 socks $4 >$config_file
  299. ;;
  300. esac
  301. ;;
  302. hysteria)
  303. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 $5 >$config_file
  304. ;;
  305. tuic)
  306. lua /usr/share/shadowsocksr/gen_config.lua $1 $mode $4 >$config_file
  307. ;;
  308. socks5)
  309. /usr/share/shadowsocksr/genred2config.sh $config_file $2 $mode $4 \
  310. "$(uci_get_by_name $1 server)" \
  311. "$(uci_get_by_name $1 server_port)" \
  312. "$(uci_get_by_name $1 auth_enable 0)" \
  313. "$(uci_get_by_name $1 username)" \
  314. "$(uci_get_by_name $1 password)"
  315. ;;
  316. tun)
  317. /usr/share/shadowsocksr/genred2config.sh $config_file $2 $(uci_get_by_name $1 iface "br-lan") $4
  318. ;;
  319. esac
  320. sed -i 's/\\//g' $TMP_PATH/*-ssr-*.json
  321. }
  322. start_udp() {
  323. local type=$(uci_get_by_name $UDP_RELAY_SERVER type)
  324. redir_udp=1
  325. case "$type" in
  326. ss | ssr)
  327. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_port
  328. ss_program="$(first_type ${type}local ${type}-redir)"
  329. ln_start_bin $ss_program ${type}-redir -c $udp_config_file
  330. echolog "UDP TPROXY Relay:$(get_name $type) Started!"
  331. ;;
  332. v2ray)
  333. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_port
  334. ln_start_bin $(first_type xray v2ray) v2ray run -config $udp_config_file
  335. echolog "UDP TPROXY Relay:$($(first_type "xray" "v2ray") version | head -1) Started!"
  336. ;;
  337. trojan) #client
  338. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_local_port
  339. ln_start_bin $(first_type trojan) $type --config $udp_config_file
  340. ln_start_bin $(first_type ipt2socks) ipt2socks -U -b 0.0.0.0 -4 -s 127.0.0.1 -p $tmp_udp_local_port -l $tmp_udp_port
  341. echolog "UDP TPROXY Relay:$($(first_type trojan) --version 2>&1 | head -1) Started!"
  342. ;;
  343. naiveproxy)
  344. echolog "NaïveProxy UDP TPROXY Relay not supported!"
  345. redir_udp=0
  346. ARG_UDP=""
  347. ;;
  348. hysteria)
  349. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_port
  350. ln_start_bin $(first_type hysteria) hysteria client --config $udp_config_file
  351. echolog "UDP TPROXY Relay:$($(first_type "hysteria") --version | awk '{print $1,$3}') Started!"
  352. ;;
  353. tuic)
  354. # gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_local_port
  355. # ln_start_bin $(first_type tuic-client) tuic-client --config $udp_config_file
  356. # ln_start_bin $(first_type ipt2socks) ipt2socks -U -b 0.0.0.0 -4 -s 127.0.0.1 -p $tmp_udp_local_port -l $tmp_udp_port
  357. # echolog "UDP TPROXY Relay:tuic-client $($(first_type tuic-client) --version) Started!"
  358. # FIXME: ipt2socks cannot handle udp reply from tuic
  359. echolog "TUIC UDP TPROXY Relay not supported!"
  360. redir_udp=0
  361. ARG_UDP=""
  362. ;;
  363. socks5)
  364. # if [ "$(uci_get_by_name $UDP_RELAY_SERVER auth_enable 0)" == "1" ]; then
  365. # local auth="-a $(uci_get_by_name $UDP_RELAY_SERVER username) -k $(uci_get_by_name $UDP_RELAY_SERVER password)"
  366. # fi
  367. # ln_start_bin $(first_type ipt2socks) ipt2socks $udp_config_file -U -4 -s $(uci_get_by_name $UDP_RELAY_SERVER server) -p $(uci_get_by_name $UDP_RELAY_SERVER server_port) -l $tmp_udp_port $auth
  368. gen_config_file $UDP_RELAY_SERVER $type 2 $tmp_udp_port
  369. ln_start_bin $(first_type redsocks2) redsocks2 -c $udp_config_file
  370. echolog "UDP TPROXY Relay:Socks5 REDIRECT/TPROXY Started!"
  371. ;;
  372. tun)
  373. echolog "Network Tunnel UDP TPROXY Relay not supported!"
  374. redir_udp=0
  375. ARG_UDP=""
  376. ;;
  377. esac
  378. }
  379. start_shunt() {
  380. local type=$(uci_get_by_name $SHUNT_SERVER type)
  381. case "$type" in
  382. ss | ssr)
  383. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  384. ss_program="$(first_type ${type}local ${type}-redir)"
  385. ln_start_bin $ss_program ${type}-redir -c $shunt_config_file
  386. if [ -n "$tmp_local_port" ]; then
  387. local tmp_port=$tmp_local_port
  388. else
  389. local tmp_port=$tmp_shunt_local_port
  390. ln_start_bin $(first_type ${type}local ${type}-local) ${type}-local -c $shunt_dns_config_file
  391. fi
  392. ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_port 8.8.8.8:53 127.0.0.1:$tmp_shunt_dns_port -q
  393. echolog "shunt:$(get_name $type) Started!"
  394. ;;
  395. v2ray)
  396. local tmp_port=${tmp_local_port:-$tmp_shunt_local_port}
  397. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port $tmp_port
  398. ln_start_bin $(first_type xray v2ray) v2ray run -config $shunt_config_file
  399. ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_port 8.8.8.8:53 127.0.0.1:$tmp_shunt_dns_port -q
  400. echolog "shunt:$($(first_type xray v2ray) version | head -1) Started!"
  401. ;;
  402. trojan)
  403. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  404. ln_start_bin $(first_type trojan) $type --config $shunt_config_file
  405. if [ -n "$tmp_local_port" ]; then
  406. local tmp_port=$tmp_local_port
  407. else
  408. local tmp_port=$tmp_shunt_local_port
  409. ln_start_bin $(first_type trojan) $type --config $shunt_dns_config_file
  410. fi
  411. ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_port 8.8.8.8:53 127.0.0.1:$tmp_shunt_dns_port -q
  412. echolog "shunt:$($(first_type trojan) --version 2>&1 | head -1) Started!"
  413. ;;
  414. naiveproxy)
  415. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  416. ln_start_bin $(first_type naive) naive --config $shunt_config_file
  417. if [ -n "$tmp_local_port" ]; then
  418. local tmp_port=$tmp_local_port
  419. else
  420. local tmp_port=$tmp_shunt_local_port
  421. ln_start_bin $(first_type naive) naive --config $shunt_dns_config_file
  422. fi
  423. ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_port 8.8.8.8:53 127.0.0.1:$tmp_shunt_dns_port -q
  424. echolog "shunt:$($(first_type "naive") --version 2>&1 | head -1) Started!"
  425. redir_udp=0
  426. ;;
  427. hysteria)
  428. if [ -n "$tmp_local_port" ]; then
  429. local tmp_port=$tmp_local_port
  430. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  431. else
  432. local tmp_port=$tmp_shunt_local_port
  433. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port $tmp_port
  434. fi
  435. ln_start_bin $(first_type hysteria) hysteria client --config $shunt_config_file
  436. ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_port 8.8.8.8:53 127.0.0.1:$tmp_shunt_dns_port -q
  437. echolog "shunt:$($(first_type hysteria) --version | awk '{print $1,$3}') Started!"
  438. ;;
  439. tuic)
  440. if [ -n "$tmp_local_port" ]; then
  441. local tmp_port=$tmp_local_port
  442. else
  443. local tmp_port=$tmp_shunt_local_port
  444. gen_config_file $SHUNT_SERVER $type 3 $tmp_port
  445. ln_start_bin $(first_type tuic-client) tuic-client --config $shunt_config_file
  446. fi
  447. ln_start_bin $(first_type ipt2socks) ipt2socks -R -b 0.0.0.0 -4 -s 127.0.0.1 -p $tmp_port -l $tmp_shunt_port
  448. ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_port 8.8.8.8:53 127.0.0.1:$tmp_shunt_dns_port -q
  449. echolog "shunt:tuic-client $($(first_type tuic-client) --version) Started!"
  450. # FIXME: ipt2socks cannot handle udp reply from tuic
  451. redir_udp=0
  452. ;;
  453. # socks5)
  454. # if [ "$(uci_get_by_name $SHUNT_SERVER auth_enable 0)" == "1" ]; then
  455. # local auth="-a $(uci_get_by_name $SHUNT_SERVER username) -k $(uci_get_by_name $SHUNT_SERVER password)"
  456. # fi
  457. # ln_start_bin $(first_type ipt2socks) ipt2socks $shunt_config_file -R -4 -s $(uci_get_by_name $SHUNT_SERVER server) -p $(uci_get_by_name $SHUNT_SERVER server_port) -l $tmp_shunt_port $auth
  458. # #gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  459. # #ln_start_bin $(first_type redsocks2) redsocks2 -c $shunt_config_file
  460. # if [ -n "$tmp_local_port" ]; then
  461. # local tmp_port=$tmp_local_port
  462. # else
  463. # local tmp_port=$tmp_shunt_local_port
  464. # ln_start_bin $(first_type microsocks) microsocks -i 127.0.0.1 -p $tmp_port shunt-dns-ssr-plus
  465. # fi
  466. # ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_port 8.8.8.8:53 127.0.0.1:$tmp_shunt_dns_port -q
  467. # echolog "shunt:$type REDIRECT/TPROXY Started!"
  468. # ;;
  469. *)
  470. gen_config_file $SHUNT_SERVER $type 3 $tmp_shunt_port
  471. ln_start_bin $(first_type redsocks2) redsocks2 -c $shunt_config_file
  472. if [ -n "$tmp_local_port" ]; then
  473. local tmp_port=$tmp_local_port
  474. else
  475. local tmp_port=$tmp_shunt_local_port
  476. ln_start_bin $(first_type microsocks) microsocks -i 127.0.0.1 -p $tmp_port shunt-dns-ssr-plus
  477. fi
  478. ln_start_bin $(first_type dns2socks) dns2socks 127.0.0.1:$tmp_port 8.8.8.8:53 127.0.0.1:$tmp_shunt_dns_port -q
  479. echolog "shunt:$type REDIRECT/TPROXY Started!"
  480. ;;
  481. esac
  482. return 0
  483. }
  484. start_local() {
  485. [ "$LOCAL_SERVER" = "nil" ] && return 1
  486. local local_port=$(uci_get_by_type socks5_proxy local_port)
  487. [ "$LOCAL_SERVER" == "$SHUNT_SERVER" ] && tmp_local_port=$local_port
  488. local type=$(uci_get_by_name $LOCAL_SERVER type)
  489. case "$type" in
  490. ss | ssr)
  491. gen_config_file $LOCAL_SERVER $type 4 $local_port
  492. ss_program="$(first_type ${type}local ${type}-local)"
  493. ln_start_bin $ss_program ${type}-local -c $local_config_file
  494. echolog "Global_Socks5:$(get_name $type) Started!"
  495. ;;
  496. v2ray)
  497. if [ "$_local" == "2" ]; then
  498. gen_config_file $LOCAL_SERVER $type 4 0 $local_port
  499. ln_start_bin $(first_type xray v2ray) v2ray run -config $local_config_file
  500. fi
  501. echolog "Global_Socks5:$($(first_type "xray" "v2ray") version | head -1) Started!"
  502. ;;
  503. trojan) #client
  504. gen_config_file $LOCAL_SERVER $type 4 $local_port
  505. ln_start_bin $(first_type trojan) $type --config $local_config_file
  506. echolog "Global_Socks5:$($(first_type trojan) --version 2>&1 | head -1) Started!"
  507. ;;
  508. naiveproxy)
  509. gen_config_file $LOCAL_SERVER $type 4 $local_port
  510. ln_start_bin $(first_type naive) naive --config $local_config_file
  511. echolog "Global_Socks5:$($(first_type $type) --version | head -1) Started!"
  512. ;;
  513. hysteria)
  514. if [ "$_local" == "2" ]; then
  515. gen_config_file $LOCAL_SERVER $type 4 0 $local_port
  516. ln_start_bin $(first_type hysteria) hysteria client --config $local_config_file
  517. echolog "Global_Socks5:$($(first_type hysteria) --version | awk '{print $1,$3}') Started!"
  518. fi
  519. ;;
  520. tuic)
  521. if [ "$_local" == "2" ]; then
  522. gen_config_file $LOCAL_SERVER $type 4 $local_port
  523. ln_start_bin $(first_type tuic-client) tuic-client --config $local_config_file
  524. fi
  525. echolog "Global_Socks5:tuic-client $($(first_type tuic-client) --version) Started!"
  526. ;;
  527. *)
  528. [ -e /proc/sys/net/ipv6 ] && local listenip='-i ::'
  529. ln_start_bin $(first_type microsocks) microsocks $listenip -p $local_port tcp-udp-ssr-local
  530. echolog "Global_Socks5:$type Started!"
  531. ;;
  532. esac
  533. local_enable=1
  534. return 0
  535. }
  536. Start_Run() {
  537. if [ "$(uci_get_by_type global threads 0)" == "0" ]; then
  538. local threads=$(cat /proc/cpuinfo | grep 'processor' | wc -l)
  539. else
  540. local threads=$(uci_get_by_type global threads)
  541. fi
  542. if [ "$(uci_get_by_name $GLOBAL_SERVER kcp_enable 0)" == "1" ]; then
  543. [ ! -f "/usr/bin/kcptun-client" ] && return 1
  544. local kcp_str=$(/usr/bin/kcptun-client -v | grep kcptun | wc -l)
  545. [ "0" == "$kcp_str" ] && return 1
  546. local kcp_server=$(uci_get_by_name $GLOBAL_SERVER server)
  547. local kcp_port=$(uci_get_by_name $GLOBAL_SERVER kcp_port)
  548. local server_port=$(uci_get_by_name $GLOBAL_SERVER server_port)
  549. local password=$(uci_get_by_name $GLOBAL_SERVER kcp_password)
  550. local kcp_param=$(uci_get_by_name $GLOBAL_SERVER kcp_param)
  551. [ "$password" != "" ] && password="--key "$password
  552. service_start /usr/bin/kcptun-client -r $kcp_server:$kcp_port -l :$server_port $password $kcp_param
  553. kcp_enable_flag=1
  554. ARG_UDP=""
  555. fi
  556. if [ "$_local" == "1" ]; then
  557. local socks_port=$(uci_get_by_type socks5_proxy local_port)
  558. tcp_config_file=$TMP_PATH/local-ssr-retcp.json
  559. [ "$mode" == "tcp,udp" ] && tcp_config_file=$TMP_PATH/local-udp-ssr-retcp.json
  560. fi
  561. local tcp_port=$(uci_get_by_name $GLOBAL_SERVER local_port)
  562. local type=$(uci_get_by_name $GLOBAL_SERVER type)
  563. case "$type" in
  564. ss | ssr)
  565. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port
  566. ss_program="$(first_type ${type}local ${type}-redir)"
  567. for i in $(seq 1 $threads); do
  568. ln_start_bin "$ss_program" ${type}-redir -c $tcp_config_file
  569. done
  570. echolog "Main node:$(get_name $type) $threads Threads Started!"
  571. ;;
  572. v2ray)
  573. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port $socks_port
  574. ln_start_bin $(first_type xray v2ray) v2ray run -config $tcp_config_file
  575. echolog "Main node:$($(first_type xray v2ray) version | head -1) Started!"
  576. ;;
  577. trojan)
  578. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port
  579. for i in $(seq 1 $threads); do
  580. ln_start_bin $(first_type $type) $type --config $tcp_config_file
  581. done
  582. echolog "Main node:$($(first_type $type) --version 2>&1 | head -1) , $threads Threads Started!"
  583. ;;
  584. naiveproxy)
  585. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port
  586. ln_start_bin $(first_type naive) naive $tcp_config_file
  587. echolog "Main node:$($(first_type naive) --version 2>&1 | head -1) , $threads Threads Started!"
  588. ;;
  589. hysteria)
  590. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port $socks_port
  591. ln_start_bin $(first_type hysteria) hysteria client --config $tcp_config_file
  592. echolog "Main node:$($(first_type hysteria) --version | awk '{print $1,$3}') Started!"
  593. ;;
  594. tuic)
  595. gen_config_file $GLOBAL_SERVER $type 1 $tmp_tcp_local_port
  596. ln_start_bin $(first_type tuic-client) tuic-client --config $tcp_config_file
  597. ln_start_bin $(first_type ipt2socks) ipt2socks -R -b 0.0.0.0 -4 -s 127.0.0.1 -p $tmp_tcp_local_port -l $tcp_port
  598. echolog "Main node:tuic-client $($(first_type tuic-client) --version) Started!"
  599. ;;
  600. socks5)
  601. if [ "$(uci_get_by_name $GLOBAL_SERVER auth_enable 0)" == "1" ]; then
  602. local auth="-a $(uci_get_by_name $GLOBAL_SERVER username) -k $(uci_get_by_name $GLOBAL_SERVER password)"
  603. fi
  604. ln_start_bin $(first_type ipt2socks) ipt2socks $tcp_config_file -R -4 -j $threads -s $(uci_get_by_name $GLOBAL_SERVER server) -p $(uci_get_by_name $GLOBAL_SERVER server_port) -l $tcp_port $auth
  605. #gen_config_file $GLOBAL_SERVER $type 1 $tcp_port
  606. #for i in $(seq 1 $threads); do
  607. # ln_start_bin $(first_type redsocks2) redsocks2 -c $tcp_config_file
  608. #done
  609. echolog "Main node:Socks5 REDIRECT/TPROXY $threads Threads Started!"
  610. ;;
  611. tun)
  612. gen_config_file $GLOBAL_SERVER $type 1 $tcp_port
  613. for i in $(seq 1 $threads); do
  614. ln_start_bin $(first_type redsocks2) redsocks2 -c $tcp_config_file
  615. done
  616. echolog "Main node:Network Tunnel REDIRECT $threads Threads Started!"
  617. ;;
  618. esac
  619. redir_tcp=1
  620. return 0
  621. }
  622. load_config() {
  623. if [ -z "$switch_server" ]; then
  624. GLOBAL_SERVER=$(uci_get_by_type global global_server nil)
  625. else
  626. GLOBAL_SERVER=$switch_server
  627. fi
  628. LOCAL_SERVER=$(uci_get_by_type socks5_proxy server nil)
  629. if [ "$GLOBAL_SERVER" == "nil" ]; then
  630. mode="tcp,udp"
  631. _local="2"
  632. local_config_file=$TMP_PATH/tcp-udp-ssr-local.json
  633. start_local
  634. return 1
  635. fi
  636. UDP_RELAY_SERVER=$(uci_get_by_type global udp_relay_server nil)
  637. SHUNT_SERVER=$(uci_get_by_type global netflix_server nil)
  638. tcp_config_file=$TMP_PATH/tcp-only-ssr-retcp.json
  639. case "$UDP_RELAY_SERVER" in
  640. nil)
  641. mode="tcp"
  642. ;;
  643. $GLOBAL_SERVER | same)
  644. mode="tcp,udp"
  645. tcp_config_file=$TMP_PATH/tcp-udp-ssr-retcp.json
  646. ARG_UDP="-u"
  647. UDP_RELAY_SERVER=$GLOBAL_SERVER
  648. ;;
  649. *)
  650. mode="udp"
  651. udp_config_file=$TMP_PATH/udp-only-ssr-reudp.json
  652. ARG_UDP="-U"
  653. start_udp
  654. mode="tcp"
  655. ;;
  656. esac
  657. case "$LOCAL_SERVER" in
  658. nil)
  659. _local="0"
  660. ;;
  661. $GLOBAL_SERVER | same)
  662. _local="1"
  663. LOCAL_SERVER=$GLOBAL_SERVER
  664. local_config_file=$TMP_PATH/tcp-udp-ssr-local.json
  665. start_local
  666. local_enable=0
  667. ;;
  668. $SHUNT_SERVER)
  669. _local="3"
  670. local_config_file=$TMP_PATH/tcp-udp-ssr-local.json
  671. start_local
  672. ;;
  673. *)
  674. _local="2"
  675. local_config_file=$TMP_PATH/tcp-udp-ssr-local.json
  676. start_local
  677. ;;
  678. esac
  679. case "$SHUNT_SERVER" in
  680. nil)
  681. shunt="0"
  682. ;;
  683. $GLOBAL_SERVER | same)
  684. shunt="1"
  685. SHUNT_SERVER=$GLOBAL_SERVER
  686. ;;
  687. $LOCAL_SERVER)
  688. shunt="$tmp_shunt_port"
  689. shunt_config_file=$TMP_PATH/tcp-udp-ssr-local.json
  690. shunt_dns_config_file=$TMP_PATH/shunt-dns-ssr-plus.json
  691. start_shunt
  692. ;;
  693. *)
  694. shunt="$tmp_shunt_port"
  695. shunt_config_file=$TMP_PATH/shunt-ssr-retcp.json
  696. shunt_dns_config_file=$TMP_PATH/shunt-dns-ssr-plus.json
  697. start_shunt
  698. ;;
  699. esac
  700. return 0
  701. }
  702. check_server() {
  703. ENABLE_SERVER=$(uci_get_by_type global global_server nil)
  704. if [ "$ENABLE_SERVER" == "nil" ]; then
  705. return 1
  706. else
  707. local STYPE=$(uci_get_by_name $ENABLE_SERVER type nil)
  708. if [ "$STYPE" == "nil" ]; then
  709. local CFGID=$(uci_get_by_cfgid servers type nil)
  710. if [ "$CFGID" == "nil" ]; then
  711. uci_set_by_type global global_server 'nil'
  712. else
  713. uci_set_by_type global global_server $CFGID
  714. fi
  715. /etc/init.d/shadowsocksr restart
  716. fi
  717. fi
  718. }
  719. start_server() {
  720. [ "$(uci_get_by_type server_global enable_server 0)" == "0" ] && return 0
  721. server_service() {
  722. [ "$(uci_get_by_name $1 enable 0)" == "0" ] && return 1
  723. let server_count=server_count+1
  724. if [ "$server_count" == "1" ]; then
  725. if ! (iptables-save -t filter | grep SSR-SERVER-RULE >/dev/null); then
  726. iptables -N SSR-SERVER-RULE && iptables -t filter -I INPUT -j SSR-SERVER-RULE
  727. fi
  728. fi
  729. local type=$(uci_get_by_name $1 type)
  730. case "$type" in
  731. ss | ssr)
  732. gen_service_file ${type} $1 $TMP_PATH/ssr-server$server_count.json
  733. ln_start_bin $(first_type ${type}server ${type}-server) ${type}-server -c $TMP_PATH/ssr-server$server_count.json
  734. echolog "Server: $(get_name ${type}) Server$server_count Started!"
  735. ;;
  736. socks5)
  737. [ -e /proc/sys/net/ipv6 ] && local listenip='-i ::'
  738. ln_start_bin $(first_type microsocks) microsocks $listenip -p $(uci_get_by_name $1 server_port) -1 -u $(uci_get_by_name $1 username) -P $(uci_get_by_name $1 password) ssr-server$server_count
  739. echolog "Server:Socks5 Server$server_count Started!"
  740. ;;
  741. esac
  742. iptables -t filter -A SSR-SERVER-RULE -p tcp --dport $(uci_get_by_name $1 server_port) -j ACCEPT
  743. iptables -t filter -A SSR-SERVER-RULE -p udp --dport $(uci_get_by_name $1 server_port) -j ACCEPT
  744. return 0
  745. }
  746. gen_serv_include() {
  747. local FWI=$(uci get firewall.shadowsocksr.path 2>/dev/null)
  748. [ -n "$FWI" ] || return 0
  749. if [ ! -f $FWI ]; then
  750. echo '#!/bin/sh' >$FWI
  751. fi
  752. extract_rules() {
  753. echo "*filter"
  754. iptables-save -t filter | grep SSR-SERVER-RULE | sed -e "s/^-A INPUT/-I INPUT/"
  755. echo 'COMMIT'
  756. }
  757. cat <<-EOF >>$FWI
  758. iptables-save -c | grep -v "SSR-SERVER" | iptables-restore -c
  759. iptables-restore -n <<-EOT
  760. $(extract_rules)
  761. EOT
  762. EOF
  763. }
  764. config_load $NAME
  765. config_foreach server_service server_config
  766. gen_serv_include
  767. return 0
  768. }
  769. start_switch() {
  770. if [ "$(uci_get_by_type global enable_switch 0)" == "1" ]; then
  771. if [ -z "$switch_server" ]; then
  772. local switch_time=$(uci_get_by_type global switch_time)s
  773. local switch_timeout=$(uci_get_by_type global switch_timeout)
  774. service_start /usr/bin/ssr-switch start $switch_time $switch_timeout
  775. fi
  776. fi
  777. }
  778. start_monitor() {
  779. if [ $(uci_get_by_type global monitor_enable 1) == "1" ]; then
  780. let total_count=server_count+redir_tcp+redir_udp+kcp_enable_flag+local_enable+pdnsd_enable_flag
  781. if [ $total_count -gt 0 ]; then
  782. service_start /usr/bin/ssr-monitor $server_count $redir_tcp $redir_udp $kcp_enable_flag $local_enable $pdnsd_enable_flag
  783. fi
  784. fi
  785. }
  786. start_rules() {
  787. local server=$(get_host_ip $GLOBAL_SERVER)
  788. local local_port=$(uci_get_by_name $GLOBAL_SERVER local_port)
  789. local lan_ac_ips=$(uci_get_by_type access_control lan_ac_ips)
  790. local lan_ac_mode=$(uci_get_by_type access_control lan_ac_mode)
  791. if [ "$kcp_enable_flag" == "0" -a "$redir_udp" == "1" ]; then
  792. local udp_server=$(get_host_ip $UDP_RELAY_SERVER)
  793. local udp_local_port=$tmp_udp_port
  794. fi
  795. if [ "$shunt" != "0" ]; then
  796. local shunt_ip=$(get_host_ip $SHUNT_SERVER)
  797. fi
  798. if [ -n "$lan_ac_ips" ]; then
  799. case "$lan_ac_mode" in
  800. w | W | b | B) local ac_ips="$lan_ac_mode$lan_ac_ips" ;;
  801. esac
  802. fi
  803. gfwmode() {
  804. case "$(uci_get_by_type global run_mode)" in
  805. gfw) echo "-g" ;;
  806. router) echo "-r" ;;
  807. oversea) echo "-c" ;;
  808. all) echo "-z" ;;
  809. esac
  810. }
  811. if [ "$(uci_get_by_type global dports 1)" == "2" ]; then
  812. local proxyport="-m multiport --dports 22,53,587,465,995,993,143,80,443,853,9418"
  813. fi
  814. get_arg_out() {
  815. case "$(uci_get_by_type access_control router_proxy 1)" in
  816. 1) echo "-o" ;;
  817. 2) echo "-O" ;;
  818. esac
  819. }
  820. /usr/share/shadowsocksr/gfw2ipset.sh
  821. /usr/bin/ssr-rules \
  822. -s "$server" \
  823. -l "$local_port" \
  824. -S "$udp_server" \
  825. -L "$udp_local_port" \
  826. -a "$ac_ips" \
  827. -i "/etc/ssrplus/china_ssr.txt" \
  828. -b "$(uci_get_by_type access_control wan_bp_ips)" \
  829. -w "$(uci_get_by_type access_control wan_fw_ips)" \
  830. -B "$(uci_get_by_type access_control lan_bp_ips)" \
  831. -p "$(uci_get_by_type access_control lan_fp_ips)" \
  832. -G "$(uci_get_by_type access_control lan_gm_ips)" \
  833. -m "$(uci_get_by_type access_control Interface)" \
  834. -D "$proxyport" \
  835. -F "$shunt" \
  836. -N "$shunt_ip" \
  837. -M "$(uci_get_by_type global netflix_proxy 0)" \
  838. -I "/etc/ssrplus/netflixip.list" \
  839. $(get_arg_out) $(gfwmode) $ARG_UDP
  840. return $?
  841. }
  842. start() {
  843. set_lock
  844. echolog "----------start------------"
  845. mkdir -p /var/run /var/lock /var/log /tmp/dnsmasq.d $TMP_BIN_PATH $TMP_DNSMASQ_PATH
  846. echo "conf-dir=${TMP_DNSMASQ_PATH}" >"/tmp/dnsmasq.d/dnsmasq-ssrplus.conf"
  847. if load_config; then
  848. Start_Run
  849. start_rules
  850. start_dns
  851. add_cron
  852. start_switch
  853. else
  854. echolog "未启动主节点,禁止连接的域名正在加载。"
  855. cat /etc/ssrplus/deny.list | sed '/^$/d' | sed '/#/d' | sed "/.*/s/.*/address=\/&\//" >$TMP_DNSMASQ_PATH/denylist.conf
  856. echolog "禁止连接的域名加载完毕。"
  857. if [ "$(uci_get_by_type global adblock 0)" == "1" ]; then
  858. echolog "未启动主节点,广告过滤正在加载。"
  859. cp -f /etc/ssrplus/ad.conf $TMP_DNSMASQ_PATH/
  860. if [ -f "$TMP_DNSMASQ_PATH/ad.conf" ]; then
  861. for line in $(cat /etc/ssrplus/black.list); do sed -i "/$line/d" $TMP_DNSMASQ_PATH/ad.conf; done
  862. for line in $(cat /etc/ssrplus/white.list); do sed -i "/$line/d" $TMP_DNSMASQ_PATH/ad.conf; done
  863. for line in $(cat /etc/ssrplus/deny.list); do sed -i "/$line/d" $TMP_DNSMASQ_PATH/ad.conf; done
  864. fi
  865. echolog "广告过滤加载完毕。"
  866. fi
  867. fi
  868. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  869. check_server
  870. start_server
  871. start_monitor
  872. clean_log
  873. echolog "-----------end------------"
  874. unset_lock
  875. }
  876. boot() {
  877. echolog "boot!"
  878. mkdir -p /var/run /var/lock /var/log /tmp/dnsmasq.d $TMP_BIN_PATH $TMP_DNSMASQ_PATH
  879. start
  880. }
  881. stop() {
  882. unlock
  883. set_lock
  884. /usr/bin/ssr-rules -f
  885. local srulecount=$(iptables -L | grep SSR-SERVER-RULE | wc -l)
  886. if [ $srulecount -gt 0 ]; then
  887. iptables -F SSR-SERVER-RULE
  888. iptables -t filter -D INPUT -j SSR-SERVER-RULE
  889. iptables -X SSR-SERVER-RULE 2>/dev/null
  890. fi
  891. if [ -z "$switch_server" ]; then
  892. $PS -w | grep -v "grep" | grep ssr-switch | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  893. rm -f /var/lock/ssr-switch.lock
  894. killall -q -9 kcptun-client
  895. fi
  896. $PS -w | grep -v "grep" | grep ssr-monitor | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  897. $PS -w | grep -v "grep" | grep "sleep 0000" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 &
  898. ( \
  899. # Graceful kill first, so programs have the chance to stop its subprocesses
  900. $PS -w | grep -v "grep" | grep "$TMP_PATH" | awk '{print $1}' | xargs kill >/dev/null 2>&1 ; \
  901. sleep 3s; \
  902. # Force kill hanged programs
  903. $PS -w | grep -v "grep" | grep "$TMP_PATH" | awk '{print $1}' | xargs kill -9 >/dev/null 2>&1 ; \
  904. )
  905. killall -q -9 v2ray-plugin obfs-local xray-plugin
  906. rm -f /var/lock/ssr-monitor.lock
  907. if [ "$(uci -q get "dhcp.@dnsmasq[0]._unused_ssrp_changed")" = "1" ]; then
  908. uci -q del "dhcp.@dnsmasq[0].noresolv"
  909. uci -q del_list "dhcp.@dnsmasq[0].server"="127.0.0.1#$china_dns_port"
  910. uci -q rename "dhcp.@dnsmasq[0]._orig_noresolv"="noresolv"
  911. uci -q rename "dhcp.@dnsmasq[0]._orig_server"="server"
  912. uci -q del "dhcp.@dnsmasq[0]._unused_ssrp_changed"
  913. uci -q commit "dhcp"
  914. fi
  915. if [ -f "/tmp/dnsmasq.d/dnsmasq-ssrplus.conf" ]; then
  916. rm -rf /tmp/dnsmasq.d/dnsmasq-ssrplus.conf $TMP_DNSMASQ_PATH $TMP_PATH/*-ssr-*.json $TMP_PATH/ssr-server*.json
  917. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  918. fi
  919. del_cron
  920. unset_lock
  921. }
  922. reset() {
  923. stop
  924. set_lock
  925. rm -rf /etc/config/shadowsocksr $LOG_FILE
  926. touch /etc/config/shadowsocksr $LOG_FILE
  927. uci -q batch <<-EOF >/dev/null
  928. add shadowsocksr global
  929. set shadowsocksr.@global[0].global_server='nil'
  930. set shadowsocksr.@global[0].netflix_server='nil'
  931. set shadowsocksr.@global[0].netflix_proxy='0'
  932. set shadowsocksr.@global[0].threads='0'
  933. set shadowsocksr.@global[0].run_mode='router'
  934. set shadowsocksr.@global[0].dports='2'
  935. set shadowsocksr.@global[0].pdnsd_enable='1'
  936. set shadowsocksr.@global[0].tunnel_forward='8.8.4.4:53'
  937. set shadowsocksr.@global[0].monitor_enable='1'
  938. set shadowsocksr.@global[0].enable_switch='1'
  939. set shadowsocksr.@global[0].switch_time='667'
  940. set shadowsocksr.@global[0].switch_timeout='5'
  941. set shadowsocksr.@global[0].switch_try_count='3'
  942. set shadowsocksr.@global[0].default_packet_encoding='xudp'
  943. set shadowsocksr.@global[0].gfwlist_url='https://fastly.jsdelivr.net/gh/YW5vbnltb3Vz/domain-list-community@release/gfwlist.txt'
  944. set shadowsocksr.@global[0].chnroute_url='https://ispip.clang.cn/all_cn.txt'
  945. set shadowsocksr.@global[0].nfip_url='https://fastly.jsdelivr.net/gh/QiuSimons/Netflix_IP/NF_only.txt'
  946. set shadowsocksr.@global[0].adblock_url='https://anti-ad.net/anti-ad-for-dnsmasq.conf'
  947. add shadowsocksr server_subscribe
  948. set shadowsocksr.@server_subscribe[0].proxy='0'
  949. set shadowsocksr.@server_subscribe[0].auto_update_time='2'
  950. set shadowsocksr.@server_subscribe[0].auto_update='1'
  951. set shadowsocksr.@server_subscribe[0].filter_words='过期时间/剩余流量/QQ群/官网/防失联地址/回国'
  952. set shadowsocksr.@server_subscribe[0].save_words=''
  953. add shadowsocksr access_control
  954. set shadowsocksr.@access_control[0].lan_ac_mode='0'
  955. set shadowsocksr.@access_control[0].router_proxy='1'
  956. add_list shadowsocksr.@access_control[0].wan_fw_ips=149.154.160.0/20
  957. add_list shadowsocksr.@access_control[0].wan_fw_ips=67.198.55.0/24
  958. add_list shadowsocksr.@access_control[0].wan_fw_ips=91.108.4.0/22
  959. add_list shadowsocksr.@access_control[0].wan_fw_ips=91.108.56.0/22
  960. add_list shadowsocksr.@access_control[0].wan_fw_ips=109.239.140.0/24
  961. add_list shadowsocksr.@access_control[0].Interface='lan'
  962. add shadowsocksr socks5_proxy
  963. set shadowsocksr.@socks5_proxy[0].server='nil'
  964. set shadowsocksr.@socks5_proxy[0].local_port='1080'
  965. add shadowsocksr server_global
  966. set shadowsocksr.@server_global[0].enable_server='0'
  967. commit shadowsocksr
  968. EOF
  969. unset_lock
  970. }