Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Use String in TURN address #42

Merged
merged 5 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
4 changes: 2 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@


[turn]
# Turn server IP address.
# Turn server host.
#
# Default:
# ip = "127.0.0.1"
# host = "localhost"

# Turn server port.
#
Expand Down
4 changes: 4 additions & 0 deletions jason/demo/minikube.vals.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ server:
tag: dev
pullPolicy: IfNotPresent

conf:
turn:
host: medea-demo.test

coturn:
image:
tag: 4.5
Expand Down
2 changes: 1 addition & 1 deletion jason/demo/staging.vals.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ server:
server:
bind_port: 9980
turn:
ip: 178.162.214.89
host: demo.medea.stg.t11913.org
pass: changeme

coturn:
Expand Down
8 changes: 4 additions & 4 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,17 @@ mod tests {
fn turn_conf() {
let default_conf = Conf::default();

env::set_var("MEDEA_TURN.IP", "5.5.5.5");
env::set_var("MEDEA_TURN.HOST", "example.com");
env::set_var("MEDEA_TURN.PORT", "1234");

let env_conf = Conf::parse().unwrap();

assert_ne!(default_conf.turn.ip, env_conf.turn.ip);
assert_ne!(default_conf.turn.host, env_conf.turn.host);
assert_ne!(default_conf.turn.port, env_conf.turn.port);

assert_eq!(env_conf.turn.ip, Ipv4Addr::new(5, 5, 5, 5));
assert_eq!(&env_conf.turn.host, "example.com");
assert_eq!(env_conf.turn.port, 1234);
assert_eq!(env_conf.turn.addr(), "5.5.5.5:1234".parse().unwrap());
assert_eq!(&env_conf.turn.addr(), "example.com:1234");
}

#[test]
Expand Down
18 changes: 7 additions & 11 deletions src/conf/turn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! STUN/TURN server settings.

use std::{
net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs as _},
net::{IpAddr, Ipv4Addr},
time::Duration,
};

Expand All @@ -14,9 +14,9 @@ use smart_default::SmartDefault;
pub struct Turn {
/// Database settings
pub db: Db,
/// IP address STUN/TURN server. Defaults to `127.0.0.1`.
#[default(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)))]
pub ip: IpAddr,
/// Host address STUN/TURN server. Defaults to `localhost`.
#[default("localhost".to_string())]
pub host: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use Cow in config as it's much more ergonomic.

/// Port to connect TURN server. Defaults to `3478`.
#[default = 3478]
pub port: u16,
Expand All @@ -29,14 +29,10 @@ pub struct Turn {
}

impl Turn {
/// Builds [`SocketAddr`] from `ip` and `port`.
/// Builds [`String`] addr from `host` and `port`.
#[inline]
pub fn addr(&self) -> SocketAddr {
(self.ip, self.port)
.to_socket_addrs()
.unwrap()
.next()
.unwrap()
pub fn addr(&self) -> String {
format!("{}:{}", self.host, self.port)
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/media/ice_user.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::net::SocketAddr;

use medea_client_api_proto::IceServer;

use crate::signalling::RoomId;
Expand All @@ -8,7 +6,7 @@ use crate::signalling::RoomId;
#[derive(Clone, Debug)]
pub struct IceUser {
/// Address of Turn server.
address: SocketAddr,
address: String,
/// Username for authorization.
user: String,
/// Password for authorization.
Expand All @@ -22,7 +20,7 @@ pub struct IceUser {
impl IceUser {
/// Build new non static [`IceUser`].
pub fn build(
address: SocketAddr,
address: String,
room_id: RoomId,
name: &str,
pass: String,
Expand All @@ -36,7 +34,7 @@ impl IceUser {
}

/// Build new static [`IceUser`].
pub fn new(address: SocketAddr, user: String, pass: String) -> Self {
pub fn new(address: String, user: String, pass: String) -> Self {
Self {
address,
user,
Expand Down Expand Up @@ -65,7 +63,7 @@ impl IceUser {
vec![stun, turn]
}

pub fn address(&self) -> &SocketAddr {
pub fn address(&self) -> &str {
&self.address
}

Expand Down
8 changes: 4 additions & 4 deletions src/turn/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, net::SocketAddr, sync::Arc};
use std::{fmt, sync::Arc};

use actix::{
fut::wrap_future, Actor, ActorFuture, Addr, Context, Handler, MailboxError,
Expand Down Expand Up @@ -141,7 +141,7 @@ struct Service {
/// TurnAuthRepo password.
db_pass: String,
/// Turn server address.
turn_address: SocketAddr,
turn_address: String,
/// Turn server static user.
turn_username: String,
/// Turn server static user password.
Expand Down Expand Up @@ -199,7 +199,7 @@ impl Service {
fn static_user(&mut self) -> IceUser {
if self.static_user.is_none() {
self.static_user.replace(IceUser::new(
self.turn_address,
self.turn_address.clone(),
self.turn_username.clone(),
self.turn_password.clone(),
));
Expand Down Expand Up @@ -233,7 +233,7 @@ impl Handler<CreateIceUser> for Service {
_ctx: &mut Self::Context,
) -> Self::Result {
let ice_user = IceUser::build(
self.turn_address,
self.turn_address.clone(),
msg.room_id,
&msg.member_id.to_string(),
self.new_password(TURN_PASS_LEN),
Expand Down