Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 72 additions & 13 deletions lib/BackgroundJob/CheckHostedSignalingServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\Talk\BackgroundJob;

use OCA\Talk\Config;
use OCA\Talk\DataObjects\AccountId;
use OCA\Talk\Exceptions\HostedSignalingServerAPIException;
use OCA\Talk\Service\HostedSignalingServerService;
Expand All @@ -32,6 +33,7 @@ public function __construct(
private IGroupManager $groupManager,
private IURLGenerator $urlGenerator,
private LoggerInterface $logger,
private Config $talkConfig,
) {
parent::__construct($timeFactory);

Expand All @@ -41,6 +43,61 @@ public function __construct(

}

private function formatTurnSchemes(array $schemes): string {
if (in_array('turn', $schemes) && in_array('turns', $schemes)) {
return 'turn,turns';
} elseif (in_array('turn', $schemes)) {
return 'turn';
} elseif (in_array('turns', $schemes)) {
return 'turns';
} else {
return 'turn';
}
}

private function formatTurnProtocols(array $protocols): string {
if (in_array('udp', $protocols) && in_array('tcp', $protocols)) {
return 'udp,tcp';
} elseif (in_array('udp', $protocols)) {
return 'udp';
} elseif (in_array('tcp', $protocols)) {
return 'tcp';
} else {
return 'udp';
}
}

private function updateStunTurnSettings(array $oldAccountInfo, array $accountInfo) {
if ($this->talkConfig->getStunServers() !== $accountInfo['stun']['servers']) {
if ($accountInfo['stun']['servers']) {
// STUN servers were added / changed
$this->config->setAppValue('spreed', 'stun_servers', json_encode($accountInfo['stun']['servers']));
} elseif ($oldAccountInfo['stun']['servers']) {
// STUN servers are no longer available, reset to default.
$this->config->deleteAppValue('spreed', 'stun_servers');
}
}
if ($accountInfo['turn']['servers']) {
$newTurnServers = [];
foreach ($accountInfo['turn']['servers'] as $server) {
$newTurnServers[] = [
'server' => $server['server'],
'secret' => $server['secret'],
'schemes' => $this->formatTurnSchemes($server['schemes']),
'protocols' => $this->formatTurnProtocols($server['protocols']),
];
}

if ($this->talkConfig->getTurnServers() !== $newTurnServers) {
// TURN servers were added / changed
$this->config->setAppValue('spreed', 'turn_servers', json_encode($newTurnServers));
}
} elseif ($oldAccountInfo['turn']['servers']) {
// TURN servers are no longer available, reset to default.
$this->config->deleteAppValue('spreed', 'turn_servers');
}
}

#[\Override]
protected function run($argument): void {
$accountId = $this->config->getAppValue('spreed', 'hosted-signaling-server-account-id', '');
Expand Down Expand Up @@ -91,6 +148,7 @@ protected function run($argument): void {
],
'secret' => $accountInfo['signaling']['secret'],
]));
$this->updateStunTurnSettings($oldAccountInfo, $accountInfo);

$notificationSubject = 'added';
}
Expand All @@ -104,19 +162,20 @@ protected function run($argument): void {
}

// only credentials have changed
} elseif ($newStatus === 'active' && (
$oldAccountInfo['signaling']['url'] !== $accountInfo['signaling']['url']
|| $oldAccountInfo['signaling']['secret'] !== $accountInfo['signaling']['secret'])
) {
$this->config->setAppValue('spreed', 'signaling_servers', json_encode([
'servers' => [
[
'server' => $accountInfo['signaling']['url'],
'verify' => true,
]
],
'secret' => $accountInfo['signaling']['secret'],
]));
} elseif ($newStatus === 'active') {
if ($oldAccountInfo['signaling']['url'] !== $accountInfo['signaling']['url']
|| $oldAccountInfo['signaling']['secret'] !== $accountInfo['signaling']['secret']) {
$this->config->setAppValue('spreed', 'signaling_servers', json_encode([
'servers' => [
[
'server' => $accountInfo['signaling']['url'],
'verify' => true,
]
],
'secret' => $accountInfo['signaling']['secret'],
]));
}
$this->updateStunTurnSettings($oldAccountInfo, $accountInfo);
}

// store new account info
Expand Down
13 changes: 13 additions & 0 deletions src/components/AdminSettings/HostedSignalingServer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<p class="settings-hint">
{{ t('spreed', 'Our partner Struktur AG provides a service where a hosted signaling server can be requested. For this you only need to fill out the form below and your Nextcloud will request it. Once the server is set up for you the credentials will be filled automatically. This will overwrite the existing signaling server settings.') }}
{{ t('spreed', 'If your high-performance backend account includes STUN and/or TURN functionality, the settings will be updated accordingly.') }}
</p>

<template v-if="!trialAccount.status">
Expand Down Expand Up @@ -103,6 +104,18 @@
<td>{{ t('spreed', 'Limits') }}</td>
<td>{{ n('spreed', '%n user', '%n users', trialAccount.limits.users) }}</td>
</tr>
<tr>
<td>{{ t('spreed', 'STUN included') }}</td>
<td>
{{ trialAccount.stun?.servers ? t('spreed', 'Yes') : t('spreed', 'No') }}
</td>
</tr>
<tr>
<td>{{ t('spreed', 'TURN included') }}</td>
<td>
{{ trialAccount.turn?.servers ? t('spreed', 'Yes') : t('spreed', 'No') }}
</td>
</tr>
</tbody>
</table>
<p v-if="requestError !== ''"
Expand Down
80 changes: 79 additions & 1 deletion tests/php/BackgroundJob/CheckHostedSignalingServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OCA\Talk\Tests\BackgroundJob;

use OCA\Talk\BackgroundJob\CheckHostedSignalingServer;
use OCA\Talk\Config;
use OCA\Talk\Service\HostedSignalingServerService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
Expand All @@ -28,6 +29,7 @@ class CheckHostedSignalingServerTest extends TestCase {
protected IGroupManager&MockObject $groupManager;
protected IURLGenerator&MockObject $urlGenerator;
protected LoggerInterface&MockObject $logger;
protected Config&MockObject $talkConfig;

public function setUp(): void {
parent::setUp();
Expand All @@ -39,6 +41,7 @@ public function setUp(): void {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->talkConfig = $this->createMock(Config::class);
}

public function getBackgroundJob(): CheckHostedSignalingServer {
Expand All @@ -49,7 +52,8 @@ public function getBackgroundJob(): CheckHostedSignalingServer {
$this->notificationManager,
$this->groupManager,
$this->urlGenerator,
$this->logger
$this->logger,
$this->talkConfig
);
}

Expand Down Expand Up @@ -119,4 +123,78 @@ public function testRunWithPendingToActiveChange(): void {

self::invokePrivate($backgroundJob, 'run', ['']);
}

public function testRunWithPendingToActiveIncludingStunAndTurn(): void {
$backgroundJob = $this->getBackgroundJob();
$newStatus = [
'status' => 'active',
'signaling' => [
'url' => 'signaling-url',
'secret' => 'signaling-secret',
],
'stun' => [
'servers' => [
'stun.domain.invalid:443',
'stun.domain.invalid:3478',
],
],
'turn' => [
'servers' => [
[
'server' => 'turn1.domain.invalid:443',
'secret' => 'turn-secret',
'schemes' => ['turns', 'turn'],
'protocols' => ['tcp', 'udp'],
],
[
'server' => 'turn2.domain.invalid:443',
'secret' => 'other-turn-secret',
'schemes' => ['turns'],
'protocols' => ['tcp'],
],
],
],
];

$this->config
->method('getAppValue')
->willReturnMap([
['spreed', 'hosted-signaling-server-account-id', '', 'my-account-id'],
['spreed', 'hosted-signaling-server-account', '{}', '{"status": "pending"}']
]);
$this->config->expects($this->once())
->method('deleteAppValue')
->with('spreed', 'signaling_mode');

$expectedCalls = [
['spreed', 'signaling_servers', '{"servers":[{"server":"signaling-url","verify":true}],"secret":"signaling-secret"}'],
['spreed', 'stun_servers', '["stun.domain.invalid:443","stun.domain.invalid:3478"]'],
['spreed', 'turn_servers', '[{"server":"turn1.domain.invalid:443","secret":"turn-secret","schemes":"turn,turns","protocols":"udp,tcp"},{"server":"turn2.domain.invalid:443","secret":"other-turn-secret","schemes":"turns","protocols":"tcp"}]'],
['spreed', 'hosted-signaling-server-account', json_encode($newStatus)],
];

$i = 0;
$this->config->expects($this->exactly(count($expectedCalls)))
->method('setAppValue')
->willReturnCallback(function () use ($expectedCalls, &$i): void {
$this->assertArrayHasKey($i, $expectedCalls);
$this->assertSame($expectedCalls[$i], func_get_args());
$i++;
});

$group = $this->createMock(IGroup::class);
$this->groupManager->expects($this->once())
->method('get')
->with('admin')
->willReturn($group);
$group->expects($this->once())
->method('getUsers')
->willReturn([]);

$this->hostedSignalingServerService->expects($this->once())
->method('fetchAccountInfo')
->willReturn($newStatus);

self::invokePrivate($backgroundJob, 'run', ['']);
}
}
Loading