initial commit
This commit is contained in:
commit
018869e0fa
10 changed files with 825 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
target
|
407
Cargo.lock
generated
Normal file
407
Cargo.lock
generated
Normal file
|
@ -0,0 +1,407 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "fern"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4316185f709b23713e41e3195f90edef7fb00c3ed4adc79769cf09cc762a3b29"
|
||||
dependencies = [
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fuchsia-cprng"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.171"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e"
|
||||
|
||||
[[package]]
|
||||
name = "macaddr"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baee0bbc17ce759db233beb01648088061bf678383130602a298e6998eedb2d8"
|
||||
|
||||
[[package]]
|
||||
name = "matchmaker"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"fern",
|
||||
"log",
|
||||
"macaddr",
|
||||
"rand 0.9.0",
|
||||
"stopwatch",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c9bdb1fb680e609c2e0930c1866cafdd0be7e7c7a1ecf92aec71ed8d99d3e133"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-complex",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1357c02fa1d647dd0769ef5bc2bf86281f064231c09c192a46c71246e3ec9258"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"rand 0.4.6",
|
||||
"rustc-serialize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "17cf384bef067563c44d41028840dbecc7f06f2aa5d7881a81dfb0fc7c72f202"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
"rustc-serialize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.46"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fbfff0773e8a07fb033d726b9ff1327466709820788e5298afce4d752965ff1e"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"rustc-serialize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
|
||||
dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.94"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
|
||||
dependencies = [
|
||||
"fuchsia-cprng",
|
||||
"libc",
|
||||
"rand_core 0.3.1",
|
||||
"rdrand",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94"
|
||||
dependencies = [
|
||||
"rand_chacha",
|
||||
"rand_core 0.9.3",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core 0.9.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
|
||||
dependencies = [
|
||||
"rand_core 0.4.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rdrand"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
|
||||
dependencies = [
|
||||
"rand_core 0.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-serialize"
|
||||
version = "0.3.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe834bc780604f4674073badbad26d7219cadfb4a2275802db12cbae17498401"
|
||||
|
||||
[[package]]
|
||||
name = "stopwatch"
|
||||
version = "0.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d04b5ebc78da44d3a456319d8bc2783e7d8cc7ccbb5cb4dc3f54afbd93bf728"
|
||||
dependencies = [
|
||||
"num",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.13.3+wasi-0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
|
||||
dependencies = [
|
||||
"wit-bindgen-rt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen-rt"
|
||||
version = "0.33.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd97444d05a4328b90e75e503a34bad781f14e28a823ad3557f0750df1ebcbc6"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6352c01d0edd5db859a63e2605f4ea3183ddbd15e2c4a9e7d32184df75e4f154"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
24
Cargo.toml
Normal file
24
Cargo.toml
Normal file
|
@ -0,0 +1,24 @@
|
|||
[package]
|
||||
name = "matchmaker"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "MIT License"
|
||||
|
||||
[dependencies]
|
||||
# Used by library
|
||||
macaddr = "1.0.1"
|
||||
log = "0.4"
|
||||
# Used by server runtime
|
||||
fern = { optional = true, version = "0.7" }
|
||||
|
||||
[dev-dependencies]
|
||||
stopwatch = "0.0.7"
|
||||
rand = "0.9"
|
||||
|
||||
[features]
|
||||
bin-deps = ["fern"]
|
||||
|
||||
[[bin]]
|
||||
name = "matchmaker_server"
|
||||
path = "src/main.rs"
|
||||
required-features = []
|
7
LICENSE
Normal file
7
LICENSE
Normal file
|
@ -0,0 +1,7 @@
|
|||
Copyright 2025 Mad Star Studio LLC
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
4
shell.nix
Normal file
4
shell.nix
Normal file
|
@ -0,0 +1,4 @@
|
|||
{ pkgs ? import <nixpkgs> {} }:
|
||||
pkgs.mkShell {
|
||||
buildInputs = [ pkgs.cargo pkgs.rustc ];
|
||||
}
|
309
src/data/auth.rs
Normal file
309
src/data/auth.rs
Normal file
|
@ -0,0 +1,309 @@
|
|||
use std::hash::{DefaultHasher, Hash, Hasher};
|
||||
|
||||
// TODO: Documentation pass!
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Interface */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
pub trait AuthKey: PartialEq {}
|
||||
|
||||
/// AuthGuards serve as a way to lock certain resources behind a requirement
|
||||
pub trait AuthGuard {
|
||||
type Key: AuthKey;
|
||||
fn verify(&self, key: &Self::Key) -> Result<bool, ()>;
|
||||
}
|
||||
|
||||
/* ------------------------------- Composition ------------------------------ */
|
||||
pub struct ComposeAndAuthGuard<A, B>
|
||||
where
|
||||
A: AuthGuard,
|
||||
B: AuthGuard<Key = A::Key>,
|
||||
{
|
||||
pub left: A,
|
||||
pub right: B,
|
||||
}
|
||||
|
||||
impl<A, B> AuthGuard for ComposeAndAuthGuard<A, B>
|
||||
where
|
||||
A: AuthGuard,
|
||||
B: AuthGuard<Key = A::Key>,
|
||||
{
|
||||
type Key = A::Key;
|
||||
|
||||
fn verify(&self, key: &Self::Key) -> Result<bool, ()> {
|
||||
// Return false as soon as one of the guards fails
|
||||
if !self.left.verify(key)? {
|
||||
return Ok(false);
|
||||
}
|
||||
self.right.verify(key)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ComposeOrAuthGuard<A, B>
|
||||
where
|
||||
A: AuthGuard,
|
||||
B: AuthGuard<Key = A::Key>,
|
||||
{
|
||||
pub left: A,
|
||||
pub right: B,
|
||||
}
|
||||
|
||||
impl<A, B> AuthGuard for ComposeOrAuthGuard<A, B>
|
||||
where
|
||||
A: AuthGuard,
|
||||
B: AuthGuard<Key = A::Key>,
|
||||
{
|
||||
type Key = A::Key;
|
||||
|
||||
fn verify(&self, key: &Self::Key) -> Result<bool, ()> {
|
||||
// Return true as soon as one of the guards succeeds
|
||||
if self.left.verify(key)? {
|
||||
return Ok(true);
|
||||
}
|
||||
self.right.verify(key)
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------ Unit Tests ------------------------------ */
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// A dummy key that implements AuthKey.
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct DummyKey(u64);
|
||||
|
||||
impl AuthKey for DummyKey {}
|
||||
|
||||
// A mock AuthGuard that always returns true.
|
||||
struct AlwaysTrue;
|
||||
|
||||
// A mock AuthGuard that always returns false.
|
||||
struct AlwaysFalse;
|
||||
|
||||
impl AuthGuard for AlwaysTrue {
|
||||
type Key = DummyKey;
|
||||
|
||||
fn verify(&self, _key: &Self::Key) -> Result<bool, ()> {
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
impl AuthGuard for AlwaysFalse {
|
||||
type Key = DummyKey;
|
||||
|
||||
fn verify(&self, _key: &Self::Key) -> Result<bool, ()> {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_and_auth_guard_both_true() {
|
||||
let guard = ComposeAndAuthGuard {
|
||||
left: AlwaysTrue,
|
||||
right: AlwaysTrue,
|
||||
};
|
||||
let key = DummyKey(42);
|
||||
assert_eq!(guard.verify(&key).unwrap(), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_and_auth_guard_true_false() {
|
||||
let guard = ComposeAndAuthGuard {
|
||||
left: AlwaysTrue,
|
||||
right: AlwaysFalse,
|
||||
};
|
||||
let key = DummyKey(42);
|
||||
assert_eq!(guard.verify(&key).unwrap(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_and_auth_guard_false_true() {
|
||||
let guard = ComposeAndAuthGuard {
|
||||
left: AlwaysFalse,
|
||||
right: AlwaysTrue,
|
||||
};
|
||||
let key = DummyKey(42);
|
||||
assert_eq!(guard.verify(&key).unwrap(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_and_auth_guard_both_false() {
|
||||
let guard = ComposeAndAuthGuard {
|
||||
left: AlwaysFalse,
|
||||
right: AlwaysFalse,
|
||||
};
|
||||
let key = DummyKey(42);
|
||||
assert_eq!(guard.verify(&key).unwrap(), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_or_auth_guard_both_true() {
|
||||
let guard = ComposeOrAuthGuard {
|
||||
left: AlwaysTrue,
|
||||
right: AlwaysTrue,
|
||||
};
|
||||
let key = DummyKey(42);
|
||||
assert_eq!(guard.verify(&key).unwrap(), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_or_auth_guard_true_false() {
|
||||
let guard = ComposeOrAuthGuard {
|
||||
left: AlwaysTrue,
|
||||
right: AlwaysFalse,
|
||||
};
|
||||
let key = DummyKey(42);
|
||||
assert_eq!(guard.verify(&key).unwrap(), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_or_auth_guard_false_true() {
|
||||
let guard = ComposeOrAuthGuard {
|
||||
left: AlwaysFalse,
|
||||
right: AlwaysTrue,
|
||||
};
|
||||
let key = DummyKey(42);
|
||||
assert_eq!(guard.verify(&key).unwrap(), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_or_auth_guard_both_false() {
|
||||
let guard = ComposeOrAuthGuard {
|
||||
left: AlwaysFalse,
|
||||
right: AlwaysFalse,
|
||||
};
|
||||
let key = DummyKey(42);
|
||||
assert_eq!(guard.verify(&key).unwrap(), false);
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Default Implementations */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CodeAuthKey {
|
||||
key: u64
|
||||
}
|
||||
|
||||
impl CodeAuthKey {
|
||||
fn _generate_key<T: Hash + ?Sized>(code: &T) -> u64 {
|
||||
let mut s = DefaultHasher::new();
|
||||
code.hash(&mut s);
|
||||
s.finish()
|
||||
}
|
||||
|
||||
pub fn new<T: Hash + ?Sized>(code: &T) -> CodeAuthKey {
|
||||
CodeAuthKey {
|
||||
key: Self::_generate_key(code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AuthKey for CodeAuthKey {}
|
||||
|
||||
impl PartialEq for CodeAuthKey {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
return self.key == other.key;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CodeAuthGuard {
|
||||
reference_key: CodeAuthKey
|
||||
}
|
||||
|
||||
impl CodeAuthGuard {
|
||||
fn new(key: CodeAuthKey) -> Self {
|
||||
Self {
|
||||
reference_key: key
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AuthGuard for CodeAuthGuard {
|
||||
type Key = CodeAuthKey;
|
||||
|
||||
fn verify(&self, key: &Self::Key) -> Result<bool, ()> {
|
||||
Ok(self.reference_key == *key)
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------- Unit Tests ------------------------------- */
|
||||
#[cfg(test)]
|
||||
mod code_authguard_tests {
|
||||
use rand::{distr::Alphanumeric, prelude::*};
|
||||
use stopwatch::Stopwatch;
|
||||
use super::*;
|
||||
|
||||
fn _create_rng_string() -> String {
|
||||
let mut rng = rand::rng();
|
||||
let count = rng.random_range(5..100);
|
||||
rng
|
||||
.sample_iter(Alphanumeric)
|
||||
.take(count)
|
||||
.map(char::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_use() {
|
||||
let code_a = "reference_a";
|
||||
let code_b = "reference_b";
|
||||
|
||||
// Using the keys directly
|
||||
let key_a = CodeAuthKey::new(&code_a);
|
||||
let key_b = CodeAuthKey::new(&code_b);
|
||||
|
||||
assert_eq!(key_a, key_a);
|
||||
assert_eq!(key_b, key_b);
|
||||
assert_ne!(key_a, key_b);
|
||||
|
||||
// Using the CodeAuthGuard
|
||||
let guard = CodeAuthGuard::new(key_a.clone());
|
||||
let mut result_verify = guard.verify(&key_a);
|
||||
assert!(result_verify.is_ok());
|
||||
assert!(result_verify.unwrap() == true);
|
||||
|
||||
result_verify = guard.verify(&key_b);
|
||||
assert!(result_verify.is_ok());
|
||||
assert!(result_verify.unwrap() == false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_benchmark_compare_speed() {
|
||||
// TODO: Look into better ways of performing benchmarks.
|
||||
|
||||
let key_global = CodeAuthKey::new("reference");
|
||||
|
||||
let count = 100000;
|
||||
|
||||
let mut sw = Stopwatch::new();
|
||||
|
||||
// Correct key test
|
||||
sw.start();
|
||||
|
||||
for i in 0..count {
|
||||
let key_local = CodeAuthKey::new(&i);
|
||||
assert_eq!(key_local, key_local);
|
||||
}
|
||||
|
||||
sw.stop();
|
||||
|
||||
println!("Correct key compare perf: {}ms", sw.elapsed_ms());
|
||||
|
||||
// Incorrect key test
|
||||
sw.reset();
|
||||
sw.start();
|
||||
|
||||
for i in 0..count {
|
||||
let key_local = CodeAuthKey::new(&i);
|
||||
assert_ne!(key_local, key_global);
|
||||
}
|
||||
|
||||
sw.stop();
|
||||
|
||||
println!("Incorrect key compare perf: {}ms", sw.elapsed_ms());
|
||||
}
|
||||
}
|
28
src/data/mod.rs
Normal file
28
src/data/mod.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
use auth::AuthGuard;
|
||||
use util::Address;
|
||||
|
||||
pub mod util;
|
||||
pub mod auth;
|
||||
|
||||
pub enum DataStorageError {
|
||||
AlreadyExists,
|
||||
AntiSpam(String)
|
||||
}
|
||||
|
||||
pub trait DataStorageItem {
|
||||
type Id;
|
||||
type Address: Address;
|
||||
fn id(&self) -> Self::Id;
|
||||
fn address_info(&self) -> Self::Address;
|
||||
}
|
||||
|
||||
pub trait DataStorage<Auth: AuthGuard, Item: DataStorageItem>: IntoIterator<Item = Item> {
|
||||
fn update_instance(&mut self, address: &impl Address, guard: Option<Auth>)
|
||||
-> Result<Item::Id, DataStorageError>;
|
||||
|
||||
fn send_heartbeat(&mut self, address: &impl Address)
|
||||
-> Result<(),DataStorageError>;
|
||||
|
||||
fn peek_instance(&self, id: Item::Id);
|
||||
}
|
||||
|
33
src/data/util.rs
Normal file
33
src/data/util.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
use std::net::IpAddr;
|
||||
|
||||
use macaddr::MacAddr;
|
||||
|
||||
pub trait Address: PartialEq {
|
||||
fn mac(&self) -> MacAddr;
|
||||
fn ip(&self) -> IpAddr;
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct SimpleAddress {
|
||||
mac: MacAddr,
|
||||
ip: IpAddr
|
||||
}
|
||||
|
||||
impl SimpleAddress {
|
||||
fn new(ip: IpAddr, mac: MacAddr) -> SimpleAddress {
|
||||
return SimpleAddress {
|
||||
ip,
|
||||
mac
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Address for SimpleAddress {
|
||||
fn mac(&self) -> MacAddr {
|
||||
self.mac
|
||||
}
|
||||
|
||||
fn ip(&self) -> IpAddr {
|
||||
self.ip
|
||||
}
|
||||
}
|
9
src/lib.rs
Normal file
9
src/lib.rs
Normal file
|
@ -0,0 +1,9 @@
|
|||
pub mod data;
|
||||
|
||||
pub trait Matchmaker {
|
||||
fn run(&mut self);
|
||||
}
|
||||
|
||||
struct DefaultMatchmaker {
|
||||
|
||||
}
|
3
src/main.rs
Normal file
3
src/main.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue