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
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Talk\Recording;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ServerException;
use OCA\Talk\Config;
use OCA\Talk\Exceptions\RecordingNotFoundException;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCP\AppFramework\Http;
use OCP\Http\Client\IClientService;
use OCP\IURLGenerator;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
class BackendNotifier {
public function __construct(
private Config $config,
private LoggerInterface $logger,
private IClientService $clientService,
private ISecureRandom $secureRandom,
private IURLGenerator $urlGenerator,
) {
}
/**
* Perform actual network request to the recording backend.
* This can be overridden in tests.
*
* @param string $url
* @param array $params
* @param int $retries
* @throws \Exception
*/
protected function doRequest(string $url, array $params, int $retries = 3): void {
if (defined('PHPUNIT_RUN')) {
// Don't perform network requests when running tests.
return;
}
$client = $this->clientService->newClient();
try {
$response = $client->post($url, $params);
} catch (ServerException | ConnectException $e) {
if ($retries > 1) {
$this->logger->error('Failed to send message to recording server, ' . $retries . ' retries left!', ['exception' => $e]);
$this->doRequest($url, $params, $retries - 1);
} else {
$this->logger->error('Failed to send message to recording server, giving up!', ['exception' => $e]);
throw $e;
}
} catch (\Exception $e) {
$this->logger->error('Failed to send message to recording server', ['exception' => $e]);
throw $e;
}
}
/**
* Perform a request to the recording backend.
*
* @param Room $room
* @param array $data
* @throws \Exception
*/
private function backendRequest(Room $room, array $data): void {
$recordingServers = $this->config->getRecordingServers();
if (empty($recordingServers)) {
$this->logger->error('No configured recording server');
return;
}
// FIXME Currently clustering is not implemented in the recording
// server, so for now only the first configured server is taken into
// account to ensure that all the "stop" requests are sent to the same
// server that received the "start" request.
$recording = $recordingServers[0];
$recording['server'] = rtrim($recording['server'], '/');
$url = '/api/v1/room/' . $room->getToken();
$url = $recording['server'] . $url;
if (str_starts_with($url, 'wss://')) {
$url = 'https://' . substr($url, 6);
} elseif (str_starts_with($url, 'ws://')) {
$url = 'http://' . substr($url, 5);
}
$body = json_encode($data);
$headers = [
'Content-Type' => 'application/json',
];
$random = $this->secureRandom->generate(64);
$hash = hash_hmac('sha256', $random . $body, $this->config->getRecordingSecret());
$headers['Talk-Recording-Random'] = $random;
$headers['Talk-Recording-Checksum'] = $hash;
$headers['Talk-Recording-Backend'] = $this->urlGenerator->getAbsoluteURL('');
$params = [
'headers' => $headers,
'body' => $body,
'nextcloud' => [
'allow_local_address' => true,
],
];
if (empty($recording['verify'])) {
$params['verify'] = false;
}
$this->doRequest($url, $params);
}
public function start(Room $room, int $status, string $owner, Participant $participant): void {
$start = microtime(true);
$this->backendRequest($room, [
'type' => 'start',
'start' => [
'status' => $status,
'owner' => $owner,
'actor' => [
'type' => $participant->getAttendee()->getActorType(),
'id' => $participant->getAttendee()->getActorId(),
],
],
]);
$duration = microtime(true) - $start;
$this->logger->debug('Send start message: {token} ({duration})', [
'token' => $room->getToken(),
'duration' => sprintf('%.2f', $duration),
'app' => 'spreed-recording',
]);
}
public function stop(Room $room, ?Participant $participant = null): void {
$parameters = [];
if ($participant !== null) {
$parameters['actor'] = [
'type' => $participant->getAttendee()->getActorType(),
'id' => $participant->getAttendee()->getActorId(),
];
}
$start = microtime(true);
try {
$this->backendRequest($room, [
'type' => 'stop',
'stop' => $parameters,
]);
} catch (ClientException $e) {
if ($e->getResponse()->getStatusCode() === Http::STATUS_NOT_FOUND) {
throw new RecordingNotFoundException();
}
throw $e;
}
$duration = microtime(true) - $start;
$this->logger->debug('Send stop message: {token} ({duration})', [
'token' => $room->getToken(),
'duration' => sprintf('%.2f', $duration),
'app' => 'spreed-recording',
]);
}
}
|