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