ss-tproxy 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. min-cache-ttl = $dnsmasq_cache_time
  282. no-negcache
  283. no-resolv
  284. port = 53
  285. server = 127.0.0.1#60053
  286. EOF
  287. );;
  288. tproxy_gfwlist)
  289. dnsmasq -C <(cat <<EOF
  290. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  291. log-async = 20
  292. log-facility = $dnsmasq_log_file
  293. domain-needed
  294. cache-size = $dnsmasq_cache_size
  295. min-cache-ttl = $dnsmasq_cache_time
  296. no-negcache
  297. no-resolv
  298. port = 53
  299. $(for dns in "${dns_direct[@]}"; do echo "server = $dns"; done)
  300. $(perl -pe 's@^.*+$@server=/$&/127.0.0.1#60053\nipset=/$&/gfwlist@' $dnsmasq_gfwlist)
  301. EOF
  302. );;
  303. tproxy_chnroute)
  304. chinadns_upstream=$(for dns in "${dns_direct[@]}"; do echo -n "$dns,"; done)
  305. chinadns_params="-b 0.0.0.0 -p 65353 -s ${chinadns_upstream}127.0.0.1:60053 -c $chinadns_chnroute"
  306. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  307. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  308. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  309. dnsmasq -C <(cat <<EOF
  310. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  311. log-async = 20
  312. log-facility = $dnsmasq_log_file
  313. domain-needed
  314. cache-size = $dnsmasq_cache_size
  315. min-cache-ttl = $dnsmasq_cache_time
  316. no-negcache
  317. no-resolv
  318. port = 53
  319. server = 127.0.0.1#65353
  320. EOF
  321. );;
  322. tproxy_global_tcp)
  323. mkdir -p /root/.dnsforwarder
  324. dnsforwarder -q -d -f <(cat <<EOF
  325. LogOn $dnsfwd_log_on
  326. LogFileThresholdLength 5242880
  327. LogFileFolder $dnsfwd_log_dir
  328. UDPLocal 0.0.0.0:53
  329. TCPGroup $dns_remote * no
  330. BlockNegativeResponse true
  331. Hosts file:///etc/hosts
  332. HostsUpdateInterval 3600
  333. UseCache $dnsfwd_cache_on
  334. MemoryCache $dnsfwd_cache_mem
  335. CacheSize $dnsfwd_cache_size
  336. IgnoreTTL $dnsfwd_ignore_ttl
  337. EOF
  338. ) &>/dev/null;;
  339. tproxy_gfwlist_tcp)
  340. dnsforwarder -q -d -f <(cat <<EOF
  341. LogOn false
  342. UseCache false
  343. UDPLocal 0.0.0.0:60053
  344. TCPGroup $dns_remote * no
  345. BlockNegativeResponse true
  346. EOF
  347. ) &>/dev/null
  348. dnsmasq -C <(cat <<EOF
  349. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  350. log-async = 20
  351. log-facility = $dnsmasq_log_file
  352. domain-needed
  353. cache-size = $dnsmasq_cache_size
  354. min-cache-ttl = $dnsmasq_cache_time
  355. no-negcache
  356. no-resolv
  357. port = 53
  358. $(for dns in "${dns_direct[@]}"; do echo "server = $dns"; done)
  359. $(perl -pe 's@^.*+$@server=/$&/127.0.0.1#60053\nipset=/$&/gfwlist@' $dnsmasq_gfwlist)
  360. EOF
  361. );;
  362. tproxy_chnroute_tcp)
  363. dnsforwarder -q -d -f <(cat <<EOF
  364. LogOn false
  365. UseCache false
  366. UDPLocal 0.0.0.0:60053
  367. TCPGroup $dns_remote * no
  368. BlockNegativeResponse true
  369. EOF
  370. ) &>/dev/null
  371. chinadns_upstream=$(for dns in "${dns_direct[@]}"; do echo -n "$dns,"; done)
  372. chinadns_params="-b 0.0.0.0 -p 65353 -s ${chinadns_upstream}127.0.0.1:60053 -c $chinadns_chnroute"
  373. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  374. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  375. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  376. mkdir -p /root/.dnsforwarder
  377. dnsforwarder -q -d -f <(cat <<EOF
  378. LogOn $dnsfwd_log_on
  379. LogFileThresholdLength 5242880
  380. LogFileFolder $dnsfwd_log_dir
  381. UDPLocal 0.0.0.0:53
  382. UDPGroup 127.0.0.1:65353 * on
  383. BlockNegativeResponse true
  384. Hosts file:///etc/hosts
  385. HostsUpdateInterval 3600
  386. UseCache $dnsfwd_cache_on
  387. MemoryCache $dnsfwd_cache_mem
  388. CacheSize $dnsfwd_cache_size
  389. IgnoreTTL $dnsfwd_ignore_ttl
  390. EOF
  391. ) &>/dev/null;;
  392. tun2socks_global)
  393. dnsmasq -C <(cat <<EOF
  394. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  395. log-async = 20
  396. log-facility = $dnsmasq_log_file
  397. domain-needed
  398. cache-size = $dnsmasq_cache_size
  399. min-cache-ttl = $dnsmasq_cache_time
  400. no-negcache
  401. no-resolv
  402. port = 53
  403. server = ${dns_remote/:/#}
  404. EOF
  405. );;
  406. tun2socks_gfwlist)
  407. dnsmasq -C <(cat <<EOF
  408. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  409. log-async = 20
  410. log-facility = $dnsmasq_log_file
  411. domain-needed
  412. cache-size = $dnsmasq_cache_size
  413. min-cache-ttl = $dnsmasq_cache_time
  414. no-negcache
  415. no-resolv
  416. port = 53
  417. $(for dns in "${dns_direct[@]}"; do echo "server = $dns"; done)
  418. $(perl -pe "s@^.*+\$@server=/$&/${dns_remote/:/#}\nipset=/$&/gfwlist@" $dnsmasq_gfwlist)
  419. EOF
  420. );;
  421. tun2socks_chnroute)
  422. chinadns_upstream=$(for dns in "${dns_direct[@]}"; do echo -n "$dns,"; done)
  423. chinadns_params="-b 0.0.0.0 -p 60053 -s ${chinadns_upstream}${dns_remote} -c $chinadns_chnroute"
  424. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  425. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  426. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  427. dnsmasq -C <(cat <<EOF
  428. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  429. log-async = 20
  430. log-facility = $dnsmasq_log_file
  431. domain-needed
  432. cache-size = $dnsmasq_cache_size
  433. min-cache-ttl = $dnsmasq_cache_time
  434. no-negcache
  435. no-resolv
  436. port = 53
  437. server = 127.0.0.1#60053
  438. EOF
  439. );;
  440. tun2socks_global_tcp)
  441. mkdir -p /root/.dnsforwarder
  442. dnsforwarder -q -d -f <(cat <<EOF
  443. LogOn $dnsfwd_log_on
  444. LogFileThresholdLength 5242880
  445. LogFileFolder $dnsfwd_log_dir
  446. UDPLocal 0.0.0.0:53
  447. TCPGroup $dns_remote * $socks5_listen
  448. BlockNegativeResponse true
  449. Hosts file:///etc/hosts
  450. HostsUpdateInterval 3600
  451. UseCache $dnsfwd_cache_on
  452. MemoryCache $dnsfwd_cache_mem
  453. CacheSize $dnsfwd_cache_size
  454. IgnoreTTL $dnsfwd_ignore_ttl
  455. EOF
  456. ) &>/dev/null;;
  457. tun2socks_gfwlist_tcp)
  458. dnsforwarder -q -d -f <(cat <<EOF
  459. LogOn false
  460. UseCache false
  461. UDPLocal 0.0.0.0:60053
  462. TCPGroup $dns_remote * $socks5_listen
  463. BlockNegativeResponse true
  464. EOF
  465. ) &>/dev/null
  466. dnsmasq -C <(cat <<EOF
  467. $([ "$dnsmasq_log_on" = 'true' ] && echo 'log-queries')
  468. log-async = 20
  469. log-facility = $dnsmasq_log_file
  470. domain-needed
  471. cache-size = $dnsmasq_cache_size
  472. min-cache-ttl = $dnsmasq_cache_time
  473. no-negcache
  474. no-resolv
  475. port = 53
  476. $(for dns in "${dns_direct[@]}"; do echo "server = $dns"; done)
  477. $(perl -pe 's@^.*+$@server=/$&/127.0.0.1#60053\nipset=/$&/gfwlist@' $dnsmasq_gfwlist)
  478. EOF
  479. );;
  480. tun2socks_chnroute_tcp)
  481. dnsforwarder -q -d -f <(cat <<EOF
  482. LogOn false
  483. UseCache false
  484. UDPLocal 0.0.0.0:60053
  485. TCPGroup $dns_remote * $socks5_listen
  486. BlockNegativeResponse true
  487. EOF
  488. ) &>/dev/null
  489. chinadns_upstream=$(for dns in "${dns_direct[@]}"; do echo -n "$dns,"; done)
  490. chinadns_params="-b 0.0.0.0 -p 65353 -s ${chinadns_upstream}127.0.0.1:60053 -c $chinadns_chnroute"
  491. [ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
  492. [ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
  493. nohup chinadns $chinadns_params </dev/null &>>"$chinadns_log" &
  494. mkdir -p /root/.dnsforwarder
  495. dnsforwarder -q -d -f <(cat <<EOF
  496. LogOn $dnsfwd_log_on
  497. LogFileThresholdLength 5242880
  498. LogFileFolder $dnsfwd_log_dir
  499. UDPLocal 0.0.0.0:53
  500. UDPGroup 127.0.0.1:65353 * on
  501. BlockNegativeResponse true
  502. Hosts file:///etc/hosts
  503. HostsUpdateInterval 3600
  504. UseCache $dnsfwd_cache_on
  505. MemoryCache $dnsfwd_cache_mem
  506. CacheSize $dnsfwd_cache_size
  507. IgnoreTTL $dnsfwd_ignore_ttl
  508. EOF
  509. ) &>/dev/null;;
  510. esac
  511. }
  512. function start_rule {
  513. case "$mode" in
  514. tproxy_global*|tproxy_chnroute*)
  515. iptables -t nat -F OUTPUT
  516. iptables -t nat -F PREROUTING
  517. iptables -t nat -F POSTROUTING
  518. iptables -t nat -F SS-TCP &>/dev/null
  519. iptables -t nat -X SS-TCP &>/dev/null
  520. iptables -t nat -N SS-TCP
  521. iptables -t nat -A SS-TCP -d 0/8 -j RETURN
  522. iptables -t nat -A SS-TCP -d 10/8 -j RETURN
  523. iptables -t nat -A SS-TCP -d 127/8 -j RETURN
  524. iptables -t nat -A SS-TCP -d 169.254/16 -j RETURN
  525. iptables -t nat -A SS-TCP -d 172.16/12 -j RETURN
  526. iptables -t nat -A SS-TCP -d 192.168/16 -j RETURN
  527. iptables -t nat -A SS-TCP -d 224/4 -j RETURN
  528. iptables -t nat -A SS-TCP -d 240/4 -j RETURN
  529. iptables -t nat -A SS-TCP -d $server_addr -j RETURN
  530. if [[ "$mode" == *chnroute* ]]; then
  531. ipset -X chnroute &>/dev/null
  532. ipset -R <$iptables_ipset_file
  533. iptables -t nat -A SS-TCP -m set --match-set chnroute dst -j RETURN
  534. fi
  535. iptables -t nat -A SS-TCP -p tcp -j REDIRECT --to-ports 60080
  536. if [[ "$mode" != *tcp ]]; then
  537. iptables -t mangle -F PREROUTING
  538. iptables -t mangle -F SS-UDP &>/dev/null
  539. iptables -t mangle -X SS-UDP &>/dev/null
  540. iptables -t mangle -N SS-UDP
  541. iptables -t mangle -A SS-UDP -d 0/8 -j RETURN
  542. iptables -t mangle -A SS-UDP -d 10/8 -j RETURN
  543. iptables -t mangle -A SS-UDP -d 127/8 -j RETURN
  544. iptables -t mangle -A SS-UDP -d 169.254/16 -j RETURN
  545. iptables -t mangle -A SS-UDP -d 172.16/12 -j RETURN
  546. iptables -t mangle -A SS-UDP -d 192.168/16 -j RETURN
  547. iptables -t mangle -A SS-UDP -d 224/4 -j RETURN
  548. iptables -t mangle -A SS-UDP -d 240/4 -j RETURN
  549. iptables -t mangle -A SS-UDP -d $server_addr -j RETURN
  550. [[ "$mode" == *chnroute* ]] && iptables -t mangle -A SS-UDP -m set --match-set chnroute dst -j RETURN
  551. iptables -t mangle -A SS-UDP -p udp -j TPROXY --tproxy-mark $iptables_pbr_fwmark --on-ip 127.0.0.1 --on-port 60080
  552. ip route add local 0/0 dev lo table $iptables_pbr_number
  553. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  554. fi
  555. iptables -t nat -A OUTPUT -p tcp -j SS-TCP
  556. for intranet in "${iptables_intranet[@]}"; do
  557. if [[ "$mode" == *tcp ]]; then
  558. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  559. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  560. else
  561. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  562. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  563. iptables -t mangle -A PREROUTING -p udp -s $intranet -j SS-UDP
  564. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  565. fi
  566. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  567. done
  568. ;;
  569. tproxy_gfwlist*)
  570. iptables -t nat -F OUTPUT
  571. iptables -t nat -F PREROUTING
  572. iptables -t nat -F POSTROUTING
  573. iptables -t nat -F SS-TCP &>/dev/null
  574. iptables -t nat -X SS-TCP &>/dev/null
  575. iptables -t nat -N SS-TCP
  576. ipset -X gfwlist &>/dev/null
  577. ipset -N gfwlist hash:ip
  578. [[ "$mode" == *tcp ]] && iptables -t nat -A SS-TCP -p tcp -d ${dns_remote%%:*} -j REDIRECT --to-ports 60080
  579. iptables -t nat -A SS-TCP -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-ports 60080
  580. if [[ "$mode" != *tcp ]]; then
  581. iptables -t mangle -F PREROUTING
  582. iptables -t mangle -F SS-UDP &>/dev/null
  583. iptables -t mangle -X SS-UDP &>/dev/null
  584. iptables -t mangle -N SS-UDP
  585. 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
  586. ip route add local 0/0 dev lo table $iptables_pbr_number
  587. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  588. fi
  589. iptables -t nat -A OUTPUT -p tcp -j SS-TCP
  590. for intranet in "${iptables_intranet[@]}"; do
  591. if [[ "$mode" == *tcp ]]; then
  592. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  593. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  594. else
  595. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  596. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  597. iptables -t mangle -A PREROUTING -p udp -s $intranet -j SS-UDP
  598. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  599. fi
  600. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  601. done
  602. ;;
  603. tun2socks_global*|tun2socks_chnroute*)
  604. iptables -t mangle -F OUTPUT
  605. iptables -t mangle -F PREROUTING
  606. iptables -t mangle -F TUN2SOCKS &>/dev/null
  607. iptables -t mangle -X TUN2SOCKS &>/dev/null
  608. iptables -t nat -F PREROUTING
  609. iptables -t nat -F POSTROUTING
  610. iptables -t mangle -N TUN2SOCKS
  611. iptables -t mangle -A TUN2SOCKS -d 0/8 -j RETURN
  612. iptables -t mangle -A TUN2SOCKS -d 10/8 -j RETURN
  613. iptables -t mangle -A TUN2SOCKS -d 127/8 -j RETURN
  614. iptables -t mangle -A TUN2SOCKS -d 169.254/16 -j RETURN
  615. iptables -t mangle -A TUN2SOCKS -d 172.16/12 -j RETURN
  616. iptables -t mangle -A TUN2SOCKS -d 192.168/16 -j RETURN
  617. iptables -t mangle -A TUN2SOCKS -d 224/4 -j RETURN
  618. iptables -t mangle -A TUN2SOCKS -d 240/4 -j RETURN
  619. iptables -t mangle -A TUN2SOCKS -d $socks5_remote -j RETURN
  620. if [[ "$mode" == *chnroute* ]]; then
  621. ipset -X chnroute &>/dev/null
  622. ipset -R <$iptables_ipset_file
  623. iptables -t mangle -A TUN2SOCKS -m set --match-set chnroute dst -j RETURN
  624. fi
  625. iptables -t mangle -A TUN2SOCKS -j MARK --set-mark $iptables_pbr_fwmark
  626. ip route add 0/0 via $tun2socks_gateway dev $tun2socks_tundev table $iptables_pbr_number
  627. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  628. iptables -t mangle -A OUTPUT -p tcp -j TUN2SOCKS
  629. [[ "$mode" != *tcp ]] && iptables -t mangle -A OUTPUT -p udp -j TUN2SOCKS
  630. for intranet in "${iptables_intranet[@]}"; do
  631. if [[ "$mode" == *tcp ]]; then
  632. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  633. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  634. else
  635. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  636. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  637. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  638. iptables -t mangle -A PREROUTING -p udp -s $intranet -j TUN2SOCKS
  639. fi
  640. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  641. done
  642. ;;
  643. tun2socks_gfwlist*)
  644. iptables -t mangle -F OUTPUT
  645. iptables -t mangle -F PREROUTING
  646. iptables -t mangle -F TUN2SOCKS &>/dev/null
  647. iptables -t mangle -X TUN2SOCKS &>/dev/null
  648. iptables -t nat -F PREROUTING
  649. iptables -t nat -F POSTROUTING
  650. iptables -t mangle -N TUN2SOCKS
  651. ipset -X gfwlist &>/dev/null
  652. ipset -N gfwlist hash:ip
  653. [[ "$mode" != *tcp ]] && iptables -t mangle -A TUN2SOCKS -p udp -d ${dns_remote%%:*} -j MARK --set-mark $iptables_pbr_fwmark
  654. iptables -t mangle -A TUN2SOCKS -m set --match-set gfwlist dst -j MARK --set-mark $iptables_pbr_fwmark
  655. ip route add 0/0 via $tun2socks_gateway dev $tun2socks_tundev table $iptables_pbr_number
  656. ip rule add fwmark $iptables_pbr_fwmark table $iptables_pbr_number
  657. iptables -t mangle -A OUTPUT -p tcp -j TUN2SOCKS
  658. [[ "$mode" != *tcp ]] && iptables -t mangle -A OUTPUT -p udp -j TUN2SOCKS
  659. for intranet in "${iptables_intranet[@]}"; do
  660. if [[ "$mode" == *tcp ]]; then
  661. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  662. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  663. else
  664. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  665. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  666. iptables -t mangle -A PREROUTING -p tcp -s $intranet -j TUN2SOCKS
  667. iptables -t mangle -A PREROUTING -p udp -s $intranet -j TUN2SOCKS
  668. fi
  669. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  670. done
  671. ;;
  672. esac
  673. }
  674. function start_kopts {
  675. [ $(sysctl net.ipv4.ip_forward | awk '{print $3}') -ne 1 ] && sysctl -w net.ipv4.ip_forward=1
  676. [[ "$mode" == tun2socks* ]] && {
  677. [ $(sysctl net.ipv4.conf.$tun2socks_tundev.rp_filter | awk '{print $3}') -ne 2 ] &&
  678. sysctl -w net.ipv4.conf.$tun2socks_tundev.rp_filter=2
  679. }
  680. }
  681. function dns_proxy {
  682. cat <<EOF >/etc/resolv.conf
  683. # Generated by ss-tproxy at $(date '+%F %T')
  684. nameserver 127.0.0.1
  685. EOF
  686. }
  687. function dns_normal {
  688. cat <<EOF >/etc/resolv.conf
  689. # Generated by ss-tproxy at $(date '+%F %T')
  690. $(for dns in "${dns_direct[@]}"; do echo "nameserver $dns"; done)
  691. EOF
  692. }
  693. function start {
  694. check_depend
  695. dns_normal
  696. start_socks
  697. start_dns
  698. start_rule
  699. start_kopts
  700. dns_proxy
  701. }
  702. function stop {
  703. check_depend
  704. dns_normal
  705. iptables -t nat -F OUTPUT
  706. iptables -t nat -F PREROUTING
  707. iptables -t nat -F POSTROUTING
  708. iptables -t nat -F SS-TCP &>/dev/null
  709. iptables -t nat -X SS-TCP &>/dev/null
  710. iptables -t mangle -F OUTPUT
  711. iptables -t mangle -F PREROUTING
  712. iptables -t mangle -F SS-UDP &>/dev/null
  713. iptables -t mangle -X SS-UDP &>/dev/null
  714. iptables -t mangle -F TUN2SOCKS &>/dev/null
  715. iptables -t mangle -X TUN2SOCKS &>/dev/null
  716. for intranet in "${iptables_intranet[@]}"; do
  717. iptables -t nat -A PREROUTING -p udp -s $intranet -d $intranet --dport 53 -j DNAT --to-destination ${dns_direct[0]}
  718. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  719. done
  720. ipset -X gfwlist &>/dev/null
  721. ipset -X chnroute &>/dev/null
  722. ip rule show | grep "fwmark $iptables_pbr_fwmark" | awk -F':' '{print $1}' | xargs -n1 ip rule del pref &>/dev/null
  723. ip route flush table $iptables_pbr_number
  724. pkill -9 '^dnsmasq$'
  725. pkill -9 '^chinadns$'
  726. pkill -9 '^dnsforwarder$'
  727. pkill -9 '^ss-redir$'
  728. pkill -9 '^ss-tunnel$'
  729. pkill -9 '^ssr-redir$'
  730. pkill -9 '^ssr-tunnel$'
  731. pkill -9 '^tun2socks$'
  732. port=$(echo $socks5_listen | awk -F: '{print $2}')
  733. ss -lnptu | grep ":$port\s" | grep -Eo 'pid=[0-9]+' | awk -F= '{print $2}' | sort -n | uniq | xargs kill -9 &>/dev/null
  734. }
  735. function status {
  736. case "$mode" in
  737. tproxy_global)
  738. echo "mode: tproxy_global"
  739. if [ "$server_use_ssr" != 'true' ]; then
  740. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  741. [ $(pgrep -c '^ss-tunnel$') -ne 0 ] && echo -e "ss-tunnel: \e[32m[running]\e[0m" || echo -e "ss-tunnel: \e[35m[stopped]\e[0m"
  742. else
  743. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  744. [ $(pgrep -c '^ssr-tunnel$') -ne 0 ] && echo -e "ssr-tunnel: \e[32m[running]\e[0m" || echo -e "ssr-tunnel: \e[35m[stopped]\e[0m"
  745. fi
  746. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  747. ;;
  748. tproxy_global_tcp)
  749. echo "mode: tproxy_global_tcp"
  750. if [ "$server_use_ssr" != 'true' ]; then
  751. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  752. else
  753. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  754. fi
  755. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  756. ;;
  757. tproxy_gfwlist)
  758. echo "mode: tproxy_gfwlist"
  759. if [ "$server_use_ssr" != 'true' ]; then
  760. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  761. [ $(pgrep -c '^ss-tunnel$') -ne 0 ] && echo -e "ss-tunnel: \e[32m[running]\e[0m" || echo -e "ss-tunnel: \e[35m[stopped]\e[0m"
  762. else
  763. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  764. [ $(pgrep -c '^ssr-tunnel$') -ne 0 ] && echo -e "ssr-tunnel: \e[32m[running]\e[0m" || echo -e "ssr-tunnel: \e[35m[stopped]\e[0m"
  765. fi
  766. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  767. ;;
  768. tproxy_gfwlist_tcp)
  769. echo "mode: tproxy_gfwlist_tcp"
  770. if [ "$server_use_ssr" != 'true' ]; then
  771. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  772. else
  773. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  774. fi
  775. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  776. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  777. ;;
  778. tproxy_chnroute)
  779. echo "mode: tproxy_chnroute"
  780. if [ "$server_use_ssr" != 'true' ]; then
  781. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  782. [ $(pgrep -c '^ss-tunnel$') -ne 0 ] && echo -e "ss-tunnel: \e[32m[running]\e[0m" || echo -e "ss-tunnel: \e[35m[stopped]\e[0m"
  783. else
  784. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  785. [ $(pgrep -c '^ssr-tunnel$') -ne 0 ] && echo -e "ssr-tunnel: \e[32m[running]\e[0m" || echo -e "ssr-tunnel: \e[35m[stopped]\e[0m"
  786. fi
  787. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  788. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  789. ;;
  790. tproxy_chnroute_tcp)
  791. echo "mode: tproxy_chnroute_tcp"
  792. if [ "$server_use_ssr" != 'true' ]; then
  793. [ $(pgrep -c '^ss-redir$') -ne 0 ] && echo -e "ss-redir: \e[32m[running]\e[0m" || echo -e "ss-redir: \e[35m[stopped]\e[0m"
  794. else
  795. [ $(pgrep -c '^ssr-redir$') -ne 0 ] && echo -e "ssr-redir: \e[32m[running]\e[0m" || echo -e "ssr-redir: \e[35m[stopped]\e[0m"
  796. fi
  797. [ $(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"
  798. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  799. [ $(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"
  800. ;;
  801. tun2socks_global)
  802. echo "mode: tun2socks_global"
  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. [ $(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"
  806. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  807. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  808. ;;
  809. tun2socks_global_tcp)
  810. echo "mode: tun2socks_global_tcp"
  811. port=$(echo $socks5_listen | awk -F: '{print $2}')
  812. [ $(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"
  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 '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  815. ;;
  816. tun2socks_gfwlist)
  817. echo "mode: tun2socks_gfwlist"
  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. [ $(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"
  821. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \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_gfwlist_tcp)
  825. echo "mode: tun2socks_gfwlist_tcp"
  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. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  829. [ $(pgrep -c '^dnsforwarder$') -ne 0 ] && echo -e "dnsforwarder: \e[32m[running]\e[0m" || echo -e "dnsforwarder: \e[35m[stopped]\e[0m"
  830. [ $(pgrep -c '^dnsmasq$') -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
  831. ;;
  832. tun2socks_chnroute)
  833. echo "mode: tun2socks_chnroute"
  834. port=$(echo $socks5_listen | awk -F: '{print $2}')
  835. [ $(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"
  836. [ $(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"
  837. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  838. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \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. tun2socks_chnroute_tcp)
  842. echo "mode: tun2socks_chnroute_tcp"
  843. port=$(echo $socks5_listen | awk -F: '{print $2}')
  844. [ $(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"
  845. [ $(pgrep -c '^tun2socks$') -ne 0 ] && echo -e "tun2socks: \e[32m[running]\e[0m" || echo -e "tun2socks: \e[35m[stopped]\e[0m"
  846. [ $(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"
  847. [ $(pgrep -c '^chinadns$') -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
  848. [ $(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"
  849. ;;
  850. esac
  851. }
  852. function usage {
  853. cat <<EOF
  854. Usage: ss-tproxy <COMMAND>
  855. COMMAND := {
  856. start start ss-tproxy
  857. stop stop ss-tproxy
  858. restart restart ss-tproxy
  859. status status of ss-tproxy
  860. flush-cache flush dns cache
  861. update-gfwlist update gfwlist
  862. update-chnroute update chnroute
  863. check-depend check dependency
  864. help show this help and exit
  865. }
  866. Issues/Bug report: zfl9.com@gmail.com (Otokaze)
  867. See also https://www.zfl9.com/ss-redir.html for more details.
  868. EOF
  869. }
  870. case $1 in
  871. start) start; status;;
  872. stop) stop; status;;
  873. status) status;;
  874. r*) stop; status; echo; sleep 1; start; status;;
  875. f*) flush_cache;;
  876. update-g*) update_gfwlist;;
  877. update-c*) update_chnroute;;
  878. c*) check_depend;;
  879. h*) usage;;
  880. *) usage; exit 1;;
  881. esac