ss-tproxy 50 KB

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