#!/bin/sh

LAN_IF="INTERNAL_INTERFACE"
WAN_IF="EXTERNAL_INTERFACE"

LOGIN=$1
IP=$2
CASH=$3
ID=$4
SPEED=`/etc/stargazer/GetSpeed $LOGIN`
UPSPEED=`/etc/stargazer/GetUpSpeed $LOGIN`
MAC=`/etc/stargazer/GetMac $LOGIN`
SCOUNT="kbit"


ipt="/usr/sbin/iptables"
ips="/usr/sbin/ipset"
tc="/usr/sbin/tc"
arpcmd="/usr/sbin/arp"

cur_date=`date \+\%Y.\%m.\%d`
cur_time=`date \+\%H:\%M:\%S`

${arpcmd} -s ${IP} ${MAC}


DMARK=`expr $ID + 2`
UMARK=`expr $ID + 5001`

${ipt} -t mangle -A FORWARD -d ${IP} -j MARK --set-mark ${DMARK}
${ipt} -t mangle -A FORWARD -s ${IP} -j MARK --set-mark ${UMARK}


${tc} class add dev ${LAN_IF} parent 1:1 classid 1:${DMARK} htb rate ${SPEED}${SCOUNT}
${tc} filter add dev ${LAN_IF} parent 1: protocol ip prio 1 handle ${DMARK} fw classid 1:${DMARK}

${tc} class add dev ${WAN_IF} parent 1:1 classid 1:${UMARK} htb rate ${UPSPEED}${SCOUNT}
${tc} filter add dev ${WAN_IF} parent 1: protocol ip prio 1 handle ${UMARK} fw classid 1:${UMARK}

${ips} -D DISCON ${IP}
${ips} -A FORW ${IP}


# ADD TO LOG
echo "$cur_date $cur_time CONNECT: ID-$ID;LOGIN-$LOGIN;IP-$IP;CASH-$CASH;SPEED-$SPEED;UPSPEED-$UPSPEED,MAC-$MAC" >> /var/stargazer/allconnect.log