generated from PlexSheep/rs-base
Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
Christoph J. Scherr | 53722f502d |
|
@ -16,11 +16,9 @@ jobs:
|
||||||
- name: get repo
|
- name: get repo
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: install rust
|
- name: install rust
|
||||||
uses: https://github.com/dtolnay/rust-toolchain@stable
|
uses: dtolnay/rust-toolchain@stable
|
||||||
- name: install additional rust things
|
- name: install additional rust things
|
||||||
run: |
|
run: rustup component add rustfmt
|
||||||
rustup component add rustfmt
|
|
||||||
rustup component add clippy
|
|
||||||
- name: config custom registry
|
- name: config custom registry
|
||||||
run: |
|
run: |
|
||||||
mkdir -p ~/.cargo/
|
mkdir -p ~/.cargo/
|
||||||
|
@ -30,16 +28,16 @@ jobs:
|
||||||
echo '[registries.cscherr]' >> ~/.cargo/config.toml
|
echo '[registries.cscherr]' >> ~/.cargo/config.toml
|
||||||
echo 'index = "https://git.cscherr.de/PlexSheep/_cargo-index.git"' >> ~/.cargo/config.toml
|
echo 'index = "https://git.cscherr.de/PlexSheep/_cargo-index.git"' >> ~/.cargo/config.toml
|
||||||
cat ~/.cargo/config.toml
|
cat ~/.cargo/config.toml
|
||||||
- name: cargo clippy check
|
- name: cargo check
|
||||||
run: cargo clippy --all-features --all-targets
|
run: cargo check --all-features --all-targets
|
||||||
- name: cargo clippy fix
|
- name: cargo fix
|
||||||
run: cargo clippy --fix --all-features --all-targets
|
run: cargo fix --all-features --all-targets
|
||||||
- name: cargo fmt
|
- name: cargo fmt
|
||||||
run: cargo fmt --all
|
run: cargo fmt --all
|
||||||
- name: cargo test
|
- name: cargo test
|
||||||
run: cargo test --all-features --all-targets
|
run: cargo test --all-features --all-targets
|
||||||
- name: commit back to repository
|
- name: commit back to repository
|
||||||
uses: https://github.com/stefanzweifel/git-auto-commit-action@v5
|
uses: stefanzweifel/git-auto-commit-action@v5
|
||||||
with:
|
with:
|
||||||
# Optional. Commit message for the created commit.
|
# Optional. Commit message for the created commit.
|
||||||
# Defaults to "Apply automatic changes"
|
# Defaults to "Apply automatic changes"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "netpong"
|
name = "netpong"
|
||||||
version = "0.2.2"
|
version = "0.2.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = true
|
publish = true
|
||||||
authors = ["Christoph J. Scherr <software@cscherr.de>"]
|
authors = ["Christoph J. Scherr <software@cscherr.de>"]
|
||||||
|
@ -17,12 +17,12 @@ anyhow = "1.0.79"
|
||||||
clap = "4.4.18"
|
clap = "4.4.18"
|
||||||
clap-num = "1.0.2"
|
clap-num = "1.0.2"
|
||||||
clap-verbosity-flag = "2.1.2"
|
clap-verbosity-flag = "2.1.2"
|
||||||
|
libpt = { version = "0.3.11", features = ["net"] }
|
||||||
thiserror = "1.0.56"
|
thiserror = "1.0.56"
|
||||||
tokio = { version = "1.35.1", features = ["net", "rt-multi-thread", "macros", "time", "io-util"] }
|
tokio = { version = "1.35.1", features = ["net", "rt", "macros"] }
|
||||||
rustls-pemfile = "2.0.0"
|
rustls-pemfile = "2.0.0"
|
||||||
tokio-rustls = "0.25.0"
|
tokio-rustls = "0.25.0"
|
||||||
webpki-roots = "0.26.0"
|
webpki-roots = "0.26.0"
|
||||||
libpt = { version = "0.6.0", features = ["log"] }
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["server"]
|
default = ["server"]
|
||||||
|
|
|
@ -3,20 +3,24 @@ use std::{fs::File, io::BufReader, sync::Arc};
|
||||||
|
|
||||||
use crate::{common::decode, Config};
|
use crate::{common::decode, Config};
|
||||||
|
|
||||||
|
use anyhow;
|
||||||
use libpt::log::{error, info, trace};
|
use libpt::log::{error, info, trace};
|
||||||
use rustls_pemfile::certs;
|
use rustls_pemfile::certs;
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::{
|
||||||
use tokio::net::TcpStream;
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
|
net::TcpStream,
|
||||||
|
};
|
||||||
use tokio_rustls::{
|
use tokio_rustls::{
|
||||||
rustls::{self, pki_types},
|
rustls::{self, pki_types},
|
||||||
TlsConnector, TlsStream,
|
TlsConnector,
|
||||||
};
|
};
|
||||||
|
use webpki_roots;
|
||||||
|
|
||||||
const BUF_SIZE: usize = 512;
|
const BUF_SIZE: usize = 512;
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
cfg: Config,
|
cfg: Config,
|
||||||
stream: TlsStream<TcpStream>,
|
stream: TcpStream,
|
||||||
connector: TlsConnector,
|
connector: TlsConnector,
|
||||||
domain: pki_types::ServerName<'static>,
|
domain: pki_types::ServerName<'static>,
|
||||||
}
|
}
|
||||||
|
@ -37,6 +41,7 @@ impl Client {
|
||||||
.with_root_certificates(root_cert_store)
|
.with_root_certificates(root_cert_store)
|
||||||
.with_no_client_auth();
|
.with_no_client_auth();
|
||||||
let connector = TlsConnector::from(Arc::new(tls_config));
|
let connector = TlsConnector::from(Arc::new(tls_config));
|
||||||
|
let stream = TcpStream::connect(&cfg.addr).await?;
|
||||||
let domain = match pki_types::ServerName::try_from(cfg.hostname.clone()) {
|
let domain = match pki_types::ServerName::try_from(cfg.hostname.clone()) {
|
||||||
Ok(domain) => domain,
|
Ok(domain) => domain,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -44,29 +49,27 @@ impl Client {
|
||||||
return Err(err.into());
|
return Err(err.into());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let stream = connector
|
|
||||||
.connect(domain.clone(), TcpStream::connect(&cfg.addr).await?)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(Client {
|
Ok(Client {
|
||||||
cfg: cfg.clone(),
|
cfg: cfg.clone(),
|
||||||
stream: tokio_rustls::TlsStream::Client(stream),
|
stream,
|
||||||
connector,
|
connector,
|
||||||
domain,
|
domain,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(mut self) -> anyhow::Result<()> {
|
pub async fn run(self) -> anyhow::Result<()> {
|
||||||
|
let mut stream = self.connector.connect(self.domain, self.stream).await?;
|
||||||
let mut buf = [0; BUF_SIZE];
|
let mut buf = [0; BUF_SIZE];
|
||||||
self.stream.write_all(b"ping").await?;
|
stream.write_all(b"ping").await?;
|
||||||
info!("> ({}) ping", self.cfg.hostname);
|
info!("> ({}) ping", self.cfg.hostname);
|
||||||
while self.stream.read(&mut buf).await? > 0 {
|
while stream.read(&mut buf).await? > 0 {
|
||||||
let response = decode(&buf)?;
|
let response = decode(&buf)?;
|
||||||
info!("< ({}) {}", self.cfg.hostname, response);
|
info!("< ({}) {}", self.cfg.hostname, response);
|
||||||
if response == "You win!" {
|
if response == "You win!" {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
self.stream.write_all(b"ping").await?;
|
stream.write_all(b"ping").await?;
|
||||||
info!("> ({}) ping", self.cfg.hostname);
|
info!("> ({}) ping", self.cfg.hostname);
|
||||||
// we should wait, so that we don't spam the client
|
// we should wait, so that we don't spam the client
|
||||||
std::thread::sleep(self.cfg.delay);
|
std::thread::sleep(self.cfg.delay);
|
||||||
|
|
|
@ -6,11 +6,11 @@ use clap::Parser;
|
||||||
use clap_verbosity_flag::{InfoLevel, Verbosity};
|
use clap_verbosity_flag::{InfoLevel, Verbosity};
|
||||||
|
|
||||||
/// short about section displayed in help
|
/// short about section displayed in help
|
||||||
const ABOUT_ROOT: &str = r##"
|
const ABOUT_ROOT: &'static str = r##"
|
||||||
Let your hosts play ping pong over the network
|
Let your hosts play ping pong over the network
|
||||||
"##;
|
"##;
|
||||||
/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT)
|
/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT)
|
||||||
static LONG_ABOUT_ROOT: &str = r##"
|
static LONG_ABOUT_ROOT: &'static str = r##"
|
||||||
|
|
||||||
Connect to a ping pong server and periodically send a ping. The server will reply with a pong.
|
Connect to a ping pong server and periodically send a ping. The server will reply with a pong.
|
||||||
That's it really. You can also host your own netpong server (server feature).
|
That's it really. You can also host your own netpong server (server feature).
|
||||||
|
@ -74,18 +74,11 @@ impl Cli {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if cli.meta {
|
if cli.meta {
|
||||||
let _ = Logger::builder()
|
Logger::init(None, Some(ll), true).expect("could not initialize Logger");
|
||||||
.set_level(ll)
|
|
||||||
.display_filename(true)
|
|
||||||
.build()
|
|
||||||
.expect("could not initialize Logger");
|
|
||||||
} else {
|
} else {
|
||||||
// less verbose version
|
// less verbose version
|
||||||
let _ = Logger::builder()
|
Logger::init_mini(Some(ll)).expect("could not initialize Logger");
|
||||||
.set_level(ll)
|
|
||||||
.build()
|
|
||||||
.expect("could not initialize Logger");
|
|
||||||
}
|
}
|
||||||
cli
|
return cli;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ pub mod conf;
|
||||||
pub fn decode(buf: &[u8]) -> Result<String, Utf8Error> {
|
pub fn decode(buf: &[u8]) -> Result<String, Utf8Error> {
|
||||||
Ok(match std::str::from_utf8(buf) {
|
Ok(match std::str::from_utf8(buf) {
|
||||||
Ok(s) => s.to_string(),
|
Ok(s) => s.to_string(),
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err.into()),
|
||||||
}
|
}
|
||||||
.trim_matches(char::from(0))
|
.trim_matches(char::from(0))
|
||||||
.to_string())
|
.to_string())
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use libpt::log::*;
|
use libpt::log::*;
|
||||||
|
use tokio;
|
||||||
|
|
||||||
mod client;
|
mod client;
|
||||||
mod common;
|
mod common;
|
||||||
|
|
|
@ -13,7 +13,7 @@ use rustls_pemfile::{certs, private_key};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncReadExt, AsyncWriteExt},
|
io::{AsyncReadExt, AsyncWriteExt},
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
time,
|
time::{self},
|
||||||
};
|
};
|
||||||
use tokio_rustls::{rustls, TlsAcceptor};
|
use tokio_rustls::{rustls, TlsAcceptor};
|
||||||
|
|
||||||
|
@ -109,10 +109,10 @@ impl Server {
|
||||||
error!("the server needs a key!");
|
error!("the server needs a key!");
|
||||||
return Err(std::io::ErrorKind::InvalidInput.into());
|
return Err(std::io::ErrorKind::InvalidInput.into());
|
||||||
}
|
}
|
||||||
|
let key = private_key(&mut std::io::BufReader::new(File::open(
|
||||||
private_key(&mut std::io::BufReader::new(File::open(
|
|
||||||
cfg.key.clone().unwrap(),
|
cfg.key.clone().unwrap(),
|
||||||
)?))
|
)?));
|
||||||
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_certs(cfg: Config) -> std::io::Result<Vec<CertificateDer<'static>>> {
|
fn load_certs(cfg: Config) -> std::io::Result<Vec<CertificateDer<'static>>> {
|
||||||
|
@ -121,18 +121,18 @@ impl Server {
|
||||||
return Err(std::io::ErrorKind::InvalidInput.into());
|
return Err(std::io::ErrorKind::InvalidInput.into());
|
||||||
}
|
}
|
||||||
match certs(&mut std::io::BufReader::new(File::open(
|
match certs(&mut std::io::BufReader::new(File::open(
|
||||||
cfg.certs.clone().unwrap(),
|
&cfg.certs.clone().unwrap(),
|
||||||
)?))
|
)?))
|
||||||
.collect::<std::io::Result<Vec<CertificateDer<'static>>>>()
|
.collect::<std::io::Result<Vec<CertificateDer<'static>>>>()
|
||||||
{
|
{
|
||||||
Ok(v) if !v.is_empty() => Ok(v),
|
Ok(v) if !v.is_empty() => Ok(v),
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
error!("no certs found in provided file {:?}", cfg.certs);
|
error!("no certs found in provided file {:?}", cfg.certs);
|
||||||
Err(std::io::ErrorKind::InvalidInput.into())
|
return Err(std::io::ErrorKind::InvalidInput.into());
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("could not load certs: {err:?}");
|
error!("could not load certs: {err:?}");
|
||||||
Err(err)
|
return Err(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue