Compare commits
4 Commits
CI
...
15672536f6
| Author | SHA1 | Date | |
|---|---|---|---|
| 15672536f6 | |||
| b5d2a63909 | |||
| 904091c455 | |||
| 02f44cfab6 |
@@ -1,38 +0,0 @@
|
|||||||
name: "Code Checks"
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
# Check formatting with rustfmt
|
|
||||||
formatting:
|
|
||||||
name: cargo fmt
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
# Ensure rustfmt is installed and setup problem matcher
|
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
|
||||||
with:
|
|
||||||
components: rustfmt
|
|
||||||
- name: Rustfmt Check
|
|
||||||
uses: actions-rust-lang/rustfmt@v1
|
|
||||||
|
|
||||||
# Check code with clippy
|
|
||||||
clippy:
|
|
||||||
name: cargo clippy
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
# Ensure clippy is installed and setup problem matcher
|
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
|
||||||
with:
|
|
||||||
components: clippy
|
|
||||||
- run: cargo clippy -- -D warnings
|
|
||||||
|
|
||||||
# Run tests
|
|
||||||
test:
|
|
||||||
name: cargo test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
|
||||||
- run: cargo test
|
|
||||||
25
src/main.rs
25
src/main.rs
@@ -1,5 +1,5 @@
|
|||||||
#![warn(clippy::pedantic)]
|
#![warn(clippy::pedantic)]
|
||||||
#![allow(clippy::let_underscore_untyped)] // false positive in stable
|
// #![allow(clippy::missing_errors_doc)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
@@ -350,16 +350,23 @@ async fn tokio_main(config: Arc<Config>) -> eyre::Result<()> {
|
|||||||
"centralex server listening"
|
"centralex server listening"
|
||||||
);
|
);
|
||||||
|
|
||||||
while let Ok((stream, addr)) = listener.accept().await {
|
loop {
|
||||||
info!(%addr, "new connection");
|
let connection = listener.accept().await;
|
||||||
|
|
||||||
spawn(
|
match connection {
|
||||||
&format!("connection to {addr}"),
|
Ok((stream, addr)) => {
|
||||||
connection_handler(stream, addr, config.clone(), port_handler.clone()),
|
info!(%addr, "new connection");
|
||||||
);
|
|
||||||
|
spawn(
|
||||||
|
&format!("connection to {addr}"),
|
||||||
|
connection_handler(stream, addr, config.clone(), port_handler.clone()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!(%err, "failed to accept connection");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use std::fmt::Debug;
|
use std::{ffi::CStr, fmt::Debug};
|
||||||
|
|
||||||
use bytemuck::{Pod, Zeroable};
|
use bytemuck::{Pod, Zeroable};
|
||||||
use eyre::eyre;
|
use eyre::eyre;
|
||||||
@@ -85,18 +85,18 @@ impl Debug for Packet {
|
|||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let data = &self.data;
|
let data = &self.data;
|
||||||
|
|
||||||
let str_data = std::str::from_utf8(&data[..data.len().saturating_sub(1)]).ok();
|
let mut debugger = f.debug_struct("Packet");
|
||||||
|
|
||||||
let data = if let Some(str_data) = str_data.as_ref() {
|
debugger.field("kind", &PacketKind::from_u8(self.header.kind));
|
||||||
str_data as &dyn Debug
|
|
||||||
|
let c_str = CStr::from_bytes_until_nul(data).ok();
|
||||||
|
if let Some(str_data) = c_str.as_ref().and_then(|x| x.to_str().ok()) {
|
||||||
|
debugger.field("data", &str_data);
|
||||||
} else {
|
} else {
|
||||||
&data as &dyn Debug
|
debugger.field("data", &data);
|
||||||
};
|
}
|
||||||
|
|
||||||
f.debug_struct("Packet")
|
debugger.finish()
|
||||||
.field("kind", &PacketKind::from_u8(self.header.kind))
|
|
||||||
.field("data", &data)
|
|
||||||
.finish()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
20
src/ports.rs
20
src/ports.rs
@@ -283,7 +283,7 @@ impl PortHandler {
|
|||||||
self.last_update = Some(now);
|
self.last_update = Some(now);
|
||||||
self.change_sender
|
self.change_sender
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.expect("PortHandler is missing it's change_sender")
|
.expect("PortHandler is missing its change_sender")
|
||||||
.send(now)
|
.send(now)
|
||||||
.expect("failed to notify cache writer");
|
.expect("failed to notify cache writer");
|
||||||
}
|
}
|
||||||
@@ -301,15 +301,9 @@ impl PortHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::missing_errors_doc)]
|
#[allow(clippy::missing_errors_doc)]
|
||||||
#[instrument(skip(change_sender))]
|
pub fn load(cache: &Path) -> std::io::Result<Self> {
|
||||||
pub fn load(
|
|
||||||
cache: &Path,
|
|
||||||
change_sender: tokio::sync::watch::Sender<Instant>,
|
|
||||||
) -> std::io::Result<Self> {
|
|
||||||
info!("loading cache");
|
info!("loading cache");
|
||||||
let mut cache: Self = serde_json::from_reader(BufReader::new(File::open(cache)?))?;
|
Ok(serde_json::from_reader(BufReader::new(File::open(cache)?))?)
|
||||||
cache.change_sender = Some(change_sender);
|
|
||||||
Ok(cache)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
@@ -318,10 +312,14 @@ impl PortHandler {
|
|||||||
path: &Path,
|
path: &Path,
|
||||||
change_sender: tokio::sync::watch::Sender<Instant>,
|
change_sender: tokio::sync::watch::Sender<Instant>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self::load(path, change_sender).unwrap_or_else(|error| {
|
let mut this = Self::load(path).unwrap_or_else(|error| {
|
||||||
error!(?path, %error, "failed to parse cache file");
|
error!(?path, %error, "failed to parse cache file");
|
||||||
Self::default()
|
Self::default()
|
||||||
})
|
});
|
||||||
|
|
||||||
|
this.change_sender = Some(change_sender);
|
||||||
|
|
||||||
|
this
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_allowed_ports(&mut self, allowed_ports: &AllowedList) {
|
pub fn update_allowed_ports(&mut self, allowed_ports: &AllowedList) {
|
||||||
|
|||||||
Reference in New Issue
Block a user