Browse Source

Don't provide a stun without internet connection

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/2997/head
Joas Schilling 6 years ago
parent
commit
a628729d9b
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. 17
      lib/Config.php
  2. 53
      tests/php/ConfigTest.php

17
lib/Config.php

@ -165,11 +165,17 @@ class Config {
$config = $this->config->getAppValue('spreed', 'stun_servers', json_encode(['stun.nextcloud.com:443']));
$servers = json_decode($config, true);
if (is_array($servers) && !empty($servers)) {
return $servers;
if (!is_array($servers) || empty($servers)) {
$servers = ['stun.nextcloud.com:443'];
}
return ['stun.nextcloud.com:443'];
if (!$this->config->getSystemValueBool('has_internet_connection', true)) {
$servers = array_filter($servers, static function($server) {
return $server !== 'stun.nextcloud.com:443';
});
}
return $servers;
}
/**
@ -177,6 +183,11 @@ class Config {
*/
public function getStunServer(): string {
$servers = $this->getStunServers();
if (empty($servers)) {
return '';
}
// For now we use a random server from the list
try {
return $servers[random_int(0, count($servers) - 1)];

53
tests/php/ConfigTest.php

@ -49,11 +49,64 @@ class ConfigTest extends TestCase {
->method('getAppValue')
->with('spreed', 'stun_servers', json_encode(['stun.nextcloud.com:443']))
->willReturn(json_encode($servers));
$config
->expects($this->once())
->method('getSystemValueBool')
->with('has_internet_connection', true)
->willReturn(true);
$helper = new Config($config, $secureRandom, $groupManager, $timeFactory);
$this->assertTrue(in_array($helper->getStunServer(), $servers, true));
}
public function testGetDefaultStunServer() {
/** @var MockObject|ITimeFactory $timeFactory */
$timeFactory = $this->createMock(ITimeFactory::class);
/** @var MockObject|ISecureRandom $secureRandom */
$secureRandom = $this->createMock(ISecureRandom::class);
/** @var MockObject|IGroupManager $secureRandom */
$groupManager = $this->createMock(IGroupManager::class);
/** @var MockObject|IConfig $config */
$config = $this->createMock(IConfig::class);
$config
->expects($this->once())
->method('getAppValue')
->with('spreed', 'stun_servers', json_encode(['stun.nextcloud.com:443']))
->willReturn(json_encode([]));
$config
->expects($this->once())
->method('getSystemValueBool')
->with('has_internet_connection', true)
->willReturn(true);
$helper = new Config($config, $secureRandom, $groupManager, $timeFactory);
$this->assertSame('stun.nextcloud.com:443', $helper->getStunServer());
}
public function testGetDefaultStunServerNoInternet() {
/** @var MockObject|ITimeFactory $timeFactory */
$timeFactory = $this->createMock(ITimeFactory::class);
/** @var MockObject|ISecureRandom $secureRandom */
$secureRandom = $this->createMock(ISecureRandom::class);
/** @var MockObject|IGroupManager $secureRandom */
$groupManager = $this->createMock(IGroupManager::class);
/** @var MockObject|IConfig $config */
$config = $this->createMock(IConfig::class);
$config
->expects($this->once())
->method('getAppValue')
->with('spreed', 'stun_servers', json_encode(['stun.nextcloud.com:443']))
->willReturn(json_encode([]));
$config
->expects($this->once())
->method('getSystemValueBool')
->with('has_internet_connection', true)
->willReturn(false);
$helper = new Config($config, $secureRandom, $groupManager, $timeFactory);
$this->assertSame('', $helper->getStunServer());
}
public function testGenerateTurnSettings() {
/** @var MockObject|IConfig $config */

Loading…
Cancel
Save