ss-tproxy 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. [ ! -f "$dnsmasq_gfwlist" ] && { echo "dnsmasq_gfwlist: $dnsmasq_gfwlist: No such file or directory" 1>&2; exit 1; }
  10. [ ! -f "$dnsmasq_gfwlist_ext" ] && { echo "dnsmasq_gfwlist_ext: $dnsmasq_gfwlist_ext: No such file or directory" 1>&2; exit 1; }
  11. [ ! -f "$chinadns_chnroute" ] && { echo "chinadns_chnroute: $chinadns_chnroute: No such file or directory" 1>&2; exit 1; }
  12. [ ! -f "$iptables_ipset_file" ] && { echo "iptables_ipset_file: $iptables_ipset_file: No such file or directory" 1>&2; exit 1; }
  13. function check_depend {
  14. case "$mode" in
  15. tproxy_global)
  16. { 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; }
  17. { 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; }
  18. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  19. modprobe xt_TPROXY &>/dev/null || { echo "Error: xt_TPROXY module is not installed." 1>&2; exit 1; }
  20. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  21. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  22. ;;
  23. tproxy_global_tcp)
  24. { 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; }
  25. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  26. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  27. ;;
  28. tproxy_gfwlist)
  29. { 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; }
  30. { 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; }
  31. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  32. modprobe xt_TPROXY &>/dev/null || { echo "Error: xt_TPROXY module is not installed." 1>&2; exit 1; }
  33. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  34. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  35. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  36. command -v perl &>/dev/null || { echo "Error: perl is not installed." 1>&2; exit 1; }
  37. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  38. command -v base64 &>/dev/null || { echo "Warning: base64 is not installed." 1>&2; }
  39. ;;
  40. tproxy_gfwlist_tcp)
  41. { 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; }
  42. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  43. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  44. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  45. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  46. command -v perl &>/dev/null || { echo "Error: perl is not installed." 1>&2; exit 1; }
  47. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  48. command -v base64 &>/dev/null || { echo "Warning: base64 is not installed." 1>&2; }
  49. ;;
  50. tproxy_chnroute)
  51. { 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; }
  52. { 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; }
  53. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  54. modprobe xt_TPROXY &>/dev/null || { echo "Error: xt_TPROXY module is not installed." 1>&2; exit 1; }
  55. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  56. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  57. command -v chinadns &>/dev/null || { echo "Error: chinadns is not installed." 1>&2; exit 1; }
  58. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  59. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  60. ;;
  61. tproxy_chnroute_tcp)
  62. { 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; }
  63. command -v haveged &>/dev/null || { echo "Warning: haveged is not installed." 1>&2; }
  64. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  65. command -v chinadns &>/dev/null || { echo "Error: chinadns is not installed." 1>&2; exit 1; }
  66. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  67. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  68. ;;
  69. tun2socks_global)
  70. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  71. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  72. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  73. ;;
  74. tun2socks_global_tcp)
  75. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  76. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  77. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  78. ;;
  79. tun2socks_gfwlist)
  80. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  81. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  82. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  83. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  84. command -v perl &>/dev/null || { echo "Error: perl is not installed." 1>&2; exit 1; }
  85. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  86. command -v base64 &>/dev/null || { echo "Warning: base64 is not installed." 1>&2; }
  87. ;;
  88. tun2socks_gfwlist_tcp)
  89. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  90. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  91. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  92. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  93. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  94. command -v perl &>/dev/null || { echo "Error: perl is not installed." 1>&2; exit 1; }
  95. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  96. command -v base64 &>/dev/null || { echo "Warning: base64 is not installed." 1>&2; }
  97. ;;
  98. tun2socks_chnroute)
  99. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  100. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  101. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  102. command -v chinadns &>/dev/null || { echo "Error: chinadns is not installed." 1>&2; exit 1; }
  103. command -v dnsmasq &>/dev/null || { echo "Error: dnsmasq is not installed." 1>&2; exit 1; }
  104. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  105. ;;
  106. tun2socks_chnroute_tcp)
  107. command -v tun2socks &>/dev/null || { echo "Error: tun2socks is not installed." 1>&2; exit 1; }
  108. command -v ip &>/dev/null || { echo "Error: iproute2 is not installed." 1>&2; exit 1; }
  109. command -v ipset &>/dev/null || { echo "Error: ipset is not installed." 1>&2; exit 1; }
  110. command -v chinadns &>/dev/null || { echo "Error: chinadns is not installed." 1>&2; exit 1; }
  111. command -v dnsforwarder &>/dev/null || { echo "Error: dnsforwarder is not installed." 1>&2; exit 1; }
  112. command -v curl &>/dev/null || { echo "Warning: curl is not installed." 1>&2; }
  113. ;;
  114. esac
  115. }
  116. function update_chnonly {
  117. curl -4sSkL https://raw.github.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf | perl -ne 'm@/\K.+(?=/)@; print "$&\n"' >$dnsmasq_gfwlist
  118. }
  119. function update_gfwlist {
  120. curl -4sSkL https://raw.github.com/gfwlist/gfwlist/master/gfwlist.txt | base64 -d | { perl -pe '
  121. if (/URL Keywords/i) { $null = <> until $null =~ /^!/ }
  122. s#^\s*+$|^!.*+$|^@@.*+$|^\[AutoProxy.*+$|^/.*/$##i;
  123. s@^\|\|?|\|$@@;
  124. s@^https?:/?/?@@i;
  125. s@(?:/|%).*+$@@;
  126. s@\*[^.*]++$@\n@;
  127. s@^.*?\*[^.]*+(?=[^*]+$)@@;
  128. s@^\*?\.|^.*\.\*?$@@;
  129. s@(?=[^0-9a-zA-Z.-]).*+$@@;
  130. s@^\d+\.\d+\.\d+\.\d+(?::\d+)?$@@;
  131. s@^\s*+$@@'
  132. echo 'twimg.edgesuite.net'
  133. 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'
  134. 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'
  135. } | sort | uniq -i >$dnsmasq_gfwlist
  136. }
  137. function update_chnroute {
  138. #chnroute_url="http://f.ip.cn/rt/chnroutes.txt"
  139. chnroute_url="https://github.com/17mon/china_ip_list/raw/master/china_ip_list.txt"
  140. { curl -4sSkL "$chnroute_url"; echo; } | grep -Ev '^\s*$|^\s*#' >$chinadns_chnroute
  141. echo '-N chnroute hash:net' >$iptables_ipset_file; sed -r 's/^.+$/-A chnroute &/' $chinadns_chnroute >>$iptables_ipset_file
  142. }
  143. function flush_cache {
  144. case "$mode" in
  145. *global|*gfwlist*|*chnroute)
  146. pgrep '^dnsmasq$' | xargs kill -HUP &>/dev/null
  147. ;;
  148. tproxy_global_tcp)
  149. if [ $(pgrep -c '^dnsforwarder$') -ne 0 ]; then
  150. pkill -9 '^dnsforwarder$' &>/dev/null
  151. dnsforwarder -q -d -f <(cat <<EOF
  152. LogOn $dnsfwd_log_on
  153. LogFileThresholdLength 5242880
  154. LogFileFolder $dnsfwd_log_dir
  155. UDPLocal 0.0.0.0:53
  156. TCPGroup $dns_remote * no
  157. BlockNegativeResponse true
  158. Hosts file:///etc/hosts
  159. HostsUpdateInterval 3600
  160. UseCache $dnsfwd_cache_on
  161. MemoryCache $dnsfwd_cache_mem
  162. CacheSize $dnsfwd_cache_size
  163. IgnoreTTL $dnsfwd_ignore_ttl
  164. EOF
  165. ) &>/dev/null
  166. fi
  167. ;;
  168. tproxy_chnroute_tcp)
  169. if [ $(pgrep -c '^dnsforwarder$') -ne 0 ]; then
  170. pkill -9 '^dnsforwarder$' &>/dev/null
  171. dnsforwarder -q -d -f <(cat <<EOF
  172. LogOn false
  173. UseCache false
  174. UDPLocal 0.0.0.0:60053
  175. TCPGroup $dns_remote * no
  176. BlockNegativeResponse true
  177. EOF
  178. ) &>/dev/null
  179. dnsforwarder -q -d -f <(cat <<EOF
  180. LogOn $dnsfwd_log_on
  181. LogFileThresholdLength 5242880
  182. LogFileFolder $dnsfwd_log_dir
  183. UDPLocal 0.0.0.0:53
  184. UDPGroup 127.0.0.1:65353 * on
  185. BlockNegativeResponse true
  186. Hosts file:///etc/hosts
  187. HostsUpdateInterval 3600
  188. UseCache $dnsfwd_cache_on
  189. MemoryCache $dnsfwd_cache_mem
  190. CacheSize $dnsfwd_cache_size
  191. IgnoreTTL $dnsfwd_ignore_ttl
  192. EOF
  193. ) &>/dev/null
  194. fi
  195. ;;
  196. tun2socks_global_tcp)
  197. if [ $(pgrep -c '^dnsforwarder$') -ne 0 ]; then
  198. pkill -9 '^dnsforwarder$' &>/dev/null
  199. dnsforwarder -q -d -f <(cat <<EOF
  200. LogOn $dnsfwd_log_on
  201. LogFileThresholdLength 5242880
  202. LogFileFolder $dnsfwd_log_dir
  203. UDPLocal 0.0.0.0:53
  204. TCPGroup $dns_remote * $socks5_listen
  205. BlockNegativeResponse true
  206. Hosts file:///etc/hosts
  207. HostsUpdateInterval 3600
  208. UseCache $dnsfwd_cache_on
  209. MemoryCache $dnsfwd_cache_mem
  210. CacheSize $dnsfwd_cache_size
  211. IgnoreTTL $dnsfwd_ignore_ttl
  212. EOF
  213. ) &>/dev/null
  214. fi
  215. ;;
  216. tun2socks_chnroute_tcp)
  217. if [ $(pgrep -c '^dnsforwarder$') -ne 0 ]; then
  218. pkill -9 '^dnsforwarder$' &>/dev/null
  219. dnsforwarder -q -d -f <(cat <<EOF
  220. LogOn false
  221. UseCache false
  222. UDPLocal 0.0.0.0:60053
  223. TCPGroup $dns_remote * $socks5_listen
  224. BlockNegativeResponse true
  225. EOF
  226. ) &>/dev/null
  227. dnsforwarder -q -d -f <(cat <<EOF
  228. LogOn $dnsfwd_log_on
  229. LogFileThresholdLength 5242880
  230. LogFileFolder $dnsfwd_log_dir
  231. UDPLocal 0.0.0.0:53
  232. UDPGroup 127.0.0.1:65353 * on
  233. BlockNegativeResponse true
  234. Hosts file:///etc/hosts
  235. HostsUpdateInterval 3600
  236. UseCache $dnsfwd_cache_on
  237. MemoryCache $dnsfwd_cache_mem
  238. CacheSize $dnsfwd_cache_size
  239. IgnoreTTL $dnsfwd_ignore_ttl
  240. EOF
  241. ) &>/dev/null
  242. fi
  243. ;;
  244. esac
  245. }
  246. function start_socks {
  247. case "$mode" in
  248. tproxy*)
  249. if [ "$server_use_ssr" = 'false' ]; then
  250. redir_params="-s'$server_addr' -p'$server_port' -m'$server_method' -k'$server_passwd' -b0.0.0.0 -l60080 --no-delay --reuse-port"
  251. [[ "$mode" != *tcp ]] && redir_params+=' -u'
  252. [ "$redir_verbose" = 'true' ] && redir_params+=' -v'
  253. [ "$redir_fast_open" = 'true' ] && redir_params+=' --fast-open'
  254. eval "nohup ss-redir $redir_params </dev/null &>>'$redir_log' &"
  255. if [[ "$mode" != *tcp ]]; then
  256. 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"
  257. [ "$tunnel_verbose" = 'true' ] && tunnel_params+=' -v'
  258. eval "nohup ss-tunnel $tunnel_params </dev/null &>>'$tunnel_log' &";
  259. fi
  260. else
  261. 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"
  262. [ -n "$server_protocol_param" ] && redir_params+=" -G'$server_protocol_param'"
  263. [ -n "$server_obfs_param" ] && redir_params+=" -g'$server_obfs_param'"
  264. [[ "$mode" != *tcp ]] && redir_params+=' -u'
  265. [ "$redir_verbose" = 'true' ] && redir_params+=' -v'
  266. eval "nohup ssr-redir $redir_params </dev/null &>>'$redir_log' &"
  267. if [[ "$mode" != *tcp ]]; then
  268. 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"
  269. [ -n "$server_protocol_param" ] && tunnel_params+=" -G'$server_protocol_param'"
  270. [ -n "$server_obfs_param" ] && tunnel_params+=" -g'$server_obfs_param'"
  271. [ "$tunnel_verbose" = 'true' ] && tunnel_params+=' -v'
  272. eval "nohup ssr-tunnel $tunnel_params </dev/null &>>'$tunnel_log' &"
  273. fi
  274. fi
  275. ;;
  276. tun2socks*)
  277. eval "$socks5_runcmd"
  278. 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" &
  279. ;;
  280. esac
  281. }
  282. function start_dns {
  283. case "$mode" in
  284. tproxy_global)
  285. dnsmasq -C <(cat <<EOF
  286. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  287. log-async = 20
  288. log-facility = $dnsmasq_log_file
  289. domain-needed
  290. cache-size = $dnsmasq_cache_size
  291. $([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
  292. no-negcache
  293. no-resolv
  294. port = 53
  295. server = 127.0.0.1#60053
  296. EOF
  297. )
  298. ;;
  299. tproxy_gfwlist)
  300. dnsmasq -C <(cat <<EOF
  301. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  302. log-async = 20
  303. log-facility = $dnsmasq_log_file
  304. domain-needed
  305. cache-size = $dnsmasq_cache_size
  306. $([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
  307. no-negcache
  308. no-resolv
  309. port = 53
  310. server = $dns_direct
  311. $(perl -pe 's@^.*+$@server=/$&/127.0.0.1#60053\nipset=/$&/gfwlist@' $dnsmasq_gfwlist <(
  312. perl -ne 'print unless m@^\s|\s\n$|^#|^\d++\.\d++\.\d++\.\d++(?:/\d++)?$@' $dnsmasq_gfwlist_ext
  313. ))
  314. EOF
  315. )
  316. ;;
  317. tproxy_chnroute)
  318. chinadns_chnroute_temp=$(mktemp)
  319. cat $chinadns_chnroute >$chinadns_chnroute_temp
  320. echo "$dns_direct/32" >>$chinadns_chnroute_temp
  321. chinadns_params="-b 0.0.0.0 -p 65353 -s $dns_direct,127.0.0.1:60053 -c $chinadns_chnroute_temp"
  322. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  323. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  324. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  325. dnsmasq -C <(cat <<EOF
  326. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  327. log-async = 20
  328. log-facility = $dnsmasq_log_file
  329. domain-needed
  330. cache-size = $dnsmasq_cache_size
  331. $([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
  332. no-negcache
  333. no-resolv
  334. port = 53
  335. server = 127.0.0.1#65353
  336. EOF
  337. )
  338. ;;
  339. tproxy_global_tcp)
  340. mkdir -p /root/.dnsforwarder &>/dev/null
  341. dnsforwarder -q -d -f <(cat <<EOF
  342. LogOn $dnsfwd_log_on
  343. LogFileThresholdLength 5242880
  344. LogFileFolder $dnsfwd_log_dir
  345. UDPLocal 0.0.0.0:53
  346. TCPGroup $dns_remote * no
  347. BlockNegativeResponse true
  348. Hosts file:///etc/hosts
  349. HostsUpdateInterval 3600
  350. UseCache $dnsfwd_cache_on
  351. MemoryCache $dnsfwd_cache_mem
  352. CacheSize $dnsfwd_cache_size
  353. IgnoreTTL $dnsfwd_ignore_ttl
  354. EOF
  355. ) &>/dev/null
  356. ;;
  357. tproxy_gfwlist_tcp)
  358. dnsforwarder -q -d -f <(cat <<EOF
  359. LogOn false
  360. UseCache false
  361. UDPLocal 0.0.0.0:60053
  362. TCPGroup $dns_remote * no
  363. BlockNegativeResponse true
  364. EOF
  365. ) &>/dev/null
  366. dnsmasq -C <(cat <<EOF
  367. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  368. log-async = 20
  369. log-facility = $dnsmasq_log_file
  370. domain-needed
  371. cache-size = $dnsmasq_cache_size
  372. $([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
  373. no-negcache
  374. no-resolv
  375. port = 53
  376. server = $dns_direct
  377. $(perl -pe 's@^.*+$@server=/$&/127.0.0.1#60053\nipset=/$&/gfwlist@' $dnsmasq_gfwlist <(
  378. perl -ne 'print unless m@^\s|\s\n$|^#|^\d++\.\d++\.\d++\.\d++(?:/\d++)?$@' $dnsmasq_gfwlist_ext
  379. ))
  380. EOF
  381. )
  382. ;;
  383. tproxy_chnroute_tcp)
  384. dnsforwarder -q -d -f <(cat <<EOF
  385. LogOn false
  386. UseCache false
  387. UDPLocal 0.0.0.0:60053
  388. TCPGroup $dns_remote * no
  389. BlockNegativeResponse true
  390. EOF
  391. ) &>/dev/null
  392. chinadns_chnroute_temp=$(mktemp)
  393. cat $chinadns_chnroute >$chinadns_chnroute_temp
  394. echo "$dns_direct/32" >>$chinadns_chnroute_temp
  395. chinadns_params="-b 0.0.0.0 -p 65353 -s $dns_direct,127.0.0.1:60053 -c $chinadns_chnroute_temp"
  396. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  397. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  398. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  399. mkdir -p /root/.dnsforwarder &>/dev/null
  400. dnsforwarder -q -d -f <(cat <<EOF
  401. LogOn $dnsfwd_log_on
  402. LogFileThresholdLength 5242880
  403. LogFileFolder $dnsfwd_log_dir
  404. UDPLocal 0.0.0.0:53
  405. UDPGroup 127.0.0.1:65353 * on
  406. BlockNegativeResponse true
  407. Hosts file:///etc/hosts
  408. HostsUpdateInterval 3600
  409. UseCache $dnsfwd_cache_on
  410. MemoryCache $dnsfwd_cache_mem
  411. CacheSize $dnsfwd_cache_size
  412. IgnoreTTL $dnsfwd_ignore_ttl
  413. EOF
  414. ) &>/dev/null
  415. ;;
  416. tun2socks_global)
  417. dnsmasq -C <(cat <<EOF
  418. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  419. log-async = 20
  420. log-facility = $dnsmasq_log_file
  421. domain-needed
  422. cache-size = $dnsmasq_cache_size
  423. $([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
  424. no-negcache
  425. no-resolv
  426. port = 53
  427. server = ${dns_remote/:/#}
  428. EOF
  429. )
  430. ;;
  431. tun2socks_gfwlist)
  432. dnsmasq -C <(cat <<EOF
  433. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  434. log-async = 20
  435. log-facility = $dnsmasq_log_file
  436. domain-needed
  437. cache-size = $dnsmasq_cache_size
  438. $([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
  439. no-negcache
  440. no-resolv
  441. port = 53
  442. server = $dns_direct
  443. $(perl -pe "s@^.*+\$@server=/$&/${dns_remote/:/#}\nipset=/$&/gfwlist@" $dnsmasq_gfwlist <(
  444. perl -ne 'print unless m@^\s|\s\n$|^#|^\d++\.\d++\.\d++\.\d++(?:/\d++)?$@' $dnsmasq_gfwlist_ext
  445. ))
  446. EOF
  447. )
  448. ;;
  449. tun2socks_chnroute)
  450. chinadns_chnroute_temp=$(mktemp)
  451. cat $chinadns_chnroute >$chinadns_chnroute_temp
  452. echo "$dns_direct/32" >>$chinadns_chnroute_temp
  453. chinadns_params="-b 0.0.0.0 -p 60053 -s $dns_direct,$dns_remote -c $chinadns_chnroute_temp"
  454. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  455. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  456. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  457. dnsmasq -C <(cat <<EOF
  458. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  459. log-async = 20
  460. log-facility = $dnsmasq_log_file
  461. domain-needed
  462. cache-size = $dnsmasq_cache_size
  463. $([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
  464. no-negcache
  465. no-resolv
  466. port = 53
  467. server = 127.0.0.1#60053
  468. EOF
  469. )
  470. ;;
  471. tun2socks_global_tcp)
  472. mkdir -p /root/.dnsforwarder &>/dev/null
  473. dnsforwarder -q -d -f <(cat <<EOF
  474. LogOn $dnsfwd_log_on
  475. LogFileThresholdLength 5242880
  476. LogFileFolder $dnsfwd_log_dir
  477. UDPLocal 0.0.0.0:53
  478. TCPGroup $dns_remote * $socks5_listen
  479. BlockNegativeResponse true
  480. Hosts file:///etc/hosts
  481. HostsUpdateInterval 3600
  482. UseCache $dnsfwd_cache_on
  483. MemoryCache $dnsfwd_cache_mem
  484. CacheSize $dnsfwd_cache_size
  485. IgnoreTTL $dnsfwd_ignore_ttl
  486. EOF
  487. ) &>/dev/null
  488. ;;
  489. tun2socks_gfwlist_tcp)
  490. dnsforwarder -q -d -f <(cat <<EOF
  491. LogOn false
  492. UseCache false
  493. UDPLocal 0.0.0.0:60053
  494. TCPGroup $dns_remote * $socks5_listen
  495. BlockNegativeResponse true
  496. EOF
  497. ) &>/dev/null
  498. dnsmasq -C <(cat <<EOF
  499. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  500. log-async = 20
  501. log-facility = $dnsmasq_log_file
  502. domain-needed
  503. cache-size = $dnsmasq_cache_size
  504. $([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
  505. no-negcache
  506. no-resolv
  507. port = 53
  508. server = $dns_direct
  509. $(perl -pe 's@^.*+$@server=/$&/127.0.0.1#60053\nipset=/$&/gfwlist@' $dnsmasq_gfwlist <(
  510. perl -ne 'print unless m@^\s|\s\n$|^#|^\d++\.\d++\.\d++\.\d++(?:/\d++)?$@' $dnsmasq_gfwlist_ext
  511. ))
  512. EOF
  513. )
  514. ;;
  515. tun2socks_chnroute_tcp)
  516. dnsforwarder -q -d -f <(cat <<EOF
  517. LogOn false
  518. UseCache false
  519. UDPLocal 0.0.0.0:60053
  520. TCPGroup $dns_remote * $socks5_listen
  521. BlockNegativeResponse true
  522. EOF
  523. ) &>/dev/null
  524. chinadns_chnroute_temp=$(mktemp)
  525. cat $chinadns_chnroute >$chinadns_chnroute_temp
  526. echo "$dns_direct/32" >>$chinadns_chnroute_temp
  527. chinadns_params="-b 0.0.0.0 -p 65353 -s $dns_direct,127.0.0.1:60053 -c $chinadns_chnroute_temp"
  528. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  529. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  530. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  531. mkdir -p /root/.dnsforwarder &>/dev/null
  532. dnsforwarder -q -d -f <(cat <<EOF
  533. LogOn $dnsfwd_log_on
  534. LogFileThresholdLength 5242880
  535. LogFileFolder $dnsfwd_log_dir
  536. UDPLocal 0.0.0.0:53
  537. UDPGroup 127.0.0.1:65353 * on
  538. BlockNegativeResponse true
  539. Hosts file:///etc/hosts
  540. HostsUpdateInterval 3600
  541. UseCache $dnsfwd_cache_on
  542. MemoryCache $dnsfwd_cache_mem
  543. CacheSize $dnsfwd_cache_size
  544. IgnoreTTL $dnsfwd_ignore_ttl
  545. EOF
  546. ) &>/dev/null
  547. ;;
  548. esac
  549. }
  550. function start_rule {
  551. function dec2bin {
  552. for ((n = $1; n > 0; n >>= 1)); do bit="$((n & 1))$bit"; done
  553. printf "%08d\n" "$bit"
  554. }
  555. function net2bin {
  556. net=$(awk -F/ '{print $1}' <<<$1)
  557. len=$(awk -F/ '{print $2}' <<<$1)
  558. IFS='.' read -ra bytes <<<"$net"
  559. for byte in "${bytes[@]}"; do result+="$(dec2bin $byte)"; done
  560. echo "${result:0:len}"
  561. }
  562. for cidr in "${iptables_intranet[@]}"; do
  563. curnet=$(net2bin $cidr)
  564. for stdnet in 00001010 101011000001 1010100111111110 1100000010101000; do
  565. [[ "$curnet" == "$stdnet"* ]] && continue 2
  566. done
  567. iptables_intranet_nonstd+=($cidr)
  568. done
  569. case "$mode" in
  570. tproxy_global*|tproxy_chnroute*)
  571. iptables -t nat -F OUTPUT
  572. iptables -t nat -F PREROUTING
  573. iptables -t nat -F POSTROUTING
  574. iptables -t nat -F SS-TCP &>/dev/null
  575. iptables -t nat -X SS-TCP &>/dev/null
  576. iptables -t nat -N SS-TCP
  577. iptables -t nat -A SS-TCP -d 0/8 -j RETURN
  578. iptables -t nat -A SS-TCP -d 10/8 -j RETURN
  579. iptables -t nat -A SS-TCP -d 127/8 -j RETURN
  580. iptables -t nat -A SS-TCP -d 169.254/16 -j RETURN
  581. iptables -t nat -A SS-TCP -d 172.16/12 -j RETURN
  582. iptables -t nat -A SS-TCP -d 192.168/16 -j RETURN
  583. for cidr in "${iptables_intranet_nonstd[@]}"; do
  584. iptables -t nat -A SS-TCP -d $cidr -j RETURN
  585. done
  586. iptables -t nat -A SS-TCP -d 224/4 -j RETURN
  587. iptables -t nat -A SS-TCP -d 240/4 -j RETURN
  588. iptables -t nat -A SS-TCP -d $server_addr -j RETURN
  589. if [[ "$mode" == *chnroute* ]]; then
  590. ipset -X chnroute &>/dev/null
  591. ipset -R <$iptables_ipset_file
  592. iptables -t nat -A SS-TCP -m set --match-set chnroute dst -j RETURN
  593. fi
  594. iptables -t nat -A SS-TCP -p tcp -j REDIRECT --to-ports 60080
  595. if [[ "$mode" != *tcp ]]; then
  596. iptables -t mangle -F PREROUTING
  597. iptables -t mangle -F SS-UDP &>/dev/null
  598. iptables -t mangle -X SS-UDP &>/dev/null
  599. iptables -t mangle -N SS-UDP
  600. iptables -t mangle -A SS-UDP -d 0/8 -j RETURN
  601. iptables -t mangle -A SS-UDP -d 10/8 -j RETURN
  602. iptables -t mangle -A SS-UDP -d 127/8 -j RETURN
  603. iptables -t mangle -A SS-UDP -d 169.254/16 -j RETURN
  604. iptables -t mangle -A SS-UDP -d 172.16/12 -j RETURN
  605. iptables -t mangle -A SS-UDP -d 192.168/16 -j RETURN
  606. for cidr in "${iptables_intranet_nonstd[@]}"; do
  607. iptables -t mangle -A SS-UDP -d $cidr -j RETURN
  608. done
  609. iptables -t mangle -A SS-UDP -d 224/4 -j RETURN
  610. iptables -t mangle -A SS-UDP -d 240/4 -j RETURN
  611. iptables -t mangle -A SS-UDP -d $server_addr -j RETURN
  612. [[ "$mode" == *chnroute* ]] && iptables -t mangle -A SS-UDP -m set --match-set chnroute dst -j RETURN
  613. iptables -t mangle -A SS-UDP -p udp -j TPROXY --tproxy-mark $iptables_pbr_fwmark --on-ip 127.0.0.1 --on-port 60080
  614. ip route add local 0/0 dev lo table $iptables_pbr_number
  615. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  616. fi
  617. iptables -t nat -A OUTPUT -p tcp -j SS-TCP
  618. for intranet in "${iptables_intranet[@]}"; do
  619. if [[ "$mode" == *tcp ]]; then
  620. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  621. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  622. else
  623. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  624. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  625. iptables -t mangle -A PREROUTING -p udp -s $intranet -j SS-UDP
  626. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  627. fi
  628. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  629. done
  630. ;;
  631. tproxy_gfwlist*)
  632. iptables -t nat -F OUTPUT
  633. iptables -t nat -F PREROUTING
  634. iptables -t nat -F POSTROUTING
  635. iptables -t nat -F SS-TCP &>/dev/null
  636. iptables -t nat -X SS-TCP &>/dev/null
  637. iptables -t nat -N SS-TCP
  638. ipset -N gfwlist hash:net &>/dev/null
  639. perl -ne 'print if m@^\d++\.\d++\.\d++\.\d++(?:/\d++)?$@' $dnsmasq_gfwlist_ext | xargs -n1 ipset -A gfwlist &>/dev/null
  640. [[ "$mode" == *tcp ]] && iptables -t nat -A SS-TCP -p tcp -d ${dns_remote%%:*} -j REDIRECT --to-ports 60080
  641. iptables -t nat -A SS-TCP -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-ports 60080
  642. if [[ "$mode" != *tcp ]]; then
  643. iptables -t mangle -F PREROUTING
  644. iptables -t mangle -F SS-UDP &>/dev/null
  645. iptables -t mangle -X SS-UDP &>/dev/null
  646. iptables -t mangle -N SS-UDP
  647. 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
  648. ip route add local 0/0 dev lo table $iptables_pbr_number
  649. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  650. fi
  651. iptables -t nat -A OUTPUT -p tcp -j SS-TCP
  652. for intranet in "${iptables_intranet[@]}"; do
  653. if [[ "$mode" == *tcp ]]; then
  654. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  655. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  656. else
  657. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  658. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  659. iptables -t mangle -A PREROUTING -p udp -s $intranet -j SS-UDP
  660. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  661. fi
  662. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  663. done
  664. ;;
  665. tun2socks_global*|tun2socks_chnroute*)
  666. iptables -t mangle -F OUTPUT
  667. iptables -t mangle -F PREROUTING
  668. iptables -t mangle -F TUN2SOCKS &>/dev/null
  669. iptables -t mangle -X TUN2SOCKS &>/dev/null
  670. iptables -t nat -F PREROUTING
  671. iptables -t nat -F POSTROUTING
  672. iptables -t mangle -N TUN2SOCKS
  673. iptables -t mangle -A TUN2SOCKS -d 0/8 -j RETURN
  674. iptables -t mangle -A TUN2SOCKS -d 10/8 -j RETURN
  675. iptables -t mangle -A TUN2SOCKS -d 127/8 -j RETURN
  676. iptables -t mangle -A TUN2SOCKS -d 169.254/16 -j RETURN
  677. iptables -t mangle -A TUN2SOCKS -d 172.16/12 -j RETURN
  678. iptables -t mangle -A TUN2SOCKS -d 192.168/16 -j RETURN
  679. for cidr in "${iptables_intranet_nonstd[@]}"; do
  680. iptables -t mangle -A TUN2SOCKS -d $cidr -j RETURN
  681. done
  682. iptables -t mangle -A TUN2SOCKS -d 224/4 -j RETURN
  683. iptables -t mangle -A TUN2SOCKS -d 240/4 -j RETURN
  684. iptables -t mangle -A TUN2SOCKS -d $socks5_remote -j RETURN
  685. if [[ "$mode" == *chnroute* ]]; then
  686. ipset -X chnroute &>/dev/null
  687. ipset -R <$iptables_ipset_file
  688. iptables -t mangle -A TUN2SOCKS -m set --match-set chnroute dst -j RETURN
  689. fi
  690. iptables -t mangle -A TUN2SOCKS -j MARK --set-mark $iptables_pbr_fwmark
  691. ip route add 0/0 via $tun2socks_gateway dev $tun2socks_tundev table $iptables_pbr_number
  692. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  693. iptables -t mangle -A OUTPUT -p tcp -j TUN2SOCKS
  694. [[ "$mode" != *tcp ]] && iptables -t mangle -A OUTPUT -p udp -j TUN2SOCKS
  695. for intranet in "${iptables_intranet[@]}"; do
  696. if [[ "$mode" == *tcp ]]; then
  697. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  698. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  699. else
  700. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  701. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  702. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  703. iptables -t mangle -A PREROUTING -p udp -s $intranet -j TUN2SOCKS
  704. fi
  705. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  706. done
  707. ;;
  708. tun2socks_gfwlist*)
  709. iptables -t mangle -F OUTPUT
  710. iptables -t mangle -F PREROUTING
  711. iptables -t mangle -F TUN2SOCKS &>/dev/null
  712. iptables -t mangle -X TUN2SOCKS &>/dev/null
  713. iptables -t nat -F PREROUTING
  714. iptables -t nat -F POSTROUTING
  715. iptables -t mangle -N TUN2SOCKS
  716. ipset -N gfwlist hash:net &>/dev/null
  717. perl -ne 'print if m@^\d++\.\d++\.\d++\.\d++(?:/\d++)?$@' $dnsmasq_gfwlist_ext | xargs -n1 ipset -A gfwlist &>/dev/null
  718. [[ "$mode" != *tcp ]] && iptables -t mangle -A TUN2SOCKS -p udp -d ${dns_remote%%:*} -j MARK --set-mark $iptables_pbr_fwmark
  719. iptables -t mangle -A TUN2SOCKS -m set --match-set gfwlist dst -j MARK --set-mark $iptables_pbr_fwmark
  720. ip route add 0/0 via $tun2socks_gateway dev $tun2socks_tundev table $iptables_pbr_number
  721. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  722. iptables -t mangle -A OUTPUT -p tcp -j TUN2SOCKS
  723. [[ "$mode" != *tcp ]] && iptables -t mangle -A OUTPUT -p udp -j TUN2SOCKS
  724. for intranet in "${iptables_intranet[@]}"; do
  725. if [[ "$mode" == *tcp ]]; then
  726. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  727. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  728. else
  729. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  730. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  731. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  732. iptables -t mangle -A PREROUTING -p udp -s $intranet -j TUN2SOCKS
  733. fi
  734. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  735. done
  736. ;;
  737. esac
  738. }
  739. function start_kopts {
  740. [ $(sysctl net.ipv4.ip_forward | awk '{print $3}') -ne 1 ] && sysctl -w net.ipv4.ip_forward=1 &>/dev/null
  741. [[ "$mode" == tun2socks* ]] && {
  742. [ $(sysctl net.ipv4.conf.$tun2socks_tundev.rp_filter | awk '{print $3}') -ne 2 ] &&
  743. sysctl -w net.ipv4.conf.$tun2socks_tundev.rp_filter=2 &>/dev/null
  744. }
  745. }
  746. function dns_proxy {
  747. while umount /etc/resolv.conf; do :; done &>/dev/null
  748. resolv=$(mktemp)
  749. chmod 0644 $resolv
  750. cat <<EOF >$resolv
  751. # Generated by ss-tproxy at $(date '+%F %T')
  752. nameserver 127.0.0.1
  753. EOF
  754. mount -B $resolv /etc/resolv.conf
  755. rm -f $resolv &>/dev/null
  756. }
  757. function start {
  758. check_depend
  759. while umount /etc/resolv.conf; do :; done &>/dev/null
  760. start_socks
  761. start_dns
  762. start_rule
  763. start_kopts
  764. dns_proxy
  765. rm -f $chinadns_chnroute_temp &>/dev/null
  766. }
  767. function stop {
  768. while umount /etc/resolv.conf; do :; done &>/dev/null
  769. iptables -t nat -F OUTPUT
  770. iptables -t nat -F PREROUTING
  771. iptables -t nat -F POSTROUTING
  772. iptables -t nat -F SS-TCP &>/dev/null
  773. iptables -t nat -X SS-TCP &>/dev/null
  774. iptables -t mangle -F OUTPUT
  775. iptables -t mangle -F PREROUTING
  776. iptables -t mangle -F SS-UDP &>/dev/null
  777. iptables -t mangle -X SS-UDP &>/dev/null
  778. iptables -t mangle -F TUN2SOCKS &>/dev/null
  779. iptables -t mangle -X TUN2SOCKS &>/dev/null
  780. for intranet in "${iptables_intranet[@]}"; do
  781. iptables -t nat -A PREROUTING -p udp -s $intranet -d $intranet --dport 53 -j DNAT --to-destination $dns_direct
  782. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  783. done
  784. ipset -X chnroute &>/dev/null
  785. ip rule show | grep "fwmark $iptables_pbr_fwmark" | awk -F':' '{print $1}' | xargs -n1 ip rule del pref &>/dev/null
  786. ip route flush table $iptables_pbr_number &>/dev/null
  787. pkill -9 '^dnsmasq$'
  788. pkill -9 '^chinadns$'
  789. pkill -9 '^dnsforwarder$'
  790. pkill -9 '^ss-redir$'
  791. pkill -9 '^ss-tunnel$'
  792. pkill -9 '^ssr-redir$'
  793. pkill -9 '^ssr-tunnel$'
  794. pkill -9 '^tun2socks$'
  795. port=$(echo $socks5_listen | awk -F: '{print $2}')
  796. ss -lnptu | grep ":$port\s" | grep -Eo 'pid=[0-9]+' | awk -F= '{print $2}' | sort -n | uniq | xargs kill -9 &>/dev/null
  797. }
  798. function status {
  799. case "$mode" in
  800. tproxy_global)
  801. echo "mode: tproxy_global"
  802. if [ "$server_use_ssr" != 'true' ]; then
  803. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  804. [ $(pgrep -c '^ss-tunnel$') -ne 0 ] && echo -e "ss-tunnel: \e[32m[running]\e[0m" || echo -e "ss-tunnel: \e[35m[stopped]\e[0m"
  805. else
  806. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  807. [ $(pgrep -c '^ssr-tunnel$') -ne 0 ] && echo -e "ssr-tunnel: \e[32m[running]\e[0m" || echo -e "ssr-tunnel: \e[35m[stopped]\e[0m"
  808. fi
  809. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  810. ;;
  811. tproxy_global_tcp)
  812. echo "mode: tproxy_global_tcp"
  813. if [ "$server_use_ssr" != 'true' ]; then
  814. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  815. else
  816. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  817. fi
  818. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  819. ;;
  820. tproxy_gfwlist)
  821. echo "mode: tproxy_gfwlist"
  822. if [ "$server_use_ssr" != 'true' ]; then
  823. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  824. [ $(pgrep -c '^ss-tunnel$') -ne 0 ] && echo -e "ss-tunnel: \e[32m[running]\e[0m" || echo -e "ss-tunnel: \e[35m[stopped]\e[0m"
  825. else
  826. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  827. [ $(pgrep -c '^ssr-tunnel$') -ne 0 ] && echo -e "ssr-tunnel: \e[32m[running]\e[0m" || echo -e "ssr-tunnel: \e[35m[stopped]\e[0m"
  828. fi
  829. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  830. ;;
  831. tproxy_gfwlist_tcp)
  832. echo "mode: tproxy_gfwlist_tcp"
  833. if [ "$server_use_ssr" != 'true' ]; then
  834. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  835. else
  836. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  837. fi
  838. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  839. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  840. ;;
  841. tproxy_chnroute)
  842. echo "mode: tproxy_chnroute"
  843. if [ "$server_use_ssr" != 'true' ]; then
  844. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  845. [ $(pgrep -c '^ss-tunnel$') -ne 0 ] && echo -e "ss-tunnel: \e[32m[running]\e[0m" || echo -e "ss-tunnel: \e[35m[stopped]\e[0m"
  846. else
  847. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  848. [ $(pgrep -c '^ssr-tunnel$') -ne 0 ] && echo -e "ssr-tunnel: \e[32m[running]\e[0m" || echo -e "ssr-tunnel: \e[35m[stopped]\e[0m"
  849. fi
  850. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  851. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  852. ;;
  853. tproxy_chnroute_tcp)
  854. echo "mode: tproxy_chnroute_tcp"
  855. if [ "$server_use_ssr" != 'true' ]; then
  856. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  857. else
  858. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  859. fi
  860. [ $(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"
  861. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  862. [ $(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"
  863. ;;
  864. tun2socks_global)
  865. echo "mode: tun2socks_global"
  866. port=$(echo $socks5_listen | awk -F: '{print $2}')
  867. [ $(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"
  868. [ $(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"
  869. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  870. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  871. ;;
  872. tun2socks_global_tcp)
  873. echo "mode: tun2socks_global_tcp"
  874. port=$(echo $socks5_listen | awk -F: '{print $2}')
  875. [ $(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"
  876. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  877. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  878. ;;
  879. tun2socks_gfwlist)
  880. echo "mode: tun2socks_gfwlist"
  881. port=$(echo $socks5_listen | awk -F: '{print $2}')
  882. [ $(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"
  883. [ $(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"
  884. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  885. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  886. ;;
  887. tun2socks_gfwlist_tcp)
  888. echo "mode: tun2socks_gfwlist_tcp"
  889. port=$(echo $socks5_listen | awk -F: '{print $2}')
  890. [ $(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"
  891. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  892. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  893. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  894. ;;
  895. tun2socks_chnroute)
  896. echo "mode: tun2socks_chnroute"
  897. port=$(echo $socks5_listen | awk -F: '{print $2}')
  898. [ $(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"
  899. [ $(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"
  900. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  901. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  902. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  903. ;;
  904. tun2socks_chnroute_tcp)
  905. echo "mode: tun2socks_chnroute_tcp"
  906. port=$(echo $socks5_listen | awk -F: '{print $2}')
  907. [ $(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"
  908. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  909. [ $(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"
  910. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  911. [ $(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"
  912. ;;
  913. esac
  914. }
  915. function usage {
  916. cat <<EOF
  917. Usage: ss-tproxy <COMMAND>
  918. COMMAND := {
  919. start start ss-tproxy
  920. stop stop ss-tproxy
  921. restart restart ss-tproxy
  922. status status of ss-tproxy
  923. flush-cache flush dns cache
  924. flush-gfwlist flush ipset-gfwlist
  925. update-chnonly update chnonly
  926. update-gfwlist update gfwlist
  927. update-chnroute update chnroute
  928. check-depend check dependency
  929. help show this help and exit
  930. }
  931. Issues or Bug report: zfl9.com@gmail.com (Otokaze)
  932. See https://www.zfl9.com/ss-redir.html for more details
  933. EOF
  934. }
  935. case $1 in
  936. start) start; status;;
  937. stop) stop; status;;
  938. status) status;;
  939. r*) stop; status; echo; start; status;;
  940. flush-cache) flush_cache;;
  941. flush-gfwlist) ipset -F gfwlist &>/dev/null;;
  942. update-chnonly) update_chnonly;;
  943. update-gfwlist) update_gfwlist;;
  944. update-chnroute) update_chnroute;;
  945. c*) check_depend;;
  946. h*) usage;;
  947. *) usage; exit 1;;
  948. esac