Ignore:
Timestamp:
Aug 2, 2009, 4:56:48 PM (15 years ago)
Author:
george
Message:
  • Upraveno: Výrazné zrychlení načítání seznamů pomocí print terse.
  • Upraveno: Pro použití nové třídy Routerboard přizpůsoben i skript pro generování řízení toku.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/system/generators/traffic_shaping_routerboard.php

    r214 r241  
    66$SessionDisable = true;
    77include_once('../../global.php');
     8include('../routerboard.php');
     9$PathFirewall = array('ip', 'firewall', 'mangle');
     10$PathQueue = array('queue', 'tree');
     11
     12$Routerboard = new Routerboard();
     13$Routerboard->UserName = $Config['MainRouter']['UserName'];
     14$Routerboard->Timeout = $Config['MainRouter']['ConnectTimeout'];
     15$Routerboard->HostName = $Config['MainRouter']['HostName'];
     16
    817$Finance = &$System->Modules['Finance'];
    918$Finance->LoadMonthParameters(0);
     
    3039$FreeInetSpeed = 64 * 1024;
    3140
    32 $Commands = array();
    33 $Commands[] = '/ip firewall mangle { remove [find] }';
    34 $Commands[] = '/queue tree { remove [find] }';
     41$ItemsMangle = array();
     42$ItemsQueue = array();
    3543
    3644// Root of tree and main limit
    3745$PacketMark = 1;
    38 $Commands[] = ' /queue tree add name=main-out limit-at='.$UsersMaxSpeedIn.' max-limit='.$UsersMaxSpeedIn.' parent=global-out';
    39 $Commands[] = ' /ip firewall mangle add chain=forward out-interface='.$InetInterface.' action=jump jump-target=inet-out';
    40 $Commands[] = ' /queue tree add name=main-in limit-at='.$UsersMaxSpeedOut.' max-limit='.$UsersMaxSpeedOut.' parent=global-out';
    41 $Commands[] = ' /ip firewall mangle add chain=forward in-interface='.$InetInterface.' action=jump jump-target=inet-in';
     46$ItemsQueue[] = array('name' => 'main-out', 'limit-at' => $UsersMaxSpeedIn, 'max-limit' => $UsersMaxSpeedIn, 'parent' => 'global-out', 'queue' => 'wireless-default');
     47$ItemsFirewall[] = array('chain' => 'forward', 'out-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-out');
     48$ItemsQueue[] = array('name' => 'main-in', 'limit-at' => $UsersMaxSpeedOut, 'max-limit' => $UsersMaxSpeedOut, 'parent' => 'global-out', 'queue' => 'wireless-default');
     49$ItemsFirewall[] = array('chain' => 'forward', 'in-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-in');
    4250
    4351// Divide rules by subnet number
     
    4755  $SubnetParts = explode('.', $Subnet['AddressRange']);
    4856  $SubnetNumber = $SubnetParts[2];
    49   $Commands[] = ' /ip firewall mangle add chain=inet-out src-address='.$Subnet['AddressRange'].'/'.$Subnet['Mask'].' out-interface='.$InetInterface.' action=jump jump-target=inet-out-'.$SubnetNumber;
    50   $Commands[] = ' /ip firewall mangle add chain=inet-in dst-address='.$Subnet['AddressRange'].'/'.$Subnet['Mask'].' in-interface='.$InetInterface.' action=jump jump-target=inet-in-'.$SubnetNumber;
     57  $ItemsFirewall[] = array('chain' => 'inet-out', 'src-address' => $Subnet['AddressRange'].'/'.$Subnet['Mask'], 'out-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-out-'.$SubnetNumber);
     58  $ItemsFirewall[] = array('chain' => 'inet-in', 'dst-address' => $Subnet['AddressRange'].'/'.$Subnet['Mask'], 'in-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-in-'.$SubnetNumber);
    5159}
    5260
    5361// Slow free internet
    54 $Commands[] = ' /ip firewall mangle add chain=inet-out out-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' comment="Free inet"';
    55 $Commands[] = ' /queue tree add name=free-out limit-at='.$FreeInetSpeed.' max-limit='.$FreeInetSpeed.' parent=main-out packet-mark='.$PacketMark.' queue=wireless-default';
     62$ItemsFirewall[] = array('chain' => 'inet-out', 'out-interface' => $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'comment' => 'Free_inet', 'passthrough' => 'no');
     63$ItemsQueue[] = array('name' => 'free-out', 'limit-at' => $FreeInetSpeed, 'max-limit' => $FreeInetSpeed, 'parent' => 'main-out', 'packet-mark' => $PacketMark, 'queue' => 'wireless-default');
    5664$PacketMark++;
    57 $Commands[] = ' /ip firewall mangle add chain=inet-in in-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' comment="Free inet"';
    58 $Commands[] = ' /queue tree add name=free-in limit-at='.$FreeInetSpeed.' max-limit='.$FreeInetSpeed.' parent=main-in packet-mark='.$PacketMark.' queue=wireless-default';
     65$ItemsFirewall[] = array('chain' => 'inet-in', 'in-interface' => $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'comment' => 'Free_inet', 'passthrough' => 'no');
     66$ItemsQueue[] = array('name' => 'free-in', 'limit-at' => $FreeInetSpeed, 'max-limit' => $FreeInetSpeed, 'parent' => 'main-in', 'packet-mark' =>$PacketMark, 'queue' => 'wireless-default');
    5967$PacketMark++;
    6068
    6169// Process users
    62 $Index = 0;
    6370$DbResult = $Database->query('SELECT Member.*, Subject.Name FROM Member LEFT JOIN Subject ON Subject.Id = Member.Subject');
    6471while($Member = $DbResult->fetch_assoc())
    6572{
    66   $Member['Name'] = RouterOSIdent($Member['Name']);
     73  $Member['Name'] = RouterOSIdent($Member['Name'].'-'.$Member['Id'] );
    6774
    6875  echo('Uživatel '.$Member['Name'].': ');
     
    7279  $UserMaxSpeedIn = round($Tariff['SpeedMax'] / $InDivider);
    7380  $UserMaxSpeedOut = round($Tariff['SpeedMax'] / $OutDivider);
    74   $Commands[] = ' /queue tree add name='.$Member['Name'].'-out limit-at='.$SpeedIn.' max-limit='.$UserMaxSpeedIn.' parent=main-out queue=wireless-default';
    75   $Commands[] = ' /queue tree add name='.$Member['Name'].'-in limit-at='.$SpeedOut.' max-limit='.$UserMaxSpeedOut.' parent=main-in queue=wireless-default';
     81  $ItemsQueue[] = array('name' => $Member['Name'].'-out', 'limit-at' => $SpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => 'main-out', 'queue' => 'wireless-default');
     82  $ItemsQueue[] = array('name' => $Member['Name'].'-in', 'limit-at' => $SpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => 'main-in', 'queue' => 'wireless-default');
    7683
    7784  $DbResult2 = $Database->select('NetworkDevice', 'COUNT(*)', 'Used = 1 AND Member='.$Member['Id']);
     
    100107      $IPParts = explode('.', $Interface['LocalIP']);
    101108      $Subnet = $IPParts[2];
    102       $Commands[] = ' /ip firewall mangle add chain=inet-out-'.$Subnet.' src-address='.$Interface['LocalIP'].' out-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' passthrough=no comment="'.$Name.'"';
    103       $Commands[] = ' /queue tree add name='.$Name.'-out limit-at='.$HostSpeedIn.' max-limit='.$UserMaxSpeedIn.' parent='.$Member['Name'].'-out packet-mark='.$PacketMark.' queue=wireless-default';
     109      $ItemsFirewall[] = array('chain' => 'inet-out-'.$Subnet, 'src-address' => $Interface['LocalIP'], 'out-interface' => $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'passthrough' => 'no', 'comment' => $Name);
     110      $ItemsQueue[] = array('name' => $Name.'-out', 'limit-at' => $HostSpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => $Member['Name'].'-out', 'packet-mark' => $PacketMark, 'queue' => 'wireless-default');
    104111      $PacketMark++;
    105       $Commands[] = ' /ip firewall mangle add chain=inet-in-'.$Subnet.' dst-address='.$Interface['LocalIP'].' in-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' passthrough=no comment="'.$Name.'"';
    106       $Commands[] = ' /queue tree add name='.$Name.'-in limit-at='.$HostSpeedOut.' max-limit='.$UserMaxSpeedOut.' parent='.$Member['Name'].'-in packet-mark='.$PacketMark.' queue=wireless-default';
     112      $ItemsFirewall[] = array('chain' => 'inet-in-'.$Subnet, 'dst-address' => $Interface['LocalIP'], 'in-interface' => $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'passthrough' => 'no', 'comment' => $Name);
     113      $ItemsQueue[] = array('name' => $Name.'-in', 'limit-at' => $HostSpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => $Member['Name'].'-in', 'packet-mark' => $PacketMark, 'queue' => 'wireless-default');
    107114      $PacketMark++;
    108 
    109       // Posílej po menších částech
    110       if($Index > 50)
    111       {
    112         //print_r($Commands);
    113         $Commands = addslashes(implode(';', $Commands));
    114         $Command = '/usr/bin/ssh -l '.$Config['MainRouter']['UserName'].' -i id_dsa '.$Config['MainRouter']['HostName'].' "'.$Commands.'"';
    115         $Output = array();
    116         exec($Command, $Output);
    117         print_r($Output);
    118         $Commands = array();
    119         $Index = 0;
    120       }
    121       $Index++;
    122115    }
    123116  }
     
    130123    $IPParts = explode('.', $Subnet['AddressRange']);
    131124    $SubnetNumber = $IPParts[2];
    132     $Commands[] = ' /ip firewall mangle add chain=inet-out-'.$SubnetNumber.' src-address='.$Subnet['AddressRange'].'/'.$Subnet['Mask'].' out-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' passthrough=no comment="'.$Subnet['Name'].'"';
    133     $Commands[] = ' /queue tree add name='.$Subnet['Name'].'-out limit-at='.$HostSpeedIn.' max-limit='.$UserMaxSpeedIn.' parent='.$Member['Name'].'-out packet-mark='.$PacketMark.' queue=wireless-default';
     125    $ItemsFirewall[] = array('chain' => 'inet-out-'.$SubnetNumber, 'src-address' => $Subnet['AddressRange'].'/'.$Subnet['Mask'], 'out-interface' =>  $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'passthrough' => 'no', 'comment' => $Subnet['Name']);
     126    $ItemsQueue[] = array('name' => $Subnet['Name'].'-out', 'limit-at' => $HostSpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => $Member['Name'].'-out', 'packet-mark' => $PacketMark, 'queue' => 'wireless-default');
    134127    $PacketMark++;
    135     $Commands[] = ' /ip firewall mangle add chain=inet-in-'.$SubnetNumber.' dst-address='.$Subnet['AddressRange'].'/'.$Subnet['Mask'].' in-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' passthrough=no comment="'.$Subnet['Name'].'"';
    136     $Commands[] = ' /queue tree add name='.$Subnet['Name'].'-in limit-at='.$HostSpeedOut.' max-limit='.$UserMaxSpeedOut.' parent='.$Member['Name'].'-in packet-mark='.$PacketMark.' queue=wireless-default';
     128    $ItemsFirewall[] = array('chain' => 'inet-in-'.$SubnetNumber, 'dst-address' => $Subnet['AddressRange'].'/'.$Subnet['Mask'], 'in-interface' => $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'passthrough' => 'no', 'comment' => $Subnet['Name']);
     129    $ItemsQueue[] = array('name' => $Subnet['Name'].'-in', 'limit-at' => $HostSpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => $Member['Name'].'-in', 'packet-mark' => $PacketMark, 'queue' => 'wireless-default');
    137130    $PacketMark++;
    138 
    139     // Posílej po menších částech
    140     if($Index > 50)
    141     {
    142       //print_r($Commands);
    143       $Commands = addslashes(implode(';', $Commands));
    144       $Command = '/usr/bin/ssh -l '.$Config['MainRouter']['UserName'].' -i id_dsa '.$Config['MainRouter']['HostName'].' "'.$Commands.'"';
    145       //echo($Command."\n");
    146       $Output = array();
    147       exec($Command, $Output);
    148       print_r($Output);
    149           $Commands = array();
    150       $Index = 0;
    151     }
    152     $Index++;
    153131  }
    154132  echo("\n");
    155133}
    156 //print_r($Commands);
    157 $Commands = addslashes(implode(';', $Commands));
    158 $Command = '/usr/bin/ssh -l '.$Config['MainRouter']['UserName'].' -i id_dsa '.$Config['MainRouter']['HostName'].' "'.$Commands.'"';
    159 //echo($Command."\n");
    160 $Output = array();
    161 exec($Command, $Output);
    162 //array_pop($Output);
    163 print_r($Output);
     134
     135//$Routerboard->ListUpdate($PathQueue, array('name', 'limit-at', 'max-limit', 'parent', 'packet-mark', 'queue'), $ItemsQueue, array(), true);
     136$Routerboard->ListUpdate($PathFirewall, array('chain', 'dst-address', 'in-interface', 'action', 'new-packet-mark', 'passthrough', 'comment', 'out-interface', 'src-address', 'jump-target'), $ItemsFirewall, array(), true);
    164137
    165138?>
Note: See TracChangeset for help on using the changeset viewer.