~jshholland/reltci

0b5e0310150b12b42607a293ef2806c2c80dd588 — Josh Holland 2 years ago 56c187d master
Factor more configuration into the dedicated struct
3 files changed, 28 insertions(+), 10 deletions(-)

M src/config.rs
M src/gemini.rs
M src/main.rs
M src/config.rs => src/config.rs +7 -1
@@ 1,4 1,10 @@
#[derive(Debug)]
use std::net::SocketAddr;
use std::path::PathBuf;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
    pub hostname: String,
    pub identity_file: PathBuf,
    pub identity_password: Option<String>,
    pub bind_addr: Vec<SocketAddr>,
}

M src/gemini.rs => src/gemini.rs +12 -8
@@ 5,6 5,8 @@ use native_tls::{Identity, TlsAcceptor, TlsStream};
use thiserror::Error;
use url::Url;

use crate::config::Config;

const MAX_URL: usize = 1024;
const MAX_META: usize = 1024;



@@ 148,18 150,20 @@ impl Response {

#[derive(Debug, Clone)]
pub struct Server {
    /// The hostname we are serving content for.
    pub hostname: String,
    pub config: Config,
}

impl Server {
    pub fn new(hostname: String) -> Self {
        Server { hostname }
    pub fn new(config: Config) -> Self {
        Server { config }
    }

    pub fn run(&mut self) -> anyhow::Result<()> {
        let id = Identity::from_pkcs12(&std::fs::read("identity.pfx")?, "")?;
        let listener = TcpListener::bind("localhost:1965")?;
        let id = Identity::from_pkcs12(
            &std::fs::read(&self.config.identity_file)?,
            self.config.identity_password.as_deref().unwrap_or(""),
        )?;
        let listener = TcpListener::bind(&self.config.bind_addr[..])?;
        let acceptor = TlsAcceptor::new(id)?;
        for stream in listener.incoming() {
            let accepted = acceptor.accept(stream?)?;


@@ 183,10 187,10 @@ impl Server {
                return Err(e.into());
            }
        };
        if url.host_str().unwrap() != self.hostname {
        if url.host_str().unwrap() != self.config.hostname {
            let resp = Response {
                status: Status::ProxyRefused,
                meta: format!("will only serve requests for {}", self.hostname),
                meta: format!("will only serve requests for {}", self.config.hostname),
                body: Vec::new(),
            };
            resp.send(&mut stream)?;

M src/main.rs => src/main.rs +9 -1
@@ 1,7 1,15 @@
use std::net::ToSocketAddrs;

mod config;
mod gemini;

fn main() -> anyhow::Result<()> {
    let mut server = gemini::Server::new("localhost".to_string());
    let config = config::Config {
        hostname: "localhost".into(),
        identity_file: "identity.pfx".into(),
        identity_password: None,
        bind_addr: "localhost:1965".to_socket_addrs()?.collect(),
    };
    let mut server = gemini::Server::new(config);
    server.run()
}