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 ( 5e22a5...75d35a )
by François
03:03
created

ServerConfig::getFixMtu()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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