shadowsocksr 45 KB

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