shadowsocksr 54 KB

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