move ProxyConfig to proxmox_http

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
Fabian Grünbichler 2021-05-14 15:44:54 +02:00 committed by Dietmar Maurer
parent 3ed7e87538
commit 4229633d98
5 changed files with 10 additions and 85 deletions

View File

@ -7,9 +7,11 @@ use proxmox::api::{api, RpcEnvironment, RpcEnvironmentType, Permission};
use proxmox::api::router::{Router, SubdirMap}; use proxmox::api::router::{Router, SubdirMap};
use proxmox::tools::fs::{replace_file, CreateOptions}; use proxmox::tools::fs::{replace_file, CreateOptions};
use proxmox_http::http::ProxyConfig;
use crate::config::node; use crate::config::node;
use crate::server::WorkerTask; use crate::server::WorkerTask;
use crate::tools::{apt, SimpleHttp, http::ProxyConfig, subscription}; use crate::tools::{apt, SimpleHttp, subscription};
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY}; use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
use crate::api2::types::{Authid, APTUpdateInfo, NODE_SCHEMA, UPID_SCHEMA}; use crate::api2::types::{Authid, APTUpdateInfo, NODE_SCHEMA, UPID_SCHEMA};

View File

@ -1660,7 +1660,7 @@ pub struct NodeStatus {
pub const HTTP_PROXY_SCHEMA: Schema = StringSchema::new( pub const HTTP_PROXY_SCHEMA: Schema = StringSchema::new(
"HTTP proxy configuration [http://]<host>[:port]") "HTTP proxy configuration [http://]<host>[:port]")
.format(&ApiStringFormat::VerifyFn(|s| { .format(&ApiStringFormat::VerifyFn(|s| {
crate::tools::http::ProxyConfig::parse_proxy_url(s)?; proxmox_http::http::ProxyConfig::parse_proxy_url(s)?;
Ok(()) Ok(())
})) }))
.min_length(1) .min_length(1)

View File

@ -10,11 +10,12 @@ use proxmox::api::api;
use proxmox::api::schema::{ApiStringFormat, Updater}; use proxmox::api::schema::{ApiStringFormat, Updater};
use proxmox::tools::fs::{replace_file, CreateOptions}; use proxmox::tools::fs::{replace_file, CreateOptions};
use proxmox_http::http::ProxyConfig;
use crate::acme::AcmeClient; use crate::acme::AcmeClient;
use crate::api2::types::{ use crate::api2::types::{
AcmeAccountName, AcmeDomain, ACME_DOMAIN_PROPERTY_SCHEMA, HTTP_PROXY_SCHEMA, AcmeAccountName, AcmeDomain, ACME_DOMAIN_PROPERTY_SCHEMA, HTTP_PROXY_SCHEMA,
}; };
use crate::tools::http::ProxyConfig;
const CONF_FILE: &str = configdir!("/node.cfg"); const CONF_FILE: &str = configdir!("/node.cfg");
const LOCK_FILE: &str = configdir!("/.node.lck"); const LOCK_FILE: &str = configdir!("/.node.lck");

View File

@ -19,7 +19,7 @@ use tokio::{
use tokio_openssl::SslStream; use tokio_openssl::SslStream;
use proxmox::sys::linux::socket::set_tcp_keepalive; use proxmox::sys::linux::socket::set_tcp_keepalive;
use proxmox_http::http::MaybeTlsStream; use proxmox_http::http::{MaybeTlsStream, ProxyConfig};
// Build a http::uri::Authority ("host:port"), use '[..]' around IPv6 addresses // Build a http::uri::Authority ("host:port"), use '[..]' around IPv6 addresses
pub(crate) fn build_authority(host: &str, port: u16) -> Result<Authority, Error> { pub(crate) fn build_authority(host: &str, port: u16) -> Result<Authority, Error> {
@ -33,86 +33,6 @@ pub(crate) fn build_authority(host: &str, port: u16) -> Result<Authority, Error>
Ok(authority) Ok(authority)
} }
/// HTTP Proxy Configuration
#[derive(Clone)]
pub struct ProxyConfig {
pub host: String,
pub port: u16,
pub authorization: Option<String>, // user:pass
pub force_connect: bool,
}
impl ProxyConfig {
/// Parse proxy config from ALL_PROXY environment var
pub fn from_proxy_env() -> Result<Option<ProxyConfig>, Error> {
// We only support/use ALL_PROXY environment
match std::env::var_os("ALL_PROXY") {
None => return Ok(None),
Some(all_proxy) => {
let all_proxy = match all_proxy.to_str() {
Some(s) => String::from(s),
None => bail!("non UTF-8 content in env ALL_PROXY"),
};
if all_proxy.is_empty() {
return Ok(None);
}
let config = Self::parse_proxy_url(&all_proxy)?;
Ok(Some(config))
}
}
}
/// Parse proxy configuration string [http://]<host>[:port]
///
/// Default port is 1080 (like curl)
pub fn parse_proxy_url(http_proxy: &str) -> Result<ProxyConfig, Error> {
proxmox::try_block!({
let proxy_uri: Uri = http_proxy.parse()?;
let proxy_authority = match proxy_uri.authority() {
Some(authority) => authority,
None => bail!("missing proxy authority"),
};
let host = proxy_authority.host().to_owned();
let port = match proxy_uri.port() {
Some(port) => port.as_u16(),
None => 1080, // CURL default port
};
match proxy_uri.scheme_str() {
Some("http") => { /* Ok */ }
Some(scheme) => bail!("unsupported proxy scheme '{}'", scheme),
None => { /* assume HTTP */ }
}
let authority_vec: Vec<&str> = proxy_authority.as_str().rsplitn(2, '@').collect();
let authorization = if authority_vec.len() == 2 {
Some(authority_vec[1].to_string())
} else {
None
};
Ok(ProxyConfig {
host,
port,
authorization,
force_connect: false,
})
}).map_err(|err| format_err!("parse_proxy_url failed: {}", err))
}
/// Assemble canonical proxy string (including scheme and port)
pub fn to_proxy_string(&self) -> Result<String, Error> {
let authority = build_authority(&self.host, self.port)?;
Ok(match self.authorization {
None => format!("http://{}", authority),
Some(ref authorization) => format!("http://{}@{}", authorization, authority)
})
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct HttpsConnector { pub struct HttpsConnector {
connector: HttpConnector, connector: HttpConnector,

View File

@ -7,8 +7,10 @@ use http::{Request, Response, HeaderValue};
use openssl::ssl::{SslConnector, SslMethod}; use openssl::ssl::{SslConnector, SslMethod};
use futures::*; use futures::*;
use proxmox_http::http::ProxyConfig;
use crate::tools::PROXMOX_BACKUP_TCP_KEEPALIVE_TIME; use crate::tools::PROXMOX_BACKUP_TCP_KEEPALIVE_TIME;
use crate::tools::http::{HttpsConnector, ProxyConfig}; use crate::tools::http::HttpsConnector;
/// Asyncrounous HTTP client implementation /// Asyncrounous HTTP client implementation
pub struct SimpleHttp { pub struct SimpleHttp {