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 ( 0f5b77...f26127 )
by François
05:01
created

ServerConfig::getConfig()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 85
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 85
rs 8.3367
cc 5
eloc 47
nc 3
nop 1

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
18
namespace fkooman\VPN\Server;
19
20
class ServerConfig
21
{
22
    public static function getConfig(Pools $pools)
23
    {
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"';
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...
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()),
0 ignored issues
show
Bug introduced by
The method getAddress cannot be called on $pool->getListen() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
206
        ];
207
    }
208
209
    private static function getProto(Pool $pool, Instance $instance)
210
    {
211
        if (6 === $pool->getListen()->getFamily()) {
0 ignored issues
show
Bug introduced by
The method getFamily cannot be called on $pool->getListen() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
212
            if ('tcp' === $instance->getProto()) {
213
                $proto = 'tcp6-server';
214
            } else {
215
                $proto = 'udp6';
216
            }
217
        } else {
218
            if ('tcp' === $instance->getProto()) {
219
                $proto = 'tcp-server';
220
            } else {
221
                $proto = 'udp';
222
            }
223
        }
224
225
        return [
226
            sprintf('proto %s', $proto),
227
        ];
228
    }
229
}
230