6 Commits

Author SHA1 Message Date
5e6552dd6c Change docker repository
All checks were successful
CI / build (push) Successful in 23m2s
2023-07-13 14:58:40 +02:00
41d10c5c94 Add listener closing when the target set has changed.
All checks were successful
CI / build (push) Successful in 22m46s
2023-07-13 11:53:51 +02:00
c90a8fff4a Bumping version to 0.1.11 2023-06-07 08:08:25 +02:00
f290fa2eb1 Adjusting Earthfile 2023-06-07 08:07:40 +02:00
635488ddeb Bumping version 2023-06-06 22:28:23 +02:00
a7e06ca5ce Adding Earthfile for CI 2023-06-06 22:27:49 +02:00
10 changed files with 89 additions and 28 deletions

1
.earthlyignore Normal file
View File

@ -0,0 +1 @@
target/

36
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,36 @@
# .github/workflows/ci.yml
name: CI
on:
push:
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
env:
MY_DOCKER_USERNAME: ${{ secrets.MY_DOCKER_USERNAME }}
MY_DOCKER_PASSWORD: ${{ secrets.MY_DOCKER_PASSWORD }}
FORCE_COLOR: 1
steps:
- uses: https://github.com/earthly/actions-setup@v1
with:
version: v0.7.0
- uses: actions/checkout@v2
- name: Put back the git branch into git (Earthly uses it for tagging)
run: |
branch=""
if [ -n "$GITHUB_HEAD_REF" ]; then
branch="$GITHUB_HEAD_REF"
else
branch="${GITHUB_REF##*/}"
fi
git checkout -b "$branch" || true
- name: Docker Login
run: docker login git.hibas.dev --username "$MY_DOCKER_USERNAME" --password "$MY_DOCKER_PASSWORD"
- name: Earthly version
run: earthly --version
- name: Run build
run: earthly --push +docker-multi

2
Cargo.lock generated
View File

@ -838,7 +838,7 @@ dependencies = [
[[package]]
name = "rustocat"
version = "0.1.9"
version = "0.1.12"
dependencies = [
"async-trait",
"chrono",

View File

@ -1,6 +1,6 @@
[package]
name = "rustocat"
version = "0.1.9"
version = "0.1.12"
edition = "2021"
description = "Socat in rust with many less features and a configuration file"
license = "ISC"

View File

@ -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" ]

25
Earthfile Normal file
View File

@ -0,0 +1,25 @@
VERSION 0.7
FROM rust:alpine
WORKDIR /rustbuild
prepare-env:
RUN apk add --no-cache musl-dev libssl3 openssl-dev
RUN cargo search test # Super simple way to cache the cargo index
docker-multi:
BUILD --platform linux/amd64 --platform linux/arm64 +docker
build:
FROM +prepare-env
COPY . .
RUN cargo build --release
SAVE ARTIFACT target/release/rustocat rustocat
docker:
FROM alpine
RUN apk add --no-cache libssl3
COPY +build/rustocat rustocat
ENTRYPOINT ["./rustocat"]
ARG EARTHLY_TARGET_TAG
ARG TAG=$EARTHLY_TARGET_TAG
SAVE IMAGE --push git.hibas.dev/ops/rustocat:$TAG

View File

@ -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<RwLock<Vec<String>>>,
pub(crate) targets_changed: broadcast::Receiver<()>,
}

View File

@ -20,6 +20,7 @@ pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
struct ActiveListener {
notify_shutdown: broadcast::Sender<()>,
targets: Arc<RwLock<Vec<String>>>,
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);
}

View File

@ -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(());

View File

@ -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(());
}