Initial public release

This commit is contained in:
thakares committed 2026-06-11 20:19:03 +05:30
commit c1107147b4
92 files changed
+10562

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
use sha2::{Sha256, Digest};
// General SHA-256 hash helper
pub fn sha256_hash(data: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(data.as_bytes());
hex::encode(hasher.finalize())
}
+11
View File
@@ -0,0 +1,11 @@
pub mod time;
pub mod system;
pub mod hashing;
pub mod network;
pub mod random;
pub use time::format_duration;
pub use system::{get_memory_usage, get_db_file_info};
pub use hashing::sha256_hash;
pub use network::get_client_ip;
pub use random::generate_token;
+24
View File
@@ -0,0 +1,24 @@
use std::net::SocketAddr;
use axum::{
extract::ConnectInfo,
http::HeaderMap,
};
// Extract client IP address from proxy headers or connection info
pub fn get_client_ip(headers: &HeaderMap, connect_info: Option<ConnectInfo<SocketAddr>>) -> String {
if let Some(ip) = headers.get("cf-connecting-ip").and_then(|h| h.to_str().ok()) {
return ip.to_string();
}
if let Some(ip) = headers.get("x-real-ip").and_then(|h| h.to_str().ok()) {
return ip.to_string();
}
if let Some(ips) = headers.get("x-forwarded-for").and_then(|h| h.to_str().ok()) {
if let Some(ip) = ips.split(',').next() {
return ip.trim().to_string();
}
}
if let Some(ConnectInfo(addr)) = connect_info {
return addr.ip().to_string();
}
"127.0.0.1".to_string()
}
+8
View File
@@ -0,0 +1,8 @@
use rand::{RngCore, thread_rng};
// Generate a secure random token (hex-encoded)
pub fn generate_token(bytes_len: usize) -> String {
let mut key = vec![0u8; bytes_len];
thread_rng().fill_bytes(&mut key);
hex::encode(key)
}
+40
View File
@@ -0,0 +1,40 @@
use std::path::Path;
// Read memory usage on Linux
pub fn get_memory_usage() -> String {
if let Ok(statm) = std::fs::read_to_string("/proc/self/statm") {
let fields: Vec<&str> = statm.split_whitespace().collect();
if !fields.is_empty() {
if let Ok(pages) = fields[0].parse::<u64>() {
// Page size is usually 4096 bytes
let bytes = pages * 4096;
return format!("{:.2} MB", bytes as f64 / 1_048_576.0);
}
}
}
"N/A".to_string()
}
// Generate diagnostic size report for the SQLite files
pub fn get_db_file_info(data_dir: &Path) -> String {
let mut stats = String::new();
let files = vec![
("admin.db", "Admin DB"),
("content.db", "Content DB"),
("analytics.db", "Analytics DB"),
("system.db", "System DB"),
];
for (f, name) in files {
let path = data_dir.join(f);
if path.exists() {
if let Ok(metadata) = std::fs::metadata(&path) {
let size = metadata.len();
stats.push_str(&format!("{}: {} bytes ({:.2} MB)\n", name, size, size as f64 / 1_048_576.0));
}
} else {
stats.push_str(&format!("{}: File not created yet\n", name));
}
}
stats
}
+12
View File
@@ -0,0 +1,12 @@
use std::time::Duration;
// Formats a duration as "Xd Xh Xm Xs"
pub fn format_duration(d: Duration) -> String {
format!(
"{}d {}h {}m {}s",
d.as_secs() / 86400,
(d.as_secs() % 86400) / 3600,
(d.as_secs() % 3600) / 60,
d.as_secs() % 60
)
}