- Timestamp:
- Aug 2, 2009, 4:56:48 PM (15 years ago)
- Location:
- trunk/system
- Files:
-
- 4 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/system/generators/traffic_shaping_routerboard.php
r214 r241 6 6 $SessionDisable = true; 7 7 include_once('../../global.php'); 8 include('../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 8 17 $Finance = &$System->Modules['Finance']; 9 18 $Finance->LoadMonthParameters(0); … … 30 39 $FreeInetSpeed = 64 * 1024; 31 40 32 $Commands = array(); 33 $Commands[] = '/ip firewall mangle { remove [find] }'; 34 $Commands[] = '/queue tree { remove [find] }'; 41 $ItemsMangle = array(); 42 $ItemsQueue = array(); 35 43 36 44 // Root of tree and main limit 37 45 $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'); 42 50 43 51 // Divide rules by subnet number … … 47 55 $SubnetParts = explode('.', $Subnet['AddressRange']); 48 56 $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); 51 59 } 52 60 53 61 // 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'); 56 64 $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'); 59 67 $PacketMark++; 60 68 61 69 // Process users 62 $Index = 0;63 70 $DbResult = $Database->query('SELECT Member.*, Subject.Name FROM Member LEFT JOIN Subject ON Subject.Id = Member.Subject'); 64 71 while($Member = $DbResult->fetch_assoc()) 65 72 { 66 $Member['Name'] = RouterOSIdent($Member['Name'] );73 $Member['Name'] = RouterOSIdent($Member['Name'].'-'.$Member['Id'] ); 67 74 68 75 echo('Uživatel '.$Member['Name'].': '); … … 72 79 $UserMaxSpeedIn = round($Tariff['SpeedMax'] / $InDivider); 73 80 $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'); 76 83 77 84 $DbResult2 = $Database->select('NetworkDevice', 'COUNT(*)', 'Used = 1 AND Member='.$Member['Id']); … … 100 107 $IPParts = explode('.', $Interface['LocalIP']); 101 108 $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'); 104 111 $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'); 107 114 $PacketMark++; 108 109 // Posílej po menších částech110 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++;122 115 } 123 116 } … … 130 123 $IPParts = explode('.', $Subnet['AddressRange']); 131 124 $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'); 134 127 $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'); 137 130 $PacketMark++; 138 139 // Posílej po menších částech140 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++;153 131 } 154 132 echo("\n"); 155 133 } 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); 164 137 165 138 ?> -
trunk/system/queues_import.php
r239 r241 8 8 $Routerboard = new Routerboard($Config['MainRouter']['HostName'], $Config['MainRouter']['UserName'], $Config['MainRouter']['Password']); 9 9 $Routerboard->PrivateKey = 'generators/id_dsa'; 10 print_r($Routerboard->ListUpdate(array('tool', 'netwatch'), array('host', 'comment'), array()));10 //print_r($Routerboard->ListGetPrint(array('queue', 'tree'), array('name', 'limit-at', 'max-limit', 'parent', 'packet-mark', 'queue'))); 11 11 //print_r($Routerboard->Execute('ip address {:foreach i in=[find] do={:put "address=".[get $i address]." network=".[get $i network])}}')); 12 12 //print_r($Routerboard->GetItem('system ntp client print')); 13 13 14 print_r(ksort(array(1 => 'aaa', 0 => 'bbb'))); 15 14 16 ?> -
trunk/system/routerboard.php
r240 r241 109 109 } 110 110 111 function ListGetPrint($Path, $Properties, $Conditions = array()) 112 { 113 $ConditionList = ''; 114 foreach($Conditions as $Index => $Item) 115 { 116 $ConditionList .= $Index.'="'.$Item.'" '; 117 } 118 $ConditionList = substr($ConditionList, 0, -1); 119 if(trim($ConditionList) != '') 120 $ConditionList = ' where '.$ConditionList; 121 122 $Result = $this->Execute(implode(' ', $Path).' print terse'.$ConditionList); 123 $List = array(); 124 foreach($Result as $ResultLine) 125 { 126 $ResultLineParts = explode(' ', $ResultLine); 127 $ListItem = array(); 128 foreach($ResultLineParts as $ResultLinePart) 129 { 130 $Value = explode('=', $ResultLinePart); 131 if(in_array($Value[0], $Properties)) 132 { 133 if(count($Value) > 1) 134 { 135 if($Value[1]{0} == '"') $Value[1] = substr($Value[1], 1, -1); 136 //if(strlen($Value[1]) > 0) 137 $ListItem[$Value[0]] = $Value[1]; 138 } else $ListItem[$Value[0]] = ''; 139 } 140 } 141 if(count($ListItem) > 0) $List[] = $ListItem; 142 } 143 return($List); 144 } 145 111 146 function ListEraseAll($Path) 112 147 { … … 114 149 } 115 150 116 function ListUpdate($Path, $Properties, $Values, $Condition = array()) 117 { 118 $List = $this->ListGet($Path, $Properties, $Condition); 151 function ListUpdate($Path, $Properties, $Values, $Condition = array(), $UsePrint = false) 152 { 153 if($UsePrint == 0) $List = $this->ListGet($Path, $Properties, $Condition); 154 else $List = $this->ListGetPrint($Path, $Properties, $Condition); 119 155 $Commands = array(); 120 156 … … 129 165 $Values[$Index] = $Item; 130 166 } 131 167 foreach($List as $Index => $Item) 168 { 169 foreach($Properties as $Property) 170 { 171 if(!array_key_exists($Property, $Item)) 172 $Item[$Property] = ''; 173 } 174 $List[$Index] = $Item; 175 } 176 177 // Sort properties 178 foreach($Values as $Index => $Item) 179 { 180 ksort($Values[$Index]); 181 } 182 foreach($List as $Index => $Item) 183 { 184 ksort($List[$Index]); 185 } 132 186 print_r($List); 133 187 print_r($Values); … … 144 198 } 145 199 $Prop = substr($Prop, 0, -1); 146 $Commands[] = implode(' ', $Path).' remove [find '.$Prop.']'; 200 if(trim($Prop) != '') 201 $Commands[] = implode(' ', $Path).' remove [find '.$Prop.']'; 147 202 } 148 203 }
Note:
See TracChangeset
for help on using the changeset viewer.