GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 96f4c1...39b1a2 )
by François
02:46
created

ServerConfig::getConfig()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 101
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 3 Features 4
Metric Value
c 10
b 3
f 4
dl 0
loc 101
rs 6.4589
cc 7
eloc 53
nc 8
nop 2

How to fix   Long Method   

Long Method

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:

1
<?php
2
/**
3
 * Copyright 2016 François Kooman <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
namespace fkooman\VPN\Server;
18
19
class ServerConfig
20
{
21
    /**
22
     * Get the server configuration.
23
     *
24
     * @param string|null $instanceId the instance ID, may be null in case we
25
     *                                run in single instance configuration
26
     * @param Pools       $pools      the VPN pools for this instance
27
     */
28
    public static function getConfig($instanceId, Pools $pools)
29
    {
30
        $allConfig = [];
31
32
        $tlsDir = '/etc/openvpn/tls';
33
        if (!is_null($instanceId)) {
34
            $tlsDir = sprintf('%s/%s', $tlsDir, $instanceId);
35
        }
36
37
        foreach ($pools as $pool) {
38
            foreach ($pool->getInstances() as $i => $instance) {
39
                // static options
40
                $serverConfig = [
41
                    '# OpenVPN Server Configuration',
42
                    'verb 3',
43
                    'user openvpn',
44
                    'group openvpn',
45
                    'topology subnet',
46
                    'persist-key',
47
                    'persist-tun',
48
                    'keepalive 10 60',
49
                    'comp-lzo no',
50
                    'remote-cert-tls client',
51
                    'tls-version-min 1.2',
52
                    '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',
53
                    'auth SHA256',
54
                    'cipher AES-256-CBC',
55
                    sprintf('ca %s/ca.crt', $tlsDir),
56
                    sprintf('cert %s/server.crt', $tlsDir),
57
                    sprintf('key %s/server.key', $tlsDir),
58
                    sprintf('dh %s/dh.pem', $tlsDir),
59
                    sprintf('tls-auth %s/ta.key 0', $tlsDir),
60
                    'client-connect /usr/bin/vpn-server-api-client-connect',
61
                    'client-disconnect /usr/bin/vpn-server-api-client-disconnect',
62
                    'push "comp-lzo no"',
63
                    'push "explicit-exit-notify 3"',
64
                ];
65
66
                // Routes
67
                $serverConfig = array_merge($serverConfig, self::getRoutes($pool));
68
69
                // DNS
70
                $serverConfig = array_merge($serverConfig, self::getDns($pool));
71
72
                // Client-to-client
73
                $serverConfig = array_merge($serverConfig, self::getClientToClient($pool));
74
75
                // OTP
76
                $serverConfig = array_merge($serverConfig, self::getOtp($pool));
77
78
                // IP configuration
79
                $serverConfig[] = sprintf('server %s %s', $instance->getRange()->getNetwork(), $instance->getRange()->getNetmask());
80
                $serverConfig[] = sprintf('server-ipv6 %s', $instance->getRange6());
81
                $serverConfig[] = sprintf('max-clients %d', $instance->getRange()->getNumberOfHosts() - 1);
82
83
                // TCP options
84
                $serverConfig = array_merge($serverConfig, self::getTcpOptions($instance));
85
86
                // Script Security
87
                $serverConfig[] = sprintf('script-security %d', $pool->getTwoFactor() ? 3 : 2);
88
89
                // increase the renegotiation time to 8h from the default of 1h when
90
                // using 2FA, otherwise the user will be asked for the 2FA key every
91
                // hour
92
                $serverConfig[] = sprintf('reneg-sec %d', $pool->getTwoFactor() ? 28800 : 3600);
93
94
                // Management
95
                $serverConfig[] = sprintf('management %s %d', $pool->getManagementIp()->getAddress(), $instance->getManagementPort());
96
97
                // Listen
98
                $serverConfig = array_merge($serverConfig, self::getListen($pool, $instance));
99
100
                // Dev
101
                $serverConfig[] = sprintf('dev %s', $instance->getDev());
102
103
                // Proto
104
                $serverConfig = array_merge($serverConfig, self::getProto($pool, $instance));
105
106
                // Port
107
                $serverConfig[] = sprintf('port %d', $instance->getPort());
108
109
                // Log
110
                $serverConfig = array_merge($serverConfig, self::getLog($pool));
111
112
                // Instance ID
113
                if (!is_null($instanceId)) {
114
                    // only in multi instance configuration
115
                    $serverConfig[] = sprintf('setenv INSTANCE_ID %s', $instanceId);
116
                }
117
118
                // Pool ID
119
                $serverConfig[] = sprintf('setenv POOL_ID %s', $pool->getId());
120
121
                sort($serverConfig, SORT_STRING);
122
123
                $allConfig[sprintf('%s-%d', $pool->getId(), $i)] = $serverConfig;
124
            }
125
        }
126
127
        return $allConfig;
128
    }
129
130
    private static function getRoutes(Pool $pool)
131
    {
132
        $routeConfig = [];
133
        if ($pool->getDefaultGateway()) {
134
            $routeConfig[] = 'push "redirect-gateway def1 bypass-dhcp"';
135
136
            // for Windows clients we need this extra route to mark the TAP adapter as
137
            // trusted and as having "Internet" access to allow the user to set it to
138
            // "Home" or "Work" to allow accessing file shares and printers
139
            // NOTE: this will break OS X tunnelblick because on disconnect it will
140
            // remove all default routes, including the one set before the VPN
141
            // was brought up
142
            //$routeConfig[] = 'push "route 0.0.0.0 0.0.0.0"';
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
143
144
            // for iOS we need this OpenVPN 2.4 "ipv6" flag to redirect-gateway
145
            // See https://docs.openvpn.net/docs/openvpn-connect/openvpn-connect-ios-faq.html
146
            $routeConfig[] = 'push "redirect-gateway ipv6"';
147
148
            // we use 2000::/3 instead of ::/0 because it seems to break on native IPv6
149
            // networks where the ::/0 default route already exists
150
            $routeConfig[] = 'push "route-ipv6 2000::/3"';
151
        } else {
152
            // there may be some routes specified, push those, and not the default
153
            foreach ($pool->getRoutes() as $route) {
154
                if (6 === $route->getFamily()) {
155
                    // IPv6
156
                    $routeConfig[] = sprintf('push "route-ipv6 %s"', $route->getAddressPrefix());
157
                } else {
158
                    // IPv4
159
                    $routeConfig[] = sprintf('push "route %s %s"', $route->getAddress(), $route->getNetmask());
160
                }
161
            }
162
        }
163
164
        return $routeConfig;
165
    }
166
167
    private static function getDns(Pool $pool)
168
    {
169
        // only push DNS if we are the default route
170
        if (!$pool->getDefaultGateway()) {
171
            return [];
172
        }
173
174
        $dnsEntries = [];
175
        foreach ($pool->getDns() as $dnsAddress) {
176
            $dnsEntries[] = sprintf('push "dhcp-option DNS %s"', $dnsAddress->getAddress());
177
        }
178
179
        // prevent DNS leakage on Windows
180
        $dnsEntries[] = 'push "block-outside-dns"';
181
182
        return $dnsEntries;
183
    }
184
185
    private static function getOtp(Pool $pool)
186
    {
187
        if (!$pool->getTwoFactor()) {
188
            return [];
189
        }
190
191
        return ['auth-user-pass-verify /usr/bin/vpn-server-api-verify-otp via-env'];
192
    }
193
194
    private static function getLog(Pool $pool)
195
    {
196
        if ($pool->getEnableLog()) {
197
            return [];
198
        }
199
200
        return ['log /dev/null'];
201
    }
202
203
    private static function getClientToClient(Pool $pool)
204
    {
205
        if (!$pool->getClientToClient()) {
206
            return [];
207
        }
208
209
        return [
210
            'client-to-client',
211
            sprintf('push "route %s %s"', $pool->getRange()->getAddress(), $pool->getRange()->getNetmask()),
212
            sprintf('push "route-ipv6 %s"', $pool->getRange6()->getAddressPrefix()),
213
        ];
214
    }
215
216
    private static function getTcpOptions(Instance $instance)
217
    {
218
        if ('tcp' !== $instance->getProto()) {
219
            return [];
220
        }
221
222
        return [
223
            'tcp-nodelay',
224
        ];
225
    }
226
227
    private static function getListen(Pool $pool, Instance $instance)
228
    {
229
        // TCP instance always listens on management IP as sniproxy
230
        // will redirect traffic there
231
        if ('tcp' === $instance->getProto()) {
232
            return [
233
                sprintf('local %s', $pool->getManagementIp()->getAddress()),
234
            ];
235
        }
236
237
        return [
238
            sprintf('local %s', $pool->getListen()->getAddress()),
239
        ];
240
    }
241
242
    private static function getProto(Pool $pool, Instance $instance)
243
    {
244
        if ('tcp' === $instance->getProto()) {
245
            // tcp
246
            if (4 === $pool->getListen()->getFamily() || '::' === $pool->getListen()->getAddress()) {
247
                // this is the default, so we listen on IPv4
248
                $proto = 'tcp-server';
249
            } else {
250
                $proto = 'tcp6-server';
251
            }
252
        } else {
253
            // udp
254
            if (6 === $pool->getListen()->getFamily()) {
255
                $proto = 'udp6';
256
            } else {
257
                $proto = 'udp';
258
            }
259
        }
260
261
        return [
262
            sprintf('proto %s', $proto),
263
        ];
264
    }
265
}
266