From e3f91a7a6a3b535e7340c39b74368207dc6f6ce1 Mon Sep 17 00:00:00 2001 From: Yonsm Date: Sun, 28 Jun 2020 18:00:59 +0800 Subject: [PATCH] Refine wing script, merge port to host and the port can be blank by default --- trunk/user/scripts/mtd_storage.sh | 4 +- trunk/user/wing/wing | 68 +++++++++++++++++-------------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/trunk/user/scripts/mtd_storage.sh b/trunk/user/scripts/mtd_storage.sh index 9054793ed53..fe44bf782a3 100755 --- a/trunk/user/scripts/mtd_storage.sh +++ b/trunk/user/scripts/mtd_storage.sh @@ -274,8 +274,8 @@ sync && echo 3 > /proc/sys/vm/drop_caches # Mount SATA disk #mdev -s -#wing 443 -#wing 192.168.1.9 1080 +#wing +#wing 192.168.1.9:1080 #ipset add gfwlist 8.8.4.4 diff --git a/trunk/user/wing/wing b/trunk/user/wing/wing index 86d6f287ca4..792043bc196 100755 --- a/trunk/user/wing/wing +++ b/trunk/user/wing/wing @@ -9,9 +9,9 @@ usage() { echo "Usage: $0 <[start]|restart|stop|pause|resume|redir|status|update>" echo - echo "Local Transparent mode (trojan only): $0 " - echo "Local Socks mode (trojan+ipt2socks): $0 client" - echo "Remote Socks mode (ipt2socks only): $0 " + echo "Local Transparent mode (trojan only): $0 [:PORT] " + echo "Local Socks mode (trojan+ipt2socks): $0 [:PORT] client" + echo "Remote Socks mode (ipt2socks only): $0 [:PORT]" echo exit 0 } @@ -41,30 +41,36 @@ redir() start() { # prepage - [ -z $3 ] && usage + [ -z $2 ] && usage check + # resolve + if echo $2 | grep : > /dev/null; then + HOST=`echo $2 | cut -d : -f 1` + PORT=`echo $2 | cut -d : -f 2` + else + HOST=$2 + PORT= + fi + if ! expr match $HOST [0-9.]*$ > /dev/null; then + PING=`ping $HOST -c 1 -w 1` 2> /dev/null + [ ! -z "$PING" ] && HOST=`echo $PING | sed "1{s/[^(]*(//;s/).*//;q}"` + fi + # trojan - if [ -z $4 ]; then - RUN_MODE="Remote Socks" - SOCKS_HOST=$2 - SOCKS_PORT=$3 + if [ -z $3 ]; then + [ -z $PORT ] && PORT=1080 + RUN_TYPE=socks else - TROJAN_HOST=$2 - TROJAN_PORT=$3 - TROJAN_PASS=$4 - if [ -z $5 ]; then RUN_TYPE=nat; else RUN_TYPE=$5; fi - if [ "$RUN_TYPE" = "nat" ]; then - RUN_MODE="Local Transparent" + [ -z $PORT ] && PORT=443 + if [ -z $4 ]; then + RUN_TYPE=nat LOCAL_PORT=1088 - SOCKS_HOST= else - RUN_MODE="Local Socks" + RUN_TYPE=client LOCAL_PORT=1080 - SOCKS_HOST=127.0.0.1 - SOCKS_PORT=$LOCAL_PORT fi - echo "{\"run_type\":\"$RUN_TYPE\",\"local_addr\":\"0.0.0.0\",\"local_port\":$LOCAL_PORT,\"remote_addr\":\"$TROJAN_HOST\",\"remote_port\":$TROJAN_PORT,\"password\":[\"$TROJAN_PASS\"],\"log_level\":$1,\"ssl\":{\"verify\":false}}" > /tmp/trojan.conf + echo "{\"run_type\":\"$RUN_TYPE\",\"local_addr\":\"0.0.0.0\",\"local_port\":$LOCAL_PORT,\"remote_addr\":\"$HOST\",\"remote_port\":$PORT,\"password\":[\"$3\"],\"log_level\":$1,\"ssl\":{\"verify\":false}}" > /tmp/trojan.conf if [ $1 -ge 5 ]; then trojan -c /tmp/trojan.conf & else @@ -73,8 +79,12 @@ start() fi # ipt2socks - if [ ! -z $SOCKS_HOST ]; then - IPT2SOCKS_CMD="ipt2socks -s $SOCKS_HOST -p $SOCKS_PORT -b 0.0.0.0 -l 1088 -T -4 -R" + if [ $RUN_TYPE != nat ]; then + if [ $RUN_TYPE = client ]; then + HOST=127.0.0.1 + PORT=$LOCAL_PORT + fi + IPT2SOCKS_CMD="ipt2socks -s $HOST -p $PORT -b 0.0.0.0 -l 1088 -T -4 -R -j `cat /proc/cpuinfo|grep processor|wc -l`" if [ $1 -ge 5 ]; then $IPT2SOCKS_CMD & elif [ $1 -ge 2 ]; then @@ -91,13 +101,12 @@ start() # dnsmasq GFWLIST_LINE="gfwlist=$GFWLIST_CONF" - grep "$GFWLIST_LINE" $DNSMASQ_CONF > /dev/null - if [ $? != 0 ]; then + if ! grep "$GFWLIST_LINE" $DNSMASQ_CONF > /dev/null; then killall dnsmasq echo "$GFWLIST_LINE" >> $DNSMASQ_CONF dnsmasq fi - echo "Wing is running in $RUN_MODE mode..." + echo "Wing is running in $RUN_TYPE mode..." } stop() @@ -105,15 +114,14 @@ stop() redir -D ipset destroy gfwlist - [ -f /tmp/dns-forwarder.log ] && rm /tmp/dns-forwarder.log && killall dns-forwarder - [ -f /tmp/ipt2socks.log ] && rm /tmp/ipt2socks.log && killall ipt2socks - [ -f /tmp/trojan.log ] && rm /tmp/trojan.* && killall trojan + [ -f /tmp/ipt2socks.log ] && rm /tmp/ipt2socks.log ; killall ipt2socks 2> /dev/null + [ -f /tmp/trojan.conf ] && rm /tmp/trojan.* && killall trojan } status() { - if [ ${1::1} = t ]; then [ -f /tmp/trojan.log ] && tail -f /tmp/trojan.log; return; fi - if [ ${1::4} = ipt2 ]; then [ -f /tmp/ipt2socks.log ] && tail -f /tmp/ipt2socks.log; return; fi + [ ${1::1} = t ] && ([ -f /tmp/trojan.log ] && tail -f /tmp/trojan.log; return) + [ ${1::4} = ipt2 ] && ([ -f /tmp/ipt2socks.log ] && tail -f /tmp/ipt2socks.log; return) ([ -z $1 ] || [ ${1::3} = ipt ]) && iptables -t nat -L PREROUTING && iptables -t nat -L OUTPUT ([ -z $1 ] || [ ${1::3} = ips ]) && echo && ipset -L gfwlist | more @@ -131,7 +139,7 @@ case "$1" in stop) stop check - sed -i /conf-file=${GFWLIST_CONF//\//\\/}/d $DNSMASQ_CONF + sed -i /gfwlist=${GFWLIST_CONF//\//\\/}/d $DNSMASQ_CONF killall dnsmasq dnsmasq ;;