feat: basic foundations

This commit is contained in:
Tamipes 2025-11-25 16:18:38 +01:00
parent 5874d06016
commit e7d06a45f6
5 changed files with 112 additions and 125 deletions

104
Cargo.lock generated
View file

@ -148,6 +148,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.42" version = "0.4.42"
@ -317,16 +323,6 @@ version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" 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]] [[package]]
name = "educe" name = "educe"
version = "0.6.0" version = "0.6.0"
@ -371,16 +367,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" 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]] [[package]]
name = "event-listener" name = "event-listener"
version = "5.4.1" version = "5.4.1"
@ -914,18 +900,6 @@ version = "0.2.177"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" 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]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.14" version = "0.4.14"
@ -964,6 +938,18 @@ dependencies = [
"windows-sys 0.61.2", "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]] [[package]]
name = "nu-ansi-term" name = "nu-ansi-term"
version = "0.50.3" version = "0.50.3"
@ -1138,14 +1124,15 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"edit",
"either", "either",
"futures", "futures",
"k8s-openapi", "k8s-openapi",
"kube", "kube",
"nix",
"schemars", "schemars",
"serde", "serde",
"serde-value", "serde-value",
"serde_derive",
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",
"tokio", "tokio",
@ -1241,32 +1228,6 @@ dependencies = [
"windows-sys 0.52.0", "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]] [[package]]
name = "rustls" name = "rustls"
version = "0.23.35" version = "0.23.35"
@ -1561,19 +1522,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" 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]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.69" version = "1.0.69"
@ -1908,18 +1856,6 @@ dependencies = [
"unicode-ident", "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]] [[package]]
name = "windows-link" name = "windows-link"
version = "0.1.3" version = "0.1.3"

View file

@ -24,16 +24,16 @@ tracing-subscriber = "0.3.17"
# Needed to parse data to yaml in kubectl # Needed to parse data to yaml in kubectl
serde = "1.0.130" serde = "1.0.130"
serde_json = "1.0.68" serde_json = "1.0.68"
serde_derive = "1.0.130"
serde_yaml = "0.9.19" serde_yaml = "0.9.19"
serde-value = "0.7.0" serde-value = "0.7.0"
# .with_context() # .with_context()
# Should be a error handling and context library # Should be a error handling and context library
anyhow = "1.0.71" anyhow = "1.0.71"
# Open the $EDITOR
edit = "0.1.3"
# CLI arg parser # CLI arg parser
clap = { version = "4.0", default-features = false, features = ["std", "cargo", "derive"] } clap = { version = "4.0", default-features = false, features = ["std", "cargo", "derive"] }
futures = { version = "0.3.17", default-features = false } futures = { version = "0.3.17", default-features = false }
either = "1.6.1" either = "1.6.1"
nix = { version= "0.30.1", features = [ "zerocopy"] }

41
src/KubeCache.rs Normal file
View file

@ -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<Deployment>,
}
impl Cache {
pub fn create() -> Option<Cache> {
let kubeconfig = kube::config::Kubeconfig::read().unwrap();
let client = Client::try_from(kubeconfig).unwrap();
let deployments: Api<Deployment> = Api::default_namespaced(client);
return Some(Cache { deployments });
}
pub async fn get_deploys(&self) -> ObjectList<Deployment> {
// 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<String> {
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
}

View file

@ -1,37 +1,46 @@
//! This is a simple imitation of the basic functionality of kubectl: //! This is a simple imitation of the basic functionality of kubectl:
//! kubectl {get, delete, apply, watch, edit} <resource> [name] //! kubectl {get, delete, apply, watch, edit} <resource> [name]
//! with labels and namespace selectors supported. //! with labels and namespace selectors supported.
use anyhow::{bail, Context, Result}; use std::sync::Arc;
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 tokio::net::{TcpListener, TcpStream};
mod KubeCache;
mod packets; mod packets;
mod types; mod types;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
let cache = KubeCache::Cache::create().unwrap();
let arcCache = Arc::new(cache);
let kubeconfig = kube::config::Kubeconfig::read()?; let mut listener = TcpListener::bind("0.0.0.0:25565").await.unwrap();
let client = Client::try_from(kubeconfig)?;
let deployments: Api<Deployment> = Api::default_namespaced(client); loop {
let (socket, _) = listener.accept().await.unwrap();
let acc = arcCache.clone();
let lp: ListParams = ListParams::default(); tokio::spawn(async move {
println!("{:?}", deployments.list(&lp).await?); if let Err(e) = process_socket(socket, acc).await {
eprintln!("ERR: {e:?}");
Ok(())
} }
});
}
}
async fn process_socket(stream: TcpStream, cache: Arc<KubeCache::Cache>) -> 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
// );

View file

@ -1,8 +1,9 @@
use mc_proxy::{types::*, ProtocolState};
use std::{ use std::{
io::{self, Read, Write}, io::{self, Read, Write},
net::TcpStream, net::TcpStream,
}; };
use crate::types::VarInt;
pub mod clientbound; pub mod clientbound;
pub mod serverbound; pub mod serverbound;
@ -103,18 +104,18 @@ impl Packet {
all.append(&mut vec); all.append(&mut vec);
return Some(all); return Some(all);
} }
pub fn proto_name(&self, state: &ProtocolState) -> String { // pub fn proto_name(&self, state: &Type) -> String {
match state { // match state {
ProtocolState::Handshaking => match self.id.get_int() { // ProtocolState::Handshaking => match self.id.get_int() {
0 => "Handshake".to_owned(), // 0 => "Handshake".to_owned(),
_ => "error".to_owned(), // _ => "error".to_owned(),
}, // },
ProtocolState::Status => match self.id.get_int() { // ProtocolState::Status => match self.id.get_int() {
0 => "StatusRequest".to_owned(), // 0 => "StatusRequest".to_owned(),
1 => "PingRequest".to_owned(), // 1 => "PingRequest".to_owned(),
_ => "error".to_owned(), // _ => "error".to_owned(),
}, // },
_ => "Dont care state".to_owned(), // _ => "Dont care state".to_owned(),
} // }
} // }
} }