initial commit

This commit is contained in:
Snorre 2025-02-05 01:15:33 +01:00
commit eff065466a
7 changed files with 325 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "webbisitey"
version = "0.1.0"

4
Cargo.toml Normal file
View file

@ -0,0 +1,4 @@
[package]
name = "webbisitey"
version = "0.1.0"
edition = "2024"

48
flake.lock Normal file
View file

@ -0,0 +1,48 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1738546358,
"narHash": "sha256-nLivjIygCiqLp5QcL7l56Tca/elVqM9FG1hGd9ZSsrg=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "c6e957d81b96751a3d5967a0fd73694f303cc914",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1738635966,
"narHash": "sha256-5MbJhh6nz7tx8FYVOJ0+ixMaEn0ibGzV/hScPMmqVTE=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "1ff8663cd75a11e61f8046c62f4dbb05d1907b44",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

37
flake.nix Normal file
View file

@ -0,0 +1,37 @@
{
description = "Flake for mc rust proxy";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
self,
nixpkgs,
rust-overlay,
}: let
# rust-overlay = import rust-overlay;
pkgs =
import nixpkgs
{
system = "x86_64-linux";
overlays = [rust-overlay.overlays.default];
};
in {
devShells.x86_64-linux.default = pkgs.mkShell {
buildInputs = with pkgs; [
rust-bin.nightly.latest.default
rust-analyzer
];
# shellHook = ''
# echo "Hello from nix dev shell"
# '';
};
formatter.x86_64-linux = pkgs.alejandra;
};
}

133
public/index.html Normal file
View file

@ -0,0 +1,133 @@
<!DOCTYPE html>
<html>
<head>
<style>
@import url(https://fonts.bunny.net/css?family=azeret-mono:500);
:root {
--background: #2e3440;
--background-text: #3b4252;
--border: #5e81ac;
--border2: #b48ead;
--text: #eceff4;
--link: #88c0d0;
--link-visited: #81a1c1;
--padding: 10px 20px;
--margin: 4px;
--gap: 10px;
--radius: 4px;
}
html {
color: var(--text);
background-color: var(--background);
font-family: 'Azeret Mono', monospace;
}
body {
background-color: initial !important;
}
a {
color: var(--link);
}
a:visited {
color: var(--link-visited);
}
body {
min-height: 100vh;
min-width: 100vw;
margin: 0px;
padding: 0px;
display: flex;
align-items: center;
justify-content: center;
}
#content {
display: grid;
grid-template-areas:
"header1 header1 header1"
"content1 content2 content3"
"footer1 footer2 footer3"
;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: var(--gap);
padding: var(--padding);
}
#content>div {
background: var(--background);
border: 4px solid var(--border);
border-radius: var(--radius);
padding: var(--padding);
align-content: center;
}
#content>div:nth-child(2n) {
border: 4px solid var(--border2);
}
#background {
position: fixed;
z-index: -3;
left: 0;
right: 0;
top: 0;
bottom: 0;
color: var(--background-text);
opacity: 0.3;
aspect-ratio: 1/1;
transform-origin: top;
transform: scale(150%) rotate(-20deg) translateY(-25%);
align-content: center;
text-align: center;
}
</style>
</head>
<body style="background-color: #222;">
<div id="content">
<div class="header" style="grid-area: header1">something something snorre.net</div>
<div class="content" style="grid-area: content1">content</div>
<div class="content" style="grid-area: content2">content</div>
<div class="content" style="grid-area: content3">content</div>
<div class="footer" style="grid-area: footer1">cool fucking footer</div>
<div class="footer" style="grid-area: footer2; text-align: center"><a
href="https://nixwebr.ing/next/<name>">&leftarrow; prev</a> <a href="https://nixwebr.ing">nixwebr.ing</a>
<a href="https://nixwebr.ing/prev/<name>">next &rightarrow;</a>
</div>
<div class="footer" style="grid-area: footer3; text-align: center;">
<a href="https://512kb.club"><img style="aspect-ratio: 234.383/30" height=30
alt="a proud member of the green team of 512KB club"
src="https://512kb.club/assets/images/green-team.svg"></a>
</div>
</div>
<div id="background"></div>
<script>
(() => {
let str = "";
let terms = "${i} :3 Nix Linux Test Portchain Programming Rust C++ Openbirch Godot".split(" ");
for (let i = 0; i < 5000; i++) {
let idx = Math.floor(Math.random() * terms.length);
str += terms[idx] + " ";
}
document.getElementById("background").innerText = str;
})()
</script>
</body>
</html>

95
src/main.rs Normal file
View file

@ -0,0 +1,95 @@
use std::{
env::current_dir,
io::{BufRead, BufReader, Write},
net::{TcpListener, TcpStream},
path::PathBuf,
};
static CLRF: &str = "\r\n";
fn main() {
let listener =
TcpListener::bind("0.0.0.0:8080").expect("Failed to create TcpListener on 0.0.0.0:8080");
for stream in listener.incoming() {
let mut stream = stream.unwrap(); // safe because TcpListener::incoming never returns None.
handle_connection(stream);
}
}
#[derive(Debug)]
enum RequestMethod {
GET,
POST,
}
#[derive(Debug)]
struct Request {
method: RequestMethod,
path: Option<String>,
}
impl Request {
pub fn from(string: &str) -> Self {
let mut parts = string.split(" ");
Self {
method: match parts.next().expect("Invalid request") {
"GET" => RequestMethod::GET,
"POST" => RequestMethod::POST,
_ => panic!("Unknown method"),
},
path: Some(parts.next().expect("Invalid request").to_string()),
}
}
pub fn file(&self) -> Option<PathBuf> {
if self.path == None {
return None;
}
let mut path = PathBuf::new();
path.push("public");
path.push(
PathBuf::from(self.path.as_ref().unwrap())
.strip_prefix("/")
.expect("Failed to strip prefix"),
);
if path.extension() == None {
path.push("index.html");
}
let mut cwd = current_dir().expect("Failed to get CWD");
cwd.push(path);
Some(cwd)
}
}
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&stream);
let http_request: Vec<_> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
println!("Request: {http_request:#?}");
if http_request.len() == 0 {
return;
}
let req = Request::from(http_request.get(0).expect("Empty request"));
println!("Requested file: {:#?}", req.file());
let response = match req.file() {
Some(path) => match std::fs::read_to_string(path) {
Ok(content) => format!("HTTP/1.1 200 OK{0}Content-Length: {1}{0}{0}{content}", CLRF, content.len()),
Err(e) => format!("HTTP/1.1 500{0}{0}{e}", CLRF),
}
None => format!("HTTP/1.1 500{0}{0}", CLRF),
};
stream.write_all(response.as_bytes()).unwrap();
}