From c0713400778eb412d113ea217982a586d79f35fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=81LI=20G=C3=A1bor=20J=C3=A1nos?= Date: Tue, 29 Nov 2022 04:53:11 +0100 Subject: Implement a forwarding service. - Add the `forwarding` system service (script + OpenRC support). - Enable support for UDP ports for `socat`. - Adjust UDS pass-through to avoid conflicts with forwarding. - Update documentation. Requested by: Universal Devices --- aports/forwarding/APKBUILD | 26 +++++++++++++++ aports/forwarding/forwarding | 65 ++++++++++++++++++++++++++++++++++++++ aports/forwarding/forwarding.initd | 13 ++++++++ 3 files changed, 104 insertions(+) create mode 100644 aports/forwarding/APKBUILD create mode 100644 aports/forwarding/forwarding create mode 100644 aports/forwarding/forwarding.initd (limited to 'aports/forwarding') diff --git a/aports/forwarding/APKBUILD b/aports/forwarding/APKBUILD new file mode 100644 index 0000000..f735cf4 --- /dev/null +++ b/aports/forwarding/APKBUILD @@ -0,0 +1,26 @@ +# Maintainer: Gabor Pali + +pkgname=forwarding +pkgver=0.1.0 +pkgrel=0 +pkgdesc="Forwarding management service for Wifibox" +url="https://github.com/pgj/freebsd-wifibox-alpine" +arch="all" +license="BSD2" +depends="socat" +install="" +subpackages= +options="!check" +arch="noarch" +source="forwarding.initd + forwarding +" + +package() { + install -D -m755 "$srcdir"/forwarding.initd "$pkgdir"/etc/init.d/forwarding + install -D -m755 "$srcdir"/forwarding "$pkgdir"/sbin/forwarding +} +sha512sums=" +4df2661906f5f1c3bc93ef29f2e9265e5391ae1a24a990e7bf70b4687b65cfae5afe4be481214e9fa2646137b37c9fe688074d0e4ebe1c2a5a73a3ec8fbb0098 forwarding.initd +ffc13f4384e896e59c695826a659e5ab5df06e08a0b418e67cebf0fe43ac22dfb57489031db7e9bef4465cd6f6cdf188606ad2b02c945acfcd1911fd21ee4a1e forwarding +" diff --git a/aports/forwarding/forwarding b/aports/forwarding/forwarding new file mode 100644 index 0000000..be1fc64 --- /dev/null +++ b/aports/forwarding/forwarding @@ -0,0 +1,65 @@ +#!/bin/sh +# shellcheck disable=SC2034,SC3043,SC3060 + +log() { + local _level="$1" + local _message="$2" + + /usr/bin/logger -p "daemon.${_level}" -t "forwarding[$$]" "${_message}" +} + +capture_output() { + local _type="$1" + local _id="$2" + + while read -r message; do + log "${_type}" "[${_id}] ${message}" + done +} + +_block= + +terminate() { + local _sleep + + if [ -n "${_block}" ]; then + log info "Received signal for termination, stopping PID ${_block}." + /bin/kill -KILL "${_block}" + log debug "Killed: $?" + _sleep=$(/bin/ps -o ppid,comm,pid \ + | /bin/grep -F "1 sleep" \ + | /usr/bin/head -1 \ + | /bin/sed "s@[ ]*1[ ]*sleep[ ]*@@") + log info "Stopping the orphaned sleep as PID ${_sleep}." + /bin/kill -KILL "${_sleep}" + log debug "Killed: $?" + else + log warn "Nothing is blocked, nothing to do." + fi +} + +trap terminate TERM + +while read -r _line; do + _parameters=$(echo "${_line}" | /bin/sed '/^[[:blank:]]*#/d;s/#.*//') + + [ -z "${_parameters}" ] && continue + + log info "Launching socat with parameters: [${_parameters}]" + /usr/bin/socat ${_parameters} 2>&1 \ + | capture_output debug socat & +done < /media/etc/forwarding.conf + +(while /bin/true; do /bin/sleep 365d; done) & +_block=$! + +log info "Waiting for PID ${_block} to stop." +wait "${_block}" + +_socats=$(/usr/bin/pgrep -P $$ socat) +log info "Stopping, own socat processes: [${_socats}]" + +# shellcheck disable=SC2086 +[ -n "${_socats}" ] && /bin/kill -TERM ${_socats} + +log info "Finished." diff --git a/aports/forwarding/forwarding.initd b/aports/forwarding/forwarding.initd new file mode 100644 index 0000000..fd63aa0 --- /dev/null +++ b/aports/forwarding/forwarding.initd @@ -0,0 +1,13 @@ +#!/sbin/openrc-run + +supervisor=supervise-daemon +name="Forwarding service" +description="Forwarding support" + +command=/sbin/forwarding +command_background=true + +depend() { + need net + after firewall +} -- cgit v1.2.3