Skip to content

Commit

Permalink
Add TURN server to personal settings.
Browse files Browse the repository at this point in the history
This implements the second part of #2 to add TURN support. For now each user
has to have an account on the TURN server. Support for shared credentials will
be added in a separate PR.
  • Loading branch information
fancycode authored and MorrisJobke committed Nov 15, 2016
1 parent c48d740 commit 930adb5
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 2 deletions.
2 changes: 2 additions & 0 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@
});

\OCP\Util::connectHook('OC_User', 'post_deleteUser', \OCA\Spreed\Util::class, 'deleteUser');

OC_App::registerPersonal('spreed', 'personal');
7 changes: 6 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@
],
[
'name' => 'AppSettings#setSpreedSettings',
'url' => '/settings',
'url' => '/settings/admin',
'verb' => 'POST',
],
[
'name' => 'PersonalSettings#setSpreedSettings',
'url' => '/settings/personal',
'verb' => 'POST',
],
[
Expand Down
2 changes: 1 addition & 1 deletion js/settings-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $(document).ready(function(){
$('#spreed_settings_form').change(function(){
OC.msg.startSaving('#spreed_settings_msg');
var post = $( "#spreed_settings_form" ).serialize();
$.post(OC.generateUrl('/apps/spreed/settings'), post, function(data){
$.post(OC.generateUrl('/apps/spreed/settings/admin'), post, function(data){
OC.msg.finishedSaving('#spreed_settings_msg', data);
});
});
Expand Down
11 changes: 11 additions & 0 deletions js/settings-personal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$(document).ready(function(){

$('#spreed_settings_form').change(function(){
OC.msg.startSaving('#spreed_settings_msg');
var post = $("#spreed_settings_form").serialize();
$.post(OC.generateUrl('/apps/spreed/settings/personal'), post, function(data){
OC.msg.finishedSaving('#spreed_settings_msg', data);
});
});

});
136 changes: 136 additions & 0 deletions lib/Controller/PersonalSettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* @author Joachim Bauch <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Spreed\Controller;

use OCP\AppFramework\Controller;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserSession;

class PersonalSettingsController extends Controller {

/** @var IL10N */
private $l10n;
/** @var IConfig */
private $config;
/**
* @var bool|IUser
*/
private $user;

/**
* @param string $appName
* @param IRequest $request
* @param IL10N $l10n
* @param IConfig $config
* @param IUserSession $userSession
*/
public function __construct($appName,
IRequest $request,
IL10N $l10n,
IConfig $config,
IUserSession $userSession) {
parent::__construct($appName, $request);
$this->l10n = $l10n;
$this->config = $config;
$this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser() : false;
}

/**
* Configure the personal settings of the Spreed app. The TURN server must
* be passed in the form "turnserver:port", e.g. "turn.domain.invalid:1234".
*
* @NoAdminRequired
* @param string $turn_server
* @param string $turn_username
* @param string $turn_password
* @param string $turn_protocols
*/
public function setSpreedSettings($turn_server, $turn_username, $turn_password, $turn_protocols) {
if (!$this->user) {
return array('data' =>
array('message' =>
(string) $this->l10n->t('Not logged in.')
),
'status' => 'error'
);
}

$turn_server = trim($turn_server);
if ($turn_server !== "") {
$parts = explode(":", $turn_server);
if (count($parts) > 2) {
return array('data' =>
array('message' =>
(string) $this->l10n->t('Invalid format, must be turnserver:port.')
),
'status' => 'error'
);
}

$options = array(
'options' => array(
'default' => 0,
'max_range' => 65535,
'min_range' => 1,
),
);
if (count($parts) === 2 && !filter_var($parts[1], FILTER_VALIDATE_INT, $options)) {
return array('data' =>
array('message' =>
(string) $this->l10n->t('Invalid port specified.')
),
'status' => 'error'
);
}
}

if (empty($turn_server) || empty($turn_username) || empty($turn_password)) {
return array('data' =>
array('message' =>
(string) $this->l10n->t('All fields have to be filled out.')
),
'status' => 'error'
);
}

$turn_settings = array(
'server' => $turn_server,
'username' => $turn_username,
'password' => $turn_password,
'protocols' => $turn_protocols
);

$this->config->setUserValue($this->user->getUID(),
'spreed',
'turn_settings',
json_encode($turn_settings));
return array('data' =>
array('message' =>
(string) $this->l10n->t('Saved')
),
'status' => 'success'
);
}

}
14 changes: 14 additions & 0 deletions lib/Controller/SignallingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ public function signalling($ev, $fn) {
]);
}
break;
case 'turnservers':
$response = [];
$turnSettings = Util::getTurnSettings($this->config, $this->userId);
if (!empty($turnSettings)) {
$protocols = explode(",", $turnSettings['protocols']);
foreach($protocols as $proto) {
array_push($response, [
'url' => 'turn:' . $turnSettings['server'] . '?transport=' . $proto,
'username' => $turnSettings['username'],
'credential' => $turnSettings['password'],
]);
}
}
break;
}
return new JSONResponse($response);
}
Expand Down
9 changes: 9 additions & 0 deletions lib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ public static function deleteUser(IUser $user) {
}
}
}

public static function getTurnSettings(IConfig $config, string $uid) {
$value = $config->getUserValue($uid, 'spreed', 'turn_settings');
if (empty($value)) {
return array();
}
return json_decode($value, true);
}

}
14 changes: 14 additions & 0 deletions personal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use OCA\Spreed\Util;

\OC_Util::checkLoggedIn();

$tmpl = new OCP\Template('spreed', 'settings-personal');
$config = \OC::$server->getConfig();
$uid = \OC::$server->getUserSession()->getUser()->getUID();

$settings = Util::getTurnSettings($config, $uid);
//server":"1","username":"2","password":"3","protocols
$tmpl->assign('turnSettings', $settings);
return $tmpl->fetchPage();
45 changes: 45 additions & 0 deletions templates/settings-personal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
script('spreed', ['settings-personal']);
?>

<div id="spreedSettings" class="section">
<form id="spreed_settings_form" class="spreed_settings">
<h2><?php p($l->t('Spreed')); ?></h2>
<p>
<?php p($l->t('The TURN server is used to relay audio/video streams in cases where the participants can\'t connect directly to each other.')) ?>
</p>
<span id="spreed_settings_msg" class="msg"></span>
<p>
<label for="turn_server"><?php p($l->t('TURN server')) ?></label>
<input type="text" id="turn_server"
name="turn_server" placeholder="turnserver:port"
value="<?php p($_['turnSettings']['server']) ?>" />
</p>
<p>
<label for="turn_username"><?php p($l->t('Username')) ?></label>
<input type="text" id="turn_username"
name="turn_username" placeholder="username"
value="<?php p($_['turnSettings']['username']) ?>" />
</p>
<p>
<label for="turn_password"><?php p($l->t('Password')) ?></label>
<input type="text" id="turn_password"
name="turn_password" placeholder="password"
value="<?php p($_['turnSettings']['password']) ?>" />
</p>
<p>
<label for="turn_protocols"><?php p($l->t('Protocols')) ?></label>
<select id="turn_protocols" name="turn_protocols">
<option value="udp,tcp"
<?php p($_['turnSettings']['protocols'] === 'udp,tcp' ? 'selected' : '') ?>>
udp and tcp</option>
<option value="udp"
<?php p($_['turnSettings']['protocols'] === 'udp' ? 'selected' : '') ?>>
udp only</option>
<option value="tcp"
<?php p($_['turnSettings']['protocols'] === 'tcp' ? 'selected' : '') ?>>
tcp only</option>
</select>
</p>
</form>
</div>

0 comments on commit 930adb5

Please sign in to comment.