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 ( 0a4855...ab7b77 )
by François
02:45 queued 12s
created

ServerConfig   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 23
Bugs 2 Features 3
Metric Value
wmc 27
c 23
b 2
f 3
lcom 1
cbo 3
dl 0
loc 227
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
B getConfig() 0 88 5
B getRoutes() 0 36 4
A getDns() 0 17 3
A getOtp() 0 8 2
A getLog() 0 8 2
A getClientToClient() 0 12 2
A getTcpOptions() 0 10 2
A getListen() 0 14 2
B getProto() 0 23 5
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
                // Log
100
                $serverConfig = array_merge($serverConfig, self::getLog($pool));
101
102
                sort($serverConfig, SORT_STRING);
103
104
                $allConfig[sprintf('%s-%d', $pool->getId(), $i)] = $serverConfig;
105
            }
106
        }
107
108
        return $allConfig;
109
    }
110
111
    private static function getRoutes(Pool $pool)
112
    {
113
        $routeConfig = [];
114
        if ($pool->getDefaultGateway()) {
115
            $routeConfig[] = 'push "redirect-gateway def1 bypass-dhcp"';
116
117
            # for Windows clients we need this extra route to mark the TAP adapter as 
118
            # trusted and as having "Internet" access to allow the user to set it to 
119
            # "Home" or "Work" to allow accessing file shares and printers
120
            # NOTE: this will break OS X tunnelblick because on disconnect it will
121
            # remove all default routes, including the one set before the VPN 
122
            # was brought up
123
            #$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...
124
125
            # for iOS we need this OpenVPN 2.4 "ipv6" flag to redirect-gateway
126
            # See https://docs.openvpn.net/docs/openvpn-connect/openvpn-connect-ios-faq.html
127
            $routeConfig[] = 'push "redirect-gateway ipv6"';
128
129
            # we use 2000::/3 instead of ::/0 because it seems to break on native IPv6 
130
            # networks where the ::/0 default route already exists
131
            $routeConfig[] = 'push "route-ipv6 2000::/3"';
132
        } else {
133
            // there may be some routes specified, push those, and not the default 
134
            foreach ($pool->getRoutes() as $route) {
135
                if (6 === $route->getFamily()) {
136
                    // IPv6
137
                    $routeConfig[] = sprintf('push "route-ipv6 %s"', $route->getAddressPrefix());
138
                } else {
139
                    // IPv4
140
                    $routeConfig[] = sprintf('push "route %s %s"', $route->getAddress(), $route->getNetmask());
141
                }
142
            }
143
        }
144
145
        return $routeConfig;
146
    }
147
148
    private static function getDns(Pool $pool)
149
    {
150
        // only push DNS if we are the default route
151
        if (!$pool->getDefaultGateway()) {
152
            return [];
153
        }
154
155
        $dnsEntries = [];
156
        foreach ($pool->getDns() as $dnsAddress) {
157
            $dnsEntries[] = sprintf('push "dhcp-option DNS %s"', $dnsAddress->getAddress());
158
        }
159
160
        # prevent DNS leakage on Windows
161
        $dnsEntries[] = 'push "block-outside-dns"';
162
163
        return $dnsEntries;
164
    }
165
166
    private static function getOtp(Pool $pool)
167
    {
168
        if (!$pool->getTwoFactor()) {
169
            return [];
170
        }
171
172
        return ['auth-user-pass-verify /usr/bin/vpn-server-api-verify-otp via-env'];
173
    }
174
175
    private static function getLog(Pool $pool)
176
    {
177
        if ($pool->getEnableLog()) {
178
            return [];
179
        }
180
181
        return ['log /dev/null'];
182
    }
183
184
    private static function getClientToClient(Pool $pool)
185
    {
186
        if (!$pool->getClientToClient()) {
187
            return [];
188
        }
189
190
        return [
191
            'client-to-client',
192
            sprintf('push "route %s %s"', $pool->getRange()->getAddress(), $pool->getRange()->getNetmask()),
193
            sprintf('push "route-ipv6 %s"', $pool->getRange6()->getAddressPrefix()),
194
        ];
195
    }
196
197
    private static function getTcpOptions(Instance $instance)
198
    {
199
        if ('tcp' !== $instance->getProto()) {
200
            return [];
201
        }
202
203
        return [
204
            'tcp-nodelay',
205
        ];
206
    }
207
208
    private static function getListen(Pool $pool, Instance $instance)
209
    {
210
        // TCP instance always listens on management IP as sniproxy
211
        // will redirect traffic there
212
        if ('tcp' === $instance->getProto()) {
213
            return [
214
                sprintf('local %s', $pool->getManagementIp()->getAddress()),
215
            ];
216
        }
217
218
        return [
219
            sprintf('local %s', $pool->getListen()->getAddress()),
220
        ];
221
    }
222
223
    private static function getProto(Pool $pool, Instance $instance)
224
    {
225
        if ('tcp' === $instance->getProto()) {
226
            // tcp
227
            if (4 === $pool->getListen()->getFamily() || '::' === $pool->getListen()->getAddress()) {
228
                // this is the default, so we listen on IPv4
229
                $proto = 'tcp-server';
230
            } else {
231
                $proto = 'tcp6-server';
232
            }
233
        } else {
234
            // udp
235
            if (6 === $pool->getListen()->getFamily()) {
236
                $proto = 'udp6';
237
            } else {
238
                $proto = 'udp';
239
            }
240
        }
241
242
        return [
243
            sprintf('proto %s', $proto),
244
        ];
245
    }
246
}
247