add config for relay listen address
this will make it easier to run on Wireguard or LANs or whatevermain
parent
522cbc5991
commit
6990be48d6
|
@ -4,7 +4,9 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
convert::{TryFrom},
|
convert::{TryFrom},
|
||||||
|
net::IpAddr,
|
||||||
path::Path,
|
path::Path,
|
||||||
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -90,6 +92,7 @@ pub mod file {
|
||||||
#[serde (flatten)]
|
#[serde (flatten)]
|
||||||
pub iso: Isomorphic,
|
pub iso: Isomorphic,
|
||||||
|
|
||||||
|
pub address: Option <String>,
|
||||||
pub port: Option <u16>,
|
pub port: Option <u16>,
|
||||||
pub servers: Option <Vec <Server>>,
|
pub servers: Option <Vec <Server>>,
|
||||||
|
|
||||||
|
@ -102,16 +105,29 @@ pub mod file {
|
||||||
|
|
||||||
/// Config fields as they are used at runtime
|
/// Config fields as they are used at runtime
|
||||||
|
|
||||||
#[derive (Default)]
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub iso: file::Isomorphic,
|
pub iso: file::Isomorphic,
|
||||||
|
|
||||||
|
pub address: IpAddr,
|
||||||
pub port: Option <u16>,
|
pub port: Option <u16>,
|
||||||
pub servers: HashMap <String, file::Server>,
|
pub servers: HashMap <String, file::Server>,
|
||||||
pub scraper_keys: HashMap <String, ScraperKey <Valid30Days>>,
|
pub scraper_keys: HashMap <String, ScraperKey <Valid30Days>>,
|
||||||
pub news_url: Option <String>,
|
pub news_url: Option <String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Config {
|
||||||
|
fn default () -> Self {
|
||||||
|
Self {
|
||||||
|
iso: Default::default (),
|
||||||
|
address: IpAddr::from ([0, 0, 0, 0]),
|
||||||
|
port: None,
|
||||||
|
servers: Default::default (),
|
||||||
|
scraper_keys: Default::default (),
|
||||||
|
news_url: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl TryFrom <file::Config> for Config {
|
impl TryFrom <file::Config> for Config {
|
||||||
type Error = ConfigError;
|
type Error = ConfigError;
|
||||||
|
|
||||||
|
@ -131,6 +147,7 @@ impl TryFrom <file::Config> for Config {
|
||||||
|
|
||||||
Ok (Self {
|
Ok (Self {
|
||||||
iso: f.iso,
|
iso: f.iso,
|
||||||
|
address: parse_address (f.address.as_ref ().map (|s| &s[..]))?,
|
||||||
port: f.port,
|
port: f.port,
|
||||||
servers,
|
servers,
|
||||||
scraper_keys,
|
scraper_keys,
|
||||||
|
@ -139,6 +156,14 @@ impl TryFrom <file::Config> for Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_address (s: Option <&str>) -> Result <IpAddr, ConfigError> {
|
||||||
|
Ok (s
|
||||||
|
.map (|s| IpAddr::from_str (s))
|
||||||
|
.transpose ().map_err (|_| ConfigError::BadServerAddress)?
|
||||||
|
.unwrap_or (IpAddr::from ([0, 0, 0, 0]))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub async fn from_file (path: &Path) -> Result <Self, ConfigError> {
|
pub async fn from_file (path: &Path) -> Result <Self, ConfigError> {
|
||||||
let config_s = tokio::fs::read_to_string (path).await?;
|
let config_s = tokio::fs::read_to_string (path).await?;
|
||||||
|
@ -147,3 +172,23 @@ impl Config {
|
||||||
Self::try_from (new_config)
|
Self::try_from (new_config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg (test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ip_address () {
|
||||||
|
for (input, expected) in vec! [
|
||||||
|
(None, Some (IpAddr::from ([0, 0, 0, 0]))),
|
||||||
|
(Some ("bogus"), None),
|
||||||
|
(Some ("0.0.0.0"), Some (IpAddr::from ([0, 0, 0, 0]))),
|
||||||
|
(Some ("0"), None),
|
||||||
|
(Some ("127.0.0.1"), Some (IpAddr::from ([127, 0, 0, 1]))),
|
||||||
|
(Some ("10.0.0.1"), Some (IpAddr::from ([10, 0, 0, 1]))),
|
||||||
|
].into_iter () {
|
||||||
|
let actual = parse_address (input).ok ();
|
||||||
|
assert_eq! (actual, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -17,6 +17,9 @@ pub enum ConfigError {
|
||||||
#[error ("tripcode not 32 bytes after decoding")]
|
#[error ("tripcode not 32 bytes after decoding")]
|
||||||
TripcodeBadLength,
|
TripcodeBadLength,
|
||||||
|
|
||||||
|
#[error ("Bad server address")]
|
||||||
|
BadServerAddress,
|
||||||
|
|
||||||
#[error ("unknown config error")]
|
#[error ("unknown config error")]
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
|
@ -683,16 +683,22 @@ pub async fn run_relay (
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let addr = SocketAddr::from ((
|
let addr = {
|
||||||
[0, 0, 0, 0],
|
let guard = state.config.read ().await;
|
||||||
state.config.read ().await.port.unwrap_or (4000),
|
|
||||||
));
|
SocketAddr::from ((
|
||||||
|
guard.address,
|
||||||
|
guard.port.unwrap_or (4000),
|
||||||
|
))
|
||||||
|
};
|
||||||
|
|
||||||
let server = Server::bind (&addr)
|
let server = Server::bind (&addr)
|
||||||
.serve (make_svc);
|
.serve (make_svc);
|
||||||
|
|
||||||
state.audit_log.push (AuditEvent::new (AuditData::RelayStart)).await;
|
state.audit_log.push (AuditEvent::new (AuditData::RelayStart)).await;
|
||||||
|
|
||||||
|
trace! ("Serving relay on {:?}", addr);
|
||||||
|
|
||||||
server.with_graceful_shutdown (async {
|
server.with_graceful_shutdown (async {
|
||||||
use ShuttingDownError::ShuttingDown;
|
use ShuttingDownError::ShuttingDown;
|
||||||
|
|
||||||
|
|
|
@ -200,6 +200,11 @@ impl Builder {
|
||||||
Relay::try_from (self.config)
|
Relay::try_from (self.config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn address (mut self, addr: std::net::IpAddr) -> Self {
|
||||||
|
self.config.address = addr;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn enable_scraper_api (mut self, b: bool) -> Self {
|
pub fn enable_scraper_api (mut self, b: bool) -> Self {
|
||||||
self.config.iso.enable_scraper_api = b;
|
self.config.iso.enable_scraper_api = b;
|
||||||
self
|
self
|
||||||
|
|
Loading…
Reference in New Issue