- Timestamp:
- Jan 8, 2016, 11:00:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/NetworkConfigRouterOS/Generators/FirewallNAT.php
r778 r781 1 1 <?php 2 2 3 if(isset($_SERVER['REMOTE_ADDR'])) die(); 4 include_once(dirname(__FILE__).'/../../../Application/System.php'); 5 $System = new System(); 6 $System->ShowPage = false; 7 $System->Run(); 8 $Path = array('ip', 'firewall', 'nat'); 3 class ConfigRouterOSFirewallNAT extends NetworkConfigItem 4 { 5 function Run() 6 { 7 $Path = array('ip', 'firewall', 'nat'); 9 8 10 $Routerboard = new Routerboard($Config['MainRouter']['HostName']);11 $Routerboard->UserName = $Config['MainRouter']['UserName'];12 $Routerboard->Timeout = $Config['MainRouter']['ConnectTimeout'];13 $Routerboard->Debug = true;9 $Routerboard = new Routerboard($this->System->Config['MainRouter']['HostName']); 10 $Routerboard->UserName = $this->System->Config['MainRouter']['UserName']; 11 $Routerboard->Timeout = $this->System->Config['MainRouter']['ConnectTimeout']; 12 $Routerboard->Debug = true; 14 13 15 $InetInterface = $Config['MainRouter']['InetInterface'];16 $LocalInterface = $Config['MainRouter']['LocalInterface'];17 $IPCentrala = '10.145.64.8';14 $InetInterface = $this->System->Config['MainRouter']['InetInterface']; 15 $LocalInterface = $this->System->Config['MainRouter']['LocalInterface']; 16 $IPCentrala = '10.145.64.8'; 18 17 19 $Items = array();18 $Items = array(); 20 19 21 /*22 // NTP redirect23 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.1', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_4');24 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.161', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_5');25 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.193', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_1');26 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.225', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_2');27 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.250', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_3');28 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.253', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_6');29 */20 /* 21 // NTP redirect 22 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.1', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_4'); 23 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.161', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_5'); 24 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.193', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_1'); 25 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.225', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_2'); 26 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.250', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_3'); 27 $Items[] = array('chain' => 'srcnat', 'src-address' => '10.145.66.253', 'protocol' => 'udp', 'src-port' => 123, 'action' => 'src-nat', 'to-addresses' => '10.145.64.1', 'comment' => 'NTP_redirect_6'); 28 */ 30 29 31 // Chain for inet interface32 $Items[] = array('chain' => 'srcnat', 'out-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-out', 'comment' => 'inet-out');33 $Items[] = array('chain' => 'dstnat', 'in-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-in', 'comment' => 'inet-in');30 // Chain for inet interface 31 $Items[] = array('chain' => 'srcnat', 'out-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-out', 'comment' => 'inet-out'); 32 $Items[] = array('chain' => 'dstnat', 'in-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-in', 'comment' => 'inet-in'); 34 33 35 // Skip local subnet36 //$Items[] = array('chain' => 'inet-out', 'dst-address' => '172.16.1.1/30', 'action' => 'accept', 'comment' => 'Local_subnet');37 //$Items[] = array('chain' => 'inet-in', 'dst-address' => '172.16.1.1/30', 'action' => 'accept', 'comment' => 'Local_subnet');34 // Skip local subnet 35 //$Items[] = array('chain' => 'inet-out', 'dst-address' => '172.16.1.1/30', 'action' => 'accept', 'comment' => 'Local_subnet'); 36 //$Items[] = array('chain' => 'inet-in', 'dst-address' => '172.16.1.1/30', 'action' => 'accept', 'comment' => 'Local_subnet'); 38 37 39 $DbResult = $System->Database->query('SELECT `Member`.*, `Subject`.`Name` FROM `Member` '. 40 'LEFT JOIN `Subject` ON `Subject`.`Id` = `Member`.`Subject` '. 41 'WHERE `Member`.`Blocked` = 0'); 42 while($Member = $DbResult->fetch_assoc()) 43 { 44 echo($Member['Name'].': '); 45 // Hosts 46 $DbResult2 = $System->Database->query('SELECT `NetworkInterface`.*, `NetworkDevice`.`Name` AS `DeviceName`, `NetworkDevice`.`InboundNATPriority` FROM `NetworkInterface`'. 47 ' LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` WHERE (`NetworkInterface`.`ExternalIP` <> "")'. 48 ' AND (`NetworkInterface`.`LocalIP` <> "")'. 49 ' AND (`NetworkDevice`.`Member` = '.$Member['Id'].') AND (`NetworkInterface`.`LocalIP` != `NetworkInterface`.`ExternalIP`) ORDER BY `id` DESC'); 50 while($Interface = $DbResult2->fetch_assoc()) 51 { 52 $Name = $Interface['DeviceName']; 53 if($Interface['Name'] != '') $Name .= '-'.$Interface['Name']; 54 $Name = RouterOSIdent($Name); 55 echo($Name.'('.$Interface['LocalIP'].'), '); 56 if($Member['Blocked'] == 0) 38 $DbResult = $this->Database->query('SELECT `Member`.*, `Subject`.`Name` FROM `Member` '. 39 'LEFT JOIN `Subject` ON `Subject`.`Id` = `Member`.`Subject` '. 40 'WHERE `Member`.`Blocked` = 0'); 41 while($Member = $DbResult->fetch_assoc()) 57 42 { 58 $Items[] = array('chain' => 'inet-out', 'src-address' => $Interface['LocalIP'], 'action' => 'src-nat', 'to-addresses' => $Interface['ExternalIP'], 'comment' => $Name.'-out'); 59 if($Interface['InboundNATPriority'] > 0) 60 $Items[] = array('chain' => 'inet-in', 'dst-address' => $Interface['ExternalIP'], 'action' => 'dst-nat', 'to-addresses' => $Interface['LocalIP'], 'comment' => $Name.'-in'); 61 } else 62 { 63 $Items[] = array('chain' => 'dstnat', 'src-address' => $Interface['LocalIP'], 'protocol' => 'tcp', 'dst-port' => 80, 'action' => 'dst-nat', 'to-addresses' => $IPCentrala, 'to-ports' => 81, 'comment' => $Name.'-out'); 43 echo($Member['Name'].': '); 44 // Hosts 45 $DbResult2 = $this->Database->query('SELECT `NetworkInterface`.*, `NetworkDevice`.`Name` AS `DeviceName`, `NetworkDevice`.`InboundNATPriority` FROM `NetworkInterface`'. 46 ' LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` WHERE (`NetworkInterface`.`ExternalIP` <> "")'. 47 ' AND (`NetworkInterface`.`LocalIP` <> "")'. 48 ' AND (`NetworkDevice`.`Member` = '.$Member['Id'].') AND (`NetworkInterface`.`LocalIP` != `NetworkInterface`.`ExternalIP`) ORDER BY `id` DESC'); 49 while($Interface = $DbResult2->fetch_assoc()) 50 { 51 $Name = $Interface['DeviceName']; 52 if($Interface['Name'] != '') $Name .= '-'.$Interface['Name']; 53 $Name = RouterOSIdent($Name); 54 echo($Name.'('.$Interface['LocalIP'].'), '); 55 if($Member['Blocked'] == 0) 56 { 57 $Items[] = array('chain' => 'inet-out', 'src-address' => $Interface['LocalIP'], 'action' => 'src-nat', 'to-addresses' => $Interface['ExternalIP'], 'comment' => $Name.'-out'); 58 if($Interface['InboundNATPriority'] > 0) 59 $Items[] = array('chain' => 'inet-in', 'dst-address' => $Interface['ExternalIP'], 'action' => 'dst-nat', 'to-addresses' => $Interface['LocalIP'], 'comment' => $Name.'-in'); 60 } else 61 { 62 $Items[] = array('chain' => 'dstnat', 'src-address' => $Interface['LocalIP'], 'protocol' => 'tcp', 'dst-port' => 80, 'action' => 'dst-nat', 'to-addresses' => $IPCentrala, 'to-ports' => 81, 'comment' => $Name.'-out'); 63 } 64 } 65 66 // Subnets 67 $DbResult2 = $this->Database->select('NetworkSubnet', '*', '`Member`='.$Member['Id']); 68 while($Subnet = $DbResult2->fetch_assoc()) 69 { 70 $Subnet['Name'] = RouterOSIdent('subnet-'.$Subnet['Name']); 71 echo($Subnet['Name'].'('.$Subnet['AddressRange'].'/'.$Subnet['Mask'].'), '); 72 if($Member['Blocked'] == 0) 73 { 74 $NewAddress = new NetworkAddressIPv4(); 75 $NewAddress->AddressFromString($Subnet['ExtAddressRange']); 76 $NewAddress->Prefix = $Subnet['ExtMask']; 77 $Range = $NewAddress->GetRange(); 78 if($Subnet['ExtMask'] != 32) $Range = $Range['From']->AddressToString().'-'.$Range['To']->AddressToString(); 79 else $Range = $Range['From']->AddressToString(); 80 if($Subnet['Mask'] == 32) $Src = $Subnet['AddressRange']; 81 else $Src = $Subnet['AddressRange'].'/'.$Subnet['Mask']; 82 $Items[] = array('chain' => 'inet-out', 'src-address' => $Src, 'action' => 'src-nat', 'to-addresses' => $Range, 'comment' => $Subnet['Name'].'-out'); 83 84 $NewAddress = new NetworkAddressIPv4(); 85 $NewAddress->AddressFromString($Subnet['AddressRange']); 86 $NewAddress->Prefix = $Subnet['Mask']; 87 $Range = $NewAddress->GetRange(); 88 if($Subnet['Mask'] != 32) $Range = $Range['From']->AddressToString().'-'.$Range['To']->AddressToString(); 89 else $Range = $Range['From']->AddressToString(); 90 if($Subnet['ExtMask'] == 32) $Dest = $Subnet['ExtAddressRange']; 91 else $Dest = $Subnet['ExtAddressRange'].'/'.$Subnet['ExtMask']; 92 $Items[] = array('chain' => 'inet-in', 'dst-address' => $Dest, 'action' => 'dst-nat', 'to-addresses' => $Range, 'comment' => $Subnet['Name'].'-in'); 93 } else 94 { 95 if($Subnet['Mask'] == 32) $Src = $Subnet['AddressRange']; 96 else $Src = $Subnet['AddressRange'].'/'.$Subnet['Mask']; 97 $Items[] = array('chain' => 'dstnat', 'src-address' => $Src, 'protocol' => 'tcp', 'dst-port' => 80, 'action' => 'dst-nat', 'to-addresses' => $IPCentrala, 'to-ports' => 81, 'comment' => $Subnet['Name'].'-out'); 98 } 99 } 100 echo("\n"); 64 101 } 102 103 // Redirect DNS port 104 $Items[] = array('chain' => 'dstnat', 'dst-address' => '212.111.4.174', 'protocol' => 'tcp', 'dst-port' => 53, 'in-interface' => $InetInterface, 'action' => 'dst-nat', 'to-addresses' => '10.145.64.8', 'to-ports' => 53, 'comment' => 'DNS_redirection_TCP'); 105 $Items[] = array('chain' => 'dstnat', 'dst-address' => '212.111.4.174', 'protocol' => 'udp', 'dst-port' => 53, 'in-interface' => $InetInterface, 'action' => 'dst-nat', 'to-addresses' => '10.145.64.8', 'to-ports' => 53, 'comment' => 'DNS_redirection_UDP'); 106 107 // Chain for local interface 108 $Items[] = array('chain' => 'srcnat', 'out-interface' => $LocalInterface, 'action' => 'jump', 'jump-target' => 'local-out', 'comment' => 'local-out'); 109 $Items[] = array('chain' => 'dstnat', 'in-interface' => $LocalInterface, 'action' => 'jump', 'jump-target' => 'local-in', 'comment' => 'local-in'); 110 111 // Accept free-access clients 112 $Items[] = array('chain' => 'dstnat', 'dst-address' => '!10.145.0.0/16', 113 'src-address-list' => 'free-access', 'in-interface' => $LocalInterface, 114 'action' => 'accept', 'comment' => 'Free_access'); 115 // Redirect unregistred clients to free access activation page 116 $Items[] = array('chain' => 'dstnat', 'dst-address' => '!10.145.0.0/16', 117 'src-address-list' => 'unregistred', 'in-interface' => $LocalInterface, 'protocol' => 'tcp', 118 'action' => 'dst-nat', 'to-addresses' => '10.145.64.70', 'to-ports' => 8080, 'comment' => 'Redirect_unregistred'); 119 120 // Masquerade hosts without public ip 121 $Items[] = array('chain' => 'inet-out', 'src-address'=> '!212.111.4.174', 'action' => 'src-nat', 'to-addresses' => '77.92.221.188', 'comment' => 'Default_NAT'); 122 123 /* 124 // Route public addresses localy 125 $DbResult = $this->Database->query('SELECT Member.*, Subject.Name FROM Member JOIN Subject ON Member.Subject = Subject.Id'); 126 while($Member = $DbResult->fetch_assoc()) 127 { 128 echo($Member['Name'].': '); 129 // Hosts 130 $DbResult2 = $this->Database->query('SELECT NetworkInterface.*, NetworkDevice.Name AS DeviceName FROM NetworkInterface LEFT JOIN NetworkDevice ON NetworkDevice.Id = NetworkInterface.Device WHERE (NetworkInterface.ExternalIP <> "") AND (NetworkDevice.Member = '.$Member['Id'].') AND (NetworkInterface.LocalIP != NetworkInterface.ExternalIP) ORDER BY id DESC'); 131 while($Interface = $DbResult2->fetch_assoc()) 132 { 133 $Name = $Interface['DeviceName']; 134 if($Interface['Name'] != '') $Name .= '-'.$Interface['Name']; 135 $Name = RouterOSIdent($Name); 136 echo($Name.'('.$Interface['LocalIP'].'), '); 137 $Items[] = array('chain' => 'local-in', 'dst-address' => $Interface['ExternalIP'], 'action' => 'dst-nat', 'to-addresses' => $Interface['LocalIP'], 'comment' => $Name.'-in-local'); 138 } 139 echo("\n"); 140 } 141 142 // Map returned local traffic to virtual subnet 143 $Items[] = array('chain' => 'local-out', 'src-address' => '10.145.0.0/16', 'dst-address' => '10.145.0.0/16', 'action' => 'netmap', 'to-addresses' => '10.45.0.0-10.45.255.255', 'comment' => 'map-local'); 144 */ 145 146 //print_r($Items); 147 $Routerboard->ListUpdate($Path, array('chain', 'dst-address', 'in-interface', 'src-address', 'out-interface', 'to-ports', 'dst-port', 'protocol', 'action', 'to-addresses', 'comment', 'jump-target', 'src-port'), $Items); 65 148 } 66 67 // Subnets68 $DbResult2 = $System->Database->select('NetworkSubnet', '*', '`Member`='.$Member['Id']);69 while($Subnet = $DbResult2->fetch_assoc())70 {71 $Subnet['Name'] = RouterOSIdent('subnet-'.$Subnet['Name']);72 echo($Subnet['Name'].'('.$Subnet['AddressRange'].'/'.$Subnet['Mask'].'), ');73 if($Member['Blocked'] == 0)74 {75 $NewAddress = new NetworkAddressIPv4();76 $NewAddress->AddressFromString($Subnet['ExtAddressRange']);77 $NewAddress->Prefix = $Subnet['ExtMask'];78 $Range = $NewAddress->GetRange();79 if($Subnet['ExtMask'] != 32) $Range = $Range['From']->AddressToString().'-'.$Range['To']->AddressToString();80 else $Range = $Range['From']->AddressToString();81 if($Subnet['Mask'] == 32) $Src = $Subnet['AddressRange'];82 else $Src = $Subnet['AddressRange'].'/'.$Subnet['Mask'];83 $Items[] = array('chain' => 'inet-out', 'src-address' => $Src, 'action' => 'src-nat', 'to-addresses' => $Range, 'comment' => $Subnet['Name'].'-out');84 85 $NewAddress = new NetworkAddressIPv4();86 $NewAddress->AddressFromString($Subnet['AddressRange']);87 $NewAddress->Prefix = $Subnet['Mask'];88 $Range = $NewAddress->GetRange();89 if($Subnet['Mask'] != 32) $Range = $Range['From']->AddressToString().'-'.$Range['To']->AddressToString();90 else $Range = $Range['From']->AddressToString();91 if($Subnet['ExtMask'] == 32) $Dest = $Subnet['ExtAddressRange'];92 else $Dest = $Subnet['ExtAddressRange'].'/'.$Subnet['ExtMask'];93 $Items[] = array('chain' => 'inet-in', 'dst-address' => $Dest, 'action' => 'dst-nat', 'to-addresses' => $Range, 'comment' => $Subnet['Name'].'-in');94 } else95 {96 if($Subnet['Mask'] == 32) $Src = $Subnet['AddressRange'];97 else $Src = $Subnet['AddressRange'].'/'.$Subnet['Mask'];98 $Items[] = array('chain' => 'dstnat', 'src-address' => $Src, 'protocol' => 'tcp', 'dst-port' => 80, 'action' => 'dst-nat', 'to-addresses' => $IPCentrala, 'to-ports' => 81, 'comment' => $Subnet['Name'].'-out');99 }100 }101 echo("\n");102 149 } 103 104 // Redirect DNS port105 $Items[] = array('chain' => 'dstnat', 'dst-address' => '212.111.4.174', 'protocol' => 'tcp', 'dst-port' => 53, 'in-interface' => $InetInterface, 'action' => 'dst-nat', 'to-addresses' => '10.145.64.8', 'to-ports' => 53, 'comment' => 'DNS_redirection_TCP');106 $Items[] = array('chain' => 'dstnat', 'dst-address' => '212.111.4.174', 'protocol' => 'udp', 'dst-port' => 53, 'in-interface' => $InetInterface, 'action' => 'dst-nat', 'to-addresses' => '10.145.64.8', 'to-ports' => 53, 'comment' => 'DNS_redirection_UDP');107 108 // Chain for local interface109 $Items[] = array('chain' => 'srcnat', 'out-interface' => $LocalInterface, 'action' => 'jump', 'jump-target' => 'local-out', 'comment' => 'local-out');110 $Items[] = array('chain' => 'dstnat', 'in-interface' => $LocalInterface, 'action' => 'jump', 'jump-target' => 'local-in', 'comment' => 'local-in');111 112 // Accept free-access clients113 $Items[] = array('chain' => 'dstnat', 'dst-address' => '!10.145.0.0/16',114 'src-address-list' => 'free-access', 'in-interface' => $LocalInterface,115 'action' => 'accept', 'comment' => 'Free_access');116 // Redirect unregistred clients to free access activation page117 $Items[] = array('chain' => 'dstnat', 'dst-address' => '!10.145.0.0/16',118 'src-address-list' => 'unregistred', 'in-interface' => $LocalInterface, 'protocol' => 'tcp',119 'action' => 'dst-nat', 'to-addresses' => '10.145.64.70', 'to-ports' => 8080, 'comment' => 'Redirect_unregistred');120 121 // Masquerade hosts without public ip122 $Items[] = array('chain' => 'inet-out', 'src-address'=> '!212.111.4.174', 'action' => 'src-nat', 'to-addresses' => '77.92.221.188', 'comment' => 'Default_NAT');123 124 /*125 // Route public addresses localy126 $DbResult = $System->Database->query('SELECT Member.*, Subject.Name FROM Member JOIN Subject ON Member.Subject = Subject.Id');127 while($Member = $DbResult->fetch_assoc())128 {129 echo($Member['Name'].': ');130 // Hosts131 $DbResult2 = $System->Database->query('SELECT NetworkInterface.*, NetworkDevice.Name AS DeviceName FROM NetworkInterface LEFT JOIN NetworkDevice ON NetworkDevice.Id = NetworkInterface.Device WHERE (NetworkInterface.ExternalIP <> "") AND (NetworkDevice.Member = '.$Member['Id'].') AND (NetworkInterface.LocalIP != NetworkInterface.ExternalIP) ORDER BY id DESC');132 while($Interface = $DbResult2->fetch_assoc())133 {134 $Name = $Interface['DeviceName'];135 if($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];136 $Name = RouterOSIdent($Name);137 echo($Name.'('.$Interface['LocalIP'].'), ');138 $Items[] = array('chain' => 'local-in', 'dst-address' => $Interface['ExternalIP'], 'action' => 'dst-nat', 'to-addresses' => $Interface['LocalIP'], 'comment' => $Name.'-in-local');139 }140 echo("\n");141 }142 143 // Map returned local traffic to virtual subnet144 $Items[] = array('chain' => 'local-out', 'src-address' => '10.145.0.0/16', 'dst-address' => '10.145.0.0/16', 'action' => 'netmap', 'to-addresses' => '10.45.0.0-10.45.255.255', 'comment' => 'map-local');145 */146 147 //print_r($Items);148 $Routerboard->ListUpdate($Path, array('chain', 'dst-address', 'in-interface', 'src-address', 'out-interface', 'to-ports', 'dst-port', 'protocol', 'action', 'to-addresses', 'comment', 'jump-target', 'src-port'), $Items);
Note:
See TracChangeset
for help on using the changeset viewer.