use std::fmt;
use std::net::SocketAddr;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ConnectionProtocol {
#[default]
Http,
Https,
}
#[derive(Clone, Debug, Default)]
pub struct Server {
pub(crate) protocol: ConnectionProtocol,
pub(crate) host: Option<String>,
pub(crate) v_host: Option<String>,
pub(crate) port: i32,
pub(crate) server_type: Option<String>,
pub(crate) source_id: i32,
pub(crate) cell_id: u32,
pub(crate) load: i32,
pub(crate) weighted_load: f32,
pub(crate) num_entries: i32,
pub(crate) steam_china_only: bool,
pub(crate) use_as_proxy: bool,
pub(crate) proxy_request_path_template: Option<String>,
pub(crate) allowed_app_ids: Vec<u32>,
pub(crate) bypass_proxies_of_type: Vec<String>,
}
impl Server {
pub fn from_socket_addr(end_point: SocketAddr) -> Self {
let host = end_point.ip().to_string();
Server {
protocol: if end_point.port() == 443 {
ConnectionProtocol::Https
} else {
ConnectionProtocol::Http
},
host: Some(host.clone()),
v_host: Some(host),
port: i32::from(end_point.port()),
..Server::default()
}
}
pub fn from_dns_end_point(host: &str, port: u16) -> Self {
Server {
protocol: if port == 443 {
ConnectionProtocol::Https
} else {
ConnectionProtocol::Http
},
host: Some(host.to_string()),
v_host: Some(host.to_string()),
port: i32::from(port),
..Server::default()
}
}
pub fn protocol(&self) -> ConnectionProtocol {
self.protocol
}
pub fn host(&self) -> Option<&str> {
self.host.as_deref()
}
pub fn v_host(&self) -> Option<&str> {
self.v_host.as_deref()
}
pub fn port(&self) -> i32 {
self.port
}
pub fn server_type(&self) -> Option<&str> {
self.server_type.as_deref()
}
pub fn source_id(&self) -> i32 {
self.source_id
}
pub fn cell_id(&self) -> u32 {
self.cell_id
}
pub fn load(&self) -> i32 {
self.load
}
pub fn weighted_load(&self) -> f32 {
self.weighted_load
}
pub fn num_entries(&self) -> i32 {
self.num_entries
}
pub fn steam_china_only(&self) -> bool {
self.steam_china_only
}
pub fn use_as_proxy(&self) -> bool {
self.use_as_proxy
}
pub fn proxy_request_path_template(&self) -> Option<&str> {
self.proxy_request_path_template.as_deref()
}
pub fn allowed_app_ids(&self) -> &[u32] {
&self.allowed_app_ids
}
pub fn bypass_proxies_of_type(&self) -> &[String] {
&self.bypass_proxies_of_type
}
}
impl From<SocketAddr> for Server {
fn from(end_point: SocketAddr) -> Self {
Server::from_socket_addr(end_point)
}
}
impl fmt::Display for Server {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}:{} ({})",
self.host.as_deref().unwrap_or_default(),
self.port,
self.server_type.as_deref().unwrap_or_default()
)
}
}