shadowsocksr 29 KB

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