Reorginized config
This commit is contained in:
@@ -32,11 +32,14 @@ pub async fn cgi(
|
|||||||
envs.insert("QUERY_STRING", q);
|
envs.insert("QUERY_STRING", q);
|
||||||
}
|
}
|
||||||
|
|
||||||
if srv.cgienv.len() != 0 {
|
match &srv.server.cgienv {
|
||||||
for (k, v) in srv.cgienv.iter() {
|
Some(c) => {
|
||||||
|
for (k, v) in c.iter() {
|
||||||
envs.insert(&k, &v);
|
envs.insert(&k, &v);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
|
||||||
let cmd = Command::new(path.to_str().unwrap())
|
let cmd = Command::new(path.to_str().unwrap())
|
||||||
.env_clear()
|
.env_clear()
|
||||||
|
|||||||
@@ -27,17 +27,8 @@ pub struct Server {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ServerCfg {
|
pub struct ServerCfg {
|
||||||
pub hostname: String,
|
|
||||||
pub dir: String,
|
|
||||||
pub key: String,
|
|
||||||
pub cert: String,
|
|
||||||
pub index: String,
|
|
||||||
pub cgi: String,
|
|
||||||
pub cgienv: HashMap<String, String>,
|
|
||||||
pub usrdir: bool,
|
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
pub proxy: HashMap<String, String>,
|
pub server: Server,
|
||||||
pub redirect: HashMap<String, String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
@@ -49,50 +40,11 @@ impl Config {
|
|||||||
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();
|
||||||
for srv in &self.server {
|
for srv in &self.server {
|
||||||
let cgi = match srv.cgi.to_owned() {
|
|
||||||
Some(c) => c,
|
|
||||||
None => "".to_string(),
|
|
||||||
};
|
|
||||||
let usrdir = match srv.usrdir {
|
|
||||||
Some(u) => u,
|
|
||||||
None => false,
|
|
||||||
};
|
|
||||||
let mut pmap = HashMap::new();
|
|
||||||
match &srv.proxy {
|
|
||||||
Some(pr) => pmap = pr.clone(),
|
|
||||||
None => {}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut rmap = HashMap::new();
|
|
||||||
match &srv.redirect {
|
|
||||||
Some(r) => rmap = r.clone(),
|
|
||||||
None => {}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut cmap = HashMap::new();
|
|
||||||
match &srv.cgienv {
|
|
||||||
Some(c) => cmap = c.clone(),
|
|
||||||
None => {}
|
|
||||||
};
|
|
||||||
let index = match srv.index.to_owned() {
|
|
||||||
Some(i) => i,
|
|
||||||
None => "index.gemini".to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
map.insert(
|
map.insert(
|
||||||
srv.hostname.clone(),
|
srv.hostname.clone(),
|
||||||
ServerCfg {
|
ServerCfg {
|
||||||
hostname: srv.hostname.clone(),
|
|
||||||
dir: srv.dir.clone(),
|
|
||||||
key: srv.key.clone(),
|
|
||||||
cert: srv.cert.clone(),
|
|
||||||
index: index.to_string(),
|
|
||||||
cgi: cgi,
|
|
||||||
cgienv: cmap.clone(),
|
|
||||||
usrdir: usrdir,
|
|
||||||
port: self.port.clone(),
|
port: self.port.clone(),
|
||||||
proxy: pmap.clone(),
|
server: srv.clone(),
|
||||||
redirect: rmap.clone(),
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,8 @@ impl Connection {
|
|||||||
Some(m) => m,
|
Some(m) => m,
|
||||||
None => &stat.to_str(),
|
None => &stat.to_str(),
|
||||||
};
|
};
|
||||||
self.send_raw(format!("{}\t{}\r\n", stat as u8, meta).as_bytes()).await?;
|
self.send_raw(format!("{}\t{}\r\n", stat as u8, meta).as_bytes())
|
||||||
|
.await?;
|
||||||
if let Some(b) = body {
|
if let Some(b) = body {
|
||||||
self.send_raw(b.as_bytes()).await?;
|
self.send_raw(b.as_bytes()).await?;
|
||||||
}
|
}
|
||||||
|
|||||||
45
src/main.rs
45
src/main.rs
@@ -41,10 +41,10 @@ fn get_mime(path: &PathBuf) -> String {
|
|||||||
_ => {
|
_ => {
|
||||||
m = mime_guess::from_ext(ext).first().unwrap();
|
m = mime_guess::from_ext(ext).first().unwrap();
|
||||||
m.essence_str()
|
m.essence_str()
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return mime.to_string()
|
return mime.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
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<()> {
|
||||||
@@ -99,6 +99,10 @@ async fn handle_connection(
|
|||||||
mut con: conn::Connection,
|
mut con: conn::Connection,
|
||||||
srv: &config::ServerCfg,
|
srv: &config::ServerCfg,
|
||||||
) -> Result<(), io::Error> {
|
) -> Result<(), io::Error> {
|
||||||
|
let index = match &srv.server.index {
|
||||||
|
Some(i) => i.clone(),
|
||||||
|
None => "index.gemini".to_string(),
|
||||||
|
};
|
||||||
let mut buffer = [0; 1024];
|
let mut buffer = [0; 1024];
|
||||||
con.stream.read(&mut buffer).await?;
|
con.stream.read(&mut buffer).await?;
|
||||||
let mut request = match String::from_utf8(buffer[..].to_vec()) {
|
let mut request = match String::from_utf8(buffer[..].to_vec()) {
|
||||||
@@ -122,7 +126,7 @@ async fn handle_connection(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if Some(srv.hostname.as_str()) != url.host_str() {
|
if Some(srv.server.hostname.as_str()) != url.host_str() {
|
||||||
logger::logger(con.peer_addr, Status::ProxyRequestRefused, &request);
|
logger::logger(con.peer_addr, Status::ProxyRequestRefused, &request);
|
||||||
con.send_status(Status::ProxyRequestRefused, None).await?;
|
con.send_status(Status::ProxyRequestRefused, None).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -145,9 +149,10 @@ async fn handle_connection(
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if srv.redirect.len() != 0 {
|
match &srv.server.redirect {
|
||||||
|
Some(re) => {
|
||||||
let u = url.path().trim_end_matches("/");
|
let u = url.path().trim_end_matches("/");
|
||||||
match srv.redirect.get(u) {
|
match re.get(u) {
|
||||||
Some(r) => {
|
Some(r) => {
|
||||||
logger::logger(con.peer_addr, Status::RedirectTemporary, &request);
|
logger::logger(con.peer_addr, Status::RedirectTemporary, &request);
|
||||||
con.send_status(Status::RedirectTemporary, Some(r)).await?;
|
con.send_status(Status::RedirectTemporary, Some(r)).await?;
|
||||||
@@ -156,9 +161,11 @@ async fn handle_connection(
|
|||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if srv.proxy.len() != 0 {
|
None => {}
|
||||||
match url.path_segments().map(|c| c.collect::<Vec<_>>()) {
|
}
|
||||||
Some(s) => match srv.proxy.get(s[0]) {
|
match &srv.server.proxy {
|
||||||
|
Some(pr) => match url.path_segments().map(|c| c.collect::<Vec<_>>()) {
|
||||||
|
Some(s) => match pr.get(s[0]) {
|
||||||
Some(p) => {
|
Some(p) => {
|
||||||
revproxy::proxy(p.to_string(), url, con).await?;
|
revproxy::proxy(p.to_string(), url, con).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -166,12 +173,13 @@ async fn handle_connection(
|
|||||||
None => {}
|
None => {}
|
||||||
},
|
},
|
||||||
None => {}
|
None => {}
|
||||||
}
|
},
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut path = PathBuf::new();
|
let mut path = PathBuf::new();
|
||||||
|
|
||||||
if url.path().starts_with("/~") && srv.usrdir == true {
|
if url.path().starts_with("/~") && srv.server.usrdir.is_some() {
|
||||||
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();
|
||||||
path.push("/home/");
|
path.push("/home/");
|
||||||
@@ -181,7 +189,7 @@ async fn handle_connection(
|
|||||||
path.push(format!("{}/{}/", usr[0], "public_gemini"));
|
path.push(format!("{}/{}/", usr[0], "public_gemini"));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
path.push(&srv.dir);
|
path.push(&srv.server.dir);
|
||||||
if url.path() != "" || url.path() != "/" {
|
if url.path() != "" || url.path() != "/" {
|
||||||
path.push(url.path().trim_start_matches("/"));
|
path.push(url.path().trim_start_matches("/"));
|
||||||
}
|
}
|
||||||
@@ -208,8 +216,8 @@ async fn handle_connection(
|
|||||||
.await?;
|
.await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
if path.join(&srv.index).exists() {
|
if path.join(&index).exists() {
|
||||||
path.push(&srv.index);
|
path.push(index);
|
||||||
meta = fs::metadata(&path).expect("Unable to read metadata");
|
meta = fs::metadata(&path).expect("Unable to read metadata");
|
||||||
perm = meta.permissions();
|
perm = meta.permissions();
|
||||||
if perm.mode() & 0o0444 != 0o444 {
|
if perm.mode() & 0o0444 != 0o444 {
|
||||||
@@ -223,7 +231,9 @@ async fn handle_connection(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO add timeout
|
// TODO add timeout
|
||||||
if srv.cgi.trim_end_matches("/") == path.parent().unwrap().to_str().unwrap() {
|
match &srv.server.cgi {
|
||||||
|
Some(c) => {
|
||||||
|
if c.trim_end_matches("/") == path.parent().unwrap().to_str().unwrap() {
|
||||||
if perm.mode() & 0o0111 == 0o0111 {
|
if perm.mode() & 0o0111 == 0o0111 {
|
||||||
cgi::cgi(con, srv, path, url).await?;
|
cgi::cgi(con, srv, path, url).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -233,6 +243,9 @@ async fn handle_connection(
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
|
||||||
if perm.mode() & 0o0444 != 0o0444 {
|
if perm.mode() & 0o0444 != 0o0444 {
|
||||||
logger::logger(con.peer_addr, Status::NotFound, &request);
|
logger::logger(con.peer_addr, Status::NotFound, &request);
|
||||||
@@ -300,11 +313,9 @@ fn main() -> io::Result<()> {
|
|||||||
.expect("Couldn't accept");
|
.expect("Couldn't accept");
|
||||||
|
|
||||||
let srv = match stream.ssl().servername(NameType::HOST_NAME) {
|
let srv = match stream.ssl().servername(NameType::HOST_NAME) {
|
||||||
Some(s) => {
|
Some(s) => match cmap.get(s) {
|
||||||
match cmap.get(s) {
|
|
||||||
Some(ss) => ss,
|
Some(ss) => ss,
|
||||||
None => cmap.get(&default).unwrap(),
|
None => cmap.get(&default).unwrap(),
|
||||||
}
|
|
||||||
},
|
},
|
||||||
None => cmap.get(&default).unwrap(),
|
None => cmap.get(&default).unwrap(),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user