diff --git a/public/index.html b/public/index.html index f2b05b5..fbaadad 100644 --- a/public/index.html +++ b/public/index.html @@ -63,7 +63,16 @@ if (response.status != 200) return; - let posts = (await response.text()).split("\n"); + let posts = (await response.text()).split("\n").filter(x => x != ""); + + if (posts.length == 0) { + let p = document.querySelector("#content>.content.posts>h2"); + p.innerHTML = "This is where I would put my posts...
IF I HAD ANY!"; + let elem = document.querySelector("#content>.content.posts>ul"); + elem.remove(); + + return; + } let p = document.querySelector("#content>.content.posts>h2"); p.innerHTML = "Recent posts"; diff --git a/public/js/code_highlighter.js b/public/js/code_highlighter.js index ac2ee26..997e92d 100644 --- a/public/js/code_highlighter.js +++ b/public/js/code_highlighter.js @@ -178,6 +178,8 @@ const maps = { keyword("Ok"), keyword("Err"), keyword("in"), + keyword("Self"), + keyword("mut"), altKeyword("if"), altKeyword("else"), @@ -185,6 +187,9 @@ const maps = { altKeyword("loop"), altKeyword("for"), altKeyword("while"), + altKeyword("return"), + altKeyword("async"), + altKeyword("await"), altKeyword("mod"), altKeyword("use"), diff --git a/public/posts/I hate github & forgejo actions/post.md b/public/posts/_I hate github & forgejo actions/post.md similarity index 100% rename from public/posts/I hate github & forgejo actions/post.md rename to public/posts/_I hate github & forgejo actions/post.md diff --git a/public/posts/My language is 700x slower than python/openbirch.png b/public/posts/_My language is 700x slower than python/openbirch.png similarity index 100% rename from public/posts/My language is 700x slower than python/openbirch.png rename to public/posts/_My language is 700x slower than python/openbirch.png diff --git a/public/posts/My language is 700x slower than python/post.md b/public/posts/_My language is 700x slower than python/post.md similarity index 100% rename from public/posts/My language is 700x slower than python/post.md rename to public/posts/_My language is 700x slower than python/post.md diff --git a/public/posts/My language is 700x slower than python/thumbnail.png b/public/posts/_My language is 700x slower than python/thumbnail.png similarity index 100% rename from public/posts/My language is 700x slower than python/thumbnail.png rename to public/posts/_My language is 700x slower than python/thumbnail.png diff --git a/public/posts/Putting hungry minecraft servers to sleep/hibernating.png b/public/posts/_Putting hungry minecraft servers to sleep/hibernating.png similarity index 100% rename from public/posts/Putting hungry minecraft servers to sleep/hibernating.png rename to public/posts/_Putting hungry minecraft servers to sleep/hibernating.png diff --git a/public/posts/_Putting hungry minecraft servers to sleep/ping.png b/public/posts/_Putting hungry minecraft servers to sleep/ping.png new file mode 100644 index 0000000..5f85dd2 Binary files /dev/null and b/public/posts/_Putting hungry minecraft servers to sleep/ping.png differ diff --git a/public/posts/Putting hungry minecraft servers to sleep/post.md b/public/posts/_Putting hungry minecraft servers to sleep/post.md similarity index 72% rename from public/posts/Putting hungry minecraft servers to sleep/post.md rename to public/posts/_Putting hungry minecraft servers to sleep/post.md index 5311ff9..e7e39ee 100644 --- a/public/posts/Putting hungry minecraft servers to sleep/post.md +++ b/public/posts/_Putting hungry minecraft servers to sleep/post.md @@ -61,17 +61,74 @@ wanted. [One crate](https://www.youtube.com/watch?v=E4WlUXrJgy4) was useful for seeing how an implementation of parsing packets was done, but ultimately I had to write my own parser. -***images of minecraft protocol*** +![An image of the steps taken to do a Ping using the minecraft protocol](/posts/Putting hungry minecraft servers to sleep/ping.png "Source: minecraft.wiki") ## Detecting the subdomain +The first step in routing the traffic was determining where to send it to. + Luckily for me, the Minecraft procotol sends the full address it is trying to connect to during the handshake. This meant that I simply had to parse the handshake and extract the subdomain from the address, see if it matches any configured server, and then redirect all traffic to the internal port that server runs on. -***code explanation*** +`$ vim protocol/handshake.rs` +```rust +impl Handshake { + pub async fn new(buf: &mut BufferedReader) -> Result { + // Packets start with their type id. The handshake is id 0x00 + if !check_bytes(&[0x00], buf).await? { + return Err(Error::Err( + "Wrong packet type for handshake. Expected {} got {}".into(), + )); + } + buf.read(1).await?; // Consume handhake byte + + let protocol_version = VarInt::read_from(buf).await?; + let address = read_string(buf).await?; + let port = buf.read_u16().await?; + let next_state = VarInt::read_from(buf).await?; + + return Ok( + Self { + protocol_version, + address, + port, + next_state, + }, + ); + } +} +``` + +The only thing I was interrested in was the `address` part of the packet. This is a full string +containing the text the user typed in the `IP Address` field in their client. + +Once I had this it was as simple as splitting by `.` and taking the first result. This "server name" would now be used to look up the corosponding servers information in a table. + +```rust + + let result = Handshake::new(&mut reader).await; + if !result.is_ok() { + return Ok(None); + } + + let handshake = result.ok().unwrap(); + let split = handshake.address.split(".").collect::>(); + let server_name = *split.first().unwrap(); + + if !SERVERS.lock().unwrap().contains_key(server_name) { + println!("Unknown server {}", server_name); + write_unknown_server_status(server_name.into(), &mut reader).await?; + return Ok(None); + } + let mut server_lock = SERVERS.lock().unwrap(); + let server = server_lock.get_mut(server_name).unwrap(); + status = server.status; + port = server.port; + +``` ***show images of hibernation and starting*** diff --git a/public/posts/Putting hungry minecraft servers to sleep/starting.png b/public/posts/_Putting hungry minecraft servers to sleep/starting.png similarity index 100% rename from public/posts/Putting hungry minecraft servers to sleep/starting.png rename to public/posts/_Putting hungry minecraft servers to sleep/starting.png diff --git a/public/posts/Putting hungry minecraft servers to sleep/thumbnail.png b/public/posts/_Putting hungry minecraft servers to sleep/thumbnail.png similarity index 100% rename from public/posts/Putting hungry minecraft servers to sleep/thumbnail.png rename to public/posts/_Putting hungry minecraft servers to sleep/thumbnail.png diff --git a/src/main.rs b/src/main.rs index 20478e5..018fb76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -127,10 +127,10 @@ fn handle_connection(mut stream: TcpStream) { let y = file_name.to_str(); let ft = x.file_type(); if y.is_some() { - if ft.is_ok() && ft.unwrap().is_dir() { - true - } else if y.unwrap().starts_with("_") { + if y.unwrap().starts_with("_") { false + } else if ft.is_ok() && ft.unwrap().is_dir() { + true } else if y.unwrap().ends_with(".md") { true } else { @@ -228,7 +228,7 @@ fn handle_connection(mut stream: TcpStream) { } }) .reduce(|a, b| format!("{}\n{}", b, a)) - .unwrap(); + .unwrap_or("".to_owned()); format!("HTTP/1.1 200 OK\r\nContent-Type: text/text\r\nContent-Length: {}\r\n\r\n{}", data.len(), data) } Err(e) => {