Procházet zdrojové kódy

update on 2018-05-19 13:08:22 multi intranet support

zfl9 před 8 roky
rodič
revize
111410953d
1 změnil soubory, kde provedl 42 přidání a 41 odebrání
  1. 42 41
      ss-tproxy

+ 42 - 41
ss-tproxy

@@ -3,7 +3,7 @@
 main_cfg='/etc/tproxy/ss-tproxy.conf'
 
 if [ ! -e "${main_cfg}" ]; then
-    echo -e "\e[37m${main_cfg}\e[0m  [\e[1;35mnot_exist\e[0m]"
+    echo -e "\e[37m${main_cfg}\e[0m  [\e[1;35mnot_exist\e[0m]" 1>&2
     exit 1
 else
     source "${main_cfg}"
@@ -89,6 +89,19 @@ function ipts_update() {
     iptables -t nat -F SS-TCP &> /dev/null
     iptables -t nat -X SS-TCP &> /dev/null
 
+    iptables -t nat -N SS-TCP
+    iptables -t nat -A SS-TCP -d 0/8 -j RETURN
+    iptables -t nat -A SS-TCP -d 10/8 -j RETURN
+    iptables -t nat -A SS-TCP -d 127/8 -j RETURN
+    iptables -t nat -A SS-TCP -d 169.254/16 -j RETURN
+    iptables -t nat -A SS-TCP -d 172.16/12 -j RETURN
+    iptables -t nat -A SS-TCP -d 192.168/16 -j RETURN
+    iptables -t nat -A SS-TCP -d 224/4 -j RETURN
+    iptables -t nat -A SS-TCP -d 240/4 -j RETURN
+    iptables -t nat -A SS-TCP -d ${server_addr} -j RETURN
+    iptables -t nat -A SS-TCP -m set --match-set chnroute dst -j RETURN
+    iptables -t nat -A SS-TCP -p tcp -j REDIRECT --to-ports ${redir_port}
+
     iptables -t mangle -N SS-UDP
     iptables -t mangle -A SS-UDP -d 0/8 -j RETURN
     iptables -t mangle -A SS-UDP -d 10/8 -j RETURN
@@ -102,27 +115,17 @@ function ipts_update() {
     iptables -t mangle -A SS-UDP -m set --match-set chnroute dst -j RETURN
     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}
 
-    iptables -t mangle -A PREROUTING -p udp -s ${iptables_intranet} ! -d ${iptables_intranet} --dport 53 -j ACCEPT
-    iptables -t mangle -A PREROUTING -p udp -s ${iptables_intranet} -j SS-UDP
+    iptables -t nat -A OUTPUT -p tcp -j SS-TCP
 
-    iptables -t nat -N SS-TCP
-    iptables -t nat -A SS-TCP -d 0/8 -j RETURN
-    iptables -t nat -A SS-TCP -d 10/8 -j RETURN
-    iptables -t nat -A SS-TCP -d 127/8 -j RETURN
-    iptables -t nat -A SS-TCP -d 169.254/16 -j RETURN
-    iptables -t nat -A SS-TCP -d 172.16/12 -j RETURN
-    iptables -t nat -A SS-TCP -d 192.168/16 -j RETURN
-    iptables -t nat -A SS-TCP -d 224/4 -j RETURN
-    iptables -t nat -A SS-TCP -d 240/4 -j RETURN
-    iptables -t nat -A SS-TCP -d ${server_addr} -j RETURN
-    iptables -t nat -A SS-TCP -m set --match-set chnroute dst -j RETURN
-    iptables -t nat -A SS-TCP -p tcp -j REDIRECT --to-ports ${redir_port}
+    for intranet in "$iptables_intranet"; do
+        iptables -t mangle -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j ACCEPT
+        iptables -t nat -A PREROUTING -p udp -s $intranet ! -d $intranet --dport 53 -j REDIRECT --to-ports 53
 
-    iptables -t nat -A OUTPUT -p tcp -j SS-TCP
-    iptables -t nat -A PREROUTING -p udp -s ${iptables_intranet} ! -d ${iptables_intranet} --dport 53 -j REDIRECT --to-ports 53
-    iptables -t nat -A PREROUTING -p tcp -s ${iptables_intranet} -j SS-TCP
+        iptables -t nat -A PREROUTING -p tcp -s $intranet -j SS-TCP
+        iptables -t mangle -A PREROUTING -p udp -s $intranet -j SS-UDP
 
-    iptables -t nat -A POSTROUTING -s ${iptables_intranet} ! -d ${iptables_intranet} -j MASQUERADE
+        iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
+    done
 }
 function ipts_origin() {
     iptables -t mangle -F PREROUTING
@@ -134,8 +137,10 @@ function ipts_origin() {
     iptables -t nat -F SS-TCP &> /dev/null
     iptables -t nat -X SS-TCP &> /dev/null
 
-    iptables -t nat -A PREROUTING -p udp -s ${iptables_intranet} -d ${iptables_intranet} --dport 53 -j DNAT --to-destination ${dns_original_1}
-    iptables -t nat -A POSTROUTING -s ${iptables_intranet} ! -d ${iptables_intranet} -j MASQUERADE
+    for intranet in "$iptables_intranet"; do
+        iptables -t nat -A PREROUTING -p udp -s $intranet -d $intranet --dport 53 -j DNAT --to-destination ${dns_original[0]}
+        iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
+    done
 }
 
 function pbr_add() {
@@ -154,24 +159,21 @@ function ipfwd_on() {
 }
 
 function dns_update() {
-cat << EOF > /etc/resolv.conf
-# Generated by ss-tproxy at $(date '+%F %T')
-nameserver 127.0.0.1
-EOF
+    echo "# Generated by ss-tproxy at $(date '+%F %T')" > /etc/resolv.conf
+    echo "nameserver 127.0.0.1" >> /etc/resolv.conf
 }
 function dns_origin() {
-cat << EOF > /etc/resolv.conf
-# Generated by ss-tproxy at $(date '+%F %T')
-nameserver ${dns_original_1}
-nameserver ${dns_original_2}
-EOF
+    echo "# Generated by ss-tproxy at $(date '+%F %T')" > /etc/resolv.conf
+    for dns in "$dns_original"; do
+        echo "nameserver $dns" >> /etc/resolv.conf
+    done
 }
 
 function current_ip() {
     ip_info=$(curl -4skL ip.chinaz.com/getip.aspx | sed -r "s/^\{ip:'(.*)',address:'(.*)'\}$/\1|\2/g")
     my_ip=$(echo ${ip_info} | awk -F'|' '{print $1}')
     my_loc=$(echo ${ip_info} | awk -F'|' '{print $2}')
-    echo -e "\e[37mIP:\e[0m ${my_ip}\t\e[37m位置:\e[0m ${my_loc}"
+    echo -e "\e[37mIP:\e[0m ${my_ip}\t\e[37mLocation:\e[0m ${my_loc}"
 }
 
 function start() {
@@ -234,12 +236,12 @@ function start() {
 }
 
 function stop() {
-    pgrep '^ss-redir$' | xargs kill -9 &> /dev/null
-    pgrep '^ss-tunnel$' | xargs kill -9 &> /dev/null
-    pgrep '^ssr-redir$' | xargs kill -9 &> /dev/null
-    pgrep '^ssr-tunnel$' | xargs kill -9 &> /dev/null
-    pgrep '^chinadns$' | xargs kill -9 &> /dev/null
-    pgrep '^pdnsd$' | xargs kill -9 &> /dev/null
+    pkill '^pdnsd$' &> /dev/null
+    pkill '^chinadns$' &> /dev/null
+    pkill '^ss-redir$' &> /dev/null
+    pkill '^ss-tunnel$' &> /dev/null
+    pkill '^ssr-redir$' &> /dev/null
+    pkill '^ssr-tunnel$' &> /dev/null
 
     ipts_origin
     ipset -X chnroute &> /dev/null
@@ -291,7 +293,6 @@ function status() {
 }
 
 function usage() {
-{
     echo -e "\e[37mUsage:\e[0m \e[36m${0}\e[0m \e[1;37mCOMMAND\e[0m"
     echo -e "\e[1;37mCOMMAND\e[0m := {"
     echo -e "\t\e[33mstart\e[0m\t\tstart ss-tproxy"
@@ -302,10 +303,9 @@ function usage() {
     echo -e "\t\e[33mflush_dnsche\e[0m\tflush dns cache for pdnsd"
     echo -e "\t\e[33mupdate_chnip\e[0m\tupdate chnroute for ipset/chinadns"
     echo -e "}"
-} | more
 }
 
-case ${1} in
+case $1 in
 star*)  # start
     start
     status
@@ -321,6 +321,7 @@ r*)     # restart
     stop
     status
     echo
+    sleep 1
     start
     status
     ;;
@@ -339,7 +340,7 @@ h*)     # help
     usage
     ;;
 *)
-    usage
+    usage 1>&2
     exit 1
     ;;
 esac