ss-tproxy 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. #!/bin/bash
  2. main_cfg='/etc/tproxy/ss-tproxy.conf'
  3. if [ -f $main_cfg ]; then
  4. source $main_cfg
  5. else
  6. echo "No such file or directory: '$main_cfg'" 1>&2
  7. exit 1
  8. fi
  9. function check_depend {
  10. case "$mode" in
  11. tproxy_global)
  12. { command -v ss-redir &>/dev/null || command -v ssr-redir &>/dev/null; } || { echo "Error: ss-redir/ssr-redir is not installed." 1>&2; exit 1; }
  13. { command -v ss-tunnel &>/dev/null || command -v ssr-tunnel &>/dev/null; } || { echo "Error: ss-tunnel/ssr-tunnel is not installed." 1>&2; exit 1; }
  14. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  15. modprobe xt_TPROXY &>/dev/null || { echo "Error: xt_TPROXY module is not installed." 1>&2; exit 1; }
  16. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  17. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  18. ;;
  19. tproxy_global_tcp)
  20. { command -v ss-redir &>/dev/null || command -v ssr-redir &>/dev/null; } || { echo "Error: ss-redir/ssr-redir is not installed." 1>&2; exit 1; }
  21. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  22. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  23. ;;
  24. tproxy_gfwlist)
  25. { command -v ss-redir &>/dev/null || command -v ssr-redir &>/dev/null; } || { echo "Error: ss-redir/ssr-redir is not installed." 1>&2; exit 1; }
  26. { command -v ss-tunnel &>/dev/null || command -v ssr-tunnel &>/dev/null; } || { echo "Error: ss-tunnel/ssr-tunnel is not installed." 1>&2; exit 1; }
  27. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  28. modprobe xt_TPROXY &>/dev/null || { echo "Error: xt_TPROXY module is not installed." 1>&2; exit 1; }
  29. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  30. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  31. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  32. command -v perl &>/dev/null || { echo "Error: perl is not installed." 1>&2; }
  33. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  34. command -v base64 &>/dev/null || { echo "Warning: base64 is not installed." 1>&2; }
  35. ;;
  36. tproxy_gfwlist_tcp)
  37. { command -v ss-redir &>/dev/null || command -v ssr-redir &>/dev/null; } || { echo "Error: ss-redir/ssr-redir is not installed." 1>&2; exit 1; }
  38. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  39. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  40. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  41. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  42. command -v perl &>/dev/null || { echo "Error: perl is not installed." 1>&2; }
  43. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  44. command -v base64 &>/dev/null || { echo "Warning: base64 is not installed." 1>&2; }
  45. ;;
  46. tproxy_chnroute)
  47. { command -v ss-redir &>/dev/null || command -v ssr-redir &>/dev/null; } || { echo "Error: ss-redir/ssr-redir is not installed." 1>&2; exit 1; }
  48. { command -v ss-tunnel &>/dev/null || command -v ssr-tunnel &>/dev/null; } || { echo "Error: ss-tunnel/ssr-tunnel is not installed." 1>&2; exit 1; }
  49. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  50. modprobe xt_TPROXY &>/dev/null || { echo "Error: xt_TPROXY module is not installed." 1>&2; exit 1; }
  51. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  52. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  53. command -v chinadns &>/dev/null || { echo "Error: chinadns is not installed." 1>&2; exit 1; }
  54. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  55. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  56. ;;
  57. tproxy_chnroute_tcp)
  58. { command -v ss-redir &>/dev/null || command -v ssr-redir &>/dev/null; } || { echo "Error: ss-redir/ssr-redir is not installed." 1>&2; exit 1; }
  59. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  60. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  61. command -v chinadns &>/dev/null || { echo "Error: chinadns is not installed." 1>&2; exit 1; }
  62. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  63. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  64. ;;
  65. tun2socks_global)
  66. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  67. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  68. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  69. ;;
  70. tun2socks_global_tcp)
  71. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  72. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  73. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  74. ;;
  75. tun2socks_gfwlist)
  76. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  77. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  78. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  79. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  80. command -v perl &>/dev/null || { echo "Error: perl is not installed." 1>&2; }
  81. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  82. command -v base64 &>/dev/null || { echo "Warning: base64 is not installed." 1>&2; }
  83. ;;
  84. tun2socks_gfwlist_tcp)
  85. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  86. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  87. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  88. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  89. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  90. command -v perl &>/dev/null || { echo "Error: perl is not installed." 1>&2; }
  91. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  92. command -v base64 &>/dev/null || { echo "Warning: base64 is not installed." 1>&2; }
  93. ;;
  94. tun2socks_chnroute)
  95. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  96. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  97. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  98. command -v chinadns &>/dev/null || { echo "Error: chinadns is not installed." 1>&2; exit 1; }
  99. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  100. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  101. ;;
  102. tun2socks_chnroute_tcp)
  103. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  104. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  105. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  106. command -v chinadns &>/dev/null || { echo "Error: chinadns is not installed." 1>&2; exit 1; }
  107. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  108. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  109. ;;
  110. esac
  111. }
  112. function update_gfwlist {
  113. curl -4sSkL https://raw.github.com/gfwlist/gfwlist/master/gfwlist.txt | base64 -d | { perl -pe '
  114. if (/URL Keywords/i) { $null = <> until $null =~ /^!/ }
  115. s#^\s*+$|^!.*+$|^@@.*+$|^\[AutoProxy.*+$|^/.*/$##i;
  116. s@^\|\|?|\|$@@;
  117. s@^https?:/?/?@@i;
  118. s@(?:/|%).*+$@@;
  119. s@\*[^.*]++$@\n@;
  120. s@^.*?\*[^.]*+(?=[^*]+$)@@;
  121. s@^\*?\.|^.*\.\*?$@@;
  122. s@(?=[^0-9a-zA-Z.-]).*+$@@;
  123. s@^\d+\.\d+\.\d+\.\d+(?::\d+)?$@@;
  124. s@^\s*+$@@'
  125. echo 'twimg.edgesuite.net'
  126. echo -e 'blogspot.ae\nblogspot.al\nblogspot.am\nblogspot.ba\nblogspot.be\nblogspot.bg\nblogspot.bj\nblogspot.ca\nblogspot.cat\nblogspot.cf\nblogspot.ch\nblogspot.cl\nblogspot.co.at\nblogspot.co.id\nblogspot.co.il\nblogspot.co.ke\nblogspot.com\nblogspot.com.ar\nblogspot.com.au\nblogspot.com.br\nblogspot.com.by\nblogspot.com.co\nblogspot.com.cy\nblogspot.com.ee\nblogspot.com.eg\nblogspot.com.es\nblogspot.com.mt\nblogspot.com.ng\nblogspot.com.tr\nblogspot.com.uy\nblogspot.co.nz\nblogspot.co.uk\nblogspot.co.za\nblogspot.cv\nblogspot.cz\nblogspot.de\nblogspot.dk\nblogspot.fi\nblogspot.fr\nblogspot.gr\nblogspot.hk\nblogspot.hr\nblogspot.hu\nblogspot.ie\nblogspot.in\nblogspot.is\nblogspot.it\nblogspot.jp\nblogspot.kr\nblogspot.li\nblogspot.lt\nblogspot.lu\nblogspot.md\nblogspot.mk\nblogspot.mr\nblogspot.mx\nblogspot.my\nblogspot.nl\nblogspot.no\nblogspot.pe\nblogspot.pt\nblogspot.qa\nblogspot.re\nblogspot.ro\nblogspot.rs\nblogspot.ru\nblogspot.se\nblogspot.sg\nblogspot.si\nblogspot.sk\nblogspot.sn\nblogspot.td\nblogspot.tw\nblogspot.ug\nblogspot.vn'
  127. echo -e 'google.ac\ngoogle.ad\ngoogle.ae\ngoogle.al\ngoogle.am\ngoogle.as\ngoogle.at\ngoogle.az\ngoogle.ba\ngoogle.be\ngoogle.bf\ngoogle.bg\ngoogle.bi\ngoogle.bj\ngoogle.bs\ngoogle.bt\ngoogle.by\ngoogle.ca\ngoogle.cat\ngoogle.cc\ngoogle.cd\ngoogle.cf\ngoogle.cg\ngoogle.ch\ngoogle.ci\ngoogle.cl\ngoogle.cm\ngoogle.cn\ngoogle.co.ao\ngoogle.co.bw\ngoogle.co.ck\ngoogle.co.cr\ngoogle.co.id\ngoogle.co.il\ngoogle.co.in\ngoogle.co.jp\ngoogle.co.ke\ngoogle.co.kr\ngoogle.co.ls\ngoogle.com\ngoogle.co.ma\ngoogle.com.af\ngoogle.com.ag\ngoogle.com.ai\ngoogle.com.ar\ngoogle.com.au\ngoogle.com.bd\ngoogle.com.bh\ngoogle.com.bn\ngoogle.com.bo\ngoogle.com.br\ngoogle.com.bz\ngoogle.com.co\ngoogle.com.cu\ngoogle.com.cy\ngoogle.com.do\ngoogle.com.ec\ngoogle.com.eg\ngoogle.com.et\ngoogle.com.fj\ngoogle.com.gh\ngoogle.com.gi\ngoogle.com.gt\ngoogle.com.hk\ngoogle.com.jm\ngoogle.com.kh\ngoogle.com.kw\ngoogle.com.lb\ngoogle.com.lc\ngoogle.com.ly\ngoogle.com.mm\ngoogle.com.mt\ngoogle.com.mx\ngoogle.com.my\ngoogle.com.na\ngoogle.com.nf\ngoogle.com.ng\ngoogle.com.ni\ngoogle.com.np\ngoogle.com.om\ngoogle.com.pa\ngoogle.com.pe\ngoogle.com.pg\ngoogle.com.ph\ngoogle.com.pk\ngoogle.com.pr\ngoogle.com.py\ngoogle.com.qa\ngoogle.com.sa\ngoogle.com.sb\ngoogle.com.sg\ngoogle.com.sl\ngoogle.com.sv\ngoogle.com.tj\ngoogle.com.tr\ngoogle.com.tw\ngoogle.com.ua\ngoogle.com.uy\ngoogle.com.vc\ngoogle.com.vn\ngoogle.co.mz\ngoogle.co.nz\ngoogle.co.th\ngoogle.co.tz\ngoogle.co.ug\ngoogle.co.uk\ngoogle.co.uz\ngoogle.co.ve\ngoogle.co.vi\ngoogle.co.za\ngoogle.co.zm\ngoogle.co.zw\ngoogle.cv\ngoogle.cz\ngoogle.de\ngoogle.dj\ngoogle.dk\ngoogle.dm\ngoogle.dz\ngoogle.ee\ngoogle.es\ngoogle.fi\ngoogle.fm\ngoogle.fr\ngoogle.ga\ngoogle.ge\ngoogle.gf\ngoogle.gg\ngoogle.gl\ngoogle.gm\ngoogle.gp\ngoogle.gr\ngoogle.gy\ngoogle.hn\ngoogle.hr\ngoogle.ht\ngoogle.hu\ngoogle.ie\ngoogle.im\ngoogle.io\ngoogle.iq\ngoogle.is\ngoogle.it\ngoogle.je\ngoogle.jo\ngoogle.kg\ngoogle.ki\ngoogle.kz\ngoogle.la\ngoogle.li\ngoogle.lk\ngoogle.lt\ngoogle.lu\ngoogle.lv\ngoogle.md\ngoogle.me\ngoogle.mg\ngoogle.mk\ngoogle.ml\ngoogle.mn\ngoogle.ms\ngoogle.mu\ngoogle.mv\ngoogle.mw\ngoogle.ne\ngoogle.net\ngoogle.nl\ngoogle.no\ngoogle.nr\ngoogle.nu\ngoogle.org\ngoogle.pl\ngoogle.pn\ngoogle.ps\ngoogle.pt\ngoogle.ro\ngoogle.rs\ngoogle.ru\ngoogle.rw\ngoogle.sc\ngoogle.se\ngoogle.sh\ngoogle.si\ngoogle.sk\ngoogle.sm\ngoogle.sn\ngoogle.so\ngoogle.sr\ngoogle.st\ngoogle.td\ngoogle.tg\ngoogle.tk\ngoogle.tl\ngoogle.tm\ngoogle.tn\ngoogle.to\ngoogle.tt\ngoogle.vg\ngoogle.vu\ngoogle.ws'; } | sort | uniq -i >$dnsmasq_gfwlist
  128. }
  129. function update_chnroute {
  130. #chnroute_url="http://f.ip.cn/rt/chnroutes.txt"
  131. chnroute_url="https://github.com/17mon/china_ip_list/raw/master/china_ip_list.txt"
  132. { curl -4sSkL "$chnroute_url"; echo; } | grep -Ev '^\s*$|^\s*#' |
  133. tee >$chinadns_chnroute >(echo '-N chnroute hash:net' >$iptables_ipset_file; xargs -n1 echo -A chnroute >>$iptables_ipset_file)
  134. }
  135. function flush_cache {
  136. case "$mode" in
  137. *global|*gfwlist*|*chnroute) pgrep '^dnsmasq$' | xargs kill -HUP;;
  138. tproxy_global_tcp)
  139. if [ $(pgrep -c '^dnsforwarder$') -ne 0 ]; then
  140. pkill '^dnsforwarder$' &>/dev/null
  141. sleep 2
  142. dnsforwarder -q -d -f <(cat <<EOF
  143. LogOn $dnsfwd_log_on
  144. LogFileThresholdLength 5242880
  145. LogFileFolder $dnsfwd_log_dir
  146. UDPLocal 0.0.0.0:53
  147. TCPGroup $dns_remote * no
  148. BlockNegativeResponse true
  149. Hosts file:///etc/hosts
  150. HostsUpdateInterval 3600
  151. UseCache $dnsfwd_cache_on
  152. MemoryCache $dnsfwd_cache_mem
  153. CacheSize $dnsfwd_cache_size
  154. IgnoreTTL $dnsfwd_ignore_ttl
  155. EOF
  156. ) &>/dev/null
  157. fi;;
  158. tproxy_chnroute_tcp)
  159. if [ $(pgrep -c '^dnsforwarder$') -ne 0 ]; then
  160. pkill '^dnsforwarder$' &>/dev/null
  161. sleep 2
  162. dnsforwarder -q -d -f <(cat <<EOF
  163. LogOn false
  164. UseCache false
  165. UDPLocal 0.0.0.0:60053
  166. TCPGroup $dns_remote * no
  167. BlockNegativeResponse true
  168. EOF
  169. ) &>/dev/null
  170. dnsforwarder -q -d -f <(cat <<EOF
  171. LogOn $dnsfwd_log_on
  172. LogFileThresholdLength 5242880
  173. LogFileFolder $dnsfwd_log_dir
  174. UDPLocal 0.0.0.0:53
  175. UDPGroup 127.0.0.1:65353 * on
  176. BlockNegativeResponse true
  177. Hosts file:///etc/hosts
  178. HostsUpdateInterval 3600
  179. UseCache $dnsfwd_cache_on
  180. MemoryCache $dnsfwd_cache_mem
  181. CacheSize $dnsfwd_cache_size
  182. IgnoreTTL $dnsfwd_ignore_ttl
  183. EOF
  184. ) &>/dev/null
  185. fi;;
  186. tun2socks_global_tcp)
  187. if [ $(pgrep -c '^dnsforwarder$') -ne 0 ]; then
  188. pkill '^dnsforwarder$' &>/dev/null
  189. sleep 2
  190. dnsforwarder -q -d -f <(cat <<EOF
  191. LogOn $dnsfwd_log_on
  192. LogFileThresholdLength 5242880
  193. LogFileFolder $dnsfwd_log_dir
  194. UDPLocal 0.0.0.0:53
  195. TCPGroup $dns_remote * $socks5_listen
  196. BlockNegativeResponse true
  197. Hosts file:///etc/hosts
  198. HostsUpdateInterval 3600
  199. UseCache $dnsfwd_cache_on
  200. MemoryCache $dnsfwd_cache_mem
  201. CacheSize $dnsfwd_cache_size
  202. IgnoreTTL $dnsfwd_ignore_ttl
  203. EOF
  204. ) &>/dev/null
  205. fi;;
  206. tun2socks_chnroute_tcp)
  207. if [ $(pgrep -c '^dnsforwarder$') -ne 0 ]; then
  208. pkill '^dnsforwarder$' &>/dev/null
  209. sleep 2
  210. dnsforwarder -q -d -f <(cat <<EOF
  211. LogOn false
  212. UseCache false
  213. UDPLocal 0.0.0.0:60053
  214. TCPGroup $dns_remote * $socks5_listen
  215. BlockNegativeResponse true
  216. EOF
  217. ) &>/dev/null
  218. dnsforwarder -q -d -f <(cat <<EOF
  219. LogOn $dnsfwd_log_on
  220. LogFileThresholdLength 5242880
  221. LogFileFolder $dnsfwd_log_dir
  222. UDPLocal 0.0.0.0:53
  223. UDPGroup 127.0.0.1:65353 * on
  224. BlockNegativeResponse true
  225. Hosts file:///etc/hosts
  226. HostsUpdateInterval 3600
  227. UseCache $dnsfwd_cache_on
  228. MemoryCache $dnsfwd_cache_mem
  229. CacheSize $dnsfwd_cache_size
  230. IgnoreTTL $dnsfwd_ignore_ttl
  231. EOF
  232. ) &>/dev/null
  233. fi;;
  234. esac
  235. }
  236. function start_socks {
  237. case "$mode" in
  238. tproxy*)
  239. if [ "$server_use_ssr" = 'false' ]; then
  240. redir_params="-s'$server_addr' -p'$server_port' -m'$server_method' -k'$server_passwd' -b0.0.0.0 -l60080 --no-delay --reuse-port"
  241. [[ "$mode" != *tcp ]] && redir_params+=' -u'
  242. [ "$redir_verbose" = 'true' ] && redir_params+=' -v'
  243. [ "$redir_fast_open" = 'true' ] && redir_params+=' --fast-open'
  244. eval "nohup ss-redir $redir_params </dev/null &>>'$redir_log' &"
  245. if [[ "$mode" != *tcp ]]; then
  246. tunnel_params="-s'$server_addr' -p'$server_port' -m'$server_method' -k'$server_passwd' -b0.0.0.0 -l60053 -L'$dns_remote' -U --no-delay --reuse-port"
  247. [ "$tunnel_verbose" = 'true' ] && tunnel_params+=' -v'
  248. eval "nohup ss-tunnel $tunnel_params </dev/null &>>'$tunnel_log' &";
  249. fi
  250. else
  251. redir_params="-s'$server_addr' -p'$server_port' -m'$server_method' -k'$server_passwd' -O'$server_protocol' -o'$server_obfs' -b0.0.0.0 -l60080"
  252. [ -n "$server_protocol_param" ] && redir_params+=" -G'$server_protocol_param'"
  253. [ -n "$server_obfs_param" ] && redir_params+=" -g'$server_obfs_param'"
  254. [[ "$mode" != *tcp ]] && redir_params+=' -u'
  255. [ "$redir_verbose" = 'true' ] && redir_params+=' -v'
  256. eval "nohup ssr-redir $redir_params </dev/null &>>'$redir_log' &"
  257. if [[ "$mode" != *tcp ]]; then
  258. tunnel_params="-s'$server_addr' -p'$server_port' -m'$server_method' -k'$server_passwd' -O'$server_protocol' -o'$server_obfs' -b0.0.0.0 -l60053 -L'$dns_remote' -U"
  259. [ -n "$server_protocol_param" ] && tunnel_params+=" -G'$server_protocol_param'"
  260. [ -n "$server_obfs_param" ] && tunnel_params+=" -g'$server_obfs_param'"
  261. [ "$tunnel_verbose" = 'true' ] && tunnel_params+=' -v'
  262. eval "nohup ssr-tunnel $tunnel_params </dev/null &>>'$tunnel_log' &"
  263. fi
  264. fi
  265. ;;
  266. tun2socks*)
  267. eval "$socks5_runcmd"
  268. nohup tun2socks -tun-device $tun2socks_tundev -tun-address $tun2socks_address -tun-mask $tun2socks_netmask -tun-gw $tun2socks_gateway -local-socks-addr $socks5_listen -public-only </dev/null &>>"$tun2socks_log" &
  269. ;;
  270. esac
  271. }
  272. function start_dns {
  273. case "$mode" in
  274. tproxy_global)
  275. dnsmasq -C <(cat <<EOF
  276. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  277. log-async = 20
  278. log-facility = $dnsmasq_log_file
  279. domain-needed
  280. cache-size = $dnsmasq_cache_size
  281. no-negcache
  282. no-resolv
  283. port = 53
  284. server = 127.0.0.1#60053
  285. EOF
  286. );;
  287. tproxy_gfwlist)
  288. dnsmasq -C <(cat <<EOF
  289. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  290. log-async = 20
  291. log-facility = $dnsmasq_log_file
  292. domain-needed
  293. cache-size = $dnsmasq_cache_size
  294. no-negcache
  295. no-resolv
  296. port = 53
  297. $(for dns in "${dns_direct[@]}"; do echo "server = $dns"; done)
  298. $(perl -pe 's@^.*+$@server=/$&/127.0.0.1#60053\nipset=/$&/gfwlist@' $dnsmasq_gfwlist)
  299. EOF
  300. );;
  301. tproxy_chnroute)
  302. chinadns_upstream=$(for dns in "${dns_direct[@]}"; do echo -n "$dns,"; done)
  303. chinadns_params="-b 0.0.0.0 -p 65353 -s ${chinadns_upstream}127.0.0.1:60053 -c $chinadns_chnroute"
  304. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  305. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  306. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  307. dnsmasq -C <(cat <<EOF
  308. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  309. log-async = 20
  310. log-facility = $dnsmasq_log_file
  311. domain-needed
  312. cache-size = $dnsmasq_cache_size
  313. no-negcache
  314. no-resolv
  315. port = 53
  316. server = 127.0.0.1#65353
  317. EOF
  318. );;
  319. tproxy_global_tcp)
  320. mkdir -p /root/.dnsforwarder
  321. dnsforwarder -q -d -f <(cat <<EOF
  322. LogOn $dnsfwd_log_on
  323. LogFileThresholdLength 5242880
  324. LogFileFolder $dnsfwd_log_dir
  325. UDPLocal 0.0.0.0:53
  326. TCPGroup $dns_remote * no
  327. BlockNegativeResponse true
  328. Hosts file:///etc/hosts
  329. HostsUpdateInterval 3600
  330. UseCache $dnsfwd_cache_on
  331. MemoryCache $dnsfwd_cache_mem
  332. CacheSize $dnsfwd_cache_size
  333. IgnoreTTL $dnsfwd_ignore_ttl
  334. EOF
  335. ) &>/dev/null;;
  336. tproxy_gfwlist_tcp)
  337. dnsforwarder -q -d -f <(cat <<EOF
  338. LogOn false
  339. UseCache false
  340. UDPLocal 0.0.0.0:60053
  341. TCPGroup $dns_remote * no
  342. BlockNegativeResponse true
  343. EOF
  344. ) &>/dev/null
  345. dnsmasq -C <(cat <<EOF
  346. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  347. log-async = 20
  348. log-facility = $dnsmasq_log_file
  349. domain-needed
  350. cache-size = $dnsmasq_cache_size
  351. no-negcache
  352. no-resolv
  353. port = 53
  354. $(for dns in "${dns_direct[@]}"; do echo "server = $dns"; done)
  355. $(perl -pe 's@^.*+$@server=/$&/127.0.0.1#60053\nipset=/$&/gfwlist@' $dnsmasq_gfwlist)
  356. EOF
  357. );;
  358. tproxy_chnroute_tcp)
  359. dnsforwarder -q -d -f <(cat <<EOF
  360. LogOn false
  361. UseCache false
  362. UDPLocal 0.0.0.0:60053
  363. TCPGroup $dns_remote * no
  364. BlockNegativeResponse true
  365. EOF
  366. ) &>/dev/null
  367. chinadns_upstream=$(for dns in "${dns_direct[@]}"; do echo -n "$dns,"; done)
  368. chinadns_params="-b 0.0.0.0 -p 65353 -s ${chinadns_upstream}127.0.0.1:60053 -c $chinadns_chnroute"
  369. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  370. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  371. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  372. mkdir -p /root/.dnsforwarder
  373. dnsforwarder -q -d -f <(cat <<EOF
  374. LogOn $dnsfwd_log_on
  375. LogFileThresholdLength 5242880
  376. LogFileFolder $dnsfwd_log_dir
  377. UDPLocal 0.0.0.0:53
  378. UDPGroup 127.0.0.1:65353 * on
  379. BlockNegativeResponse true
  380. Hosts file:///etc/hosts
  381. HostsUpdateInterval 3600
  382. UseCache $dnsfwd_cache_on
  383. MemoryCache $dnsfwd_cache_mem
  384. CacheSize $dnsfwd_cache_size
  385. IgnoreTTL $dnsfwd_ignore_ttl
  386. EOF
  387. ) &>/dev/null;;
  388. tun2socks_global)
  389. dnsmasq -C <(cat <<EOF
  390. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  391. log-async = 20
  392. log-facility = $dnsmasq_log_file
  393. domain-needed
  394. cache-size = $dnsmasq_cache_size
  395. no-negcache
  396. no-resolv
  397. port = 53
  398. server = ${dns_remote/:/#}
  399. EOF
  400. );;
  401. tun2socks_gfwlist)
  402. dnsmasq -C <(cat <<EOF
  403. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  404. log-async = 20
  405. log-facility = $dnsmasq_log_file
  406. domain-needed
  407. cache-size = $dnsmasq_cache_size
  408. no-negcache
  409. no-resolv
  410. port = 53
  411. $(for dns in "${dns_direct[@]}"; do echo "server = $dns"; done)
  412. $(perl -pe "s@^.*+\$@server=/$&/${dns_remote/:/#}\nipset=/$&/gfwlist@" $dnsmasq_gfwlist)
  413. EOF
  414. );;
  415. tun2socks_chnroute)
  416. chinadns_upstream=$(for dns in "${dns_direct[@]}"; do echo -n "$dns,"; done)
  417. chinadns_params="-b 0.0.0.0 -p 60053 -s ${chinadns_upstream}${dns_remote} -c $chinadns_chnroute"
  418. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  419. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  420. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  421. dnsmasq -C <(cat <<EOF
  422. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  423. log-async = 20
  424. log-facility = $dnsmasq_log_file
  425. domain-needed
  426. cache-size = $dnsmasq_cache_size
  427. no-negcache
  428. no-resolv
  429. port = 53
  430. server = 127.0.0.1#60053
  431. EOF
  432. );;
  433. tun2socks_global_tcp)
  434. mkdir -p /root/.dnsforwarder
  435. dnsforwarder -q -d -f <(cat <<EOF
  436. LogOn $dnsfwd_log_on
  437. LogFileThresholdLength 5242880
  438. LogFileFolder $dnsfwd_log_dir
  439. UDPLocal 0.0.0.0:53
  440. TCPGroup $dns_remote * $socks5_listen
  441. BlockNegativeResponse true
  442. Hosts file:///etc/hosts
  443. HostsUpdateInterval 3600
  444. UseCache $dnsfwd_cache_on
  445. MemoryCache $dnsfwd_cache_mem
  446. CacheSize $dnsfwd_cache_size
  447. IgnoreTTL $dnsfwd_ignore_ttl
  448. EOF
  449. ) &>/dev/null;;
  450. tun2socks_gfwlist_tcp)
  451. dnsforwarder -q -d -f <(cat <<EOF
  452. LogOn false
  453. UseCache false
  454. UDPLocal 0.0.0.0:60053
  455. TCPGroup $dns_remote * $socks5_listen
  456. BlockNegativeResponse true
  457. EOF
  458. ) &>/dev/null
  459. dnsmasq -C <(cat <<EOF
  460. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  461. log-async = 20
  462. log-facility = $dnsmasq_log_file
  463. domain-needed
  464. cache-size = $dnsmasq_cache_size
  465. no-negcache
  466. no-resolv
  467. port = 53
  468. $(for dns in "${dns_direct[@]}"; do echo "server = $dns"; done)
  469. $(perl -pe 's@^.*+$@server=/$&/127.0.0.1#60053\nipset=/$&/gfwlist@' $dnsmasq_gfwlist)
  470. EOF
  471. );;
  472. tun2socks_chnroute_tcp)
  473. dnsforwarder -q -d -f <(cat <<EOF
  474. LogOn false
  475. UseCache false
  476. UDPLocal 0.0.0.0:60053
  477. TCPGroup $dns_remote * $socks5_listen
  478. BlockNegativeResponse true
  479. EOF
  480. ) &>/dev/null
  481. chinadns_upstream=$(for dns in "${dns_direct[@]}"; do echo -n "$dns,"; done)
  482. chinadns_params="-b 0.0.0.0 -p 65353 -s ${chinadns_upstream}127.0.0.1:60053 -c $chinadns_chnroute"
  483. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  484. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  485. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  486. mkdir -p /root/.dnsforwarder
  487. dnsforwarder -q -d -f <(cat <<EOF
  488. LogOn $dnsfwd_log_on
  489. LogFileThresholdLength 5242880
  490. LogFileFolder $dnsfwd_log_dir
  491. UDPLocal 0.0.0.0:53
  492. UDPGroup 127.0.0.1:65353 * on
  493. BlockNegativeResponse true
  494. Hosts file:///etc/hosts
  495. HostsUpdateInterval 3600
  496. UseCache $dnsfwd_cache_on
  497. MemoryCache $dnsfwd_cache_mem
  498. CacheSize $dnsfwd_cache_size
  499. IgnoreTTL $dnsfwd_ignore_ttl
  500. EOF
  501. ) &>/dev/null;;
  502. esac
  503. }
  504. function start_rule {
  505. case "$mode" in
  506. tproxy_global*|tproxy_chnroute*)
  507. iptables -t nat -F OUTPUT
  508. iptables -t nat -F PREROUTING
  509. iptables -t nat -F POSTROUTING
  510. iptables -t nat -F SS-TCP &>/dev/null
  511. iptables -t nat -X SS-TCP &>/dev/null
  512. iptables -t nat -N SS-TCP
  513. iptables -t nat -A SS-TCP -d 0/8 -j RETURN
  514. iptables -t nat -A SS-TCP -d 10/8 -j RETURN
  515. iptables -t nat -A SS-TCP -d 127/8 -j RETURN
  516. iptables -t nat -A SS-TCP -d 169.254/16 -j RETURN
  517. iptables -t nat -A SS-TCP -d 172.16/12 -j RETURN
  518. iptables -t nat -A SS-TCP -d 192.168/16 -j RETURN
  519. iptables -t nat -A SS-TCP -d 224/4 -j RETURN
  520. iptables -t nat -A SS-TCP -d 240/4 -j RETURN
  521. iptables -t nat -A SS-TCP -d $server_addr -j RETURN
  522. if [[ "$mode" == *chnroute* ]]; then
  523. ipset -X chnroute &>/dev/null
  524. ipset -R <$iptables_ipset_file
  525. iptables -t nat -A SS-TCP -m set --match-set chnroute dst -j RETURN
  526. fi
  527. iptables -t nat -A SS-TCP -p tcp -j REDIRECT --to-ports 60080
  528. if [[ "$mode" != *tcp ]]; then
  529. iptables -t mangle -F PREROUTING
  530. iptables -t mangle -F SS-UDP &>/dev/null
  531. iptables -t mangle -X SS-UDP &>/dev/null
  532. iptables -t mangle -N SS-UDP
  533. iptables -t mangle -A SS-UDP -d 0/8 -j RETURN
  534. iptables -t mangle -A SS-UDP -d 10/8 -j RETURN
  535. iptables -t mangle -A SS-UDP -d 127/8 -j RETURN
  536. iptables -t mangle -A SS-UDP -d 169.254/16 -j RETURN
  537. iptables -t mangle -A SS-UDP -d 172.16/12 -j RETURN
  538. iptables -t mangle -A SS-UDP -d 192.168/16 -j RETURN
  539. iptables -t mangle -A SS-UDP -d 224/4 -j RETURN
  540. iptables -t mangle -A SS-UDP -d 240/4 -j RETURN
  541. iptables -t mangle -A SS-UDP -d $server_addr -j RETURN
  542. [[ "$mode" == *chnroute* ]] && iptables -t mangle -A SS-UDP -m set --match-set chnroute dst -j RETURN
  543. iptables -t mangle -A SS-UDP -p udp -j TPROXY --tproxy-mark $iptables_pbr_fwmark --on-ip 127.0.0.1 --on-port 60080
  544. ip route add local 0/0 dev lo table $iptables_pbr_number
  545. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  546. fi
  547. iptables -t nat -A OUTPUT -p tcp -j SS-TCP
  548. for intranet in "${iptables_intranet[@]}"; do
  549. if [[ "$mode" == *tcp ]]; then
  550. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  551. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  552. else
  553. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  554. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  555. iptables -t mangle -A PREROUTING -p udp -s $intranet -j SS-UDP
  556. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  557. fi
  558. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  559. done
  560. ;;
  561. tproxy_gfwlist*)
  562. iptables -t nat -F OUTPUT
  563. iptables -t nat -F PREROUTING
  564. iptables -t nat -F POSTROUTING
  565. iptables -t nat -F SS-TCP &>/dev/null
  566. iptables -t nat -X SS-TCP &>/dev/null
  567. iptables -t nat -N SS-TCP
  568. ipset -X gfwlist &>/dev/null
  569. ipset -N gfwlist hash:ip
  570. [[ "$mode" == *tcp ]] && iptables -t nat -A SS-TCP -p tcp -d ${dns_remote%%:*} -j REDIRECT --to-ports 60080
  571. iptables -t nat -A SS-TCP -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-ports 60080
  572. if [[ "$mode" != *tcp ]]; then
  573. iptables -t mangle -F PREROUTING
  574. iptables -t mangle -F SS-UDP &>/dev/null
  575. iptables -t mangle -X SS-UDP &>/dev/null
  576. iptables -t mangle -N SS-UDP
  577. iptables -t mangle -A SS-UDP -p udp -m set --match-set gfwlist dst -j TPROXY --tproxy-mark $iptables_pbr_fwmark --on-ip 127.0.0.1 --on-port 60080
  578. ip route add local 0/0 dev lo table $iptables_pbr_number
  579. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  580. fi
  581. iptables -t nat -A OUTPUT -p tcp -j SS-TCP
  582. for intranet in "${iptables_intranet[@]}"; do
  583. if [[ "$mode" == *tcp ]]; then
  584. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  585. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  586. else
  587. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  588. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  589. iptables -t mangle -A PREROUTING -p udp -s $intranet -j SS-UDP
  590. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  591. fi
  592. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  593. done
  594. ;;
  595. tun2socks_global*|tun2socks_chnroute*)
  596. iptables -t mangle -F OUTPUT
  597. iptables -t mangle -F PREROUTING
  598. iptables -t mangle -F TUN2SOCKS &>/dev/null
  599. iptables -t mangle -X TUN2SOCKS &>/dev/null
  600. iptables -t nat -F PREROUTING
  601. iptables -t nat -F POSTROUTING
  602. iptables -t mangle -N TUN2SOCKS
  603. iptables -t mangle -A TUN2SOCKS -d 0/8 -j RETURN
  604. iptables -t mangle -A TUN2SOCKS -d 10/8 -j RETURN
  605. iptables -t mangle -A TUN2SOCKS -d 127/8 -j RETURN
  606. iptables -t mangle -A TUN2SOCKS -d 169.254/16 -j RETURN
  607. iptables -t mangle -A TUN2SOCKS -d 172.16/12 -j RETURN
  608. iptables -t mangle -A TUN2SOCKS -d 192.168/16 -j RETURN
  609. iptables -t mangle -A TUN2SOCKS -d 224/4 -j RETURN
  610. iptables -t mangle -A TUN2SOCKS -d 240/4 -j RETURN
  611. iptables -t mangle -A TUN2SOCKS -d $socks5_remote -j RETURN
  612. if [[ "$mode" == *chnroute* ]]; then
  613. ipset -X chnroute &>/dev/null
  614. ipset -R <$iptables_ipset_file
  615. iptables -t mangle -A TUN2SOCKS -m set --match-set chnroute dst -j RETURN
  616. fi
  617. iptables -t mangle -A TUN2SOCKS -j MARK --set-mark $iptables_pbr_fwmark
  618. ip route add 0/0 via $tun2socks_gateway dev $tun2socks_tundev table $iptables_pbr_number
  619. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  620. iptables -t mangle -A OUTPUT -p tcp -j TUN2SOCKS
  621. [[ "$mode" != *tcp ]] && iptables -t mangle -A OUTPUT -p udp -j TUN2SOCKS
  622. for intranet in "${iptables_intranet[@]}"; do
  623. if [[ "$mode" == *tcp ]]; then
  624. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  625. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  626. else
  627. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  628. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  629. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  630. iptables -t mangle -A PREROUTING -p udp -s $intranet -j TUN2SOCKS
  631. fi
  632. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  633. done
  634. ;;
  635. tun2socks_gfwlist*)
  636. iptables -t mangle -F OUTPUT
  637. iptables -t mangle -F PREROUTING
  638. iptables -t mangle -F TUN2SOCKS &>/dev/null
  639. iptables -t mangle -X TUN2SOCKS &>/dev/null
  640. iptables -t nat -F PREROUTING
  641. iptables -t nat -F POSTROUTING
  642. iptables -t mangle -N TUN2SOCKS
  643. ipset -X gfwlist &>/dev/null
  644. ipset -N gfwlist hash:ip
  645. [[ "$mode" != *tcp ]] && iptables -t mangle -A TUN2SOCKS -p udp -d ${dns_remote%%:*} -j MARK --set-mark $iptables_pbr_fwmark
  646. iptables -t mangle -A TUN2SOCKS -m set --match-set gfwlist dst -j MARK --set-mark $iptables_pbr_fwmark
  647. ip route add 0/0 via $tun2socks_gateway dev $tun2socks_tundev table $iptables_pbr_number
  648. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  649. iptables -t mangle -A OUTPUT -p tcp -j TUN2SOCKS
  650. [[ "$mode" != *tcp ]] && iptables -t mangle -A OUTPUT -p udp -j TUN2SOCKS
  651. for intranet in "${iptables_intranet[@]}"; do
  652. if [[ "$mode" == *tcp ]]; then
  653. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  654. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  655. else
  656. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  657. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  658. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  659. iptables -t mangle -A PREROUTING -p udp -s $intranet -j TUN2SOCKS
  660. fi
  661. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  662. done
  663. ;;
  664. esac
  665. }
  666. function start_kopts {
  667. [ $(sysctl net.ipv4.ip_forward | awk '{print $3}') -ne 1 ] && sysctl -w net.ipv4.ip_forward=1
  668. [[ "$mode" == tun2socks* ]] && {
  669. [ $(sysctl net.ipv4.conf.$tun2socks_tundev.rp_filter | awk '{print $3}') -ne 2 ] &&
  670. sysctl -w net.ipv4.conf.$tun2socks_tundev.rp_filter=2
  671. }
  672. }
  673. function dns_proxy {
  674. cat <<EOF >/etc/resolv.conf
  675. # Generated by ss-tproxy at $(date '+%F %T')
  676. nameserver 127.0.0.1
  677. EOF
  678. }
  679. function dns_normal {
  680. cat <<EOF >/etc/resolv.conf
  681. # Generated by ss-tproxy at $(date '+%F %T')
  682. $(for dns in "${dns_direct[@]}"; do echo "nameserver $dns"; done)
  683. EOF
  684. }
  685. function start {
  686. check_depend
  687. dns_normal
  688. start_socks
  689. start_dns
  690. start_rule
  691. start_kopts
  692. dns_proxy
  693. }
  694. function stop {
  695. check_depend
  696. dns_normal
  697. iptables -t nat -F OUTPUT
  698. iptables -t nat -F PREROUTING
  699. iptables -t nat -F POSTROUTING
  700. iptables -t nat -F SS-TCP &>/dev/null
  701. iptables -t nat -X SS-TCP &>/dev/null
  702. iptables -t mangle -F OUTPUT
  703. iptables -t mangle -F PREROUTING
  704. iptables -t mangle -F SS-UDP &>/dev/null
  705. iptables -t mangle -X SS-UDP &>/dev/null
  706. iptables -t mangle -F TUN2SOCKS &>/dev/null
  707. iptables -t mangle -X TUN2SOCKS &>/dev/null
  708. for intranet in "${iptables_intranet[@]}"; do
  709. iptables -t nat -A PREROUTING -p udp -s $intranet -d $intranet --dport 53 -j DNAT --to-destination ${dns_direct[0]}
  710. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  711. done
  712. ipset -X gfwlist &>/dev/null
  713. ipset -X chnroute &>/dev/null
  714. ip rule show | grep "fwmark $iptables_pbr_fwmark" | awk -F':' '{print $1}' | xargs -n1 ip rule del pref &>/dev/null
  715. ip route flush table $iptables_pbr_number
  716. pkill -9 '^dnsmasq$'
  717. pkill -9 '^chinadns$'
  718. pkill -9 '^dnsforwarder$'
  719. pkill -9 '^ss-redir$'
  720. pkill -9 '^ss-tunnel$'
  721. pkill -9 '^ssr-redir$'
  722. pkill -9 '^ssr-tunnel$'
  723. pkill -9 '^tun2socks$'
  724. port=$(echo $socks5_listen | awk -F: '{print $2}')
  725. ss -lnptu | grep ":$port\s" | grep -Eo 'pid=[0-9]+' | awk -F= '{print $2}' | sort -n | uniq | xargs kill -9 &>/dev/null
  726. }
  727. function status {
  728. case "$mode" in
  729. tproxy_global)
  730. echo "mode: tproxy_global"
  731. if [ "$server_use_ssr" != 'true' ]; then
  732. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  733. [ $(pgrep -c '^ss-tunnel$') -ne 0 ] && echo -e "ss-tunnel: \e[32m[running]\e[0m" || echo -e "ss-tunnel: \e[35m[stopped]\e[0m"
  734. else
  735. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  736. [ $(pgrep -c '^ssr-tunnel$') -ne 0 ] && echo -e "ssr-tunnel: \e[32m[running]\e[0m" || echo -e "ssr-tunnel: \e[35m[stopped]\e[0m"
  737. fi
  738. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  739. ;;
  740. tproxy_global_tcp)
  741. echo "mode: tproxy_global_tcp"
  742. if [ "$server_use_ssr" != 'true' ]; then
  743. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  744. else
  745. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  746. fi
  747. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  748. ;;
  749. tproxy_gfwlist)
  750. echo "mode: tproxy_gfwlist"
  751. if [ "$server_use_ssr" != 'true' ]; then
  752. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  753. [ $(pgrep -c '^ss-tunnel$') -ne 0 ] && echo -e "ss-tunnel: \e[32m[running]\e[0m" || echo -e "ss-tunnel: \e[35m[stopped]\e[0m"
  754. else
  755. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  756. [ $(pgrep -c '^ssr-tunnel$') -ne 0 ] && echo -e "ssr-tunnel: \e[32m[running]\e[0m" || echo -e "ssr-tunnel: \e[35m[stopped]\e[0m"
  757. fi
  758. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  759. ;;
  760. tproxy_gfwlist_tcp)
  761. echo "mode: tproxy_gfwlist_tcp"
  762. if [ "$server_use_ssr" != 'true' ]; then
  763. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  764. else
  765. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  766. fi
  767. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  768. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  769. ;;
  770. tproxy_chnroute)
  771. echo "mode: tproxy_chnroute"
  772. if [ "$server_use_ssr" != 'true' ]; then
  773. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  774. [ $(pgrep -c '^ss-tunnel$') -ne 0 ] && echo -e "ss-tunnel: \e[32m[running]\e[0m" || echo -e "ss-tunnel: \e[35m[stopped]\e[0m"
  775. else
  776. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  777. [ $(pgrep -c '^ssr-tunnel$') -ne 0 ] && echo -e "ssr-tunnel: \e[32m[running]\e[0m" || echo -e "ssr-tunnel: \e[35m[stopped]\e[0m"
  778. fi
  779. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  780. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  781. ;;
  782. tproxy_chnroute_tcp)
  783. echo "mode: tproxy_chnroute_tcp"
  784. if [ "$server_use_ssr" != 'true' ]; then
  785. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  786. else
  787. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  788. fi
  789. [ $(ss -lnpu | grep ':60053\s' | wc -l) -ne 0 ] && echo -e "dnsforwarder0: \e[32m[running]\e[0m" || echo -e "dnsforwarder0: \e[35m[stopped]\e[0m"
  790. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  791. [ $(ss -lnpu | grep ':53\s' | wc -l) -ne 0 ] && echo -e "dnsforwarder1: \e[32m[running]\e[0m" || echo -e "dnsforwarder1: \e[35m[stopped]\e[0m"
  792. ;;
  793. tun2socks_global)
  794. echo "mode: tun2socks_global"
  795. port=$(echo $socks5_listen | awk -F: '{print $2}')
  796. [ $(ss -lnpt | grep ":$port\s" | wc -l) -ne 0 ] && echo -e "socks5/tcp: \e[32m[running]\e[0m" || echo -e "socks5/tcp: \e[35m[stopped]\e[0m"
  797. [ $(ss -lnpu | grep ":$port\s" | wc -l) -ne 0 ] && echo -e "socks5/udp: \e[32m[running]\e[0m" || echo -e "socks5/udp: \e[35m[stopped]\e[0m"
  798. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  799. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  800. ;;
  801. tun2socks_global_tcp)
  802. echo "mode: tun2socks_global_tcp"
  803. port=$(echo $socks5_listen | awk -F: '{print $2}')
  804. [ $(ss -lnpt | grep ":$port\s" | wc -l) -ne 0 ] && echo -e "socks5/tcp: \e[32m[running]\e[0m" || echo -e "socks5/tcp: \e[35m[stopped]\e[0m"
  805. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  806. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  807. ;;
  808. tun2socks_gfwlist)
  809. echo "mode: tun2socks_gfwlist"
  810. port=$(echo $socks5_listen | awk -F: '{print $2}')
  811. [ $(ss -lnpt | grep ":$port\s" | wc -l) -ne 0 ] && echo -e "socks5/tcp: \e[32m[running]\e[0m" || echo -e "socks5/tcp: \e[35m[stopped]\e[0m"
  812. [ $(ss -lnpu | grep ":$port\s" | wc -l) -ne 0 ] && echo -e "socks5/udp: \e[32m[running]\e[0m" || echo -e "socks5/udp: \e[35m[stopped]\e[0m"
  813. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  814. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  815. ;;
  816. tun2socks_gfwlist_tcp)
  817. echo "mode: tun2socks_gfwlist_tcp"
  818. port=$(echo $socks5_listen | awk -F: '{print $2}')
  819. [ $(ss -lnpt | grep ":$port\s" | wc -l) -ne 0 ] && echo -e "socks5/tcp: \e[32m[running]\e[0m" || echo -e "socks5/tcp: \e[35m[stopped]\e[0m"
  820. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  821. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  822. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  823. ;;
  824. tun2socks_chnroute)
  825. echo "mode: tun2socks_chnroute"
  826. port=$(echo $socks5_listen | awk -F: '{print $2}')
  827. [ $(ss -lnpt | grep ":$port\s" | wc -l) -ne 0 ] && echo -e "socks5/tcp: \e[32m[running]\e[0m" || echo -e "socks5/tcp: \e[35m[stopped]\e[0m"
  828. [ $(ss -lnpu | grep ":$port\s" | wc -l) -ne 0 ] && echo -e "socks5/udp: \e[32m[running]\e[0m" || echo -e "socks5/udp: \e[35m[stopped]\e[0m"
  829. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  830. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  831. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  832. ;;
  833. tun2socks_chnroute_tcp)
  834. echo "mode: tun2socks_chnroute_tcp"
  835. port=$(echo $socks5_listen | awk -F: '{print $2}')
  836. [ $(ss -lnpt | grep ":$port\s" | wc -l) -ne 0 ] && echo -e "socks5/tcp: \e[32m[running]\e[0m" || echo -e "socks5/tcp: \e[35m[stopped]\e[0m"
  837. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  838. [ $(ss -lnpu | grep ':60053\s' | wc -l) -ne 0 ] && echo -e "dnsforwarder0: \e[32m[running]\e[0m" || echo -e "dnsforwarder0: \e[35m[stopped]\e[0m"
  839. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  840. [ $(ss -lnpu | grep ':53\s' | wc -l) -ne 0 ] && echo -e "dnsforwarder1: \e[32m[running]\e[0m" || echo -e "dnsforwarder1: \e[35m[stopped]\e[0m"
  841. ;;
  842. esac
  843. }
  844. function usage {
  845. cat <<EOF
  846. Usage: ss-tproxy <COMMAND>
  847. COMMAND := {
  848. start start ss-tproxy
  849. stop stop ss-tproxy
  850. restart restart ss-tproxy
  851. status status of ss-tproxy
  852. flush-cache flush dns cache
  853. update-gfwlist update gfwlist
  854. update-chnroute update chnroute
  855. check-depend check dependency
  856. help show this help and exit
  857. }
  858. Issues/Bug report: zfl9.com@gmail.com (Otokaze)
  859. See also https://www.zfl9.com/ss-redir.html for more details.
  860. EOF
  861. }
  862. case $1 in
  863. start) start; status;;
  864. stop) stop; status;;
  865. status) status;;
  866. r*) stop; status; echo; sleep 1; start; status;;
  867. f*) flush_cache;;
  868. update-g*) update_gfwlist;;
  869. update-c*) update_chnroute;;
  870. c*) check_depend;;
  871. h*) usage;;
  872. *) usage; exit 1;;
  873. esac