shadowsocksr 39 KB

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