Skip to content

Commit

Permalink
feat: add TLS support
Browse files Browse the repository at this point in the history
  • Loading branch information
ravenclaw900 committed Nov 3, 2021
1 parent 6af1624 commit 5a8eb21
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
54 changes: 43 additions & 11 deletions src/backend/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
use nanoserde::{Toml, TomlParser};

#[derive(Debug)]
pub struct Config {
pub port: u16,
pub tls: CfgTLS,
}

pub struct CfgTLS {
pub enable: bool,
pub cert: String,
pub key: String,
}

pub fn config() -> Config {
let cfg = TomlParser::parse(&match std::fs::read_to_string("config.toml") {
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => {
std::fs::write("config.toml", "").unwrap();
String::new()
}
Ok(cfg) => cfg,
Err(e) => {
panic!("Config file could not be read: {}", e);
let cfg = TomlParser::parse(
&match std::fs::read_to_string("config.toml") {
Err(ref e) if e.kind() == std::io::ErrorKind::NotFound => {
std::fs::write("config.toml", "").unwrap();
String::new()
}
Ok(cfg) => cfg,
Err(e) => {
panic!("Config file could not be read: {}", e);
}
}
})
.lines()
.filter(|line| !line.starts_with('#')) // Remove comments, parser can't handle them
.map(|line| line.to_string() + "\n")
.collect::<String>(),
)
.expect("Invalid config file");

#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
let port: u16 = cfg.get("port").unwrap_or(&Toml::Num(8080.0)).num() as u16;

Config { port }
let tlsenable = cfg.get("tls.enable").unwrap_or(&Toml::Bool(false));

let tlscert = cfg
.get("tls.cert")
.unwrap_or(&Toml::Str(String::new()))
.str()
.to_string();
let tlskey = cfg
.get("tls.key")
.unwrap_or(&Toml::Str(String::new()))
.str()
.to_string();

Config {
port,
tls: CfgTLS {
enable: tlsenable == &Toml::Bool(true),
cert: tlscert,
key: tlskey,
},
}
}
11 changes: 10 additions & 1 deletion src/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,14 @@ async fn main() {
);
}));

warp::serve(routes).run(([0, 0, 0, 0], cfg.port)).await;
if cfg.tls.enable {
warp::serve(routes)
.tls()
.cert_path(cfg.tls.cert)
.key_path(cfg.tls.key)
.run(([0, 0, 0, 0], cfg.port))
.await;
} else {
warp::serve(routes).run(([0, 0, 0, 0], cfg.port)).await;
}
}
Binary file modified src/frontend/.yarn/install-state.gz
Binary file not shown.
5 changes: 4 additions & 1 deletion src/frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@
if (socket) {
console.log("Disconnected");
}
socket = new WebSocket(`ws://${window.location.hostname}:8080/ws`);
let proto = window.location.protocol == "https:" ? "wss" : "ws";
socket = new WebSocket(
`${proto}://${window.location.hostname}:${window.location.port}/ws`
);
socket.onopen = socketOpenListener;
socket.onmessage = socketMessageListener;
socket.onclose = socketCloseListener;
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/pages/Terminal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
let termDiv;
let proto = window.location.protocol == "https:" ? "wss" : "ws";
const socket = new WebSocket(
`ws://${window.location.hostname}:8080/ws/term`
`${proto}://${window.location.hostname}:${window.location.port}/ws/term`
);
const attachAddon = new AttachAddon(socket);
Expand Down

0 comments on commit 5a8eb21

Please sign in to comment.