Add roulette
This commit is contained in:
parent
2a13919e77
commit
541bd4b8b6
|
@ -163,6 +163,7 @@ name = "pollux"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
|
|
@ -6,4 +6,5 @@ edition = "2018"
|
|||
|
||||
[dependencies]
|
||||
native-tls = "*"
|
||||
url = "*"
|
||||
url = "*"
|
||||
rand = "*"
|
58
src/main.rs
58
src/main.rs
|
@ -1,6 +1,8 @@
|
|||
extern crate native_tls;
|
||||
extern crate rand;
|
||||
|
||||
use native_tls::{Identity, TlsAcceptor, TlsStream};
|
||||
use rand::seq::SliceRandom;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::fs::OpenOptions;
|
||||
|
@ -46,14 +48,13 @@ fn handle_request(mut stream: TlsStream<TcpStream>) {
|
|||
let request = String::from_utf8_lossy(&buffer[..]).to_owned();
|
||||
println!("Request: {}", request);
|
||||
|
||||
write_response(stream, request.to_string())
|
||||
route(stream, request.to_string());
|
||||
}
|
||||
|
||||
fn write_response(mut stream: TlsStream<TcpStream>, request: String) {
|
||||
fn respond_success(mut stream: TlsStream<TcpStream>, content: String) {
|
||||
// a header
|
||||
stream.write_all(b"20\ttext/gemini\r\n").unwrap();
|
||||
|
||||
let content = get_content(request);
|
||||
stream
|
||||
.write_all(content.as_bytes())
|
||||
.expect("Unable to serve file");
|
||||
|
@ -64,6 +65,26 @@ fn write_response(mut stream: TlsStream<TcpStream>, request: String) {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
fn respond_redirect(mut stream: TlsStream<TcpStream>) {
|
||||
let url = random_url();
|
||||
stream
|
||||
.write_all(format!("31\t{}\r\n", url).as_bytes())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn random_url() -> &'static str {
|
||||
let urls = vec![
|
||||
"gemini://gemini.conman.org",
|
||||
"gemini://zaibatsu.circumlunar.space",
|
||||
"gemini://carcosa.net",
|
||||
"gemini://heavysquare.com",
|
||||
"gemini://mozz.us",
|
||||
"gemini://dgold.eu",
|
||||
];
|
||||
let url = urls.choose(&mut rand::thread_rng()).unwrap();
|
||||
url.to_owned()
|
||||
}
|
||||
|
||||
fn make_pkcs12(pfx_file_path: &str, pfx_file_password: &str) -> native_tls::Identity {
|
||||
let mut file = File::open(pfx_file_path).expect("pfx file not found!");
|
||||
let mut pkcs12 = vec![];
|
||||
|
@ -72,22 +93,29 @@ fn make_pkcs12(pfx_file_path: &str, pfx_file_password: &str) -> native_tls::Iden
|
|||
Identity::from_pkcs12(&pkcs12, pfx_file_password).unwrap()
|
||||
}
|
||||
|
||||
fn get_content(request: String) -> String {
|
||||
fn route(stream: TlsStream<TcpStream>, request: String) {
|
||||
let url = Url::parse(&request).unwrap();
|
||||
|
||||
let path = match url.path() {
|
||||
"/" => String::from("index.gemini"),
|
||||
path => str::replace(path, "/", ""),
|
||||
let route = match url.path() {
|
||||
"/" => (20, String::from("index.gemini")),
|
||||
"/roulette" => (31, String::new()),
|
||||
path => (20, str::replace(path, "/", "")),
|
||||
};
|
||||
|
||||
let mut file = OpenOptions::new()
|
||||
.read(true)
|
||||
.open(&path)
|
||||
.expect("file not found");
|
||||
match route {
|
||||
(20, path) => {
|
||||
let mut file = OpenOptions::new()
|
||||
.read(true)
|
||||
.open(&path)
|
||||
.expect("file not found");
|
||||
|
||||
let mut content = String::new();
|
||||
file.read_to_string(&mut content)
|
||||
.expect("Unable to read file");
|
||||
let mut content = String::new();
|
||||
file.read_to_string(&mut content)
|
||||
.expect("Unable to read file");
|
||||
|
||||
content
|
||||
respond_success(stream, content)
|
||||
}
|
||||
(31, _) => respond_redirect(stream),
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue