shadowsocksr 39 KB

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