Relax ID desync check to avoid rejecting packets under normal loads

This commit is contained in:
NGnius (Graham) 2023-01-21 18:04:50 -05:00
parent 4745f0ae7e
commit 9b3facd48d
7 changed files with 22 additions and 75 deletions

61
Cargo.lock generated
View file

@ -87,12 +87,6 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "block"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.9.0" version = "0.9.0"
@ -192,18 +186,6 @@ dependencies = [
"cipher", "cipher",
] ]
[[package]]
name = "current_locale"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0be8ddcccda8be68d8e31a421ceea7c79857404daa052434ae30ce2f402cd10"
dependencies = [
"libc",
"objc",
"objc-foundation",
"winapi",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.9.0" version = "0.9.0"
@ -572,15 +554,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "malloc_buf"
version = "0.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"
@ -658,35 +631,6 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b2b2cbbfd8defa51ff24450a61d73b3ff3e158484ddd274a883e886e6fbaa78" checksum = "7b2b2cbbfd8defa51ff24450a61d73b3ff3e158484ddd274a883e886e6fbaa78"
[[package]]
name = "objc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1"
dependencies = [
"malloc_buf",
]
[[package]]
name = "objc-foundation"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9"
dependencies = [
"block",
"objc",
"objc_id",
]
[[package]]
name = "objc_id"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b"
dependencies = [
"objc",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.14.0" version = "1.14.0"
@ -1239,12 +1183,11 @@ dependencies = [
[[package]] [[package]]
name = "usdpl-back" name = "usdpl-back"
version = "0.9.0" version = "0.9.1"
dependencies = [ dependencies = [
"async-recursion", "async-recursion",
"async-trait", "async-trait",
"bytes", "bytes",
"current_locale",
"gettext-ng", "gettext-ng",
"hex", "hex",
"log", "log",
@ -1275,7 +1218,7 @@ dependencies = [
[[package]] [[package]]
name = "usdpl-front" name = "usdpl-front"
version = "0.9.0" version = "0.9.1"
dependencies = [ dependencies = [
"console_error_panic_hook", "console_error_panic_hook",
"hex", "hex",

View file

@ -1,5 +1,5 @@
[package] [package]
name = "usdpl-rs" name = "usdpl"
version = "0.9.0" version = "0.9.0"
authors = ["NGnius (Graham) <ngniusness@gmail.com>"] authors = ["NGnius (Graham) <ngniusness@gmail.com>"]
edition = "2021" edition = "2021"

View file

@ -14,7 +14,7 @@ A faster, safer way to write plugin back-ends
- [x] External API documentation - [x] External API documentation
- [ ] Internal protocol documentation - [ ] Internal protocol documentation
- [x] Async support - [x] Async support
- [ ] Encryption - [x] Encryption
- [x] PluginLoader/Decky support - [x] PluginLoader/Decky support
- [x] Plugin template - [x] Plugin template
- [ ] Crankshaft support - [ ] Crankshaft support

View file

@ -1,6 +1,6 @@
[package] [package]
name = "usdpl-back" name = "usdpl-back"
version = "0.9.0" version = "0.9.1"
edition = "2021" edition = "2021"
license = "GPL-3.0-only" license = "GPL-3.0-only"
repository = "https://github.com/NGnius/usdpl-rs" repository = "https://github.com/NGnius/usdpl-rs"

View file

@ -9,7 +9,7 @@ use usdpl_core::{socket, RemoteCallResponse};
use super::{Callable, MutCallable, AsyncCallable, WrappedCallable}; use super::{Callable, MutCallable, AsyncCallable, WrappedCallable};
static LAST_ID: AtomicU64 = AtomicU64::new(0); static LAST_ID: AtomicU64 = AtomicU64::new(0);
const MAX_ID_DIFFERENCE: u64 = 5; const MAX_ID_DIFFERENCE: u64 = 32;
//type WrappedCallable = Arc<Mutex<Box<dyn Callable>>>; // thread-safe, cloneable Callable //type WrappedCallable = Arc<Mutex<Box<dyn Callable>>>; // thread-safe, cloneable Callable
@ -94,11 +94,16 @@ impl Instance {
socket::Packet::Call(call) => { socket::Packet::Call(call) => {
log::debug!("Got USDPL call {} (`{}`, params: {})", call.id, call.function, call.parameters.len()); log::debug!("Got USDPL call {} (`{}`, params: {})", call.id, call.function, call.parameters.len());
let last_id = LAST_ID.load(Ordering::SeqCst); let last_id = LAST_ID.load(Ordering::SeqCst);
if call.id == 0 { if last_id == 0 {
log::info!("Call ID is 0, assuming new connection (resetting last id)"); log::info!("Last id is 0, assuming resumed connection (overriding last id)");
LAST_ID.store(0, Ordering::SeqCst); LAST_ID.store(call.id, Ordering::SeqCst);
} else if call.id < MAX_ID_DIFFERENCE {
log::info!("Call ID is low, assuming new connection (resetting last id)");
LAST_ID.store(call.id, Ordering::SeqCst);
} else if call.id > last_id && call.id - last_id < MAX_ID_DIFFERENCE { } else if call.id > last_id && call.id - last_id < MAX_ID_DIFFERENCE {
LAST_ID.store(call.id, Ordering::SeqCst); LAST_ID.store(call.id, Ordering::SeqCst);
} else if call.id < last_id && last_id - call.id < MAX_ID_DIFFERENCE {
// Allowed, but don't store new (lower) LAST_ID
} else { } else {
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
{ {

View file

@ -1,6 +1,6 @@
[package] [package]
name = "usdpl-front" name = "usdpl-front"
version = "0.9.0" version = "0.9.1"
authors = ["NGnius (Graham) <ngniusness@gmail.com>"] authors = ["NGnius (Graham) <ngniusness@gmail.com>"]
edition = "2021" edition = "2021"
license = "GPL-3.0-only" license = "GPL-3.0-only"

View file

@ -9,6 +9,8 @@ mod connection;
mod convert; mod convert;
mod imports; mod imports;
use std::sync::atomic::{AtomicU64, Ordering};
use js_sys::Array; use js_sys::Array;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
@ -18,7 +20,7 @@ use usdpl_core::{socket::Packet, RemoteCall};
static mut CTX: UsdplContext = UsdplContext { static mut CTX: UsdplContext = UsdplContext {
port: 31337, port: 31337,
id: 1, id: AtomicU64::new(0),
#[cfg(feature = "encrypt")] #[cfg(feature = "encrypt")]
key: Vec::new(), key: Vec::new(),
}; };
@ -37,7 +39,7 @@ fn encryption_key() -> Vec<u8> {
#[derive(Debug)] #[derive(Debug)]
struct UsdplContext { struct UsdplContext {
port: u16, port: u16,
id: u64, id: AtomicU64,
#[cfg(feature = "encrypt")] #[cfg(feature = "encrypt")]
key: Vec<u8>, key: Vec<u8>,
} }
@ -52,11 +54,8 @@ fn get_key() -> Vec<u8> {
} }
fn increment_id() -> u64 { fn increment_id() -> u64 {
let current_id = unsafe { CTX.id }; let atomic = unsafe { &CTX.id };
unsafe { atomic.fetch_add(1, Ordering::SeqCst)
CTX.id += 1;
}
current_id
} }
/// Initialize the front-end library /// Initialize the front-end library
@ -68,7 +67,7 @@ pub fn init_usdpl(port: u16) {
unsafe { unsafe {
CTX = UsdplContext { CTX = UsdplContext {
port: port, port: port,
id: 1, id: AtomicU64::new(0),
#[cfg(feature = "encrypt")] #[cfg(feature = "encrypt")]
key: encryption_key(), key: encryption_key(),
}; };