Skip to content

Commit

Permalink
Refine wing script, merge port to host and the port can be blank by d…
Browse files Browse the repository at this point in the history
…efault
  • Loading branch information
Yonsm committed Jun 28, 2020
1 parent 7d2408f commit e3f91a7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
4 changes: 2 additions & 2 deletions trunk/user/scripts/mtd_storage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ sync && echo 3 > /proc/sys/vm/drop_caches
# Mount SATA disk
#mdev -s
#wing <HOST> 443 <PASS>
#wing 192.168.1.9 1080
#wing <HOST:443> <PASS>
#wing 192.168.1.9:1080
#ipset add gfwlist 8.8.4.4
Expand Down
68 changes: 38 additions & 30 deletions trunk/user/wing/wing
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ usage()
{
echo "Usage: $0 <[start]|restart|stop|pause|resume|redir|status|update>"
echo
echo "Local Transparent mode (trojan only): $0 <TROJAN_HOST> <TROJAN_PORT> <PASSWORD>"
echo "Local Socks mode (trojan+ipt2socks): $0 <TROJAN_HOST> <TROJAN_PORT> <PASSWORD> client"
echo "Remote Socks mode (ipt2socks only): $0 <SOCKS_HOST> <SOCKS_PORT>"
echo "Local Transparent mode (trojan only): $0 <HOST>[:PORT] <PASS>"
echo "Local Socks mode (trojan+ipt2socks): $0 <HOST>[:PORT] <PASS> client"
echo "Remote Socks mode (ipt2socks only): $0 <HOST>[:PORT]"
echo
exit 0
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -91,29 +101,27 @@ 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()
{
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
Expand All @@ -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
;;
Expand Down

0 comments on commit e3f91a7

Please sign in to comment.