shadowsocksr 29 KB

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