Revproxy can't connect

This commit is contained in:
int 80h
2020-05-08 16:02:55 -04:00
parent a066e54acc
commit 839c6bbf81
3 changed files with 61 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ pub struct Server {
pub cert: String,
pub cgi: Option<String>,
pub usrdir: Option<bool>,
pub proxy: Option<Vec<String>>,
}
#[derive(Debug, Clone)]
@@ -31,6 +32,7 @@ pub struct ServerCfg {
pub cgi: String,
pub usrdir: bool,
pub port: i32,
pub proxy: HashMap<String, String>,
}
impl Config {
@@ -50,6 +52,16 @@ impl Config {
Some(u) => u,
None => false,
};
let mut pmap = HashMap::new();
match &srv.proxy {
Some(pr) => {
for p in pr {
let p: Vec<&str> = p.split("=").collect();
pmap.insert(p[0].trim().to_string(), p[1].trim().to_string());
}
},
None => {},
};
map.insert(srv.hostname.clone(), ServerCfg {
hostname: srv.hostname.clone(),
dir: srv.dir.clone(),
@@ -58,6 +70,7 @@ impl Config {
cgi: cgi,
usrdir: usrdir,
port: self.port.clone(),
proxy: pmap,
});
}
map

View File

@@ -36,6 +36,7 @@ mod config;
mod status;
mod tls;
mod conn;
mod revproxy;
fn get_mime(path: &PathBuf) -> String {
let mut mime = "text/gemini";
@@ -124,6 +125,16 @@ async fn handle_connection(mut con: conn::Connection, srv: &config::ServerCfg) -
}
};
if srv.proxy.len() != 0 {
match srv.proxy.get(url.path()) {
Some(p) => {
revproxy::proxy(p.to_string(), url).await?;
return Ok(());
},
None => {},
}
}
if Some(srv.hostname.as_str()) != url.host_str() {
con.send_status(status::Status::ProxyRequestRefused, "Url doesn't match certificate!").await?;
return Ok(());

37
src/revproxy.rs Normal file
View File

@@ -0,0 +1,37 @@
use std::io;
use std::fs::File;
use std::sync::Arc;
use std::net::ToSocketAddrs;
use std::io::BufReader;
use futures_util::future;
use tokio::net::TcpStream;
use tokio::io::{
AsyncWriteExt,
copy, split,
};
use tokio_rustls::{ TlsConnector, rustls::ClientConfig, webpki::DNSNameRef };
use url::Url;
pub async fn proxy(addr: String, _u: url::Url) -> Result<(), io::Error> {
let addr = addr.to_socket_addrs()?
.next()
.ok_or_else(|| io::Error::from(io::ErrorKind::AddrNotAvailable))?;
let mut config = ClientConfig::new();
let mut pem = BufReader::new(File::open("cafile")?);
config.root_store.add_pem_file(&mut pem)
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid cert"))?;
let connector = TlsConnector::from(Arc::new(config));
let stream = TcpStream::connect(&addr).await?;
let domain = DNSNameRef::try_from_ascii_str("cbook.lan")
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid dnsname"))?;
println!("Connecting to agena...");
let mut stream = connector.connect(domain, stream).await?;
//stream.write_all(b"gopher://gopher.club/\r\n").await?;
//stream.flush().await?;
Ok(())
}