shadowsocksr 45 KB

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