#!/usr/bin/perl
#
#      Kolejkowanie ver 0.000000000001 beta
#create by BiExi
#
###########
#   ZMIANY DHAPOLLO
###########
#
# $debugowanie = 0;
  $cid = 5;
  $handl = 320;
  $zrodlo = 600;
  $lp = 1;
  $lp2 = 1;

open (PLIK_LIMIT, "/etc/serwer/limity/limit.list") or die "open $alert_file: $!\n";
{
  sleep 1;
    if (seek(PLIK_LIMIT,0,1))
      {

          print "--------------------------------------------- \n";
          print "                 KOLEJKOWANIE ETH1 \n";
          print "--------------------------------------------- \n";
    # - -- ---[ ruch przychodzacy ]--- -- -
  system "tc qdisc del dev eth1 root handle 1:0 htb";
  system "tc qdisc add dev eth1 root handle 1:0 htb";
# zmien wartosc rate i celi na swoje predkosci kart  sieciowych LAN ETH1 
  system "tc class add dev eth1 parent 1:0 classid 1:1 htb rate 10mbit ceil 11mbit";
# Zmieniamy wartosc rate i celi na wartosci od naszego operatora (pobieranie, nieco mmniej niz ba umowie)
  system "tc class add dev eth1 parent 1:1 classid 1:2 htb rate 460kbit ceil 460kbit";
# zmieniamy wartosc rate i celi na predkosci nieco nizsze niz karty sieciowe LAN ETH1
  system "tc class add dev eth1 parent 1:1 classid 1:3 htb rate 9000kbit ceil 9000kbit";
# zmieniamy adres IP na adres przypisany do ETH1
  system "tc filter add dev eth1 protocol ip preference 1 parent 1:0 u32 match ip src 192.168.1.1 flowid 1:3";
  system "tc qdisc add dev eth1 parent 1:3 handle 299:0 sfq perturb 10";

    # - -- ---[ ruch wychodzacy ]--- -- -
  system "tc qdisc del dev eth0 root handle 1:0 htb";
  system "tc qdisc add dev eth0 root handle 1:0 htb";
# zmieniamy wartosc rate i celi dla karty ETH0 (nieco mniej niz max)
  system "tc class add dev eth0 parent 1:0 classid 1:1 htb rate 9000kbit ceil 9000kbit";
# zmieniamy wartosc rate i celi na wartosci z umowy od naszegi ISP (wysylanie, nieco miej niz max)
  system "tc class add dev eth0 parent 1:1 classid 1:2 htb rate 2300kbit ceil 2300kbit";
  system "tc qdisc add dev eth0 parent 1:2 handle 2:0 sfq perturb 10";
  system "tc filter add dev eth0 protocol ip preference 1 parent 1:0 handle 2000 fw flowid 1:2";

  system "iptables -t mangle -N LIMITY-ETH0";
  system "iptables -t mangle -D POSTROUTING -o eth0 -j  LIMITY-ETH0";
  system "iptables -t mangle -I POSTROUTING -o eth0 -j  LIMITY-ETH0";


    while (<PLIK_LIMIT>)
        {
 chop;
  next if (/^\s*$/); # wyjazd z pustymi liniami
  next if (/^#/);    # wyjazd z #
 if (/(\d+\.\d+\.\d+\.\d+) \; (\d+) \; (\d+) \; (\d+) \; (\d+) \; (\S+)/)
 {
  print "$lp Pobieranie dla -> Nazwa $6 IP:$1 \n\t\t\tmin: $2kbit max: $3kbit\n";
  system "tc class add dev eth1 parent 1:2 classid 1:$cid htb rate $2kbit ceil $3kbit burst 500k";
  system "tc filter add dev eth1 protocol ip preference 2 parent 1:0 u32 match ip dst $1 flowid 1:$cid";
  system "tc qdisc add dev eth1 parent 1:$cid handle $handl:0 sfq perturb 10";

  print "$lp2 Wysylanie dla -> Nazwa: $6 IP:$1 \n\t\t\tmin: $4kbit max: $5kbit\n";
  system "tc class add dev eth0 parent 1:2 classid 1:$cid htb rate $4kbit ceil $5kbit burst 300k";
  system "tc qdisc add dev eth0 parent 1:$cid handle $handl:0 sfq perturb 10";
  system "tc filter add dev eth0 protocol ip preference 2 parent 1:0 handle $zrodlo fw flowid 1:$cid";
  system "iptables -t mangle -D  LIMITY-ETH0 -p tcp -s $1 -j MARK --set-mark $zrodlo >>/dev/null";
  system "iptables -t mangle -A  LIMITY-ETH0 -p tcp -s $1 -j MARK --set-mark $zrodlo";
  $cid = $cid + 1;
  $handl = $handl + 1;
 $zrodlo = $zrodlo + 1;
$lp = $lp + 1;
$lp2 = $lp2 + 1;
}

} # end while
     } # end if seek
 } # end for

close PLIK_LIMIT;


system "iptables -t mangle -A  LIMITY-ETH0 -m mark --mark 0 -j MARK --set-mark 2000";
