Hugo - My IP With Javascript
Get both IPv4 and IPv6.
Hugo - Create My IP page with Caddy showed how to use Caddy templates with Hugo to create a my-ip page.
However that can only display either IPv4 or IPv6 at a time. To get both at the same time, we have to cheat(if you are a markdown purist) a bit with html and javascript, with DNS settings.
Prerequisite
- Server hosting the site must have public IPv4 and IPv6 addresses.
- Caddy must be accepting both IPv4 and IPv6 traffic.
DNS
Usually for site with IPv4 and IPv6, dns will be setup like following so traffic can come in either way:
| |
However for our purpose, we need to separate them as follow:
| |
That ensure traffic come in through myip4 will always use IPv4, and the same for myip6. Take this site as example:
| |
Once the above is ready, we can configure Caddy server.
Caddyfile
| |
Caddy v2 CROS config example here.
As mentioned in previous article, static/myip/index.html is already there:
| |
Restart Caddy and test:
| |
That confirms the text version is working.
Hugo Page
Update our Hugo page content/home/myip.md:
| |
{{.RemoteIP}} will continue to work as Caddyfile was setup last time. In order to fill in IPv4 and IPv6 addresses, add following script section at the end of content/home/myip.md:
| |
The final result is here.