From b71a0ffcf7dbf58a7cc259bb1cf57e68779b0afa Mon Sep 17 00:00:00 2001 From: picoHz <53327867+picoHz@users.noreply.github.com> Date: Wed, 14 Aug 2024 22:29:39 +0900 Subject: [PATCH 1/2] Fix clippy warnings --- taxy-api/src/log.rs | 16 ++++++++-------- taxy-api/src/subject_name.rs | 12 ++++++------ taxy-webui/src/components/proxy_config.rs | 9 +++++---- taxy-webui/src/pages/cert_list.rs | 13 +++++++------ taxy-webui/src/pages/new_acme.rs | 13 +++++++------ taxy/src/proxy/http/route.rs | 14 +------------- 6 files changed, 34 insertions(+), 43 deletions(-) diff --git a/taxy-api/src/log.rs b/taxy-api/src/log.rs index a060b66d..a9ade3e4 100644 --- a/taxy-api/src/log.rs +++ b/taxy-api/src/log.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; -use std::collections::HashMap; +use std::{collections::HashMap, fmt::Display}; use time::OffsetDateTime; use utoipa::{IntoParams, ToSchema}; @@ -28,14 +28,14 @@ impl TryFrom for LogLevel { } } -impl ToString for LogLevel { - fn to_string(&self) -> String { +impl Display for LogLevel { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - LogLevel::Error => "error".to_string(), - LogLevel::Warn => "warn".to_string(), - LogLevel::Info => "info".to_string(), - LogLevel::Debug => "debug".to_string(), - LogLevel::Trace => "trace".to_string(), + LogLevel::Error => write!(f, "error"), + LogLevel::Warn => write!(f, "warn"), + LogLevel::Info => write!(f, "info"), + LogLevel::Debug => write!(f, "debug"), + LogLevel::Trace => write!(f, "trace"), } } } diff --git a/taxy-api/src/subject_name.rs b/taxy-api/src/subject_name.rs index ee52a0b3..74d0a10c 100644 --- a/taxy-api/src/subject_name.rs +++ b/taxy-api/src/subject_name.rs @@ -1,6 +1,6 @@ use crate::error::Error; use serde::{Deserialize, Serialize}; -use std::{net::IpAddr, str::FromStr}; +use std::{fmt::Display, net::IpAddr, str::FromStr}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum SubjectName { @@ -44,12 +44,12 @@ impl<'de> Deserialize<'de> for SubjectName { } } -impl ToString for SubjectName { - fn to_string(&self) -> String { +impl Display for SubjectName { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::DnsName(name) => name.to_owned(), - Self::WildcardDnsName(name) => format!("*.{}", name), - Self::IPAddress(addr) => addr.to_string(), + Self::DnsName(name) => write!(f, "{}", name), + Self::WildcardDnsName(name) => write!(f, "*.{}", name), + Self::IPAddress(addr) => write!(f, "{}", addr), } } } diff --git a/taxy-webui/src/components/proxy_config.rs b/taxy-webui/src/components/proxy_config.rs index 6bc47417..f026fb4e 100644 --- a/taxy-webui/src/components/proxy_config.rs +++ b/taxy-webui/src/components/proxy_config.rs @@ -4,6 +4,7 @@ use crate::store::PortStore; use crate::API_ENDPOINT; use gloo_net::http::Request; use std::collections::HashMap; +use std::fmt::Display; use taxy_api::id::ShortId; use taxy_api::proxy::{HttpProxy, ProxyKind, TcpProxy}; use taxy_api::{port::PortEntry, proxy::Proxy}; @@ -18,11 +19,11 @@ pub enum ProxyProtocol { Tcp, } -impl ToString for ProxyProtocol { - fn to_string(&self) -> String { +impl Display for ProxyProtocol { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - ProxyProtocol::Http => "HTTP / HTTPS".to_string(), - ProxyProtocol::Tcp => "TCP / TCP over TLS".to_string(), + ProxyProtocol::Http => write!(f, "HTTP / HTTPS"), + ProxyProtocol::Tcp => write!(f, "TCP / TCP over TLS"), } } } diff --git a/taxy-webui/src/pages/cert_list.rs b/taxy-webui/src/pages/cert_list.rs index caf6cbb6..6628bda2 100644 --- a/taxy-webui/src/pages/cert_list.rs +++ b/taxy-webui/src/pages/cert_list.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use crate::auth::use_ensure_auth; use crate::format::format_duration; use crate::pages::Route; @@ -27,14 +29,13 @@ pub struct CertsQuery { pub tab: CertsTab, } -impl ToString for CertsTab { - fn to_string(&self) -> String { +impl Display for CertsTab { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - CertsTab::Server => "Server Certs", - CertsTab::Root => "Root Certs", - CertsTab::Acme => "ACME", + CertsTab::Server => write!(f, "Server Certs"), + CertsTab::Root => write!(f, "Root Certs"), + CertsTab::Acme => write!(f, "ACME"), } - .into() } } diff --git a/taxy-webui/src/pages/new_acme.rs b/taxy-webui/src/pages/new_acme.rs index eb13ddda..2068b89a 100644 --- a/taxy-webui/src/pages/new_acme.rs +++ b/taxy-webui/src/pages/new_acme.rs @@ -4,6 +4,7 @@ use crate::pages::cert_list::{CertsQuery, CertsTab}; use crate::{auth::use_ensure_auth, pages::Route, API_ENDPOINT}; use gloo_net::http::Request; use std::collections::HashMap; +use std::fmt::Display; use taxy_api::acme::AcmeRequest; use wasm_bindgen::{JsCast, UnwrapThrowExt}; use web_sys::HtmlSelectElement; @@ -35,13 +36,13 @@ impl Provider { } } -impl ToString for Provider { - fn to_string(&self) -> String { +impl Display for Provider { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Provider::LetsEncrypt => "Let's Encrypt".to_string(), - Provider::GoogleTrustServices => "Google Trust Services".to_string(), - Provider::ZeroSSL => "ZeroSSL".to_string(), - Provider::Custom => "Custom".to_string(), + Provider::LetsEncrypt => write!(f, "Let's Encrypt"), + Provider::GoogleTrustServices => write!(f, "Google Trust Services"), + Provider::ZeroSSL => write!(f, "ZeroSSL"), + Provider::Custom => write!(f, "Custom"), } } } diff --git a/taxy/src/proxy/http/route.rs b/taxy/src/proxy/http/route.rs index ffb9c34e..b8d79630 100644 --- a/taxy/src/proxy/http/route.rs +++ b/taxy/src/proxy/http/route.rs @@ -5,7 +5,6 @@ use taxy_api::{ id::ShortId, proxy::{ProxyEntry, ProxyKind, Route, Server}, }; -use tokio_rustls::rustls::pki_types::ServerName; use tracing::error; use url::Url; use warp::host::Authority; @@ -63,7 +62,6 @@ pub struct FilteredRoute { #[derive(Debug)] pub struct ParsedRoute { - pub path: String, pub servers: Vec, } @@ -76,10 +74,7 @@ impl TryFrom for ParsedRoute { .into_iter() .map(ParsedServer::try_from) .collect::, _>>()?; - Ok(Self { - path: route.path, - servers, - }) + Ok(Self { servers }) } } @@ -87,7 +82,6 @@ impl TryFrom for ParsedRoute { pub struct ParsedServer { pub url: Url, pub authority: Authority, - pub server_name: ServerName<'static>, } impl TryFrom for ParsedServer { @@ -100,15 +94,9 @@ impl TryFrom for ParsedServer { let hostname = server.url.hostname().ok_or(Error::InvalidServerUrl { url: server.url.to_string(), })?; - let server_name = ServerName::try_from(hostname) - .map_err(|_| Error::InvalidServerUrl { - url: server.url.to_string(), - })? - .to_owned(); Ok(Self { url: server.url.into(), authority, - server_name, }) } } From c5a7b14ba313d04c21e9223304e7565bf2516564 Mon Sep 17 00:00:00 2001 From: picoHz <53327867+picoHz@users.noreply.github.com> Date: Wed, 14 Aug 2024 22:49:39 +0900 Subject: [PATCH 2/2] taxy-api: fix a build issue on wasm --- taxy-api/src/proxy.rs | 14 +++++--------- taxy/src/proxy/http/route.rs | 8 ++++++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/taxy-api/src/proxy.rs b/taxy-api/src/proxy.rs index 701f4b88..308a2607 100644 --- a/taxy-api/src/proxy.rs +++ b/taxy-api/src/proxy.rs @@ -1,14 +1,12 @@ -use std::fmt; -use std::str::FromStr; - use crate::error::Error; use crate::subject_name::SubjectName; use crate::{id::ShortId, port::UpstreamServer}; use serde_default::DefaultFromSerde; use serde_derive::{Deserialize, Serialize}; +use std::fmt; +use std::str::FromStr; use url::Url; use utoipa::ToSchema; -use warp::filters::host::Authority; #[derive(Debug, DefaultFromSerde, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)] pub struct Proxy { @@ -118,14 +116,12 @@ impl ServerUrl { self.0.host_str() } - pub fn authority(&self) -> Option { - format!( + pub fn authority(&self) -> Option { + Some(format!( "{}:{}", self.hostname()?, self.0.port_or_known_default().unwrap_or_default() - ) - .parse() - .ok() + )) } } diff --git a/taxy/src/proxy/http/route.rs b/taxy/src/proxy/http/route.rs index b8d79630..e8c375bf 100644 --- a/taxy/src/proxy/http/route.rs +++ b/taxy/src/proxy/http/route.rs @@ -1,5 +1,6 @@ use super::filter::{FilterResult, RequestFilter}; use hyper::Request; +use std::str::FromStr; use taxy_api::{ error::Error, id::ShortId, @@ -88,6 +89,7 @@ impl TryFrom for ParsedServer { type Error = Error; fn try_from(server: Server) -> Result { + let url = server.url.clone().into(); let authority = server.url.authority().ok_or(Error::InvalidServerUrl { url: server.url.to_string(), })?; @@ -95,8 +97,10 @@ impl TryFrom for ParsedServer { url: server.url.to_string(), })?; Ok(Self { - url: server.url.into(), - authority, + url, + authority: Authority::from_str(&authority).map_err(|_| Error::InvalidServerUrl { + url: hostname.to_owned(), + })?, }) } }