Skip to content

Instantly share code, notes, and snippets.

@superbrothers
Last active October 22, 2021 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superbrothers/d59ad9c059a4f003eb029b767263cef2 to your computer and use it in GitHub Desktop.
Save superbrothers/d59ad9c059a4f003eb029b767263cef2 to your computer and use it in GitHub Desktop.
This is a strongSwan updown script that allows Docker containers to be accessed through IPSec VPN on the host.
#!/usr/bin/env bash
# This is a strongSwan updown script that allows Docker containers to be
# accessed through IPSec VPN on the host.
set -e -o pipefail; [[ -n "$DEBUG" ]] && set -x
docker_addr_pools=( $(docker system info -f "{{range .DefaultAddressPools}}{{.Base}} {{end}}") )
case "$PLUTO_VERB" in
up-client)
for addr_pool in "${docker_addr_pools[@]}"; do
sudo iptables -t nat -I POSTROUTING 1 -o "$PLUTO_INTERFACE" -d "$PLUTO_PEER_CLIENT" -s "$addr_pool" -j SNAT --to "$PLUTO_MY_SOURCEIP"
done
;;
down-client)
for addr_pool in "${docker_addr_pools[@]}"; do
sudo iptables -t nat -D POSTROUTING -o "$PLUTO_INTERFACE" -d "$PLUTO_PEER_CLIENT" -s "$addr_pool" -j SNAT --to "$PLUTO_MY_SOURCEIP"
done
;;
esac
# vim: ai ts=2 sw=2 et sts=2 ft=sh
@superbrothers
Copy link
Author

superbrothers commented Oct 22, 2021

Download the script and move it to /etc/ipsec.d/.

$ chmod +x docker-updown.sh
$ sudo mv docker-updown.sh /etc/ipsec.d/

Modify /etc/ipsec.conf.

conn <name>
     ....
     leftupdown="ipsec _updown iptables && /etc/ipsec.d/docker-updown.sh"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment