- Timestamp:
- Apr 3, 2020, 12:30:49 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/NetworkConfigRouterOS/Generators/FirewallMangle.php
r811 r870 49 49 function Run() 50 50 { 51 $this->RunIPv4(); 52 $this->RunIPv6(); 53 } 54 55 function RunIPv4() 56 { 51 57 global $ItemsFirewall; 52 58 53 59 $PathFirewall = array('ip', 'firewall', 'mangle'); 54 60 … … 61 67 $InetInterface = $this->System->Config['MainRouter']['InetInterface']; 62 68 63 64 69 // Generate address tree 65 70 $AddressTree = array('Address' => new NetworkAddressIPv4(), 'Name' => 'main', 'Items' => array(), 'ForceMark' => false); … … 72 77 $NewAddress->AddressFromString($Subnet['AddressRange']); 73 78 $NewAddress->Prefix = $Subnet['Mask']; 74 InsertToAddressTree ($AddressTree, $NewAddress, 'subnet-'.RouterOSIdent($Subnet['Name']));79 InsertToAddressTreeIPv4($AddressTree, $NewAddress, 'subnet-'.RouterOSIdent($Subnet['Name'])); 75 80 } 76 81 … … 96 101 $NewAddress = new NetworkAddressIPv4(); 97 102 $NewAddress->AddressFromString($Interface['LocalIP']); 98 $NewAddress->Prefix = 32;99 InsertToAddressTree ($AddressTree, $NewAddress, $Name);103 $NewAddress->Prefix = IPV4_BIT_WIDTH; 104 InsertToAddressTreeIPv4($AddressTree, $NewAddress, $Name); 100 105 } 101 106 } 102 107 103 $DbResult2 = $this->Database->select('NetworkSubnet', '*', ' `Member`='.$Member['Id']);108 $DbResult2 = $this->Database->select('NetworkSubnet', '*', '(`Member`='.$Member['Id'].') AND (AddressRange != "")'); 104 109 while($Subnet = $DbResult2->fetch_assoc()) 105 110 { … … 112 117 else $ForceMark = false; 113 118 echo($ForceMark.', '); 114 InsertToAddressTree ($AddressTree, $NewAddress, $Subnet['Name'], false, $ForceMark);119 InsertToAddressTreeIPv4($AddressTree, $NewAddress, $Subnet['Name'], false, $ForceMark); 115 120 } 116 121 echo("\n"); … … 143 148 $Routerboard->ListUpdate($PathFirewall, array('chain', 'dst-address', 'in-interface', 'action', 'new-packet-mark', 'passthrough', 'comment', 'out-interface', 'src-address', 'jump-target'), $ItemsFirewall, array(), true); 144 149 } 150 151 function RunIPv6() 152 { 153 global $ItemsFirewall; 154 155 $PathFirewall = array('ipv6', 'firewall', 'mangle'); 156 157 $Routerboard = new Routerboard(); 158 $Routerboard->UserName = $this->System->Config['MainRouter']['UserName']; 159 $Routerboard->Timeout = $this->System->Config['MainRouter']['ConnectTimeout']; 160 $Routerboard->HostName = $this->System->Config['MainRouter']['HostName']; 161 $Routerboard->Debug = true; 162 163 $InetInterface = $this->System->Config['MainRouter']['InetInterface']; 164 165 // Generate address tree 166 $AddressTree = array('Address' => new NetworkAddressIPv4(), 'Name' => 'main', 'Items' => array(), 'ForceMark' => false); 167 168 // Divide rules by subnet number 169 $DbResult = $this->System->Database->query('SELECT `Id`, `Name`, `AddressRangeIPv6`, `MaskIPv6` FROM `NetworkSubnet` '. 170 'WHERE (`Member` IS NULL) AND (`AddressRangeIPv6` != "")'); 171 while($Subnet = $DbResult->fetch_assoc()) 172 { 173 $NewAddress = new NetworkAddressIPv6(); 174 $NewAddress->AddressFromString($Subnet['AddressRangeIPv6']); 175 $NewAddress->Prefix = $Subnet['MaskIPv6']; 176 InsertToAddressTreeIPv6($AddressTree, $NewAddress, 'subnet-'.RouterOSIdent($Subnet['Name'])); 177 } 178 179 // Process users 180 $DbResult = $this->System->Database->query('SELECT `Member`.*, `Subject`.`Name` FROM `Member` '. 181 'LEFT JOIN `Subject` ON `Subject`.`Id` = `Member`.`Subject` '. 182 'WHERE `Member`.`Blocked` = 0'); 183 while($Member = $DbResult->fetch_assoc()) 184 { 185 $Member['Name'] = RouterOSIdent($Member['Name'].'-'.$Member['Id'] ); 186 echo('Uživatel '.$Member['Name'].': '); 187 188 $DbResult2 = $this->System->Database->select('NetworkDevice', '*', '`Used` = 1 AND `Member` = '.$Member['Id']); 189 while($Device = $DbResult2->fetch_assoc()) 190 { 191 $DbResult3 = $this->Database->select('NetworkInterface', '*', '`Device` = '.$Device['Id'].' AND `IPv6` != ""'); 192 while($Interface = $DbResult3->fetch_assoc()) 193 { 194 $Name = $Device['Name']; 195 if($Interface['Name'] != '') $Name .= '-'.$Interface['Name']; 196 $Name = RouterOSIdent($Name); 197 echo($Name.', '); 198 $NewAddress = new NetworkAddressIPv6(); 199 $NewAddress->AddressFromString($Interface['IPv6']); 200 $NewAddress->Prefix = IPV6_BIT_WIDTH; 201 InsertToAddressTreeIPv6($AddressTree, $NewAddress, $Name); 202 } 203 } 204 205 $DbResult2 = $this->Database->select('NetworkSubnet', '*', '(`Member`='.$Member['Id'].') AND (AddressRangeIPv6 != "")'); 206 while($Subnet = $DbResult2->fetch_assoc()) 207 { 208 $Subnet['Name'] = RouterOSIdent('subnet-'.$Subnet['Name']); 209 echo($Subnet['Name'].', '); 210 $NewAddress = new NetworkAddressIPv6(); 211 $NewAddress->AddressFromString($Subnet['AddressRangeIPv6']); 212 $NewAddress->Prefix = $Subnet['MaskIPv6']; 213 if($Subnet['Member'] != 0) $ForceMark = true; 214 else $ForceMark = false; 215 echo($ForceMark.', '); 216 InsertToAddressTreeIPv6($AddressTree, $NewAddress, $Subnet['Name'], false, $ForceMark); 217 } 218 echo("\n"); 219 } 220 221 ShowSubnetNode($AddressTree); 222 223 // Generate firewall rules 224 $ItemsFirewall = array(); 225 226 // Root of tree and main limit 227 $ItemsFirewall[] = array('chain' => 'forward', 'out-interface' => $InetInterface, 'dst-address' => '!2a00:e580:244::/48', 228 'action' => 'jump', 'jump-target' => 'inet-1-out', 'comment' => 'main-out'); 229 $ItemsFirewall[] = array('chain' => 'forward', 'in-interface' => $InetInterface, 'src-address' => '!2a00:e580:244::/48', 230 'action' => 'jump', 'jump-target' => 'inet-1-in', 'comment' => 'main-in'); 231 232 $this->ProcessNode($AddressTree); 233 234 // Limited free internet 235 $PacketMark = GetMarkByComment('free-out'); 236 $ItemsFirewall[] = array('chain' => 'inet-1-out', 'out-interface' => $InetInterface, 237 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'comment' => 'free-out', 'passthrough' => 'yes'); 238 $PacketMark = GetMarkByComment('free-in'); 239 $ItemsFirewall[] = array('chain' => 'inet-1-in', 'in-interface' => $InetInterface, 240 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'comment' => 'free-in', 'passthrough' => 'no'); 241 // Unregistred clients add to address list 242 $ItemsFirewall[] = array('chain' => 'inet-1-out', 'out-interface' => $InetInterface, 'src-address' => '2a00:e580:244::/48', 243 'action' => 'add-src-to-address-list', 'address-list' => 'unregistred', 'address-list-timeout' => '1d', 244 'comment' => 'unregistred-clients'); 245 246 //print_r($ItemsFirewall); 247 $Routerboard->ListUpdate($PathFirewall, array('chain', 'dst-address', 'in-interface', 'action', 'new-packet-mark', 248 'passthrough', 'comment', 'out-interface', 'src-address', 'jump-target'), $ItemsFirewall, array(), true); 249 } 145 250 }
Note:
See TracChangeset
for help on using the changeset viewer.