Skip to content

Instantly share code, notes, and snippets.

@movd
Last active November 20, 2020 15:20
Show Gist options
  • Save movd/79060947c5d2e6f28deb614a8b316381 to your computer and use it in GitHub Desktop.
Save movd/79060947c5d2e6f28deb614a8b316381 to your computer and use it in GitHub Desktop.
Create services for TCP6 to TCP4 forwards with socat
#!/usr/bin/env bash
# Forward list of ports from TCP6 to TCP4
if ! [ -x "$(command -v socat)" ]; then
echo 'Error: socat is not installed.' >&2
exit 1
fi
ports=(80 443)
for port in "${ports[@]}"
do
cat <<EOF > /usr/local/socat-ipv6-tcp-${port}-forward.sh
#!/bin/bash
# 6to4 forward
/usr/bin/socat TCP6-LISTEN:${port},fork TCP4:127.0.0.1:${port}
EOF
chmod +x /usr/local/socat-ipv6-tcp-${port}-forward.sh
cat <<EOF > /etc/systemd/system/socat-ipv6-tcp-${port}-forward.service
[Unit]
Description=socat forward TCP6 port ${port} to TCP4
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/socat-ipv6-tcp-${port}-forward.sh
Restart=on-abort
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload;
systemctl enable socat-ipv6-tcp-${port}-forward.service;
systemctl start socat-ipv6-tcp-${port}-forward.service;
systemctl status socat-ipv6-tcp-${port}-forward.service;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment