shadowsocksr 44 KB

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