ss-tproxy 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #!/bin/bash
  2. main_cfg='/etc/tproxy/ss-tproxy.conf'
  3. if [ ! -e "${main_cfg}" ]; then
  4. echo -e "\e[37m${main_cfg}\e[0m [\e[1;35mnot_exist\e[0m]" 1>&2
  5. exit 1
  6. else
  7. source "${main_cfg}"
  8. fi
  9. function pdnsd_update() {
  10. if ! id -u $pdnsd_global_runuser &> /dev/null; then
  11. useradd -M -U -d $pdnsd_global_cachedir -s /sbin/nologin $pdnsd_global_runuser
  12. fi
  13. if [ ! -d $pdnsd_global_cachedir ]; then
  14. rm -fr $pdnsd_global_cachedir
  15. mkdir -p $pdnsd_global_cachedir
  16. fi
  17. if [ ! -f $pdnsd_global_cachedir/pdnsd.cache ]; then
  18. touch $pdnsd_global_cachedir/pdnsd.cache
  19. fi
  20. chown -R $pdnsd_global_runuser:$pdnsd_global_runuser $pdnsd_global_cachedir
  21. cat << EOF > $pdnsd_global_conf
  22. # Generated by ss-tproxy at $(date '+%F %T')
  23. global {
  24. perm_cache = $pdnsd_global_cachesize;
  25. cache_dir = "$pdnsd_global_cachedir";
  26. run_as = "$pdnsd_global_runuser";
  27. server_ip = $pdnsd_global_bindaddr;
  28. server_port = $pdnsd_global_bindport;
  29. tcp_server = $pdnsd_global_bindtcp;
  30. query_method = $pdnsd_global_querymethod;
  31. status_ctl = $pdnsd_global_statusctl;
  32. paranoid = $pdnsd_global_paranoid;
  33. min_ttl = $pdnsd_global_minttl;
  34. max_ttl = $pdnsd_global_maxttl;
  35. timeout = $pdnsd_global_timeout;
  36. randomize_recs = $pdnsd_global_ramdomize;
  37. verbosity = $pdnsd_global_loglevel;
  38. }
  39. server {
  40. label = "$pdnsd_server_label";
  41. ip = $pdnsd_server_upaddr;
  42. port = $pdnsd_server_upport;
  43. proxy_only = $pdnsd_server_proxyonly;
  44. }
  45. source {
  46. owner = $pdnsd_source_owner;
  47. file = "$pdnsd_source_file";
  48. }
  49. EOF
  50. }
  51. function pdnsd_flush() {
  52. if [ $(pgrep -c '^pdnsd$') -eq 0 ]; then
  53. echo -n > $pdnsd_global_cachedir/pdnsd.cache
  54. else
  55. pdnsd-ctl -c $pdnsd_global_cachedir empty-cache &> /dev/null
  56. pdnsd-ctl -c $pdnsd_global_cachedir config &> /dev/null
  57. fi
  58. }
  59. function chnip_update() {
  60. #chnip_url="http://f.ip.cn/rt/chnroutes.txt"
  61. chnip_url="https://github.com/17mon/china_ip_list/raw/master/china_ip_list.txt"
  62. curl -4sSkL ${chnip_url} | egrep -v '^\s*$|^\s*#' > ${chinadns_chnroute}
  63. cat ${chinadns_chnroute} | xargs -n1 echo add chnroute > ${ipset_save_file}
  64. ipset -L chnroute &> /dev/null
  65. if [ $? -ne 0 ]; then
  66. ipset -N chnroute hash:net
  67. else
  68. ipset -F chnroute &> /dev/null
  69. fi
  70. ipset -R < ${ipset_save_file}
  71. ipset -S chnroute > ${ipset_save_file}
  72. }
  73. function ipts_update() {
  74. iptables -t mangle -F PREROUTING
  75. iptables -t mangle -F SS-UDP &> /dev/null
  76. iptables -t mangle -X SS-UDP &> /dev/null
  77. iptables -t nat -F OUTPUT
  78. iptables -t nat -F PREROUTING
  79. iptables -t nat -F POSTROUTING
  80. iptables -t nat -F SS-TCP &> /dev/null
  81. iptables -t nat -X SS-TCP &> /dev/null
  82. iptables -t nat -N SS-TCP
  83. iptables -t nat -A SS-TCP -d 0/8 -j RETURN
  84. iptables -t nat -A SS-TCP -d 10/8 -j RETURN
  85. iptables -t nat -A SS-TCP -d 127/8 -j RETURN
  86. iptables -t nat -A SS-TCP -d 169.254/16 -j RETURN
  87. iptables -t nat -A SS-TCP -d 172.16/12 -j RETURN
  88. iptables -t nat -A SS-TCP -d 192.168/16 -j RETURN
  89. iptables -t nat -A SS-TCP -d 224/4 -j RETURN
  90. iptables -t nat -A SS-TCP -d 240/4 -j RETURN
  91. iptables -t nat -A SS-TCP -d ${server_addr} -j RETURN
  92. iptables -t nat -A SS-TCP -m set --match-set chnroute dst -j RETURN
  93. iptables -t nat -A SS-TCP -p tcp -j REDIRECT --to-ports ${redir_port}
  94. iptables -t mangle -N SS-UDP
  95. iptables -t mangle -A SS-UDP -d 0/8 -j RETURN
  96. iptables -t mangle -A SS-UDP -d 10/8 -j RETURN
  97. iptables -t mangle -A SS-UDP -d 127/8 -j RETURN
  98. iptables -t mangle -A SS-UDP -d 169.254/16 -j RETURN
  99. iptables -t mangle -A SS-UDP -d 172.16/12 -j RETURN
  100. iptables -t mangle -A SS-UDP -d 192.168/16 -j RETURN
  101. iptables -t mangle -A SS-UDP -d 224/4 -j RETURN
  102. iptables -t mangle -A SS-UDP -d 240/4 -j RETURN
  103. iptables -t mangle -A SS-UDP -d ${server_addr} -j RETURN
  104. iptables -t mangle -A SS-UDP -m set --match-set chnroute dst -j RETURN
  105. iptables -t mangle -A SS-UDP -p udp -j TPROXY --tproxy-mark ${iptables_tproxy_mark} --on-ip ${iptables_tproxy_ip} --on-port ${iptables_tproxy_port}
  106. iptables -t nat -A OUTPUT -p tcp -j SS-TCP
  107. for intranet in "${iptables_intranet[@]}"; do
  108. iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
  109. iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
  110. iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
  111. iptables -t mangle -A PREROUTING -p udp -s $intranet -j SS-UDP
  112. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  113. done
  114. }
  115. function ipts_origin() {
  116. iptables -t mangle -F PREROUTING
  117. iptables -t mangle -F SS-UDP &> /dev/null
  118. iptables -t mangle -X SS-UDP &> /dev/null
  119. iptables -t nat -F OUTPUT
  120. iptables -t nat -F PREROUTING
  121. iptables -t nat -F POSTROUTING
  122. iptables -t nat -F SS-TCP &> /dev/null
  123. iptables -t nat -X SS-TCP &> /dev/null
  124. for intranet in "${iptables_intranet[@]}"; do
  125. iptables -t nat -A PREROUTING -p udp -s $intranet -d $intranet --dport 53 -j DNAT --to-destination ${dns_original[0]}
  126. iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
  127. done
  128. }
  129. function pbr_add() {
  130. ip route add local 0/0 dev lo table ${pbr_table}
  131. ip rule add fwmark ${iptables_tproxy_mark} table ${pbr_table}
  132. }
  133. function pbr_del() {
  134. ip rule show | grep "fwmark ${iptables_tproxy_mark}" | awk -F':' '{print $1}' | xargs -n1 ip rule del pref &> /dev/null
  135. ip route flush table ${pbr_table}
  136. }
  137. function ipfwd_on() {
  138. if [ $(cat /proc/sys/net/ipv4/ip_forward) -ne 1 ]; then
  139. #echo 1 > /proc/sys/net/ipv4/ip_forward
  140. sysctl -w net.ipv4.ip_forward=1
  141. fi
  142. }
  143. function dns_update() {
  144. echo "# Generated by ss-tproxy at $(date '+%F %T')" > /etc/resolv.conf
  145. echo "nameserver 127.0.0.1" >> /etc/resolv.conf
  146. }
  147. function dns_origin() {
  148. echo "# Generated by ss-tproxy at $(date '+%F %T')" > /etc/resolv.conf
  149. for dns in "${dns_original[@]}"; do
  150. echo "nameserver $dns" >> /etc/resolv.conf
  151. done
  152. }
  153. function current_ip() {
  154. ip_info=$(curl -4skL ip.chinaz.com/getip.aspx | sed -r "s/^\{ip:'(.*)',address:'(.*)'\}$/\1|\2/g")
  155. my_ip=$(echo ${ip_info} | awk -F'|' '{print $1}')
  156. my_loc=$(echo ${ip_info} | awk -F'|' '{print $2}')
  157. echo -e "\e[37mIP:\e[0m ${my_ip}\t\e[37mLocation:\e[0m ${my_loc}"
  158. }
  159. function start() {
  160. dns_origin
  161. if [ "${server_use_ssr}" = 'false' ]; then
  162. redir_params="-s '${server_addr}' -p '${server_port}' -m '${server_method}' -k '${server_passwd}' -b '${redir_addr}' -l '${redir_port}'"
  163. if [ "${redir_udp_relay}" = 'true' ]; then redir_params+=" -u"; fi
  164. if [ "${redir_verbose}" = 'true' ]; then redir_params+=" -v"; fi
  165. if [ "${redir_no_delay}" = 'true' ]; then redir_params+=" --no-delay"; fi
  166. if [ "${redir_reuse_port}" = 'true' ]; then redir_params+=" --reuse-port"; fi
  167. if [ "${redir_fast_open}" = 'true' ]; then redir_params+=" --fast-open"; fi
  168. eval "nohup ss-redir $redir_params < /dev/null &>> '${redir_log}' &"
  169. else
  170. redir_params="-s '${server_addr}' -p '${server_port}' -m '${server_method}' -k '${server_passwd}' -O '${server_protocol}' -o '${server_obfs}' -b '${redir_addr}' -l '${redir_port}'"
  171. if [ -n "${server_protocol_param}" ]; then redir_params+=" -G '${server_protocol_param}'"; fi
  172. if [ -n "${server_obfs_param}" ]; then redir_params+=" -g '${server_obfs_param}'"; fi
  173. if [ "${redir_udp_relay}" = 'true' ]; then redir_params+=" -u"; fi
  174. if [ "${redir_verbose}" = 'true' ]; then redir_params+=" -v"; fi
  175. eval "nohup ssr-redir $redir_params < /dev/null &>> '${redir_log}' &"
  176. fi
  177. if [ "${server_use_ssr}" = 'false' ]; then
  178. tunnel_params="-s '${server_addr}' -p '${server_port}' -m '${server_method}' -k '${server_passwd}' -b '${tunnel_addr}' -l '${tunnel_port}' -L '${tunnel_upstream}'"
  179. if [ "${tunnel_udp_relay}" = 'true' ]; then tunnel_params+=" -u"; fi
  180. if [ "${tunnel_verbose}" = 'true' ]; then tunnel_params+=" -v"; fi
  181. if [ "${tunnel_no_delay}" = 'true' ]; then tunnel_params+=" --no-delay"; fi
  182. if [ "${tunnel_reuse_port}" = 'true' ]; then tunnel_params+=" --reuse-port"; fi
  183. eval "nohup ss-tunnel $tunnel_params < /dev/null &>> '${tunnel_log}' &"
  184. else
  185. tunnel_params="-s '${server_addr}' -p '${server_port}' -m '${server_method}' -k '${server_passwd}' -O '${server_protocol}' -o '${server_obfs}' -b '${tunnel_addr}' -l '${tunnel_port}' -L '${tunnel_upstream}'"
  186. if [ -n "${server_protocol_param}" ]; then tunnel_params+=" -G '${server_protocol_param}'"; fi
  187. if [ -n "${server_obfs_param}" ]; then tunnel_params+=" -g '${server_obfs_param}'"; fi
  188. if [ "${tunnel_udp_relay}" = 'true' ]; then tunnel_params+=" -u"; fi
  189. if [ "${tunnel_verbose}" = 'true' ]; then tunnel_params+=" -v"; fi
  190. eval "nohup ssr-tunnel $tunnel_params < /dev/null &>> '${tunnel_log}' &"
  191. fi
  192. chinadns_params="-b ${chinadns_addr} -p ${chinadns_port} -s ${chinadns_upstream} -c ${chinadns_chnroute}"
  193. if [ "${chinadns_mutation}" = 'true' ]; then chinadns_params+=" -m"; fi
  194. if [ "${chinadns_verbose}" = 'true' ]; then chinadns_params+=" -v"; fi
  195. nohup chinadns $chinadns_params < /dev/null &>> "${chinadns_log}" &
  196. pdnsd_update
  197. nohup pdnsd -c $pdnsd_global_conf < /dev/null &>> $pdnsd_global_logfile &
  198. ipset -X chnroute &> /dev/null
  199. ipset -R < ${ipset_save_file}
  200. ipts_update
  201. pbr_add
  202. ipfwd_on
  203. dns_update
  204. }
  205. function stop() {
  206. pkill '^pdnsd$' &> /dev/null
  207. pkill '^chinadns$' &> /dev/null
  208. pkill '^ss-redir$' &> /dev/null
  209. pkill '^ss-tunnel$' &> /dev/null
  210. pkill '^ssr-redir$' &> /dev/null
  211. pkill '^ssr-tunnel$' &> /dev/null
  212. ipts_origin
  213. ipset -X chnroute &> /dev/null
  214. pbr_del
  215. dns_origin
  216. }
  217. function status() {
  218. if [ $(pgrep -c '^pdnsd$') -ge 1 ]; then
  219. echo -e "\e[37mpdnsd\e[0m\t\t[\e[1;32mrunning\e[0m]"
  220. else
  221. echo -e "\e[37mpdnsd\e[0m\t\t[\e[1;35mstopped\e[0m]"
  222. fi
  223. if [ $(pgrep -c '^chinadns$') -ge 1 ]; then
  224. echo -e "\e[37mchinadns\e[0m\t[\e[1;32mrunning\e[0m]"
  225. else
  226. echo -e "\e[37mchinadns\e[0m\t[\e[1;35mstopped\e[0m]"
  227. fi
  228. if [ "${server_use_ssr}" = 'false' ]; then
  229. if [ $(pgrep -c '^ss-redir$') -ge 1 ]; then
  230. echo -e "\e[37mss-redir\e[0m\t[\e[1;32mrunning\e[0m]"
  231. else
  232. echo -e "\e[37mss-redir\e[0m\t[\e[1;35mstopped\e[0m]"
  233. fi
  234. else
  235. if [ $(pgrep -c '^ssr-redir$') -ge 1 ]; then
  236. echo -e "\e[37mssr-redir\e[0m\t[\e[1;32mrunning\e[0m]"
  237. else
  238. echo -e "\e[37mssr-redir\e[0m\t[\e[1;35mstopped\e[0m]"
  239. fi
  240. fi
  241. if [ "${server_use_ssr}" = 'false' ]; then
  242. if [ $(pgrep -c '^ss-tunnel$') -ge 1 ]; then
  243. echo -e "\e[37mss-tunnel\e[0m\t[\e[1;32mrunning\e[0m]"
  244. else
  245. echo -e "\e[37mss-tunnel\e[0m\t[\e[1;35mstopped\e[0m]"
  246. fi
  247. else
  248. if [ $(pgrep -c '^ssr-tunnel$') -ge 1 ]; then
  249. echo -e "\e[37mssr-tunnel\e[0m\t[\e[1;32mrunning\e[0m]"
  250. else
  251. echo -e "\e[37mssr-tunnel\e[0m\t[\e[1;35mstopped\e[0m]"
  252. fi
  253. fi
  254. }
  255. function usage() {
  256. echo -e "\e[37mUsage:\e[0m \e[36m${0}\e[0m \e[1;37mCOMMAND\e[0m"
  257. echo -e "\e[1;37mCOMMAND\e[0m := {"
  258. echo -e "\t\e[33mstart\e[0m\t\tstart ss-tproxy"
  259. echo -e "\t\e[33mstop\e[0m\t\tstop ss-tproxy"
  260. echo -e "\t\e[33mrestart\e[0m\t\trestart ss-tproxy"
  261. echo -e "\t\e[33mstatus\e[0m\t\tstatus of ss-tproxy"
  262. echo -e "\t\e[33mcurrent_ip\e[0m\tget current ipaddr info"
  263. echo -e "\t\e[33mflush_dnsche\e[0m\tflush dns cache for pdnsd"
  264. echo -e "\t\e[33mupdate_chnip\e[0m\tupdate chnroute for ipset/chinadns"
  265. echo -e "}"
  266. }
  267. case $1 in
  268. star*) # start
  269. start
  270. status
  271. ;;
  272. stat*) # status
  273. status
  274. ;;
  275. sto*) # stop
  276. stop
  277. status
  278. ;;
  279. r*) # restart
  280. stop
  281. status
  282. echo
  283. start
  284. status
  285. ;;
  286. c*) # current_ip
  287. current_ip
  288. ;;
  289. f*) # flush_dnsche
  290. pdnsd_flush
  291. ;;
  292. u*) # update_chnip
  293. chnip_update
  294. ;;
  295. h*) # help
  296. usage
  297. ;;
  298. *)
  299. usage 1>&2
  300. exit 1
  301. ;;
  302. esac