#!/bin/sh

logTag="v2ray_iptable"

logger -t ${logTag} "check iptables"
server=`ps | grep -e "v2ray[[:space:]]\|v2ray$" | grep -v grep`
if [ "${server}" ]; then
    logger -t ${logTag} "v2ray is running, add iptables..."
    
    # set iptables 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 127.0.0.0/8 -j RETURN
    iptables -t nat -A V2RAY -d 192.168.10.0/24 -j RETURN
    # From lans redirect to Dokodemo-door's local port
    iptables -t nat -A V2RAY -s 192.168.10.0/24 -p tcp -j REDIRECT --to-ports 10808
    iptables -t nat -A PREROUTING -p tcp -j V2RAY
    iptables -t nat -A OUTPUT -p tcp -j V2RAY
    
    logger -t ${logTag} "v2ray is running, add iptables was finished."
fi
