ss-tproxy 48 KB

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