shadowsocksr 29 KB

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