| Conditions | 13 |
| Paths | 65 |
| Total Lines | 185 |
| Code Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 24 | $allConfig = []; |
||
| 25 | |||
| 26 | foreach ($pools as $pool) { |
||
| 27 | foreach ($pool->getInstances() as $i => $instance) { |
||
| 28 | // static options |
||
| 29 | $serverConfig = [ |
||
| 30 | '# OpenVPN Server Configuration', |
||
| 31 | 'verb 3', |
||
| 32 | 'user openvpn', |
||
| 33 | 'group openvpn', |
||
| 34 | 'topology subnet', |
||
| 35 | 'persist-key', |
||
| 36 | 'persist-tun', |
||
| 37 | 'keepalive 10 60', |
||
| 38 | 'comp-lzo no', |
||
| 39 | 'remote-cert-tls client', |
||
| 40 | 'tls-version-min 1.2', |
||
| 41 | 'tls-cipher TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA', |
||
| 42 | 'auth SHA256', |
||
| 43 | 'cipher AES-256-CBC', |
||
| 44 | 'ca /etc/openvpn/tls/ca.crt', |
||
| 45 | 'cert /etc/openvpn/tls/server.crt', |
||
| 46 | 'key /etc/openvpn/tls/server.key', |
||
| 47 | 'dh /etc/openvpn/tls/dh.pem', |
||
| 48 | 'tls-auth /etc/openvpn/tls/ta.key 0', |
||
| 49 | 'crl-verify /var/lib/vpn-server-api/ca.crl', |
||
| 50 | 'client-connect /usr/bin/vpn-server-api-client-connect', |
||
| 51 | 'client-disconnect /usr/bin/vpn-server-api-client-disconnect', |
||
| 52 | 'push "comp-lzo no"', |
||
| 53 | 'push "explicit-exit-notify 3"', |
||
| 54 | ]; |
||
| 55 | |||
| 56 | // Routes |
||
| 57 | $serverConfig = array_merge($serverConfig, self::getRoutes($pool)); |
||
| 58 | |||
| 59 | // DNS |
||
| 60 | $serverConfig = array_merge($serverConfig, self::getDns($pool)); |
||
| 61 | |||
| 62 | // Client-to-client |
||
| 63 | $serverConfig = array_merge($serverConfig, self::getClientToClient($pool)); |
||
| 64 | |||
| 65 | // OTP |
||
| 66 | $serverConfig = array_merge($serverConfig, self::getOtp($pool)); |
||
| 67 | |||
| 68 | // IP configuration |
||
| 69 | $serverConfig[] = sprintf('server %s %s', $instance->getRange()->getNetwork(), $instance->getRange()->getNetmask()); |
||
| 70 | $serverConfig[] = sprintf('server-ipv6 %s', $instance->getRange6()); |
||
| 71 | $serverConfig[] = sprintf('max-clients %d', $instance->getRange()->getNumberOfHosts() - 1); |
||
| 72 | |||
| 73 | // TCP options |
||
| 74 | $serverConfig = array_merge($serverConfig, self::getTcpOptions($instance)); |
||
| 75 | |||
| 76 | // Script Security |
||
| 77 | $serverConfig[] = sprintf('script-security %d', $pool->getTwoFactor() ? 3 : 2); |
||
| 78 | |||
| 79 | # increase the renegotiation time to 8h from the default of 1h when |
||
| 80 | # using 2FA, otherwise the user will be asked for the 2FA key every |
||
| 81 | # hour |
||
| 82 | $serverConfig[] = sprintf('reneg-sec %d', $pool->getTwoFactor() ? 28800 : 3600); |
||
| 83 | |||
| 84 | // Management |
||
| 85 | $serverConfig[] = sprintf('management %s %d', $pool->getManagementIp()->getAddress(), $instance->getManagementPort()); |
||
| 86 | |||
| 87 | // Listen |
||
| 88 | $serverConfig = array_merge($serverConfig, self::getListen($pool, $instance)); |
||
| 89 | |||
| 90 | // Dev |
||
| 91 | $serverConfig[] = sprintf('dev %s', $instance->getDev()); |
||
| 92 | |||
| 93 | // Proto |
||
| 94 | $serverConfig = array_merge($serverConfig, self::getProto($pool, $instance)); |
||
| 95 | |||
| 96 | // Port |
||
| 97 | $serverConfig[] = sprintf('port %d', $instance->getPort()); |
||
| 98 | |||
| 99 | sort($serverConfig, SORT_STRING); |
||
| 100 | |||
| 101 | $allConfig[sprintf('%s-%d', $pool->getId(), $i)] = $serverConfig; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return $allConfig; |
||
| 106 | } |
||
| 107 | |||
| 108 | private static function getRoutes(Pool $pool) |
||
| 109 | { |
||
| 110 | $routeConfig = []; |
||
| 111 | if ($pool->getDefaultGateway()) { |
||
| 112 | $routeConfig[] = 'push "redirect-gateway def1 bypass-dhcp"'; |
||
| 113 | |||
| 114 | # for Windows clients we need this extra route to mark the TAP adapter as |
||
| 115 | # trusted and as having "Internet" access to allow the user to set it to |
||
| 116 | # "Home" or "Work" to allow accessing file shares and printers |
||
| 117 | # NOTE: this will break OS X tunnelblick because on disconnect it will |
||
| 118 | # remove all default routes, including the one set before the VPN |
||
| 119 | # was brought up |
||
| 120 | #$routeConfig[] = 'push "route 0.0.0.0 0.0.0.0"'; |
||
|
|
|||
| 121 | |||
| 122 | # for iOS we need this OpenVPN 2.4 "ipv6" flag to redirect-gateway |
||
| 123 | # See https://docs.openvpn.net/docs/openvpn-connect/openvpn-connect-ios-faq.html |
||
| 124 | $routeConfig[] = 'push "redirect-gateway ipv6"'; |
||
| 125 | |||
| 126 | # we use 2000::/3 instead of ::/0 because it seems to break on native IPv6 |
||
| 127 | # networks where the ::/0 default route already exists |
||
| 128 | $routeConfig[] = 'push "route-ipv6 2000::/3"'; |
||
| 129 | } else { |
||
| 130 | // there may be some routes specified, push those, and not the default |
||
| 131 | foreach ($pool->getRoutes() as $route) { |
||
| 132 | if (6 === $route->getFamily()) { |
||
| 133 | // IPv6 |
||
| 134 | $routeConfig[] = sprintf('push "route-ipv6 %s"', $route->getAddressPrefix()); |
||
| 135 | } else { |
||
| 136 | // IPv4 |
||
| 137 | $routeConfig[] = sprintf('push "route %s %s"', $route->getAddress(), $route->getNetmask()); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | return $routeConfig; |
||
| 143 | } |
||
| 144 | |||
| 145 | private static function getDns(Pool $pool) |
||
| 146 | { |
||
| 147 | // only push DNS if we are the default route |
||
| 148 | if (!$pool->getDefaultGateway()) { |
||
| 149 | return []; |
||
| 150 | } |
||
| 151 | |||
| 152 | $dnsEntries = []; |
||
| 153 | foreach ($pool->getDns() as $dnsAddress) { |
||
| 154 | $dnsEntries[] = sprintf('push "dhcp-option DNS %s"', $dnsAddress->getAddress()); |
||
| 155 | } |
||
| 156 | |||
| 157 | return $dnsEntries; |
||
| 158 | } |
||
| 159 | |||
| 160 | private static function getOtp(Pool $pool) |
||
| 161 | { |
||
| 162 | if (!$pool->getTwoFactor()) { |
||
| 163 | return []; |
||
| 164 | } |
||
| 165 | |||
| 166 | return ['auth-user-pass-verify /usr/bin/vpn-server-api-verify-otp via-env']; |
||
| 167 | } |
||
| 168 | |||
| 169 | private static function getClientToClient(Pool $pool) |
||
| 170 | { |
||
| 171 | if (!$pool->getClientToClient()) { |
||
| 172 | return []; |
||
| 173 | } |
||
| 174 | |||
| 175 | return [ |
||
| 176 | 'client-to-client', |
||
| 177 | sprintf('push "route %s %s"', $pool->getRange()->getAddress(), $pool->getRange()->getNetmask()), |
||
| 178 | sprintf('push "route-ipv6 %s"', $pool->getRange6()->getAddressPrefix()), |
||
| 179 | ]; |
||
| 180 | } |
||
| 181 | |||
| 182 | private static function getTcpOptions(Instance $instance) |
||
| 183 | { |
||
| 184 | if ('tcp' !== $instance->getProto()) { |
||
| 185 | return []; |
||
| 186 | } |
||
| 187 | |||
| 188 | return [ |
||
| 189 | 'socket-flags TCP_NODELAY', |
||
| 190 | 'push "socket-flags TCP_NODELAY"', |
||
| 191 | ]; |
||
| 192 | } |
||
| 193 | |||
| 194 | private static function getListen(Pool $pool, Instance $instance) |
||
| 195 | { |
||
| 196 | // TCP instance always listens on management IP as sniproxy |
||
| 197 | // will redirect traffic there |
||
| 198 | if ('tcp' === $instance->getProto()) { |
||
| 199 | return [ |
||
| 200 | sprintf('local %s', $pool->getManagementIp()->getAddress()), |
||
| 201 | ]; |
||
| 202 | } |
||
| 203 | |||
| 204 | return [ |
||
| 205 | sprintf('local %s', $pool->getListen()->getAddress()), |
||
| 206 | ]; |
||
| 207 | } |
||
| 208 | |||
| 209 | private static function getProto(Pool $pool, Instance $instance) |
||
| 230 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.