96 lines
2.6 KiB
Rust
96 lines
2.6 KiB
Rust
// Copyright (c) 2019 Reyk Floeter <contact@reykfloeter.com>
|
|
//
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
#[macro_use]
|
|
extern crate log;
|
|
|
|
mod cert;
|
|
mod client;
|
|
mod server;
|
|
|
|
use cert::KeyPair;
|
|
use log::LevelFilter;
|
|
use std::{
|
|
io::{Error, ErrorKind, Result},
|
|
net::SocketAddr,
|
|
path::{Path, PathBuf},
|
|
time::Duration,
|
|
};
|
|
use tokio_libtls::prelude::*;
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
pub(crate) struct Config {
|
|
keypair: Option<KeyPair>,
|
|
ca: Option<PathBuf>,
|
|
timeout: Option<Duration>,
|
|
servername: Option<String>,
|
|
address: Option<SocketAddr>,
|
|
}
|
|
|
|
impl Config {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
address: "[::1]:8023".parse().ok(),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
pub fn load_keys(&self) -> Result<(&Path, &Path, &Path)> {
|
|
let keypair = self
|
|
.keypair
|
|
.as_ref()
|
|
.ok_or(Error::new(ErrorKind::Other, "keypair"))?;
|
|
let key = keypair
|
|
.key
|
|
.as_ref()
|
|
.ok_or(Error::new(ErrorKind::Other, "key"))?;
|
|
let ca = self.ca.as_ref().ok_or(Error::new(ErrorKind::Other, "CA"))?;
|
|
Ok((&keypair.cert, key, ca))
|
|
}
|
|
|
|
pub fn load_server_options(&self) -> AsyncTlsOptions {
|
|
let mut options = AsyncTlsOptions::new();
|
|
if let Some(timeout) = self.timeout {
|
|
options.timeout(timeout);
|
|
}
|
|
if let Some(ref servername) = self.servername {
|
|
options.servername(servername);
|
|
} else {
|
|
options.servername("localhost");
|
|
}
|
|
options
|
|
}
|
|
|
|
pub fn load_client_options(&self) -> AsyncTlsOptions {
|
|
self.load_server_options()
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
let mut config = Config::new();
|
|
|
|
env_logger::builder()
|
|
.filter_level(LevelFilter::Debug)
|
|
.init();
|
|
|
|
let keypair = KeyPair::new();
|
|
config.ca = Some(keypair.cert.clone());
|
|
config.keypair = Some(keypair);
|
|
|
|
info!("{:?}", config);
|
|
|
|
server::run(config).await.expect("server");
|
|
}
|