Add support for host names in config address fields, resolve them to IP
This commit is contained in:
@@ -9,6 +9,7 @@ use version_compare::Cmp;
|
|||||||
|
|
||||||
use crate::proto;
|
use crate::proto;
|
||||||
use crate::util::error::{quit_error, quit_error_msg, ErrorHintsBuilder};
|
use crate::util::error::{quit_error, quit_error_msg, ErrorHintsBuilder};
|
||||||
|
use crate::util::serde::to_socket_addrs;
|
||||||
|
|
||||||
/// Default configuration file location.
|
/// Default configuration file location.
|
||||||
pub const CONFIG_FILE: &str = "lazymc.toml";
|
pub const CONFIG_FILE: &str = "lazymc.toml";
|
||||||
@@ -127,6 +128,7 @@ impl Config {
|
|||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct Public {
|
pub struct Public {
|
||||||
/// Public address.
|
/// Public address.
|
||||||
|
#[serde(deserialize_with = "to_socket_addrs")]
|
||||||
pub address: SocketAddr,
|
pub address: SocketAddr,
|
||||||
|
|
||||||
/// Minecraft protocol version name hint.
|
/// Minecraft protocol version name hint.
|
||||||
@@ -157,7 +159,10 @@ pub struct Server {
|
|||||||
pub command: String,
|
pub command: String,
|
||||||
|
|
||||||
/// Server address.
|
/// Server address.
|
||||||
#[serde(default = "server_address_default")]
|
#[serde(
|
||||||
|
deserialize_with = "to_socket_addrs",
|
||||||
|
default = "server_address_default"
|
||||||
|
)]
|
||||||
pub address: SocketAddr,
|
pub address: SocketAddr,
|
||||||
|
|
||||||
/// Immediately wake server when starting lazymc.
|
/// Immediately wake server when starting lazymc.
|
||||||
@@ -318,6 +323,7 @@ impl Default for JoinHold {
|
|||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct JoinForward {
|
pub struct JoinForward {
|
||||||
/// IP and port to forward to.
|
/// IP and port to forward to.
|
||||||
|
#[serde(deserialize_with = "to_socket_addrs")]
|
||||||
pub address: SocketAddr,
|
pub address: SocketAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
pub mod cli;
|
pub mod cli;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
pub mod serde;
|
||||||
pub mod style;
|
pub mod style;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
30
src/util/serde.rs
Normal file
30
src/util/serde.rs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
use std::net::{SocketAddr, ToSocketAddrs};
|
||||||
|
|
||||||
|
use serde::de::{Error, Unexpected};
|
||||||
|
use serde::{Deserialize, Deserializer};
|
||||||
|
|
||||||
|
/// Deserialize a `Vec` into a `HashMap` by key.
|
||||||
|
pub fn to_socket_addrs<'de, D>(d: D) -> Result<SocketAddr, D::Error>
|
||||||
|
where
|
||||||
|
D: Deserializer<'de>,
|
||||||
|
{
|
||||||
|
// Deserialize string
|
||||||
|
let addr = String::deserialize(d)?;
|
||||||
|
|
||||||
|
// Try to socket address to resolve
|
||||||
|
match addr.to_socket_addrs() {
|
||||||
|
Ok(mut addr) => {
|
||||||
|
if let Some(addr) = addr.next() {
|
||||||
|
return Ok(addr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
dbg!(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse raw IP address
|
||||||
|
addr.parse().map_err(|_| {
|
||||||
|
Error::invalid_value(Unexpected::Str(&addr), &"IP or resolvable host and port")
|
||||||
|
})
|
||||||
|
}
|
Reference in New Issue
Block a user