tuwunel_core/config/
net.rs1use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
2
3use either::{
4 Either,
5 Either::{Left, Right},
6};
7use ruma::ServerName;
8use serde::Deserialize;
9
10use crate::{
11 Err, Result,
12 arrayvec::ArrayString,
13 err, implement,
14 utils::{BoolExt, string::to_array_string},
15};
16
17type PermissionDigits = ArrayString<10>;
18
19#[derive(Deserialize, Clone, Debug)]
20#[serde(transparent)]
21pub(super) struct ListeningPort {
22 #[serde(with = "either::serde_untagged")]
23 pub(super) ports: Either<u16, Vec<u16>>,
24}
25
26#[derive(Deserialize, Clone, Debug)]
27#[serde(transparent)]
28pub(super) struct ListeningAddr {
29 #[serde(with = "either::serde_untagged")]
30 pub(super) addrs: Either<IpAddr, Vec<IpAddr>>,
31}
32
33#[implement(super::Config)]
38pub fn get_unix_socket_perms(&self) -> Result<u32> {
39 let octal_perms: PermissionDigits = to_array_string(self.http.listen.unix_socket_perms);
40 let socket_perms = u32::from_str_radix(&octal_perms, 8).map_err(|_| {
41 err!(Config("http.listen.unix_socket_perms", "failed to convert octal permissions"))
42 })?;
43
44 Ok(socket_perms)
45}
46
47#[must_use]
53#[implement(super::Config)]
54pub fn get_bind_addrs(&self) -> Vec<SocketAddr> {
55 let mut addrs = Vec::with_capacity(
56 self.get_bind_hosts()
57 .len()
58 .saturating_mul(self.get_bind_ports().len()),
59 );
60 for host in &self.get_bind_hosts() {
61 for port in &self.get_bind_ports() {
62 addrs.push(SocketAddr::new(*host, *port));
63 }
64 }
65
66 addrs
67}
68
69#[implement(super::Config)]
70fn get_bind_hosts(&self) -> Vec<IpAddr> {
71 self.http
72 .listen
73 .address
74 .as_ref()
75 .map(|address| match &address.addrs {
76 | Left(addr) => vec![*addr],
77 | Right(addrs) => addrs.clone(),
78 })
79 .unwrap_or_else(|| {
80 self.http
81 .listen
82 .unix_socket_path
83 .is_none()
84 .then(|| vec![Ipv4Addr::LOCALHOST.into(), Ipv6Addr::LOCALHOST.into()])
85 .unwrap_or_default()
86 })
87}
88
89#[implement(super::Config)]
90fn get_bind_ports(&self) -> Vec<u16> {
91 match &self.http.listen.port.ports {
92 | Left(port) => vec![*port],
93 | Right(ports) => ports.clone(),
94 }
95}
96
97#[implement(super::Config)]
103#[inline]
104#[must_use]
105pub fn is_address_defaulted(&self) -> bool { self.http.listen.address.is_none() }
106
107#[implement(super::Config)]
112#[inline]
113pub fn require_federation(&self) -> Result {
114 self.federation
115 .enable
116 .into_option()
117 .ok_or_else(|| err!(Request(Forbidden("Federation is disabled."))))
118}
119
120#[implement(super::Config)]
127pub fn require_remote_permitted(&self, server_name: &ServerName) -> Result {
128 self.require_federation()?;
129
130 if !self.is_remote_permitted(server_name) {
131 return Err!(Request(Forbidden(debug_warn!(
132 "Federation with {server_name} is not allowed."
133 ))));
134 }
135
136 Ok(())
137}
138
139#[implement(super::Config)]
144#[inline]
145#[must_use]
146pub fn is_remote_permitted(&self, server_name: &ServerName) -> bool {
147 self.federation.enable && !self.is_forbidden_remote_server_name(server_name)
148}
149
150#[implement(super::Config)]
155#[must_use]
156pub fn is_forbidden_remote_server_name(&self, server_name: &ServerName) -> bool {
157 if server_name == self.server_name {
158 return false;
159 }
160
161 let deny_list_active = self
162 .federation
163 .forbidden_remote_server_names
164 .is_empty()
165 .is_false();
166
167 let allow_list_active = self
168 .federation
169 .allowed_remote_server_names_experimental
170 .is_empty()
171 .is_false();
172
173 if deny_list_active
174 && self
175 .federation
176 .forbidden_remote_server_names
177 .is_match(server_name.host())
178 {
179 return true;
180 }
181
182 if allow_list_active
183 && self
184 .federation
185 .allowed_remote_server_names_experimental
186 .is_match(server_name.host())
187 .is_false()
188 {
189 return true;
190 }
191
192 false
193}