From 41d10c5c94c5d09bd418a801b3ea19d785282878 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Thu, 13 Jul 2023 11:53:51 +0200 Subject: [PATCH] Add listener closing when the target set has changed. --- .earthlyignore | 1 + .github/workflows/ci.yml | 9 ++++----- Cargo.lock | 2 +- Cargo.toml | 2 +- Dockerfile | 26 -------------------------- Earthfile | 4 ++-- src/listener.rs | 2 ++ src/main.rs | 8 ++++++++ src/tcp.rs | 4 ++++ src/udp.rs | 11 +++++++++++ 10 files changed, 34 insertions(+), 35 deletions(-) create mode 100644 .earthlyignore delete mode 100644 Dockerfile diff --git a/.earthlyignore b/.earthlyignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.earthlyignore @@ -0,0 +1 @@ +target/ diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4d81efc..e893d73 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,6 @@ name: CI on: push: - branches: [main] pull_request: branches: [main] @@ -12,11 +11,11 @@ jobs: build: runs-on: ubuntu-latest env: - DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} - DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }} + MY_DOCKER_USERNAME: ${{ secrets.MY_DOCKER_USERNAME }} + MY_DOCKER_PASSWORD: ${{ secrets.MY_DOCKER_PASSWORD }} FORCE_COLOR: 1 steps: - - uses: earthly/actions/setup-earthly@v1 + - uses: https://github.com/earthly/actions-setup@v1 with: version: v0.7.0 - uses: actions/checkout@v2 @@ -30,7 +29,7 @@ jobs: fi git checkout -b "$branch" || true - name: Docker Login - run: docker login docker.hibas123.de --username "$DOCKER_USERNAME" --password "$DOCKER_TOKEN" + run: docker login git.hibas.dev --username "$MY_DOCKER_USERNAME" --password "$MY_DOCKER_PASSWORD" - name: Earthly version run: earthly --version - name: Run build diff --git a/Cargo.lock b/Cargo.lock index 04d8830..46f04e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -838,7 +838,7 @@ dependencies = [ [[package]] name = "rustocat" -version = "0.1.10" +version = "0.1.12" dependencies = [ "async-trait", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 79b9b24..b1ac83b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustocat" -version = "0.1.11" +version = "0.1.12" edition = "2021" description = "Socat in rust with many less features and a configuration file" license = "ISC" diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index b1ffafc..0000000 --- a/Dockerfile +++ /dev/null @@ -1,26 +0,0 @@ -FROM rust:alpine as builder - -WORKDIR /app - -RUN apk add --no-cache musl-dev libssl3 openssl-dev - -# This should fetch the index and cache it. This should reduce the time required of subsequent builds -RUN cargo search test - -COPY Cargo.toml Cargo.lock /app/ -# COPY .cargo /app/.cargo - -COPY src /app/src - - -RUN cargo build --release - -FROM alpine - -RUN apk add --no-cache libssl3 -COPY --from=builder /app/target/release/rustocat /usr/bin/rustocat - -ENTRYPOINT ["/bin/sh"] -CMD [ "-c", "/usr/bin/rustocat" ] - - diff --git a/Earthfile b/Earthfile index e60dcd5..45d38cb 100644 --- a/Earthfile +++ b/Earthfile @@ -19,7 +19,7 @@ docker: FROM alpine RUN apk add --no-cache libssl3 COPY +build/rustocat rustocat - ENTRYPOINT ["rustocat"] + ENTRYPOINT ["./rustocat"] ARG EARTHLY_TARGET_TAG ARG TAG=$EARTHLY_TARGET_TAG - SAVE IMAGE --push docker.hibas123.de/rustocat:$TAG + SAVE IMAGE --push git.hibas.dev/hibas123/rustocat:$TAG \ No newline at end of file diff --git a/src/listener.rs b/src/listener.rs index bdc3160..4d9ff9a 100644 --- a/src/listener.rs +++ b/src/listener.rs @@ -1,9 +1,11 @@ use crate::shutdown::ShutdownReceiver; use std::sync::Arc; +use tokio::sync::broadcast; use tokio::sync::RwLock; pub(crate) struct Listener { pub(crate) shutdown: ShutdownReceiver, pub(crate) source: String, pub(crate) targets: Arc>>, + pub(crate) targets_changed: broadcast::Receiver<()>, } diff --git a/src/main.rs b/src/main.rs index 464a381..93a7ecd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ pub type Result = std::result::Result>; struct ActiveListener { notify_shutdown: broadcast::Sender<()>, targets: Arc>>, + notify_targets: broadcast::Sender<()>, } #[tokio::main] @@ -119,19 +120,24 @@ async fn run_loop( warn!("Found invalid targets! Adjusting!"); let mut w = listener.targets.write().await; *w = target.targets; + + _ = listener.notify_targets.send(())?; //TODO: Maybe ignore this error } } else { let (notify_shutdown, _) = broadcast::channel(1); + let (notify_targets, _) = broadcast::channel(1); let listener = ActiveListener { notify_shutdown: notify_shutdown, targets: Arc::new(RwLock::new(target.targets)), + notify_targets: notify_targets, }; let l = listener::Listener { shutdown: shutdown::ShutdownReceiver::new(listener.notify_shutdown.subscribe()), source: target.source.clone(), targets: listener.targets.clone(), + targets_changed: listener.notify_targets.subscribe(), }; tokio::spawn(async move { @@ -168,6 +174,8 @@ async fn run_loop( } } + // TODO: Maybe Wait for the listener to shut down + debug!("Removing listener!"); listeners.remove(&del_key); } diff --git a/src/tcp.rs b/src/tcp.rs index 45f4d9a..7de4e96 100644 --- a/src/tcp.rs +++ b/src/tcp.rs @@ -29,6 +29,10 @@ pub(crate) async fn start_tcp_listener( loop { let (next_socket, _) = tokio::select! { res = listener.accept() => res?, + _ = listener_config.targets_changed.recv() => { + info!("Targets changed!"); + continue; + } _ = listener_config.shutdown.recv() => { info!("Exiting listener!"); return Ok(()); diff --git a/src/udp.rs b/src/udp.rs index 3349cb6..010bf7e 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -180,6 +180,15 @@ pub(crate) async fn start_udp_listener(mut listener_config: Listener) -> Result< trace!("Waiting for packet or shutdown"); let (num_bytes, src_addr) = tokio::select! { res = listener.recv_from(&mut buf) => res?, + _ = listener_config.targets_changed.recv() => { + info!("Targets changed!"); + info!("Closing all connections!"); + for (_, handler) in connections.lock().await.iter_mut() { + handler.close().await; + } + info!("Closed all connections!"); + continue; + } _ = listener_config.shutdown.recv() => { info!("Exiting listener!"); break; @@ -220,5 +229,7 @@ pub(crate) async fn start_udp_listener(mut listener_config: Listener) -> Result< handler.close().await; } + println!("Listener closed."); + return Ok(()); }