Repository navigation
Expand file tree
/
Copy pathtanguard_api.php
More file actions
170 lines (149 loc) · 5.34 KB
/
Copy pathtanguard_api.php
File metadata and controls
170 lines (149 loc) · 5.34 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* tanguard_api.php - PHP helper for the TunGuard userspace WireGuard server
*
* Calls the Go HTTP API instead of running `wg` shell commands.
*
* USAGE:
* require_once __DIR__ . '/tanguard_api.php';
*
* $api = new TunGuardAPI('http://127.0.0.1:9000', 'YOUR_API_KEY');
* $api->addPeer($pubKey, $allowedIP, $deviceId);
* $api->removePeer($pubKey);
* $api->getServerPublicKey();
*
* The API key can also be set via the TUNGARD_API_KEY environment variable.
* If no key is given, the dashboard login (HTTP Basic Auth) is required to
* call every endpoint except /api/health, so supply the key in production.
*/
class TunGuardAPI {
private string $baseUrl;
private string $apiKey;
public function __construct(string $apiUrl = 'http://127.0.0.1:9000', ?string $apiKey = null) {
$this->baseUrl = rtrim($apiUrl, '/');
$envKey = getenv('TUNGARD_API_KEY');
$this->apiKey = $apiKey ?? ($envKey !== false ? $envKey : '');
}
private function authHeader(): string {
if ($this->apiKey === '') return '';
return 'X-API-Key: ' . $this->apiKey . "\r\n";
}
private function get(string $path): ?array {
$url = $this->baseUrl . $path;
$ctx = stream_context_create([
'http' => [
'method' => 'GET',
'timeout' => 5,
'header' => "Content-Type: application/json\r\n" . $this->authHeader(),
],
]);
$resp = @file_get_contents($url, false, $ctx);
if ($resp === false) return null;
return json_decode($resp, true);
}
private function post(string $path, array $data = []): ?array {
$url = $this->baseUrl . $path;
$payload = json_encode($data);
$ctx = stream_context_create([
'http' => [
'method' => 'POST',
'timeout' => 5,
'header' => "Content-Type: application/json\r\n" . $this->authHeader(),
'content' => $payload,
],
]);
$resp = @file_get_contents($url, false, $ctx);
if ($resp === false) return null;
return json_decode($resp, true);
}
public function isHealthy(): bool {
$r = $this->get('/api/health');
return isset($r['status']) && $r['status'] === 'ok';
}
public function getServerPublicKey(): ?string {
$r = $this->get('/api/server_key');
if (!$r || !isset($r['public_key'])) return null;
return $r['public_key'];
}
public function getServerListenPort(): ?int {
$r = $this->get('/api/server_key');
if (!$r || !isset($r['listen_port'])) return null;
return intval($r['listen_port']);
}
public function addPeer(
string $publicKey,
string $allowedIP,
string $deviceId = '',
string $deviceName = '',
string $psk = ''
): bool {
$r = $this->post('/api/peer/add', [
'public_key' => $publicKey,
'allowed_ip' => $allowedIP,
'device_id' => $deviceId,
'device_name' => $deviceName,
'preshared_key' => $psk,
]);
return isset($r['success']) && $r['success'] === true;
}
public function removePeer(string $publicKey): bool {
$r = $this->post('/api/peer/remove', [
'public_key' => $publicKey,
]);
return isset($r['success']) && $r['success'] === true;
}
public function peerExists(string $publicKey): bool {
$status = $this->getStatus();
if (!$status || !isset($status['peers'])) return false;
foreach ($status['peers'] as $peer) {
if (($peer['public_key'] ?? '') === $publicKey) return true;
}
return false;
}
public function getPeerHandshake(string $publicKey): int {
$status = $this->getStatus();
if (!$status || !isset($status['peers'])) return 0;
foreach ($status['peers'] as $peer) {
if (($peer['public_key'] ?? '') === $publicKey) {
return intval($peer['latest_handshake'] ?? 0);
}
}
return 0;
}
public function hasRecentHandshake(string $publicKey, int $within = 180): bool {
$ts = $this->getPeerHandshake($publicKey);
if ($ts === 0) return false;
return (time() - $ts) < $within;
}
public function getStatus(): ?array {
return $this->get('/api/status');
}
public function getPeers(): array {
$r = $this->get('/api/peers');
return $r['peers'] ?? [];
}
}
function tanguard_addPeer(string $peerPubKey, string $allowedIP): bool {
$api = new TunGuardAPI();
return $api->addPeer($peerPubKey, "$allowedIP/32");
}
function tanguard_removePeer(string $peerPubKey): bool {
$api = new TunGuardAPI();
return $api->removePeer($peerPubKey);
}
function tanguard_peerExists(string $peerPubKey): bool {
$api = new TunGuardAPI();
return $api->peerExists($peerPubKey);
}
function tanguard_checkHandshake(string $peerPubKey, int $within = 180): bool {
$api = new TunGuardAPI();
return $api->hasRecentHandshake($peerPubKey, $within);
}
function tanguard_getServerPublicKey(): ?string {
$api = new TunGuardAPI();
return $api->getServerPublicKey();
}
function tanguard_getServerWgPort(): int {
$api = new TunGuardAPI();
return $api->getServerListenPort() ?? 13231;
}