shadowsocksr 39 KB

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