| Conditions | 11 |
| Paths | 2 |
| Total Lines | 51 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 115 | private static function getForwardChain(Pools $p, $inetFamily, $disableForward) |
||
| 116 | { |
||
| 117 | $forwardChain = [ |
||
| 118 | '-A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT', |
||
| 119 | ]; |
||
| 120 | |||
| 121 | if (!$disableForward) { |
||
| 122 | foreach ($p as $pool) { |
||
| 123 | if (4 === $inetFamily) { |
||
| 124 | // get the IPv4 range |
||
| 125 | $srcNet = $pool->getRange()->getAddressPrefix(); |
||
| 126 | } else { |
||
| 127 | // get the IPv6 range |
||
| 128 | $srcNet = $pool->getRange6()->getAddressPrefix(); |
||
| 129 | } |
||
| 130 | $forwardChain[] = sprintf('-N vpn-%s', $pool->getId()); |
||
| 131 | $forwardChain[] = sprintf('-A FORWARD -i tun-%s+ -s %s -j vpn-%s', $pool->getId(), $srcNet, $pool->getId()); |
||
| 132 | if ($pool->getClientToClient()) { |
||
| 133 | // allow client-to-client |
||
| 134 | $forwardChain[] = sprintf('-A vpn-%s -o tun-%s+ -d %s -j ACCEPT', $pool->getId(), $pool->getId(), $srcNet); |
||
| 135 | } |
||
| 136 | if ($pool->getDefaultGateway()) { |
||
| 137 | // allow all traffic to the external interface |
||
| 138 | |||
| 139 | // drop netbios |
||
| 140 | // @see https://medium.com/@ValdikSS/deanonymizing-windows-users-and-capturing-microsoft-and-vpn-accounts-f7e53fe73834 |
||
| 141 | foreach (['tcp', 'udp'] as $proto) { |
||
| 142 | $forwardChain[] = sprintf( |
||
| 143 | '-A vpn-%s -o %s -m multiport -p %s --dports 137:139,445 -j REJECT --reject-with %s', |
||
| 144 | $pool->getId(), |
||
| 145 | $pool->getExtIf(), |
||
| 146 | $proto, |
||
| 147 | 4 === $inetFamily ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited'); |
||
| 148 | } |
||
| 149 | |||
| 150 | $forwardChain[] = sprintf('-A vpn-%s -o %s -j ACCEPT', $pool->getId(), $pool->getExtIf(), $srcNet); |
||
| 151 | } else { |
||
| 152 | // only allow certain traffic to the external interface |
||
| 153 | foreach ($pool->getRoutes() as $route) { |
||
| 154 | if ($inetFamily === $route->getFamily()) { |
||
| 155 | $forwardChain[] = sprintf('-A vpn-%s -o %s -d %s -j ACCEPT', $pool->getId(), $pool->getExtIf(), $route->getAddressPrefix()); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | $forwardChain[] = sprintf('-A FORWARD -j REJECT --reject-with %s', 4 === $inetFamily ? 'icmp-host-prohibited' : 'icmp6-adm-prohibited'); |
||
| 163 | |||
| 164 | return $forwardChain; |
||
| 165 | } |
||
| 166 | |||
| 187 |