Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@KyonLi
Created February 10, 2019 07:52
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KyonLi/88fa5ea05de7b7b2571c8f1f3fc0a5cf to your computer and use it in GitHub Desktop.
Save KyonLi/88fa5ea05de7b7b2571c8f1f3fc0a5cf to your computer and use it in GitHub Desktop.
entware v2ray
#!/bin/sh
ACTION=$1
CALLER=$2
ansi_red="\033[1;31m";
ansi_white="\033[1;37m";
ansi_green="\033[1;32m";
ansi_yellow="\033[1;33m";
ansi_blue="\033[1;34m";
ansi_bell="\007";
ansi_blink="\033[5m";
ansi_std="\033[m";
ansi_rev="\033[7m";
ansi_ul="\033[4m";
start() {
[ "$CRITICAL" != "yes" -a "$CALLER" = "cron" ] && return 7
[ "$ENABLED" != "yes" ] && return 8
echo -e -n "$ansi_white Starting $DESC... $ansi_std"
if [ -n "`pidof $PROC`" ]; then
echo -e " $ansi_yellow already running. $ansi_std"
return 0
fi
$PRECMD > /dev/null 2>&1
#$PREARGS $PROC $ARGS > /dev/null 2>&1 &
daemonize `which $PROC` $ARGS
#echo $PREARGS $PROC $ARGS
COUNTER=0
LIMIT=10
while [ -z "`pidof $PROC`" -a "$COUNTER" -le "$LIMIT" ]; do
sleep 1;
COUNTER=`expr $COUNTER + 1`
done
$POSTCMD > /dev/null 2>&1
if [ -z "`pidof $PROC`" ]; then
echo -e " $ansi_red failed. $ansi_std"
logger "Failed to start $DESC from $CALLER."
return 255
else
echo -e " $ansi_green done. $ansi_std"
logger "Started $DESC from $CALLER."
return 0
fi
}
stop() {
case "$ACTION" in
stop | restart)
echo -e -n "$ansi_white Shutting down $PROC... $ansi_std"
killall $PROC 2>/dev/null
COUNTER=0
LIMIT=10
while [ -n "`pidof $PROC`" -a "$COUNTER" -le "$LIMIT" ]; do
sleep 1;
COUNTER=`expr $COUNTER + 1`
done
;;
kill)
echo -e -n "$ansi_white Killing $PROC... $ansi_std"
killall -9 $PROC 2>/dev/null
;;
esac
if [ -n "`pidof $PROC`" ]; then
echo -e " $ansi_red failed. $ansi_std"
return 255
else
echo -e " $ansi_green done. $ansi_std"
return 0
fi
}
check() {
echo -e -n "$ansi_white Checking $DESC... "
if [ -n "`pidof $PROC`" ]; then
echo -e " $ansi_green alive. $ansi_std";
return 0
else
echo -e " $ansi_red dead. $ansi_std";
return 1
fi
}
reconfigure() {
SIGNAL=SIGHUP
echo -e "$ansi_white Sending $SIGNAL to $PROC... $ansi_std"
killall -$SIGNAL $PROC 2>/dev/null
}
for PROC in $PROCS; do
case $ACTION in
start)
start
;;
stop | kill )
check && stop
;;
restart)
check > /dev/null && stop
start
;;
check)
check
;;
reconfigure)
reconfigure
;;
*)
echo -e "$ansi_white Usage: $0 (start|stop|restart|check|kill|reconfigure)$ansi_std"
exit 1
;;
esac
done
#logger "Leaving ${0##*/}."
#!/bin/sh
ENABLED=yes
PROCS=v2ray
ARGS="-config /opt/etc/v2ray/config.pb -format=pb"
PREARGS=""
DESC=$PROCS
PATH=/opt/sbin:/opt/bin:/opt/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SRC_BYPASS_IP_FILE=/opt/etc/v2ray/src_bypass_ip.txt
SRC_BYPASS_MAC_FILE=/opt/etc/v2ray/src_bypass_mac.txt
DST_FORWARD_FILE=/opt/etc/v2ray/dst_forward.txt
[ -z "$(which $PROCS)" ] && exit 0
#
# Function that generate rules from config files
#
bypass_rules(){
if [ -f $SRC_BYPASS_IP_FILE ]
then
for ip in `cat $SRC_BYPASS_IP_FILE`
do
iptables -t nat -A V2RAY -s $ip -j RETURN
done
fi
if [ -f $SRC_BYPASS_MAC_FILE ]
then
for mac in `cat $SRC_BYPASS_MAC_FILE`
do
iptables -t nat -A V2RAY -m mac --mac-source $mac -j RETURN
done
fi
}
forward_rules(){
if [ -f $DST_FORWARD_FILE ]
then
for ip in `cat $DST_FORWARD_FILE`
do
ipset add gfwlist $ip
done
fi
}
#
# Function that apply the iptables rules
#
apply_nat(){
ipset create gfwlist hash:net
ipset create whitelist hash:ip
forward_rules
iptables -t nat -N V2RAY
iptables -t nat -A V2RAY -d 0.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 10.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 100.64.0.0/10 -j RETURN
iptables -t nat -A V2RAY -d 127.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 169.254.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 172.16.0.0/12 -j RETURN
iptables -t nat -A V2RAY -d 192.0.0.0/24 -j RETURN
iptables -t nat -A V2RAY -d 192.0.2.0/24 -j RETURN
iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 198.18.0.0/15 -j RETURN
iptables -t nat -A V2RAY -d 198.51.100.0/24 -j RETURN
iptables -t nat -A V2RAY -d 203.0.113.0/24 -j RETURN
bypass_rules
iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12345
iptables -t nat -A PREROUTING -p tcp -m set --match-set gfwlist dst -j V2RAY
iptables -t nat -A OUTPUT -p tcp -m set --match-set gfwlist dst -j V2RAY
}
#
# Function that flush the iptables rules
#
flush_nat(){
iptables -t nat -D OUTPUT -p tcp -m set --match-set gfwlist dst -j V2RAY
iptables -t nat -D PREROUTING -p tcp -m set --match-set gfwlist dst -j V2RAY
iptables -t nat -F V2RAY
iptables -t nat -X V2RAY >/dev/null 2>&1
}
case "$1" in
nat-start)
apply_nat
;;
nat-stop)
flush_nat
;;
*)
. /opt/etc/init.d/rc.func
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment