#!/bin/bash
# xinetd wrapper for the Tor side only: fetch the request from Venusia and
# rewrite the clearnet host in every menu link to the .onion, so Tor visitors
# stay on the onion. Clearnet never reaches this script -- it hits Venusia on
# :70 directly.

set -Eeuo pipefail

HOST="gopher.someodd.zip"   # what Venusia emits in its links
PORT="70"                   # Venusia's port
ONION="xj2o2wylbqkprajldswuyxm6dffca4eepegelblgvux3uuqmtb2l56id.onion"

# Read one selector line from xinetd, keep any tab+query (type-7 search), and
# strip the trailing CR.
IFS= read -r selector || selector=""
selector=${selector%$'\r'}

# Forward to Venusia (CRLF), then rewrite host -> .onion in the response.
# --unbuffered so streaming responses reach the client line-by-line.
printf "%s\r\n" "$selector" | nc localhost "$PORT" | sed --unbuffered \
    -e "s|\t${HOST}\t${PORT}|\t${ONION}\t${PORT}|g" \
    -e "s|\t${HOST}/|\t${ONION}/|g"
