Skip to content

Commit

Permalink
chore: workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Dec 18, 2024
1 parent 9777a67 commit 6d001ef
Show file tree
Hide file tree
Showing 101 changed files with 834 additions and 721 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Project
/target
target
/.idea
/.vscode

Expand Down
32 changes: 17 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
[package]
name = "cdsctf"
version = "0.0.1"
edition = "2021"
description = "The CdsCTF project is an open-source, high-performance, Jeopardy-style's CTF platform."
publish = false
[workspace]
members = ["crates/*"]
resolver = "2"

[workspace.dependencies]
# Local Libraries
cds-assets = { path = "crates/assets" }
cds-media = { path = "crates/media" }
cds-db = { path = "crates/db" }
cds-env = { path = "crates/env" }
cds-web = { path = "crates/web" }
cds-config = { path = "crates/config" }
cds-queue = { path = "crates/queue" }
cds-metric = { path = "crates/metric" }
cds-cache = { path = "crates/cache" }
cds-cluster = { path = "crates/cluster" }

[dependencies]
# Async
async-trait = { version = "0.1" }
tokio = { version = "1.42", features = ["full"] }
Expand Down Expand Up @@ -100,11 +109,4 @@ sysinfo = { version = "0.32.1" }
tempfile = { version = "3.14.0" }
image = { version = "0.25.5" }
webp = { version = "0.3.0", features = ["image"] }
hex = "0.4.3"

[build-dependencies]
chrono = { version = "0.4" }

[[bin]]
name = "cdsctf"
path = "src/main.rs"
hex = "0.4.3"
9 changes: 9 additions & 0 deletions crates/assets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "cds-assets"
version = "0.0.1"
edition = "2024"
publish = false
resolver = "2"

[dependencies]
rust-embed = { workspace = true }
2 changes: 1 addition & 1 deletion src/assets/mod.rs → crates/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;
use rust_embed::Embed;

#[derive(Embed)]
#[folder = "assets/"]
#[folder = "../assets/"]
pub struct Assets;

pub fn get(path: &str) -> Option<Vec<u8>> {
Expand Down
16 changes: 16 additions & 0 deletions crates/cache/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "cds-cache"
version = "0.0.1"
edition = "2024"
publish = false
resolver = "2"

[dependencies]
cds-env = {workspace = true}

fred = { workspace = true }
once_cell = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tracing = { workspace = true }
thiserror = { workspace = true }
16 changes: 9 additions & 7 deletions src/cache/mod.rs → crates/cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;

use fred::{
prelude::{Client, ClientLike, KeysInterface},
types::{config::Config, Expiration, Key},
types::{Expiration, Key, config::Config},
};
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -41,31 +41,33 @@ where
pub async fn set(
key: impl Into<Key> + Send + Display, value: impl Serialize + Send,
) -> Result<(), CacheError> {
let value = serde_json::to_string(&value)?;
get_client().set(key, value, None, None, false).await?;
let value: String = serde_json::to_string(&value)?;
get_client()
.set::<(), _, _>(key, value, None, None, false)
.await?;

Ok(())
}

pub async fn set_ex(
key: impl Into<Key> + Send + Display, value: impl Serialize + Send, expire: u64,
) -> Result<(), CacheError> {
let value = serde_json::to_string(&value)?;
let value: String = serde_json::to_string(&value)?;
get_client()
.set(key, value, Some(Expiration::EX(expire as i64)), None, false)
.set::<(), _, _>(key, value, Some(Expiration::EX(expire as i64)), None, false)
.await?;

Ok(())
}

pub async fn flush() -> Result<(), CacheError> {
get_client().flushall(false).await?;
get_client().flushall::<()>(false).await?;

Ok(())
}

pub async fn init() {
let config = Config::from_url(&crate::env::get_env().cache.url).unwrap();
let config = Config::from_url(&cds_env::get_env().cache.url).unwrap();
let client = Client::new(config, None, None, None);
client.init().await.unwrap();

Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions crates/cluster/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "cds-cluster"
version = "0.0.1"
edition = "2024"
publish = false
resolver = "2"

[dependencies]
cds-env = { workspace = true }
cds-config = { workspace = true }
cds-db = { workspace = true }

kube = { workspace = true }
k8s-openapi = { workspace = true }
axum = { workspace = true }
once_cell = { workspace = true }
tokio-util = { workspace = true }
tracing = { workspace = true }
thiserror = { workspace = true }
wsrx = { workspace = true }
40 changes: 20 additions & 20 deletions src/cluster/mod.rs → crates/cluster/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use k8s_openapi::{
apimachinery::pkg::apis::meta::v1::ObjectMeta,
};
use kube::{
api::{Api, DeleteParams, ListParams, PostParams, ResourceExt},
runtime::{reflector::Lookup, wait::conditions},
Client as K8sClient, Config,
api::{Api, DeleteParams, ListParams, PostParams},
runtime::wait::conditions,
};
use once_cell::sync::OnceCell;
use tokio_util::codec::Framed;
use tracing::{error, info};

use crate::cluster::traits::ClusterError;
use crate::traits::ClusterError;

static K8S_CLIENT: OnceCell<K8sClient> = OnceCell::new();

Expand Down Expand Up @@ -48,11 +48,11 @@ pub async fn init() {
let namespace_api: Api<Namespace> = Api::all(get_k8s_client().clone());
let namespaces = namespace_api.list(&ListParams::default()).await.unwrap();
if !namespaces.items.iter().any(|namespace| {
namespace.metadata.name == Some(crate::env::get_env().clone().cluster.namespace)
namespace.metadata.name == Some(cds_env::get_env().clone().cluster.namespace)
}) {
let namespace = Namespace {
metadata: ObjectMeta {
name: Some(crate::env::get_env().clone().cluster.namespace),
name: Some(cds_env::get_env().clone().cluster.namespace),
..Default::default()
},
..Default::default()
Expand All @@ -65,9 +65,9 @@ pub async fn init() {
}

pub async fn create(
name: String, challenge: crate::db::entity::challenge::Model,
injected_flag: crate::db::entity::challenge::Flag,
) -> Result<Vec<crate::db::entity::pod::Nat>, ClusterError> {
name: String, challenge: cds_db::entity::challenge::Model,
injected_flag: cds_db::entity::challenge::Flag,
) -> Result<Vec<cds_db::entity::pod::Nat>, ClusterError> {
let metadata = ObjectMeta {
name: Some(name.clone()),
labels: Some(BTreeMap::from([
Expand All @@ -79,7 +79,7 @@ pub async fn create(

let pod_api: Api<Pod> = Api::namespaced(
get_k8s_client(),
crate::env::get_env().cluster.namespace.as_str(),
cds_env::get_env().cluster.namespace.as_str(),
);

let mut env_vars: Vec<EnvVar> = challenge
Expand Down Expand Up @@ -128,23 +128,23 @@ pub async fn create(
kube::runtime::wait::await_condition(pod_api.clone(), &name, conditions::is_pod_running())
.await?;

let mut nats: Vec<crate::db::entity::pod::Nat> = Vec::new();
let mut nats: Vec<cds_db::entity::pod::Nat> = Vec::new();

match crate::env::get_env().cluster.proxy.enabled {
match cds_env::get_env().cluster.proxy.enabled {
true => {
for port in challenge.ports {
nats.push(crate::db::entity::pod::Nat {
nats.push(cds_db::entity::pod::Nat {
src: format!("{}", port),
dst: None,
proxy: crate::env::get_env().cluster.proxy.enabled,
proxy: cds_env::get_env().cluster.proxy.enabled,
entry: None,
});
}
}
false => {
let service_api: Api<Service> = Api::namespaced(
get_k8s_client(),
crate::env::get_env().cluster.namespace.as_str(),
cds_env::get_env().cluster.namespace.as_str(),
);
let service_ports: Vec<ServicePort> = challenge
.ports
Expand Down Expand Up @@ -178,13 +178,13 @@ pub async fn create(
if let Some(ports) = spec.ports {
for port in ports {
if let Some(node_port) = port.node_port {
nats.push(crate::db::entity::pod::Nat {
nats.push(cds_db::entity::pod::Nat {
src: format!("{}", port.port),
dst: Some(format!("{}", node_port)),
proxy: crate::env::get_env().cluster.proxy.enabled,
proxy: cds_env::get_env().cluster.proxy.enabled,
entry: Some(format!(
"{}:{}",
crate::config::get_config().await.cluster.entry,
cds_config::get_config().await.cluster.entry,
node_port
)),
});
Expand All @@ -201,20 +201,20 @@ pub async fn create(
pub async fn delete(name: String) {
let pod_api: Api<Pod> = Api::namespaced(
get_k8s_client(),
crate::env::get_env().cluster.namespace.as_str(),
cds_env::get_env().cluster.namespace.as_str(),
);
let _ = pod_api.delete(&name, &DeleteParams::default()).await;
let service_api: Api<Service> = Api::namespaced(
get_k8s_client(),
crate::env::get_env().cluster.namespace.as_str(),
cds_env::get_env().cluster.namespace.as_str(),
);
let _ = service_api.delete(&name, &DeleteParams::default()).await;
}

pub async fn wsrx(name: String, port: u16, ws: WebSocket) -> Result<(), ClusterError> {
let pod_api: Api<Pod> = Api::namespaced(
get_k8s_client(),
crate::env::get_env().cluster.namespace.as_str(),
cds_env::get_env().cluster.namespace.as_str(),
);
let mut pf = pod_api.portforward(&name, &[port]).await?;
let pfw = pf.take_stream(port);
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions crates/config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "cds-config"
version = "0.0.1"
edition = "2024"
publish = false
resolver = "2"

[dependencies]
cds-db = { workspace = true }
cds-cache = { workspace = true }

sea-orm = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 39 additions & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub mod auth;
pub mod cluster;
pub mod site;
pub mod traits;

use cds_db::get_db;
use sea_orm::EntityTrait;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct Config {
pub site: site::Config,
pub auth: auth::Config,
pub cluster: cluster::Config,
}

impl From<cds_db::entity::config::Model> for Config {
fn from(config: cds_db::entity::config::Model) -> Self {
serde_json::from_value::<Self>(config.value).unwrap()
}
}

pub async fn init() {
let config = cds_cache::get::<Config>("config").await.unwrap();
if config.is_none() {
let model = cds_db::entity::config::Entity::find()
.one(get_db())
.await
.unwrap();
if let Some(model) = model {
let _ = cds_cache::set("config", Config::from(model.clone())).await;
}
}
}

pub async fn get_config() -> Config {
let config = cds_cache::get::<Config>("config").await.unwrap();
config.clone().unwrap()
}
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions crates/db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "cds-db"
version = "0.0.1"
edition = "2024"
publish = false
resolver = "2"

[dependencies]
cds-env = { workspace = true }
cds-cache = { workspace = true }

async-trait = { workspace = true }
serde = { workspace = true }
tracing = { workspace = true }
sea-orm = { workspace = true }
serde_repr = { workspace = true }
once_cell = { workspace = true }
chrono = { workspace = true }
serde_json = { workspace = true }

[lib]
path = "src/lib.rs"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_trait::async_trait;
use sea_orm::{
ActiveModelBehavior, ActiveModelTrait, ColumnTrait, ConnectionTrait, DbErr, DeriveActiveEnum,
ActiveModelBehavior, ActiveModelTrait, ConnectionTrait, DbErr, DeriveActiveEnum,
DeriveEntityModel, DerivePrimaryKey, EntityTrait, EnumIter, FromJsonQueryResult,
PrimaryKeyTrait, Related, RelationDef, RelationTrait, Set,
};
Expand Down
10 changes: 2 additions & 8 deletions src/db/entity/config.rs → crates/db/src/entity/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ use async_trait::async_trait;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

use crate::config::{auth, cluster, site};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize, Default)]
#[sea_orm(table_name = "configs")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i64,
#[sea_orm(column_type = "JsonBinary")]
pub auth: auth::Config,
#[sea_orm(column_type = "JsonBinary")]
pub cluster: cluster::Config,
#[sea_orm(column_type = "JsonBinary")]
pub site: site::Config,
pub value: serde_json::Value,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand All @@ -25,7 +19,7 @@ impl ActiveModelBehavior for ActiveModel {
async fn after_save<C>(model: Model, _db: &C, _insert: bool) -> Result<Model, DbErr>
where
C: ConnectionTrait, {
let _ = crate::cache::set("config", crate::config::Config::from(model.clone())).await;
let _ = cds_cache::set("config", model.value.clone()).await;
Ok(model)
}
}
Loading

0 comments on commit 6d001ef

Please sign in to comment.