source: trunk/Modules/NetworkConfigRouterOS/Generators/Common.php

Last change on this file was 892, checked in by chronos, 4 years ago
  • Added: Limit direct input/output internet traffic to gateway to cover also VPN connections traffic.
File size: 4.3 KB
Line 
1<?php
2
3function GetMarkByComment(string $Comment): int
4{
5 global $Database;
6
7 $DbResult = $Database->query('SELECT `Id` FROM `NetworkMark` WHERE `Comment`="'.$Comment.'"');
8 if ($DbResult->num_rows > 0)
9 {
10 $DbRow = $DbResult->fetch_assoc();
11 return $DbRow['Id'];
12 } else
13 {
14 $DbResult = $Database->query('INSERT INTO `NetworkMark` (`Comment`) VALUES ("'.$Comment.'")');
15 return $Database->insert_id;
16 }
17}
18
19function GetSubgroupByRange(string $AddressRange): int
20{
21 global $Database;
22
23 $DbResult = $Database->query('SELECT `Id` FROM `NetworkMangleSubgroup` WHERE `AddressRange`="'.$AddressRange.'"');
24 if ($DbResult->num_rows > 0)
25 {
26 $DbRow = $DbResult->fetch_assoc();
27 return $DbRow['Id'];
28 } else
29 {
30 $DbResult = $Database->query('INSERT INTO `NetworkMangleSubgroup` (`AddressRange`) VALUES ("'.$AddressRange.'")');
31 return $Database->insert_id;
32 }
33}
34
35function InsertToAddressTreeIPv4(array &$Tree, NetworkAddressIPv4 $Address, string $Name, bool $InterSubnets = false, bool $ForceMark = false)
36{
37 global $Config;
38
39 $Found = false;
40 foreach ($Tree['Items'] as $Index => $Node)
41 {
42 if ($Node['Address']->Contain($Address))
43 {
44 InsertToAddressTreeIPv4($Tree['Items'][$Index], $Address, $Name, true);
45 $Found = true;
46 }
47 }
48 if ($Found == false)
49 {
50 if ($InterSubnets and ($Tree['Address']->Prefix < $Config['MainRouter']['MangleRuleSubgroupMinPrefix']) and
51 ($Address->Prefix > ($Tree['Address']->Prefix + 1)))
52 {
53 $NewAddress = new NetworkAddressIPv4();
54 $NewAddress->Address = $Address->Address;
55 $NewAddress->ChangePrefix($Tree['Address']->Prefix + 1);
56 $Tree['Items'][] = array('Address' => $NewAddress, 'Name' => $Name, 'Items' => array(), 'ForceMark' => false);
57 InsertToAddressTreeIPv4($Tree['Items'][count($Tree['Items']) - 1], $Address, $Name, true);
58 } else
59 {
60 $NewNode = array('Address' => $Address, 'Name' => $Name, 'Items' => array(), 'ForceMark' => $ForceMark);
61
62 // Should be existed items placed under new node?
63 $Found = false;
64 foreach ($Tree['Items'] as $Index => $Node)
65 {
66 if (($Node['Address']->Address == $NewNode['Address']->Address) and
67 ($Node['Address']->Prefix == $NewNode['Address']->Prefix)) $Found = true;
68
69 if ($Address->Contain($Node['Address']))
70 {
71 $NewNode['Items'][] = $Node;
72 unset($Tree['Items'][$Index]);
73 }
74 }
75 if ($Found == false) $Tree['Items'][] = $NewNode;
76 }
77 }
78}
79
80function InsertToAddressTreeIPv6(array &$Tree, NetworkAddressIPv6 $Address, string $Name, bool $InterSubnets = false, bool $ForceMark = false)
81{
82 global $Config;
83
84 $Found = false;
85 foreach ($Tree['Items'] as $Index => $Node)
86 {
87 if ($Node['Address']->Contain($Address))
88 {
89 InsertToAddressTreeIPv6($Tree['Items'][$Index], $Address, $Name, true);
90 $Found = true;
91 }
92 }
93 if ($Found == false)
94 {
95 if ($InterSubnets and ($Tree['Address']->Prefix < $Config['MainRouter']['MangleRuleSubgroupMinPrefix']) and
96 ($Address->Prefix > ($Tree['Address']->Prefix + 1)))
97 {
98 $NewAddress = new NetworkAddressIPv6();
99 $NewAddress->Address = $Address->Address;
100 $NewAddress->ChangePrefix($Tree['Address']->Prefix + 1);
101 $Tree['Items'][] = array('Address' => $NewAddress, 'Name' => $Name, 'Items' => array(), 'ForceMark' => false);
102 InsertToAddressTreeIPv6($Tree['Items'][count($Tree['Items']) - 1], $Address, $Name, true);
103 } else
104 {
105 $NewNode = array('Address' => $Address, 'Name' => $Name, 'Items' => array(), 'ForceMark' => $ForceMark);
106
107 // Should be existed items placed under new node?
108 $Found = false;
109 foreach ($Tree['Items'] as $Index => $Node)
110 {
111 if (($Node['Address']->Address == $NewNode['Address']->Address) and
112 ($Node['Address']->Prefix == $NewNode['Address']->Prefix)) $Found = true;
113
114 if ($Address->Contain($Node['Address']))
115 {
116 $NewNode['Items'][] = $Node;
117 unset($Tree['Items'][$Index]);
118 }
119 }
120 if ($Found == false) $Tree['Items'][] = $NewNode;
121 }
122 }
123}
124
125function ShowSubnetNode(array $Node, int $Indent = 0): void
126{
127 echo(str_repeat(' ', $Indent).$Node['Address']->AddressToString().'/'.$Node['Address']->Prefix.' '.$Node['Name']."\n");
128 foreach ($Node['Items'] as $Index => $Item)
129 {
130 ShowSubnetNode($Item, $Indent + 1);
131 }
132}
Note: See TracBrowser for help on using the repository browser.