source: trunk/system/generators/firewall_nat.php@ 391

Last change on this file since 391 was 317, checked in by george, 14 years ago
  • Uvozovky v názvech v SQL dotazech.
File size: 8.4 KB
Line 
1<?php
2
3if(isset($_SERVER['REMOTE_ADDR'])) die();
4include('../../global.php');
5include('../routerboard.php');
6include('common.php');
7$Path = array('ip', 'firewall', 'nat');
8
9$Routerboard = new Routerboard($Config['MainRouter']['HostName']);
10$Routerboard->UserName = $Config['MainRouter']['UserName'];
11$Routerboard->Timeout = $Config['MainRouter']['ConnectTimeout'];
12$Routerboard->Debug = true;
13
14$InetInterface = $Config['MainRouter']['InetInterface'];
15$LocalInterface = $Config['MainRouter']['LocalInterface'];
16$IPCentrala = '10.145.64.8';
17
18$Items = array();
19
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*/
29
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');
33
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');
37
38$DbResult = $Database->query('SELECT `Member`.*, `Subject`.`Name` FROM `Member` JOIN `Subject` ON `Member`.`Subject` = `Subject`.`Id`');
39while($Member = $DbResult->fetch_assoc())
40{
41 echo($Member['Name'].': ');
42 // Hosts
43 $DbResult2 = $Database->query('SELECT `NetworkInterface`.*, `NetworkDevice`.`Name` AS `DeviceName`, `NetworkDevice`.`InboundNATPriority` FROM `NetworkInterface`'.
44 ' LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` WHERE (`NetworkInterface`.`ExternalIP` <> "")'.
45 ' AND (`NetworkDevice`.`Member` = '.$Member['Id'].') AND (`NetworkInterface`.`LocalIP` != `NetworkInterface`.`ExternalIP`) ORDER BY `id` DESC');
46 while($Interface = $DbResult2->fetch_assoc())
47 {
48 $Name = $Interface['DeviceName'];
49 if($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
50 $Name = RouterOSIdent($Name);
51 echo($Name.'('.$Interface['LocalIP'].'), ');
52 if($Member['Blocked'] == 0)
53 {
54 $Items[] = array('chain' => 'inet-out', 'src-address' => $Interface['LocalIP'], 'action' => 'src-nat', 'to-addresses' => $Interface['ExternalIP'], 'comment' => $Name.'-out');
55 if($Interface['InboundNATPriority'] > 0)
56 $Items[] = array('chain' => 'inet-in', 'dst-address' => $Interface['ExternalIP'], 'action' => 'dst-nat', 'to-addresses' => $Interface['LocalIP'], 'comment' => $Name.'-in');
57 } else
58 {
59 $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');
60 }
61 }
62
63 // Subnets
64 $DbResult2 = $Database->select('NetworkSubnet', '*', '`Member`='.$Member['Id']);
65 while($Subnet = $DbResult2->fetch_assoc())
66 {
67 $Subnet['Name'] = RouterOSIdent('subnet-'.$Subnet['Name']);
68 echo($Subnet['Name'].'('.$Subnet['AddressRange'].'/'.$Subnet['Mask'].'), ');
69 if($Member['Blocked'] == 0)
70 {
71 $NewAddress = new NetworkAddressIPv4();
72 $NewAddress->AddressFromString($Subnet['ExtAddressRange']);
73 $NewAddress->Prefix = $Subnet['ExtMask'];
74 $Range = $NewAddress->GetRange();
75 if($Subnet['ExtMask'] != 32) $Range = $Range['From']->AddressToString().'-'.$Range['To']->AddressToString();
76 else $Range = $Range['From']->AddressToString();
77 if($Subnet['Mask'] == 32) $Src = $Subnet['AddressRange'];
78 else $Src = $Subnet['AddressRange'].'/'.$Subnet['Mask'];
79 $Items[] = array('chain' => 'inet-out', 'src-address' => $Src, 'action' => 'src-nat', 'to-addresses' => $Range, 'comment' => $Subnet['Name'].'-out');
80
81 $NewAddress = new NetworkAddressIPv4();
82 $NewAddress->AddressFromString($Subnet['AddressRange']);
83 $NewAddress->Prefix = $Subnet['Mask'];
84 $Range = $NewAddress->GetRange();
85 if($Subnet['Mask'] != 32) $Range = $Range['From']->AddressToString().'-'.$Range['To']->AddressToString();
86 else $Range = $Range['From']->AddressToString();
87 if($Subnet['ExtMask'] == 32) $Dest = $Subnet['ExtAddressRange'];
88 else $Dest = $Subnet['ExtAddressRange'].'/'.$Subnet['ExtMask'];
89 $Items[] = array('chain' => 'inet-in', 'dst-address' => $Dest, 'action' => 'dst-nat', 'to-addresses' => $Range, 'comment' => $Subnet['Name'].'-in');
90 } else
91 {
92 if($Subnet['Mask'] == 32) $Src = $Subnet['AddressRange'];
93 else $Src = $Subnet['AddressRange'].'/'.$Subnet['Mask'];
94 $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');
95 }
96 }
97 echo("\n");
98}
99
100// Masquerade hosts without public ip
101$Items[] = array('chain' => 'inet-out', 'action' => 'src-nat', 'to-addresses' => '77.92.221.106', 'comment' => 'Default_NAT');
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
108// Chain for local interface
109$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/*
113// Route public addresses localy
114$DbResult = $Database->query('SELECT Member.*, Subject.Name FROM Member JOIN Subject ON Member.Subject = Subject.Id');
115while($Member = $DbResult->fetch_assoc())
116{
117 echo($Member['Name'].': ');
118 // Hosts
119 $DbResult2 = $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');
120 while($Interface = $DbResult2->fetch_assoc())
121 {
122 $Name = $Interface['DeviceName'];
123 if($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
124 $Name = RouterOSIdent($Name);
125 echo($Name.'('.$Interface['LocalIP'].'), ');
126 $Items[] = array('chain' => 'local-in', 'dst-address' => $Interface['ExternalIP'], 'action' => 'dst-nat', 'to-addresses' => $Interface['LocalIP'], 'comment' => $Name.'-in-local');
127 }
128 echo("\n");
129}
130
131// Map returned local traffic to virtual subnet
132$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');
133*/
134
135//print_r($Items);
136$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);
137
138?>
Note: See TracBrowser for help on using the repository browser.