1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::convert::Infallible;
use std::fs;
use std::future::Future;
use std::io::ErrorKind;
use std::net::SocketAddr;
use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Error, Method, Request, Response, Server, StatusCode};
use tokio::sync::oneshot;

/// Keyserver that can be used for testing. Shuts down when dropped.
pub struct Keyserver {
    url: String,
    // As long as the ShutdownHandle is alive, the server is running.
    // Dropping it shuts down the server.
    _handle: ShutdownHandle,
}

impl Keyserver {
    pub fn start() -> Keyserver {
        // 0 means random port
        Keyserver::start_on_port(0)
    }

    pub fn start_on_port(port: u16) -> Keyserver {
        let addr = ([127, 0, 0, 1], port).into();
        let (bound_addr, server, shutdown_handle) = server(&addr);
        tokio::spawn(async {
            server
                .await
                .unwrap_or_else(|e| eprintln!("server error: {}", e))
        });
        let url = format!("http://localhost:{}/", bound_addr.port());
        Keyserver {
            url,
            _handle: shutdown_handle,
        }
    }

    pub fn url(&self) -> &str {
        &self.url
    }

    pub async fn count(&self) -> String {
        reqwest::get(&format!("{}count", self.url()))
            .await
            .unwrap()
            .text()
            .await
            .unwrap()
    }
}

pub fn server(
    addr: &SocketAddr,
) -> (
    SocketAddr,
    impl Future<Output = Result<(), Error>>,
    ShutdownHandle,
) {
    let counter = Arc::new(AtomicUsize::new(0));
    // the function passed to make_service_fn is called once per connection
    let new_service = make_service_fn(move |_socket| {
        let counter = counter.clone();
        async {
            // the function passed to make_service is called once per connection
            Ok::<_, Infallible>(service_fn(move |r| {
                let counter = counter.clone();
                async move { Ok::<_, Infallible>(service(r, &counter).await) }
            }))
        }
    });

    let (tx, rx) = oneshot::channel::<()>();

    let server = Server::bind(addr).serve(new_service);
    let local_addr = server.local_addr();
    let server = server.with_graceful_shutdown(async {
        rx.await.ok();
    });
    (local_addr, server, ShutdownHandle { tx: Some(tx) })
}

pub struct ShutdownHandle {
    tx: Option<oneshot::Sender<()>>,
}

impl Drop for ShutdownHandle {
    fn drop(&mut self) {
        if let Some(sender) = self.tx.take() {
            let _ = sender.send(());
        }
    }
}

// Where the keys are stored.
const KEYS_PATH: &str = "support/keys/";

async fn service(request: Request<Body>, counter: &AtomicUsize) -> Response<Body> {
    match (request.method(), request.uri().path()) {
        (&Method::GET, "/count") => {
            Response::new(Body::from(counter.load(Ordering::Relaxed).to_string()))
        }

        (&Method::GET, key_path) => {
            let relative = Path::new(key_path).strip_prefix("/").unwrap();
            match fs::read(&Path::new(KEYS_PATH).join(&relative)) {
                Ok(content) => {
                    counter.fetch_add(1, Ordering::Relaxed);
                    Response::builder()
                        .header("Content-Type", "application/x-pem-file")
                        .body(Body::from(content))
                        .unwrap()
                }
                Err(e) => match e.kind() {
                    ErrorKind::NotFound => Response::builder()
                        .status(StatusCode::NOT_FOUND)
                        .body(Body::empty())
                        .unwrap(),
                    _ => {
                        eprintln!("Error reading file with path {:?}: {}", &key_path, e);
                        Response::builder()
                            .status(StatusCode::INTERNAL_SERVER_ERROR)
                            .body(Body::empty())
                            .unwrap()
                    }
                },
            }
        }

        _ => Response::builder()
            .status(StatusCode::NOT_FOUND)
            .body(Body::empty())
            .unwrap(),
    }
}