feat!(portal): return relays as plain socket addresses (#3665)

Extracted out of #3391.

We don't actually need this for #3391 though because we've added a
compatibility layer during deserialization. But, it will be good to
remove that compat layer at some point which means we have to return the
addresses as plain socket addresses. Because that is a breaking change,
I decided to extract this into a different PR.

Co-authored-by: conectado <gabrielalejandro7@gmail.com>

---------

Co-authored-by: conectado <gabrielalejandro7@gmail.com>
This commit is contained in:
Thomas Eizinger
2024-02-21 12:31:03 +11:00
committed by GitHub
parent 19a7bac4ae
commit e766407dfb
7 changed files with 42 additions and 66 deletions

View File

@@ -24,7 +24,7 @@ defmodule API.Client.Views.Relay do
[
%{
type: :stun,
uri: "stun:#{format_address(address)}:#{relay.port}"
addr: "#{format_address(address)}:#{relay.port}"
}
]
end
@@ -39,7 +39,7 @@ defmodule API.Client.Views.Relay do
[
%{
type: :turn,
uri: "turn:#{format_address(address)}:#{relay.port}",
addr: "#{format_address(address)}:#{relay.port}",
username: username,
password: password,
expires_at: expires_at

View File

@@ -597,8 +597,8 @@ defmodule API.Client.ChannelTest do
assert gateway_id == gateway.id
assert gateway_last_seen_remote_ip == gateway.last_seen_remote_ip
ipv4_turn_uri = "turn:#{global_relay.ipv4}:#{global_relay.port}"
ipv6_turn_uri = "turn:[#{global_relay.ipv6}]:#{global_relay.port}"
ipv4_turn_uri = "#{global_relay.ipv4}:#{global_relay.port}"
ipv6_turn_uri = "[#{global_relay.ipv6}]:#{global_relay.port}"
assert [
%{
@@ -606,14 +606,14 @@ defmodule API.Client.ChannelTest do
expires_at: expires_at_unix,
password: password1,
username: username1,
uri: ^ipv4_turn_uri
addr: ^ipv4_turn_uri
},
%{
type: :turn,
expires_at: expires_at_unix,
password: password2,
username: username2,
uri: ^ipv6_turn_uri
addr: ^ipv6_turn_uri
}
] = relays
@@ -684,8 +684,8 @@ defmodule API.Client.ChannelTest do
assert gateway_id == gateway.id
assert gateway_last_seen_remote_ip == gateway.last_seen_remote_ip
ipv4_turn_uri = "turn:#{relay.ipv4}:#{relay.port}"
ipv6_turn_uri = "turn:[#{relay.ipv6}]:#{relay.port}"
ipv4_turn_uri = "#{relay.ipv4}:#{relay.port}"
ipv6_turn_uri = "[#{relay.ipv6}]:#{relay.port}"
assert [
%{
@@ -693,14 +693,14 @@ defmodule API.Client.ChannelTest do
expires_at: expires_at_unix,
password: password1,
username: username1,
uri: ^ipv4_turn_uri
addr: ^ipv4_turn_uri
},
%{
type: :turn,
expires_at: expires_at_unix,
password: password2,
username: username2,
uri: ^ipv6_turn_uri
addr: ^ipv6_turn_uri
}
] = relays
@@ -771,17 +771,17 @@ defmodule API.Client.ChannelTest do
assert gateway_id == gateway.id
assert gateway_last_seen_remote_ip == gateway.last_seen_remote_ip
ipv4_turn_uri = "stun:#{global_relay.ipv4}:#{global_relay.port}"
ipv6_turn_uri = "stun:[#{global_relay.ipv6}]:#{global_relay.port}"
ipv4_turn_uri = "#{global_relay.ipv4}:#{global_relay.port}"
ipv6_turn_uri = "[#{global_relay.ipv6}]:#{global_relay.port}"
assert [
%{
type: :stun,
uri: ^ipv4_turn_uri
addr: ^ipv4_turn_uri
},
%{
type: :stun,
uri: ^ipv6_turn_uri
addr: ^ipv6_turn_uri
}
] = relays
end

View File

@@ -334,8 +334,8 @@ defmodule API.Gateway.ChannelTest do
assert payload.flow_id == flow_id
assert payload.actor == %{id: client.actor_id}
ipv4_turn_uri = "turn:#{relay.ipv4}:#{relay.port}"
ipv6_turn_uri = "turn:[#{relay.ipv6}]:#{relay.port}"
ipv4_turn_uri = "#{relay.ipv4}:#{relay.port}"
ipv6_turn_uri = "[#{relay.ipv6}]:#{relay.port}"
assert [
%{
@@ -343,14 +343,14 @@ defmodule API.Gateway.ChannelTest do
expires_at: expires_at_unix,
password: password1,
username: username1,
uri: ^ipv4_turn_uri
addr: ^ipv4_turn_uri
},
%{
type: :turn,
expires_at: expires_at_unix,
password: password2,
username: username2,
uri: ^ipv6_turn_uri
addr: ^ipv6_turn_uri
}
] = payload.relays
@@ -444,8 +444,8 @@ defmodule API.Gateway.ChannelTest do
assert payload.flow_id == flow_id
assert payload.actor == %{id: client.actor_id}
ipv4_turn_uri = "turn:#{relay.ipv4}:#{relay.port}"
ipv6_turn_uri = "turn:[#{relay.ipv6}]:#{relay.port}"
ipv4_turn_uri = "#{relay.ipv4}:#{relay.port}"
ipv6_turn_uri = "[#{relay.ipv6}]:#{relay.port}"
assert [
%{
@@ -453,14 +453,14 @@ defmodule API.Gateway.ChannelTest do
expires_at: expires_at_unix,
password: password1,
username: username1,
uri: ^ipv4_turn_uri
addr: ^ipv4_turn_uri
},
%{
type: :turn,
expires_at: expires_at_unix,
password: password2,
username: username2,
uri: ^ipv6_turn_uri
addr: ^ipv6_turn_uri
}
] = payload.relays
@@ -553,17 +553,17 @@ defmodule API.Gateway.ChannelTest do
assert payload.flow_id == flow_id
assert payload.actor == %{id: client.actor_id}
ipv4_turn_uri = "stun:#{relay.ipv4}:#{relay.port}"
ipv6_turn_uri = "stun:[#{relay.ipv6}]:#{relay.port}"
ipv4_turn_uri = "#{relay.ipv4}:#{relay.port}"
ipv6_turn_uri = "[#{relay.ipv6}]:#{relay.port}"
assert [
%{
type: :stun,
uri: ^ipv4_turn_uri
addr: ^ipv4_turn_uri
},
%{
type: :stun,
uri: ^ipv6_turn_uri
addr: ^ipv6_turn_uri
}
] = payload.relays

View File

@@ -335,24 +335,24 @@ mod test {
resource_id: "f16ecfa0-a94f-4bfd-a2ef-1cc1f2ef3da3".parse().unwrap(),
relays: vec![
Relay::Stun(Stun {
uri: "189.172.73.111:3478".parse().unwrap(),
addr: "189.172.73.111:3478".parse().unwrap(),
}),
Relay::Turn(Turn {
expires_at: NaiveDateTime::from_timestamp_opt(1686629954, 0)
.unwrap()
.and_utc(),
uri: "189.172.73.111:3478".parse().unwrap(),
addr: "189.172.73.111:3478".parse().unwrap(),
username: "1686629954:C7I74wXYFdFugMYM".to_string(),
password: "OXXRDJ7lJN1cm+4+2BWgL87CxDrvpVrn5j3fnJHye98".to_string(),
}),
Relay::Stun(Stun {
uri: "[::1]:3478".parse().unwrap(),
addr: "[::1]:3478".parse().unwrap(),
}),
Relay::Turn(Turn {
expires_at: NaiveDateTime::from_timestamp_opt(1686629954, 0)
.unwrap()
.and_utc(),
uri: "[::1]:3478".parse().unwrap(),
addr: "[::1]:3478".parse().unwrap(),
username: "1686629954:dpHxHfNfOhxPLfMG".to_string(),
password: "8Wtb+3YGxO6ia23JUeSEfZ2yFD6RhGLkbgZwqjebyKY".to_string(),
}),
@@ -373,24 +373,24 @@ mod test {
"relays": [
{
"type":"stun",
"uri": "stun:189.172.73.111:3478"
"addr": "189.172.73.111:3478"
},
{
"expires_at": 1686629954,
"password": "OXXRDJ7lJN1cm+4+2BWgL87CxDrvpVrn5j3fnJHye98",
"type": "turn",
"uri": "189.172.73.111:3478",
"addr": "189.172.73.111:3478",
"username":"1686629954:C7I74wXYFdFugMYM"
},
{
"type": "stun",
"uri": "[::1]:3478"
"addr": "[::1]:3478"
},
{
"expires_at": 1686629954,
"password": "8Wtb+3YGxO6ia23JUeSEfZ2yFD6RhGLkbgZwqjebyKY",
"type": "turn",
"uri": "turn:[::1]:3478",
"addr": "[::1]:3478",
"username": "1686629954:dpHxHfNfOhxPLfMG"
}]
},

View File

@@ -302,9 +302,8 @@ pub struct Turn {
//// Expire time of the username/password in unix millisecond timestamp UTC
#[serde(with = "ts_seconds")]
pub expires_at: DateTime<Utc>,
/// URI of the relay
#[serde(with = "stun_turn_uri", alias = "addr")]
pub uri: SocketAddr,
/// Address of the relay
pub addr: SocketAddr,
/// Username for the relay
pub username: String,
// TODO: SecretString
@@ -315,29 +314,6 @@ pub struct Turn {
/// Stun kind of relay
#[derive(Debug, Deserialize, Clone, PartialEq, Eq)]
pub struct Stun {
/// URI for the relay
#[serde(with = "stun_turn_uri", alias = "addr")]
pub uri: SocketAddr,
}
mod stun_turn_uri {
use serde::de::Error;
use serde::Deserialize;
use std::net::SocketAddr;
pub fn deserialize<'de, D>(deserializer: D) -> Result<SocketAddr, D::Error>
where
D: serde::Deserializer<'de>,
D::Error: Error,
{
let string = String::deserialize(deserializer)?;
let socket_addr = string
.trim_start_matches("stun:")
.trim_start_matches("turn:")
.parse::<SocketAddr>()
.map_err(D::Error::custom)?;
Ok(socket_addr)
}
/// Address for the relay
pub addr: SocketAddr,
}

View File

@@ -45,7 +45,7 @@ fn stun(relays: &[Relay]) -> HashSet<SocketAddr> {
.iter()
.filter_map(|r| {
if let Relay::Stun(r) = r {
Some(r.uri)
Some(r.addr)
} else {
None
}
@@ -59,7 +59,7 @@ fn turn(relays: &[Relay]) -> HashSet<(SocketAddr, String, String, String)> {
.filter_map(|r| {
if let Relay::Turn(r) = r {
Some((
r.uri,
r.addr,
r.username.clone(),
r.password.clone(),
REALM.to_string(),

View File

@@ -176,13 +176,13 @@ mod test {
"relays": [
{
"type": "stun",
"uri": "172.28.0.101:3478"
"addr": "172.28.0.101:3478"
},
{
"type": "turn",
"username": "1719367575:ZQHcVGkdnfgGmcP1",
"password": "ZWYiBeFHOJyYq0mcwAXjRpcuXIJJpzWlOXVdxwttrWg",
"uri": "172.28.0.101:3478",
"addr": "172.28.0.101:3478",
"expires_at": 1719367575
}
]