|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace SURFnet\VPN\Server\Api; |
|
19
|
|
|
|
|
20
|
|
|
use PDO; |
|
21
|
|
|
use PDOException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Keep track of VPN connections. |
|
25
|
|
|
*/ |
|
26
|
|
|
class ConnectionLog |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var PDO */ |
|
29
|
|
|
private $db; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(PDO $db) |
|
32
|
|
|
{ |
|
33
|
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
34
|
|
|
$this->db = $db; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function connect($poolId, $commonName, $ip, $ip6, $connectedAt) |
|
38
|
|
|
{ |
|
39
|
|
|
$stmt = $this->db->prepare( |
|
40
|
|
|
'INSERT INTO connection_log ( |
|
41
|
|
|
pool_id, |
|
42
|
|
|
common_name, |
|
43
|
|
|
ip, |
|
44
|
|
|
ip6, |
|
45
|
|
|
connected_at |
|
46
|
|
|
) |
|
47
|
|
|
VALUES( |
|
48
|
|
|
:pool_id, |
|
49
|
|
|
:common_name, |
|
50
|
|
|
:ip, |
|
51
|
|
|
:ip6, |
|
52
|
|
|
:connected_at |
|
53
|
|
|
)' |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
$stmt->bindValue(':pool_id', $poolId, PDO::PARAM_STR); |
|
57
|
|
|
$stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR); |
|
58
|
|
|
$stmt->bindValue(':ip', $ip, PDO::PARAM_STR); |
|
59
|
|
|
$stmt->bindValue(':ip6', $ip6, PDO::PARAM_STR); |
|
60
|
|
|
$stmt->bindValue(':connected_at', $connectedAt, PDO::PARAM_INT); |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
$stmt->execute(); |
|
64
|
|
|
} catch (PDOException $e) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function disconnect($poolId, $commonName, $ip, $ip6, $connectedAt, $disconnectedAt, $bytesTransferred) |
|
72
|
|
|
{ |
|
73
|
|
|
$stmt = $this->db->prepare( |
|
74
|
|
|
'UPDATE connection_log |
|
75
|
|
|
SET |
|
76
|
|
|
disconnected_at = :disconnected_at, |
|
77
|
|
|
bytes_transferred = :bytes_transferred |
|
78
|
|
|
WHERE |
|
79
|
|
|
pool_id = :pool_id AND |
|
80
|
|
|
common_name = :common_name AND |
|
81
|
|
|
ip = :ip AND |
|
82
|
|
|
ip6 = :ip6 AND |
|
83
|
|
|
connected_at = :connected_at |
|
84
|
|
|
' |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
$stmt->bindValue(':pool_id', $poolId, PDO::PARAM_STR); |
|
88
|
|
|
$stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR); |
|
89
|
|
|
$stmt->bindValue(':ip', $ip, PDO::PARAM_STR); |
|
90
|
|
|
$stmt->bindValue(':ip6', $ip6, PDO::PARAM_STR); |
|
91
|
|
|
$stmt->bindValue(':connected_at', $connectedAt, PDO::PARAM_INT); |
|
92
|
|
|
$stmt->bindValue(':disconnected_at', $disconnectedAt, PDO::PARAM_INT); |
|
93
|
|
|
$stmt->bindValue(':bytes_transferred', $bytesTransferred, PDO::PARAM_INT); |
|
94
|
|
|
|
|
95
|
|
|
// XXX number of affected rows should be one! |
|
96
|
|
|
try { |
|
97
|
|
|
$stmt->execute(); |
|
98
|
|
|
} catch (PDOException $e) { |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function init() |
|
106
|
|
|
{ |
|
107
|
|
|
$queryList = [ |
|
108
|
|
|
'CREATE TABLE IF NOT EXISTS connection_log ( |
|
109
|
|
|
pool_id VARCHAR(255) NOT NULL, |
|
110
|
|
|
common_name VARCHAR(255) NOT NULL, |
|
111
|
|
|
ip VARCHAR(255) NOT NULL, |
|
112
|
|
|
ip6 VARCHAR(255) NOT NULL, |
|
113
|
|
|
connected_at INTEGER NOT NULL, |
|
114
|
|
|
disconnected_at INTEGER DEFAULT NULL, |
|
115
|
|
|
bytes_transferred INTEGER DEFAULT NULL |
|
116
|
|
|
)', |
|
117
|
|
|
]; |
|
118
|
|
|
|
|
119
|
|
|
foreach ($queryList as $query) { |
|
120
|
|
|
$this->db->query($query); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|