From e7d06a45f6de76e829d51f47524431cc3135c044 Mon Sep 17 00:00:00 2001 From: Tamipes Date: Tue, 25 Nov 2025 16:18:38 +0100 Subject: [PATCH] feat: basic foundations --- Cargo.lock | 104 +++++++++------------------------------------ Cargo.toml | 4 +- src/KubeCache.rs | 41 ++++++++++++++++++ src/main.rs | 57 ++++++++++++++----------- src/packets/mod.rs | 31 +++++++------- 5 files changed, 112 insertions(+), 125 deletions(-) create mode 100644 src/KubeCache.rs diff --git a/Cargo.lock b/Cargo.lock index f423a4c..605a7c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -148,6 +148,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "chrono" version = "0.4.42" @@ -317,16 +323,6 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" -[[package]] -name = "edit" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f364860e764787163c8c8f58231003839be31276e821e2ad2092ddf496b1aa09" -dependencies = [ - "tempfile", - "which", -] - [[package]] name = "educe" version = "0.6.0" @@ -371,16 +367,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" -[[package]] -name = "errno" -version = "0.3.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" -dependencies = [ - "libc", - "windows-sys 0.61.2", -] - [[package]] name = "event-listener" version = "5.4.1" @@ -914,18 +900,6 @@ version = "0.2.177" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" -[[package]] -name = "linux-raw-sys" -version = "0.4.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" - -[[package]] -name = "linux-raw-sys" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" - [[package]] name = "lock_api" version = "0.4.14" @@ -964,6 +938,18 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nu-ansi-term" version = "0.50.3" @@ -1138,14 +1124,15 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", - "edit", "either", "futures", "k8s-openapi", "kube", + "nix", "schemars", "serde", "serde-value", + "serde_derive", "serde_json", "serde_yaml", "tokio", @@ -1241,32 +1228,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "rustix" -version = "0.38.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys 0.4.15", - "windows-sys 0.52.0", -] - -[[package]] -name = "rustix" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys 0.11.0", - "windows-sys 0.61.2", -] - [[package]] name = "rustls" version = "0.23.35" @@ -1561,19 +1522,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" -[[package]] -name = "tempfile" -version = "3.23.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16" -dependencies = [ - "fastrand", - "getrandom 0.3.4", - "once_cell", - "rustix 1.1.2", - "windows-sys 0.61.2", -] - [[package]] name = "thiserror" version = "1.0.69" @@ -1908,18 +1856,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "which" -version = "4.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" -dependencies = [ - "either", - "home", - "once_cell", - "rustix 0.38.44", -] - [[package]] name = "windows-link" version = "0.1.3" diff --git a/Cargo.toml b/Cargo.toml index 317ae5a..dd58ffa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,16 +24,16 @@ tracing-subscriber = "0.3.17" # Needed to parse data to yaml in kubectl serde = "1.0.130" serde_json = "1.0.68" +serde_derive = "1.0.130" serde_yaml = "0.9.19" serde-value = "0.7.0" # .with_context() # Should be a error handling and context library anyhow = "1.0.71" -# Open the $EDITOR -edit = "0.1.3" # CLI arg parser clap = { version = "4.0", default-features = false, features = ["std", "cargo", "derive"] } futures = { version = "0.3.17", default-features = false } either = "1.6.1" +nix = { version= "0.30.1", features = [ "zerocopy"] } diff --git a/src/KubeCache.rs b/src/KubeCache.rs new file mode 100644 index 0000000..27999df --- /dev/null +++ b/src/KubeCache.rs @@ -0,0 +1,41 @@ +use k8s_openapi::api::apps::v1::Deployment; +use kube::{ + api::{ListParams, ObjectList}, + runtime::reflector::Lookup, + Api, Client, ResourceExt, +}; + +pub struct Cache { + deployments: Api, +} +impl Cache { + pub fn create() -> Option { + let kubeconfig = kube::config::Kubeconfig::read().unwrap(); + let client = Client::try_from(kubeconfig).unwrap(); + + let deployments: Api = Api::default_namespaced(client); + + return Some(Cache { deployments }); + } + pub async fn get_deploys(&self) -> ObjectList { + // let lp: ListParams = ListParams::default(); + let lp: ListParams = ListParams::default().labels("tami.moe/minecraft"); + self.deployments.list(&lp).await.unwrap() + } + + pub async fn query_addr(&self, addr: String) -> Option { + let deploys = self.get_deploys().await; + let result = deploys + .iter() + .find(|x| filter_label_value(x, addr.clone()))?; + Some(result.name()?.to_string()) + } +} + +fn filter_label_value(dep: &Deployment, str: String) -> bool { + dep.labels() + .values() + .filter(|x| x.as_str() == str.as_str()) + .count() + > 0 +} diff --git a/src/main.rs b/src/main.rs index 0280fde..0011090 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,37 +1,46 @@ //! This is a simple imitation of the basic functionality of kubectl: //! kubectl {get, delete, apply, watch, edit} [name] //! with labels and namespace selectors supported. -use anyhow::{bail, Context, Result}; -use futures::{StreamExt, TryStreamExt}; -use k8s_openapi::{ - api::apps::v1::Deployment, apimachinery::pkg::apis::meta::v1::Time, chrono::Utc, -}; -use kube::{ - api::{Api, DynamicObject, ListParams, Patch, PatchParams, ResourceExt}, - core::GroupVersionKind, - discovery::{ApiCapabilities, ApiResource, Discovery, Scope}, - runtime::{ - wait::{await_condition, conditions::is_deleted}, - watcher, WatchStreamExt, - }, - Client, -}; -use tracing::*; +use std::sync::Arc; +use tokio::net::{TcpListener, TcpStream}; + +mod KubeCache; mod packets; mod types; #[tokio::main] -async fn main() -> Result<()> { +async fn main() { tracing_subscriber::fmt::init(); + let cache = KubeCache::Cache::create().unwrap(); + let arcCache = Arc::new(cache); - let kubeconfig = kube::config::Kubeconfig::read()?; - let client = Client::try_from(kubeconfig)?; + let mut listener = TcpListener::bind("0.0.0.0:25565").await.unwrap(); - let deployments: Api = Api::default_namespaced(client); + loop { + let (socket, _) = listener.accept().await.unwrap(); + let acc = arcCache.clone(); - let lp: ListParams = ListParams::default(); - println!("{:?}", deployments.list(&lp).await?); - - Ok(()) + tokio::spawn(async move { + if let Err(e) = process_socket(socket, acc).await { + eprintln!("ERR: {e:?}"); + } + }); + } } + +async fn process_socket(stream: TcpStream, cache: Arc) -> Result<(), ()> { + todo!() +} +// ----- Debug tools ----- +// let mc_deployments = cache.get_deploys().await; +// for dep in mc_deployments.iter() { +// println!("{:?}", dep.labels().values()) +// } +// // println!("{:?}", mc_deployments); +// println!("count: {}", mc_deployments.iter().count()); + +// println!( +// "{:?}", +// cache.query_addr("ferret.tami.moe".to_string()).await +// ); diff --git a/src/packets/mod.rs b/src/packets/mod.rs index 2f75cfc..eadcdef 100644 --- a/src/packets/mod.rs +++ b/src/packets/mod.rs @@ -1,8 +1,9 @@ -use mc_proxy::{types::*, ProtocolState}; use std::{ io::{self, Read, Write}, net::TcpStream, }; + +use crate::types::VarInt; pub mod clientbound; pub mod serverbound; @@ -103,18 +104,18 @@ impl Packet { all.append(&mut vec); return Some(all); } - pub fn proto_name(&self, state: &ProtocolState) -> String { - match state { - ProtocolState::Handshaking => match self.id.get_int() { - 0 => "Handshake".to_owned(), - _ => "error".to_owned(), - }, - ProtocolState::Status => match self.id.get_int() { - 0 => "StatusRequest".to_owned(), - 1 => "PingRequest".to_owned(), - _ => "error".to_owned(), - }, - _ => "Dont care state".to_owned(), - } - } + // pub fn proto_name(&self, state: &Type) -> String { + // match state { + // ProtocolState::Handshaking => match self.id.get_int() { + // 0 => "Handshake".to_owned(), + // _ => "error".to_owned(), + // }, + // ProtocolState::Status => match self.id.get_int() { + // 0 => "StatusRequest".to_owned(), + // 1 => "PingRequest".to_owned(), + // _ => "error".to_owned(), + // }, + // _ => "Dont care state".to_owned(), + // } + // } }