~fkooman/vpn-server-api

ref: 453cda58bae0131116fbab4e4fc686d867e87ade vpn-server-api/src/OpenVpn/ServerManager.php -rw-r--r-- 3.7 KiB
453cda58François Kooman allow configuring the log format for log entries to syslog, also allow logging originating client IP 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

/*
 * eduVPN - End-user friendly VPN.
 *
 * Copyright: 2016-2019, The Commons Conservancy eduVPN Programme
 * SPDX-License-Identifier: AGPL-3.0+
 */

namespace LC\Server\OpenVpn;

use LC\Common\Config;
use LC\Common\ProfileConfig;
use LC\OpenVpn\ConnectionManager;
use LC\OpenVpn\ManagementSocketInterface;
use Psr\Log\LoggerInterface;
use RangeException;

/**
 * Manage all OpenVPN processes controlled by this service.
 */
class ServerManager
{
    /** @var \LC\Common\Config */
    private $config;

    /** @var \Psr\Log\LoggerInterface */
    private $logger;

    /** @var \LC\OpenVpn\ManagementSocketInterface */
    private $managementSocket;

    public function __construct(Config $config, LoggerInterface $logger, ManagementSocketInterface $managementSocket)
    {
        $this->config = $config;
        $this->logger = $logger;
        $this->managementSocket = $managementSocket;
    }

    /**
     * @return array
     */
    public function connections()
    {
        $clientConnections = [];

        // loop over all profiles
        foreach ($this->config->requireArray('vpnProfiles') as $profileId => $profileData) {
            $profileConfig = new ProfileConfig(new Config($profileData));
            $managementIp = $profileConfig->managementIp();
            $profileNumber = $profileConfig->profileNumber();

            $profileConnections = [];
            $socketAddressList = [];
            for ($i = 0; $i < \count($profileConfig->vpnProtoPorts()); ++$i) {
                $socketAddressList[] = sprintf(
                    'tcp://%s:%d',
                    $managementIp,
                    11940 + self::toPort($profileNumber, $i)
                );
            }

            $connectionManager = new ConnectionManager($socketAddressList, $this->logger, $this->managementSocket);
            $profileConnections += $connectionManager->connections();
            $clientConnections[] = ['id' => $profileId, 'connections' => $profileConnections];
        }

        return $clientConnections;
    }

    /**
     * @param string $commonName
     *
     * @return int
     */
    public function kill($commonName)
    {
        $socketAddressList = [];

        // loop over all profiles
        foreach ($this->config->requireArray('vpnProfiles') as $profileData) {
            $profileConfig = new ProfileConfig(new Config($profileData));
            $managementIp = $profileConfig->managementIp();
            $profileNumber = $profileConfig->profileNumber();
            for ($i = 0; $i < \count($profileConfig->vpnProtoPorts()); ++$i) {
                $socketAddressList[] = sprintf(
                    'tcp://%s:%d',
                    $managementIp,
                    11940 + self::toPort($profileNumber, $i)
                );
            }
        }

        $connectionManager = new ConnectionManager($socketAddressList, $this->logger, $this->managementSocket);

        return $connectionManager->disconnect([$commonName]);
    }

    /**
     * @param int $profileNumber
     * @param int $processNumber
     *
     * @return int
     */
    private static function toPort($profileNumber, $processNumber)
    {
        if (1 > $profileNumber || 64 < $profileNumber) {
            throw new RangeException('1 <= profileNumber <= 64');
        }

        if (0 > $processNumber || 64 <= $processNumber) {
            throw new RangeException('0 <= processNumber < 64');
        }

        // we have 2^16 - 11940 ports available for management ports, so let's
        // say we have 2^14 ports available to distribute over profiles and
        // processes, let's take 12 bits, so we have 64 profiles with each 64
        // processes...
        return ($profileNumber - 1 << 6) | $processNumber;
    }
}