Compare commits

..

2 Commits

Author SHA1 Message Date
39009cbc18 use container hostname alias for script
All checks were successful
Check Flake / check-flake (push) Successful in 3m17s
2026-02-26 00:17:47 -08:00
3365a1652c restore port option 2026-02-26 00:16:39 -08:00
3 changed files with 19 additions and 8 deletions

View File

@@ -51,6 +51,15 @@ let
receiveForwardedPort = mkOption { receiveForwardedPort = mkOption {
type = types.nullOr (types.submodule { type = types.nullOr (types.submodule {
options = { options = {
port = mkOption {
type = types.nullOr types.port;
default = null;
description = ''
Target port to forward to. If null, forwards to the same PIA-assigned port.
PIA-assigned ports below 1000 are rejected to avoid accidentally
forwarding traffic to privileged services.
'';
};
protocol = mkOption { protocol = mkOption {
type = types.enum [ "tcp" "udp" "both" ]; type = types.enum [ "tcp" "udp" "both" ];
default = "both"; default = "both";

View File

@@ -24,15 +24,17 @@ let
let let
fwd = forwardingContainer.receiveForwardedPort; fwd = forwardingContainer.receiveForwardedPort;
targetIp = forwardingContainer.ip; targetIp = forwardingContainer.ip;
dnatTarget = if fwd.port != null then "${targetIp}:${toString fwd.port}" else targetIp;
targetPort = if fwd.port != null then toString fwd.port else "$PORT";
tcpRules = optionalString (fwd.protocol == "tcp" || fwd.protocol == "both") '' tcpRules = optionalString (fwd.protocol == "tcp" || fwd.protocol == "both") ''
echo "Setting up TCP DNAT: port $PORT ${targetIp}:$PORT" echo "Setting up TCP DNAT: port $PORT ${targetIp}:${targetPort}"
iptables -t nat -A PREROUTING -i ${cfg.interfaceName} -p tcp --dport $PORT -j DNAT --to ${targetIp} iptables -t nat -A PREROUTING -i ${cfg.interfaceName} -p tcp --dport $PORT -j DNAT --to ${dnatTarget}
iptables -A FORWARD -i ${cfg.interfaceName} -d ${targetIp} -p tcp --dport $PORT -j ACCEPT iptables -A FORWARD -i ${cfg.interfaceName} -d ${targetIp} -p tcp --dport ${targetPort} -j ACCEPT
''; '';
udpRules = optionalString (fwd.protocol == "udp" || fwd.protocol == "both") '' udpRules = optionalString (fwd.protocol == "udp" || fwd.protocol == "both") ''
echo "Setting up UDP DNAT: port $PORT ${targetIp}:$PORT" echo "Setting up UDP DNAT: port $PORT ${targetIp}:${targetPort}"
iptables -t nat -A PREROUTING -i ${cfg.interfaceName} -p udp --dport $PORT -j DNAT --to ${targetIp} iptables -t nat -A PREROUTING -i ${cfg.interfaceName} -p udp --dport $PORT -j DNAT --to ${dnatTarget}
iptables -A FORWARD -i ${cfg.interfaceName} -d ${targetIp} -p udp --dport $PORT -j ACCEPT iptables -A FORWARD -i ${cfg.interfaceName} -d ${targetIp} -p udp --dport ${targetPort} -j ACCEPT
''; '';
onPortForwarded = optionalString (forwardingContainer.onPortForwarded != null) '' onPortForwarded = optionalString (forwardingContainer.onPortForwarded != null) ''
TARGET_IP="${targetIp}" TARGET_IP="${targetIp}"

View File

@@ -67,13 +67,13 @@
onPortForwarded = '' onPortForwarded = ''
# Notify Transmission of the PIA-assigned peer port via RPC # Notify Transmission of the PIA-assigned peer port via RPC
for i in $(seq 1 30); do for i in $(seq 1 30); do
curlout=$(curl -s "http://$TARGET_IP:80/transmission/rpc" 2>/dev/null) && break curlout=$(curl -s "http://transmission.containers:80/transmission/rpc" 2>/dev/null) && break
sleep 2 sleep 2
done done
regex='X-Transmission-Session-Id: (\w*)' regex='X-Transmission-Session-Id: (\w*)'
if [[ $curlout =~ $regex ]]; then if [[ $curlout =~ $regex ]]; then
sessionId=''${BASH_REMATCH[1]} sessionId=''${BASH_REMATCH[1]}
curl -s "http://$TARGET_IP:80/transmission/rpc" \ curl -s "http://transmission.containers:80/transmission/rpc" \
-d "{\"method\":\"session-set\",\"arguments\":{\"peer-port\":$PORT}}" \ -d "{\"method\":\"session-set\",\"arguments\":{\"peer-port\":$PORT}}" \
-H "X-Transmission-Session-Id: $sessionId" -H "X-Transmission-Session-Id: $sessionId"
fi fi