refactor: Switch Token authentication to HmacSha256 algorithm

This commit is contained in:
lxien
2026-08-16 10:33:05 +08:00
parent ea3abad792
commit 078ff182e4
8 changed files with 198 additions and 58 deletions
+5
View File
@@ -0,0 +1,5 @@
[source.crates-io]
replace-with = "rsproxy-sparse"
[source.rsproxy-sparse]
registry = "sparse+https://rsproxy.cn/index/"
Generated
+163 -23
View File
@@ -36,7 +36,7 @@ dependencies = [
"accesskit",
"accesskit_consumer",
"atspi-common",
"phf",
"phf 0.13.1",
"serde",
"zvariant",
]
@@ -1342,6 +1342,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer 0.10.4",
"crypto-common 0.1.7",
"subtle",
]
[[package]]
@@ -2153,6 +2154,15 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest 0.10.7",
]
[[package]]
name = "hostname"
version = "0.4.2"
@@ -3259,16 +3269,6 @@ dependencies = [
"rayon",
]
[[package]]
name = "md-5"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
dependencies = [
"cfg-if",
"digest 0.10.7",
]
[[package]]
name = "memchr"
version = "2.8.3"
@@ -3453,6 +3453,28 @@ dependencies = [
"memchr",
]
[[package]]
name = "nom-derive"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ff943d68b88d0b87a6e0d58615e8fa07f9fd5a1319fa0a72efc1f62275c79a7"
dependencies = [
"nom 7.1.3",
"nom-derive-impl",
"rustversion",
]
[[package]]
name = "nom-derive-impl"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd0b9a93a84b0d3ec3e70e02d332dc33ac6dfac9cde63e17fcb77172dededa62"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "noop_proc_macro"
version = "0.3.0"
@@ -3966,7 +3988,7 @@ dependencies = [
[[package]]
name = "orbien-client"
version = "2.1.0-SNAPSHOT"
version = "3.0.0"
dependencies = [
"anyhow",
"async-trait",
@@ -3987,17 +4009,16 @@ dependencies = [
[[package]]
name = "orbien-core"
version = "2.1.0-SNAPSHOT"
version = "3.0.0"
dependencies = [
"anyhow",
"async-trait",
"base64",
"bytes",
"futures",
"futures-util",
"hex",
"hmac",
"kcp-tokio",
"md-5",
"quinn",
"rcgen",
"rustls",
@@ -4005,21 +4026,22 @@ dependencies = [
"rustls-pki-types",
"serde",
"serde_json",
"sha2 0.10.9",
"socket2 0.5.10",
"thiserror 2.0.19",
"tls-parser",
"tokio",
"tokio-rustls",
"tokio-tungstenite",
"tokio-util",
"toml 0.8.23",
"tracing",
"uuid",
"yamux",
]
[[package]]
name = "orbien-desktop"
version = "2.1.0-SNAPSHOT"
version = "3.0.0"
dependencies = [
"anyhow",
"libc",
@@ -4032,11 +4054,12 @@ dependencies = [
"toml 0.8.23",
"tracing",
"tracing-subscriber",
"winres",
]
[[package]]
name = "orbien-server"
version = "2.1.0-SNAPSHOT"
version = "3.0.0"
dependencies = [
"anyhow",
"axum",
@@ -4172,6 +4195,15 @@ version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "phf"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078"
dependencies = [
"phf_shared 0.11.3",
]
[[package]]
name = "phf"
version = "0.13.1"
@@ -4179,10 +4211,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf"
dependencies = [
"phf_macros",
"phf_shared",
"phf_shared 0.13.1",
"serde",
]
[[package]]
name = "phf_codegen"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aef8048c789fa5e851558d709946d6d79a8ff88c0440c587967f8e94bfb1216a"
dependencies = [
"phf_generator 0.11.3",
"phf_shared 0.11.3",
]
[[package]]
name = "phf_generator"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d"
dependencies = [
"phf_shared 0.11.3",
"rand 0.8.7",
]
[[package]]
name = "phf_generator"
version = "0.13.1"
@@ -4190,7 +4242,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "135ace3a761e564ec88c03a77317a7c6b80bb7f7135ef2544dbe054243b89737"
dependencies = [
"fastrand",
"phf_shared",
"phf_shared 0.13.1",
]
[[package]]
@@ -4199,13 +4251,22 @@ version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812f032b54b1e759ccd5f8b6677695d5268c588701effba24601f6932f8269ef"
dependencies = [
"phf_generator",
"phf_shared",
"phf_generator 0.13.1",
"phf_shared 0.13.1",
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "phf_shared"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5"
dependencies = [
"siphasher",
]
[[package]]
name = "phf_shared"
version = "0.13.1"
@@ -4557,6 +4618,15 @@ version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "rand"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22f6172bdec972074665ed81ed53b71da00bfc44b65a753cfde883ec4c702a1a"
dependencies = [
"rand_core 0.6.4",
]
[[package]]
name = "rand"
version = "0.9.5"
@@ -4588,6 +4658,12 @@ dependencies = [
"rand_core 0.9.5",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
[[package]]
name = "rand_core"
version = "0.9.5"
@@ -4934,7 +5010,7 @@ version = "8.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ffa149f6aa81b58a5b3011d01a857c4ed12c7a732d2c51947a4c7c692185f0"
dependencies = [
"sha2",
"sha2 0.11.0",
"walkdir",
]
@@ -4959,6 +5035,15 @@ dependencies = [
"semver",
]
[[package]]
name = "rusticata-macros"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632"
dependencies = [
"nom 7.1.3",
]
[[package]]
name = "rustix"
version = "0.38.44"
@@ -5282,6 +5367,17 @@ dependencies = [
"digest 0.10.7",
]
[[package]]
name = "sha2"
version = "0.10.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
dependencies = [
"cfg-if",
"cpufeatures 0.2.17",
"digest 0.10.7",
]
[[package]]
name = "sha2"
version = "0.11.0"
@@ -5432,6 +5528,7 @@ checksum = "ce12038f57f8f3a423564b0c9301a2ab3fda4246e4cbadd6b97acf9124bdc315"
dependencies = [
"const-field-offset",
"i-slint-backend-selector",
"i-slint-backend-winit",
"i-slint-common",
"i-slint-core",
"i-slint-core-macros",
@@ -5723,6 +5820,17 @@ dependencies = [
"zeno",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.119"
@@ -5997,6 +6105,20 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tls-parser"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22c36249c6082584b1f224e70f6bdadf5102197be6cfa92b353efe605d9ac741"
dependencies = [
"nom 7.1.3",
"nom-derive",
"num_enum",
"phf 0.11.3",
"phf_codegen",
"rusticata-macros",
]
[[package]]
name = "tokio"
version = "1.53.1"
@@ -6062,6 +6184,15 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
dependencies = [
"serde",
]
[[package]]
name = "toml"
version = "0.8.23"
@@ -7129,6 +7260,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "winres"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b68db261ef59e9e52806f688020631e987592bd83619edccda9c47d42cde4f6c"
dependencies = [
"toml 0.5.11",
]
[[package]]
name = "wit-bindgen"
version = "0.57.1"
+4 -2
View File
@@ -8,7 +8,7 @@ members = [
]
[workspace.package]
version = "2.1.0-SNAPSHOT"
version = "3.0.0"
edition = "2021"
license = "Apache-2.0"
authors = ["orbien"]
@@ -41,7 +41,8 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
clap = { version = "4", features = ["derive"] }
uuid = { version = "1", features = ["v4"] }
md-5 = "0.10"
hmac = "0.12"
sha2 = "0.10"
hex = "0.4"
base64 = "0.22"
bytes = "1"
@@ -61,3 +62,4 @@ axum = { version = "0.8", features = ["tokio", "http1"] }
rust-embed = "8"
hostname = "0.4"
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
tls-parser = "0.12"
+2 -1
View File
@@ -14,7 +14,8 @@ toml = { workspace = true }
thiserror = { workspace = true }
anyhow = { workspace = true }
tracing = { workspace = true }
md-5 = { workspace = true }
hmac = { workspace = true }
sha2 = { workspace = true }
hex = { workspace = true }
base64 = { workspace = true }
bytes = { workspace = true }
+16 -11
View File
@@ -1,15 +1,13 @@
use md5::{Digest, Md5};
use std::io::Write;
use hmac::{Hmac, Mac};
use sha2::Sha256;
type HmacSha256 = Hmac<Sha256>;
pub fn compute_auth_digest(token: &str, timestamp: i64) -> String {
let mut hasher = Md5::new();
hasher.update(token.as_bytes());
let mut ts = [0u8; 20];
let mut cur = std::io::Cursor::new(&mut ts[..]);
let _ = write!(cur, "{timestamp}");
let n = cur.position() as usize;
hasher.update(&ts[..n]);
hex::encode(hasher.finalize())
let mut mac =
HmacSha256::new_from_slice(token.as_bytes()).expect("HMAC-SHA256 accepts any key length");
mac.update(timestamp.to_string().as_bytes());
hex::encode(mac.finalize().into_bytes())
}
pub fn verify_login(token: &str, auth_digest: &str, timestamp: i64) -> bool {
@@ -23,5 +21,12 @@ pub fn verify_auth_digest(token: &str, auth_digest: &str, timestamp: i64) -> boo
if auth_digest.is_empty() {
return false;
}
compute_auth_digest(token, timestamp) == auth_digest
let Ok(expected) = hex::decode(auth_digest) else {
return false;
};
let Ok(mut mac) = HmacSha256::new_from_slice(token.as_bytes()) else {
return false;
};
mac.update(timestamp.to_string().as_bytes());
mac.verify_slice(&expected).is_ok()
}
+8
View File
@@ -0,0 +1,8 @@
use tokio::net::TcpStream;
#[inline]
pub fn enable_nodelay(stream: &TcpStream) {
if let Err(e) = stream.set_nodelay(true) {
tracing::trace!(error = %e, "tcp set_nodelay failed");
}
}
-12
View File
@@ -1,12 +0,0 @@
bindAddr = "0.0.0.0"
bindPort = 9527
#[auth]
#method = "token"
#token = "YOUR_TOKEN"
#[webServer]
#addr = "0.0.0.0"
#port = 8020
#user = "admin"
#password = "123456"
-9
View File
@@ -1,9 +0,0 @@
serverAddr = "127.0.0.1"
serverPort = 9527
[[proxies]]
name = "mysql"
type = "tcp"
localIP = "127.0.0.1"
localPort = 3306
remotePort = 6050