Cleaning up code
This commit is contained in:
@@ -2,10 +2,9 @@ image: alpine/edge
|
|||||||
packages:
|
packages:
|
||||||
- rust
|
- rust
|
||||||
- cargo
|
- cargo
|
||||||
- libressl-dev
|
|
||||||
sources:
|
sources:
|
||||||
- https://git.sr.ht/~int80h/gemserv
|
- https://git.sr.ht/~int80h/gemserv
|
||||||
tasks:
|
tasks:
|
||||||
- build: |
|
- build: |
|
||||||
cd gemserv/
|
cd gemserv/
|
||||||
cargo build --release
|
cargo build
|
||||||
|
|||||||
4
Cargo.lock
generated
4
Cargo.lock
generated
@@ -272,9 +272,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.108"
|
version = "0.2.109"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119"
|
checksum = "f98a04dce437184842841303488f70d0188c5f51437d2a834dc097eafa909a01"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
|
|||||||
20
src/cgi.rs
20
src/cgi.rs
@@ -54,8 +54,7 @@ fn envs(
|
|||||||
|
|
||||||
if let Some(cert) = session.peer_certificates() {
|
if let Some(cert) = session.peer_certificates() {
|
||||||
let cert = tokio_rustls::rustls::Certificate::as_ref(&cert[0]);
|
let cert = tokio_rustls::rustls::Certificate::as_ref(&cert[0]);
|
||||||
match x509_parser::parse_x509_certificate(cert) {
|
if let Ok((_, x509)) = x509_parser::parse_x509_certificate(cert) {
|
||||||
Ok((_, x509)) => {
|
|
||||||
let user = x509
|
let user = x509
|
||||||
.subject()
|
.subject()
|
||||||
.iter_common_name()
|
.iter_common_name()
|
||||||
@@ -65,9 +64,7 @@ fn envs(
|
|||||||
|
|
||||||
envs.insert("AUTH_TYPE".to_string(), "Certificate".to_string());
|
envs.insert("AUTH_TYPE".to_string(), "Certificate".to_string());
|
||||||
envs.insert("REMOTE_USER".to_string(), user.to_string());
|
envs.insert("REMOTE_USER".to_string(), user.to_string());
|
||||||
envs.insert("TLS_CLIENT_HASH".to_string(), util::fingerhex(&cert));
|
envs.insert("TLS_CLIENT_HASH".to_string(), util::fingerhex(cert));
|
||||||
}
|
|
||||||
Err(_) => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,16 +106,13 @@ pub async fn cgi(
|
|||||||
path_info: String,
|
path_info: String,
|
||||||
) -> Result<(), io::Error> {
|
) -> Result<(), io::Error> {
|
||||||
let (_, session) = con.stream.get_ref();
|
let (_, session) = con.stream.get_ref();
|
||||||
let mut envs = envs(con.peer_addr, session, &con.srv, &url);
|
let mut envs = envs(con.peer_addr, session, &con.srv, url);
|
||||||
envs.insert("SCRIPT_NAME".into(), script_name);
|
envs.insert("SCRIPT_NAME".into(), script_name);
|
||||||
envs.insert("PATH_INFO".into(), path_info);
|
envs.insert("PATH_INFO".into(), path_info);
|
||||||
|
|
||||||
match path.parent() {
|
if let Some(p) = path.parent() {
|
||||||
Some(p) => {
|
|
||||||
std::env::set_current_dir(p)?;
|
std::env::set_current_dir(p)?;
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
let cmd = Command::new(path.to_str().unwrap())
|
let cmd = Command::new(path.to_str().unwrap())
|
||||||
.env_clear()
|
.env_clear()
|
||||||
@@ -154,7 +148,7 @@ pub async fn cgi(
|
|||||||
}
|
}
|
||||||
|
|
||||||
con.send_raw(&cmd).await?;
|
con.send_raw(&cmd).await?;
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "scgi")]
|
#[cfg(feature = "scgi")]
|
||||||
@@ -175,12 +169,12 @@ pub async fn scgi(addr: String, u: url::Url, mut con: conn::Connection) -> Resul
|
|||||||
let (_, session) = con.stream.get_ref();
|
let (_, session) = con.stream.get_ref();
|
||||||
let envs = envs(con.peer_addr, session, &con.srv, &u);
|
let envs = envs(con.peer_addr, session, &con.srv, &u);
|
||||||
let len = 0usize;
|
let len = 0usize;
|
||||||
let mut byt = String::from(format!(
|
let mut byt = format!(
|
||||||
"CONTENT_LENGTH\x00{}\x00SCGI\x001\x00
|
"CONTENT_LENGTH\x00{}\x00SCGI\x001\x00
|
||||||
RQUEST_METHOD\x00POST\x00REQUEST_URI\x00{}\x00",
|
RQUEST_METHOD\x00POST\x00REQUEST_URI\x00{}\x00",
|
||||||
len,
|
len,
|
||||||
u.path()
|
u.path()
|
||||||
));
|
);
|
||||||
for (k, v) in envs.iter() {
|
for (k, v) in envs.iter() {
|
||||||
byt.push_str(&format!("{}\x00{}\x00", k, v));
|
byt.push_str(&format!("{}\x00{}\x00", k, v));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
use new_mime_guess;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, BufRead, BufReader};
|
use std::io::{self, BufRead, BufReader};
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
#[cfg(any(feature = "cgi", feature = "scgi"))]
|
#[cfg(any(feature = "cgi", feature = "scgi"))]
|
||||||
@@ -17,7 +16,7 @@ use crate::util;
|
|||||||
|
|
||||||
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
|
||||||
fn get_mime(path: &PathBuf) -> String {
|
fn get_mime(path: &Path) -> String {
|
||||||
let mut mime = "text/gemini".to_string();
|
let mut mime = "text/gemini".to_string();
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
return mime;
|
return mime;
|
||||||
@@ -32,7 +31,7 @@ fn get_mime(path: &PathBuf) -> String {
|
|||||||
None => "text/plain".to_string(),
|
None => "text/plain".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return mime;
|
mime
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_binary(mut con: conn::Connection, path: PathBuf, meta: String) -> io::Result<()> {
|
async fn get_binary(mut con: conn::Connection, path: PathBuf, meta: String) -> io::Result<()> {
|
||||||
@@ -63,8 +62,7 @@ async fn get_content(path: PathBuf, u: &url::Url) -> Result<String> {
|
|||||||
let mut files: Vec<String> = Vec::new();
|
let mut files: Vec<String> = Vec::new();
|
||||||
|
|
||||||
// needs work
|
// needs work
|
||||||
for file in fs::read_dir(&path)? {
|
for file in (fs::read_dir(&path)?).flatten() {
|
||||||
if let Ok(file) = file {
|
|
||||||
let m = file.metadata()?;
|
let m = file.metadata()?;
|
||||||
let perm = m.permissions();
|
let perm = m.permissions();
|
||||||
if perm.mode() & 0o0444 != 0o0444 {
|
if perm.mode() & 0o0444 != 0o0444 {
|
||||||
@@ -86,7 +84,6 @@ async fn get_content(path: PathBuf, u: &url::Url) -> Result<String> {
|
|||||||
files.push(format!("=> {} {}\r\n", ep, p.display()));
|
files.push(format!("=> {} {}\r\n", ep, p.display()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
dirs.sort();
|
dirs.sort();
|
||||||
files.sort();
|
files.sort();
|
||||||
@@ -101,7 +98,7 @@ async fn get_content(path: PathBuf, u: &url::Url) -> Result<String> {
|
|||||||
list.push_str(&file);
|
list.push_str(&file);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(list);
|
Ok(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle CGI and return Ok(true), or indicate this request wasn't for CGI with Ok(false)
|
// Handle CGI and return Ok(true), or indicate this request wasn't for CGI with Ok(false)
|
||||||
@@ -166,16 +163,13 @@ pub async fn handle_connection(mut con: conn::Connection, url: url::Url) -> Resu
|
|||||||
Some(re) => {
|
Some(re) => {
|
||||||
let u = match url.path() {
|
let u = match url.path() {
|
||||||
"/" => "/",
|
"/" => "/",
|
||||||
_ => url.path().trim_end_matches("/"),
|
_ => url.path().trim_end_matches('/'),
|
||||||
};
|
};
|
||||||
match re.get(u) {
|
if let Some(r) = re.get(u) {
|
||||||
Some(r) => {
|
logger::logger(con.peer_addr, Status::RedirectTemporary, url.as_str());
|
||||||
logger::logger(con.peer_addr, Status::RedirectTemporary, &url.as_str());
|
|
||||||
con.send_status(Status::RedirectTemporary, Some(r)).await?;
|
con.send_status(Status::RedirectTemporary, Some(r)).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
@@ -202,12 +196,9 @@ pub async fn handle_connection(mut con: conn::Connection, url: url::Url) -> Resu
|
|||||||
#[cfg(feature = "proxy")]
|
#[cfg(feature = "proxy")]
|
||||||
match &con.srv.server.proxy {
|
match &con.srv.server.proxy {
|
||||||
Some(pr) => match url.path_segments().map(|c| c.collect::<Vec<_>>()) {
|
Some(pr) => match url.path_segments().map(|c| c.collect::<Vec<_>>()) {
|
||||||
Some(s) => match pr.get(s[0]) {
|
Some(s) => if let Some(p) = pr.get(s[0]) {
|
||||||
Some(p) => {
|
|
||||||
revproxy::proxy(p.to_string(), url, con).await?;
|
revproxy::proxy(p.to_string(), url, con).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
|
||||||
None => {}
|
|
||||||
},
|
},
|
||||||
None => {}
|
None => {}
|
||||||
},
|
},
|
||||||
@@ -219,15 +210,12 @@ pub async fn handle_connection(mut con: conn::Connection, url: url::Url) -> Resu
|
|||||||
Some(sc) => {
|
Some(sc) => {
|
||||||
let u = match url.path() {
|
let u = match url.path() {
|
||||||
"/" => "/",
|
"/" => "/",
|
||||||
_ => url.path().trim_end_matches("/"),
|
_ => url.path().trim_end_matches('/'),
|
||||||
};
|
};
|
||||||
match sc.get(u) {
|
if let Some(r) = sc.get(u) {
|
||||||
Some(r) => {
|
|
||||||
cgi::scgi(r.to_string(), url, con).await?;
|
cgi::scgi(r.to_string(), url, con).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
@@ -236,7 +224,7 @@ pub async fn handle_connection(mut con: conn::Connection, url: url::Url) -> Resu
|
|||||||
|
|
||||||
if url.path().starts_with("/~") && con.srv.server.usrdir.unwrap_or(false) {
|
if url.path().starts_with("/~") && con.srv.server.usrdir.unwrap_or(false) {
|
||||||
let usr = url.path().trim_start_matches("/~");
|
let usr = url.path().trim_start_matches("/~");
|
||||||
let usr: Vec<&str> = usr.splitn(2, "/").collect();
|
let usr: Vec<&str> = usr.splitn(2, '/').collect();
|
||||||
if cfg!(target_os = "macos") {
|
if cfg!(target_os = "macos") {
|
||||||
path.push("/Users/");
|
path.push("/Users/");
|
||||||
} else {
|
} else {
|
||||||
@@ -255,7 +243,7 @@ pub async fn handle_connection(mut con: conn::Connection, url: url::Url) -> Resu
|
|||||||
} else {
|
} else {
|
||||||
path.push(&con.srv.server.dir);
|
path.push(&con.srv.server.dir);
|
||||||
if url.path() != "" || url.path() != "/" {
|
if url.path() != "" || url.path() != "/" {
|
||||||
let decoded = util::url_decode(url.path().trim_start_matches("/").as_bytes());
|
let decoded = util::url_decode(url.path().trim_start_matches('/').as_bytes());
|
||||||
path.push(decoded);
|
path.push(decoded);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -263,11 +251,11 @@ pub async fn handle_connection(mut con: conn::Connection, url: url::Url) -> Resu
|
|||||||
if !path.exists() {
|
if !path.exists() {
|
||||||
// See if it's a subpath of a CGI script before returning NotFound
|
// See if it's a subpath of a CGI script before returning NotFound
|
||||||
#[cfg(feature = "cgi")]
|
#[cfg(feature = "cgi")]
|
||||||
if handle_cgi(&mut con, &url.as_str(), &url, &path).await? {
|
if handle_cgi(&mut con, url.as_str(), &url, &path).await? {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
logger::logger(con.peer_addr, Status::NotFound, &url.as_str());
|
logger::logger(con.peer_addr, Status::NotFound, url.as_str());
|
||||||
con.send_status(Status::NotFound, None).await?;
|
con.send_status(Status::NotFound, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@@ -278,8 +266,8 @@ pub async fn handle_connection(mut con: conn::Connection, url: url::Url) -> Resu
|
|||||||
// TODO fix me
|
// TODO fix me
|
||||||
// This block is terrible
|
// This block is terrible
|
||||||
if meta.is_dir() {
|
if meta.is_dir() {
|
||||||
if !url.path().ends_with("/") {
|
if !url.path().ends_with('/') {
|
||||||
logger::logger(con.peer_addr, Status::RedirectPermanent, &url.as_str());
|
logger::logger(con.peer_addr, Status::RedirectPermanent, url.as_str());
|
||||||
con.send_status(
|
con.send_status(
|
||||||
Status::RedirectPermanent,
|
Status::RedirectPermanent,
|
||||||
Some(format!("{}/", url).as_str()),
|
Some(format!("{}/", url).as_str()),
|
||||||
@@ -302,18 +290,18 @@ pub async fn handle_connection(mut con: conn::Connection, url: url::Url) -> Resu
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cgi")]
|
#[cfg(feature = "cgi")]
|
||||||
if handle_cgi(&mut con, &url.as_str(), &url, &path).await? {
|
if handle_cgi(&mut con, url.as_str(), &url, &path).await? {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if meta.is_file() && perm.mode() & 0o0111 == 0o0111 {
|
if meta.is_file() && perm.mode() & 0o0111 == 0o0111 {
|
||||||
logger::logger(con.peer_addr, Status::NotFound, &url.as_str());
|
logger::logger(con.peer_addr, Status::NotFound, url.as_str());
|
||||||
con.send_status(Status::NotFound, None).await?;
|
con.send_status(Status::NotFound, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if perm.mode() & 0o0444 != 0o0444 {
|
if perm.mode() & 0o0444 != 0o0444 {
|
||||||
logger::logger(con.peer_addr, Status::NotFound, &url.as_str());
|
logger::logger(con.peer_addr, Status::NotFound, url.as_str());
|
||||||
con.send_status(Status::NotFound, None).await?;
|
con.send_status(Status::NotFound, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@@ -323,14 +311,14 @@ pub async fn handle_connection(mut con: conn::Connection, url: url::Url) -> Resu
|
|||||||
mime += &("; lang=".to_string() + &con.srv.server.lang.to_owned().unwrap());
|
mime += &("; lang=".to_string() + &con.srv.server.lang.to_owned().unwrap());
|
||||||
}
|
}
|
||||||
if !mime.starts_with("text/") {
|
if !mime.starts_with("text/") {
|
||||||
logger::logger(con.peer_addr, Status::Success, &url.as_str());
|
logger::logger(con.peer_addr, Status::Success, url.as_str());
|
||||||
get_binary(con, path, mime).await?;
|
get_binary(con, path, mime).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
let content = get_content(path, &url).await?;
|
let content = get_content(path, &url).await?;
|
||||||
con.send_body(Status::Success, Some(&mime), Some(content))
|
con.send_body(Status::Success, Some(&mime), Some(content))
|
||||||
.await?;
|
.await?;
|
||||||
logger::logger(con.peer_addr, Status::Success, &url.as_str());
|
logger::logger(con.peer_addr, Status::Success, url.as_str());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,9 +90,9 @@ impl Config {
|
|||||||
} else if config.interface.is_some() {
|
} else if config.interface.is_some() {
|
||||||
return Ok(config);
|
return Ok(config);
|
||||||
}
|
}
|
||||||
return Err(Box::new(errors::GemError(
|
Err(Box::new(errors::GemError(
|
||||||
"You need to specify either host/port or interface".into(),
|
"You need to specify either host/port or interface".into(),
|
||||||
)));
|
)))
|
||||||
}
|
}
|
||||||
pub fn to_map(&self) -> HashMap<String, ServerCfg> {
|
pub fn to_map(&self) -> HashMap<String, ServerCfg> {
|
||||||
let mut map = HashMap::new();
|
let mut map = HashMap::new();
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ impl Connection {
|
|||||||
) -> Result<(), io::Error> {
|
) -> Result<(), io::Error> {
|
||||||
let meta = match meta {
|
let meta = match meta {
|
||||||
Some(m) => m,
|
Some(m) => m,
|
||||||
None => &stat.to_str(),
|
None => stat.to_str(),
|
||||||
};
|
};
|
||||||
self.send_raw(format!("{} {}\r\n", stat as u8, meta).as_bytes())
|
self.send_raw(format!("{} {}\r\n", stat as u8, meta).as_bytes())
|
||||||
.await?;
|
.await?;
|
||||||
|
|||||||
@@ -169,9 +169,9 @@ pub async fn get_request(mut con: conn::Connection) -> Result<(conn::Connection,
|
|||||||
request = request.replacen("//", "gemini://", 1);
|
request = request.replacen("//", "gemini://", 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.ends_with("\n") {
|
if request.ends_with('\n') {
|
||||||
request.pop();
|
request.pop();
|
||||||
if request.ends_with("\r") {
|
if request.ends_with('\r') {
|
||||||
request.pop();
|
request.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,36 +187,30 @@ pub async fn get_request(mut con: conn::Connection) -> Result<(conn::Connection,
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match url.host_str() {
|
if let Some(h) = url.host_str() {
|
||||||
Some(h) => {
|
|
||||||
if con.srv.server.hostname.as_str() != h.to_lowercase() {
|
if con.srv.server.hostname.as_str() != h.to_lowercase() {
|
||||||
logger::logger(con.peer_addr, Status::ProxyRequestRefused, &url.as_str());
|
logger::logger(con.peer_addr, Status::ProxyRequestRefused, url.as_str());
|
||||||
con.send_status(Status::ProxyRequestRefused, None)
|
con.send_status(Status::ProxyRequestRefused, None)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
return Err(Box::new(GemError("Wrong host".into())));
|
return Err(Box::new(GemError("Wrong host".into())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {}
|
if let Some(p) = url.port() {
|
||||||
}
|
|
||||||
match url.port() {
|
|
||||||
Some(p) => {
|
|
||||||
if p != con.local_addr.port() {
|
if p != con.local_addr.port() {
|
||||||
logger::logger(con.peer_addr, Status::ProxyRequestRefused, &url.as_str());
|
logger::logger(con.peer_addr, Status::ProxyRequestRefused, url.as_str());
|
||||||
con.send_status(Status::ProxyRequestRefused, None)
|
con.send_status(Status::ProxyRequestRefused, None)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
if url.scheme() != "gemini" {
|
if url.scheme() != "gemini" {
|
||||||
logger::logger(con.peer_addr, Status::ProxyRequestRefused, &url.as_str());
|
logger::logger(con.peer_addr, Status::ProxyRequestRefused, url.as_str());
|
||||||
con.send_status(Status::ProxyRequestRefused, None)
|
con.send_status(Status::ProxyRequestRefused, None)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| e.to_string())?;
|
.map_err(|e| e.to_string())?;
|
||||||
return Err(Box::new(GemError("scheme not gemini".into())));
|
return Err(Box::new(GemError("scheme not gemini".into())));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok((con, url));
|
Ok((con, url))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ pub enum Status {
|
|||||||
|
|
||||||
impl Status {
|
impl Status {
|
||||||
pub fn to_str(&self) -> &str {
|
pub fn to_str(&self) -> &str {
|
||||||
let meta = match self {
|
match self {
|
||||||
Status::Input => "Input",
|
Status::Input => "Input",
|
||||||
Status::Success => "Success",
|
Status::Success => "Success",
|
||||||
Status::SuccessEndOfSession => "Success End Of Session",
|
Status::SuccessEndOfSession => "Success End Of Session",
|
||||||
@@ -51,8 +51,7 @@ impl Status {
|
|||||||
Status::CertificateNotAccepted => "Certificate Not Accepted",
|
Status::CertificateNotAccepted => "Certificate Not Accepted",
|
||||||
Status::FutureCertificateRejected => "Future Certificate Rejected",
|
Status::FutureCertificateRejected => "Future Certificate Rejected",
|
||||||
Status::ExpiredCertificateRejected => "Expired Certificate Rejected",
|
Status::ExpiredCertificateRejected => "Expired Certificate Rejected",
|
||||||
};
|
}
|
||||||
return meta;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,13 +27,13 @@ pub fn tls_acceptor_conf(cfg: config::Config) -> io::Result<TlsAcceptor> {
|
|||||||
Ok(acceptor)
|
Ok(acceptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_certs(path: &String) -> io::Result<Vec<Certificate>> {
|
pub fn load_certs(path: &str) -> io::Result<Vec<Certificate>> {
|
||||||
certs(&mut BufReader::new(File::open(path)?))
|
certs(&mut BufReader::new(File::open(path)?))
|
||||||
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid cert"))
|
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid cert"))
|
||||||
.map(|mut certs| certs.drain(..).map(Certificate).collect())
|
.map(|mut certs| certs.drain(..).map(Certificate).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_key(path: &String) -> io::Result<Vec<PrivateKey>> {
|
fn load_key(path: &str) -> io::Result<Vec<PrivateKey>> {
|
||||||
pkcs8_private_keys(&mut std::io::BufReader::new(std::fs::File::open(path)?))
|
pkcs8_private_keys(&mut std::io::BufReader::new(std::fs::File::open(path)?))
|
||||||
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
|
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
|
||||||
.map(|mut keys| keys.drain(..).map(PrivateKey).collect())
|
.map(|mut keys| keys.drain(..).map(PrivateKey).collect())
|
||||||
|
|||||||
@@ -5,8 +5,9 @@ pub fn url_decode(url: &[u8]) -> String {
|
|||||||
let decoded: String = form_urlencoded::parse(url)
|
let decoded: String = form_urlencoded::parse(url)
|
||||||
.map(|(key, val)| [key, val].concat())
|
.map(|(key, val)| [key, val].concat())
|
||||||
.collect();
|
.collect();
|
||||||
return decoded;
|
decoded
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fingerhex(x509: &[u8]) -> String {
|
pub fn fingerhex(x509: &[u8]) -> String {
|
||||||
let mut finger = sha2::Sha256::new();
|
let mut finger = sha2::Sha256::new();
|
||||||
finger.update(&x509);
|
finger.update(&x509);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ pub fn init(loglev: &Option<String>) -> errors::Result {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
simple_logger::init_with_level(loglev).unwrap();
|
simple_logger::init_with_level(loglev).unwrap();
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn logger(addr: SocketAddr, stat: status::Status, req: &str) {
|
pub fn logger(addr: SocketAddr, stat: status::Status, req: &str) {
|
||||||
|
|||||||
@@ -73,5 +73,5 @@ async fn main() -> errors::Result {
|
|||||||
{
|
{
|
||||||
return Err(e);
|
return Err(e);
|
||||||
};
|
};
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,13 +15,13 @@ use crate::status::Status;
|
|||||||
use crate::tls;
|
use crate::tls;
|
||||||
|
|
||||||
pub async fn proxy(addr: String, u: url::Url, mut con: conn::Connection) -> Result<(), io::Error> {
|
pub async fn proxy(addr: String, u: url::Url, mut con: conn::Connection) -> Result<(), io::Error> {
|
||||||
let p: Vec<&str> = u.path().trim_start_matches("/").splitn(2, "/").collect();
|
let p: Vec<&str> = u.path().trim_start_matches('/').splitn(2, '/').collect();
|
||||||
if p.len() == 1 {
|
if p.len() == 1 {
|
||||||
logger::logger(con.peer_addr, Status::NotFound, u.as_str());
|
logger::logger(con.peer_addr, Status::NotFound, u.as_str());
|
||||||
con.send_status(Status::NotFound, None).await?;
|
con.send_status(Status::NotFound, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if p[1] == "" || p[1] == "/" {
|
if p[1].is_empty() || p[1] == "/" {
|
||||||
logger::logger(con.peer_addr, Status::NotFound, u.as_str());
|
logger::logger(con.peer_addr, Status::NotFound, u.as_str());
|
||||||
con.send_status(Status::NotFound, None).await?;
|
con.send_status(Status::NotFound, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|||||||
Reference in New Issue
Block a user