source /etc/storage/script/init.sh
### Custom user script
### Called after internal VPN client connected/disconnected to remote VPN server
### $1        - action (up/down)
### $IFNAME   - tunnel interface name (e.g. ppp5 or tun0)
### $IPLOCAL  - tunnel local IP address
### $IPREMOTE - tunnel remote IP address
### $DNS1     - peer DNS1
### $DNS2     - peer DNS2
# VPN国内外自动分流功能 0关闭；1启动
vpns=`nvram get vpnc_fw_enable`

# VPN线路流向选择 0出国；1回国
vpnc_fw_rules=`nvram get vpnc_fw_rules`

#confdir=`grep "/tmp/ss/dnsmasq.d" /etc/storage/dnsmasq/dnsmasq.conf | sed 's/.*\=//g'`
#if [ -z "$confdir" ] ; then 
    confdir="/tmp/ss/dnsmasq.d"
#fi
[ ! -d "$confdir" ] && mkdir -p $confdir
restart_dhcpd
# private LAN subnet behind a remote server (example)
peer_lan="192.168.9.0"
peer_msk="255.255.255.0"

### example: add static route to private LAN subnet behind a remote server

func_ipup()
{
#  route add -net $peer_lan netmask $peer_msk gw $IPREMOTE dev $IFNAME
  if [ "$vpns" = "1" ] ; then
    [ -f /tmp/vpnc.lock ] && logger -t "【VPN 分流】" "等待45秒开始脚本"
    I=45
    while [ -f /tmp/vpnc.lock ]; do
            I=$(($I - 1))
            [ $I -lt 0 ] && break
            sleep 1
    done
    touch /tmp/vpnc.lock
    logger -t "【VPN 分流】" "下载并运行 ip-pre-up 添加规则"
    if [ ! -s "/tmp/ip-pre-up" ] ; then
        wgetcurl.sh /tmp/ip-pre-up "$hiboyfile/ip-pre-up" "$hiboyfile2/ip-pre-up"
    fi
    if [ ! -s "/tmp/ip-pre-up" ] ; then
        wgetcurl.sh /tmp/ip-pre-up "$hiboyfile/ip-pre-up" "$hiboyfile2/ip-pre-up"
    fi
    chmod 777 "/tmp/ip-pre-up"
        if [ "$vpnc_fw_rules" = "1" ] ; then
            /tmp/ip-pre-up $IPREMOTE
        else
            /tmp/ip-pre-up
        fi
    if [ ! -s "/tmp/ip-down" ] ; then
        wgetcurl.sh /tmp/ip-down "$hiboyfile/ip-down" "$hiboyfile2/ip-down"
      chmod 777 "/tmp/ip-down"
    fi
    if [ ! -s "/tmp/ip-down" ] ; then
        wgetcurl.sh /tmp/ip-down "$hiboyfile/ip-down" "$hiboyfile2/ip-down"
    fi
    rm -f /tmp/vpnc.lock
    logger -t "【VPN 分流】" "ip-pre-up 添加规则完成"
  else
    rm -f /tmp/vpnc.lock
  fi
  return 0
}

func_ipdown()
{
#  route del -net $peer_lan netmask $peer_msk gw $IPREMOTE dev $IFNAME
  if [ "$vpns" = "1" ] ; then
    [ -f /tmp/vpnc.lock ] && logger -t "【VPN 分流】" "等待45秒开始脚本"
    I=45
    while [ -f /tmp/vpnc.lock ]; do
            I=$(($I - 1))
            [ $I -lt 0 ] && break
            sleep 1
    done
    touch /tmp/vpnc.lock
    logger -t "【VPN 分流】" "下载并运行 ip-down 删除规则"
    if [ ! -s "/tmp/ip-down" ] ; then
        wgetcurl.sh /tmp/ip-down "$hiboyfile/ip-down" "$hiboyfile2/ip-down"
    fi
    if [ ! -s "/tmp/ip-down" ] ; then
        wgetcurl.sh /tmp/ip-down "$hiboyfile/ip-down" "$hiboyfile2/ip-down"
    fi
    chmod 777 "/tmp/ip-down"
    /tmp/ip-down
    #if [ -s "/tmp/ip-pre-up" ] ; then
    #  rm -f /tmp/ip-pre-up
    #  rm -f /tmp/ip-down
    #fi
    rm -f /tmp/vpnc.lock
    #logger -t "【VPN 分流】" "ip-down 删除规则完成"
  else
    rm -f /tmp/vpnc.lock
  fi
  return 0
}

logger -t "【VPN客户端脚本】" "$IFNAME $1"

case "$1" in
up)
  func_ipup
  ;;
down)
  func_ipdown
  ;;
esac
