shadowsocksr 51 KB

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