shadowsocksr 40 KB

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