ss-tproxy 48 KB

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