Caddy server that returns only IP address as text

The idea came up when I was looking. :bulb:
JSON Config Structure - Caddy Documentation :

.RemoteIP
Returns the client’s IP address.

{{.RemoteIP}}

There are some services in the world that return only IP addresses.
Used for example:
https://caddyserver.com/v1/docs/dyndns

Can this be achieved with Caddy v2 to get a good response?

Caddyfile:

example.net.eu.org {
    tls balloon@example.net.eu.org
    root * /home/balloon/example.net.eu.org
    templates
    encode zstd gzip
    file_server
}

index.html:

{{.RemoteIP}}

Since I am in Japan, I set Caddy v2 for VPS in Tokyo. VPS spec is $ 5 (minimum). result:

Seconds Services
0.02 http://whatismyip.akamai.com/
0.03 (My Service http)
0.03 http://inet-ip.info/ip
0.04 (Japanese rental server http)
0.04 http://icanhazip.com/
0.05 (My Service https)
0.06 https://icanhazip.com/
0.09 https://canhazip.com/
0.20 (Japanese rental server https)
0.21 http://ifconfig.me/ip
0.24 https://bot.whatismyipaddress.com/
0.27 http://wgetip.com/
0.28 https://ifconfig.me/ip
0.29 https://ipecho.net/plain
0.29 https://ipinfo.io/ip
0.29 https://wgetip.com/
0.36 http://checkip.amazonaws.com/
0.47 http://alma.ch/myip.cgi
0.47 http://l2.io/ip
0.52 http://ident.me/
0.53 https://ifconfig.co/ip
0.55 http://eth0.me/
0.56 https://bot.whatismyipaddress.com/
0.57 http://ip.tyk.nu/
0.76 https://checkip.amazonaws.com/
0.77 https://api.ipify.org/
0.80 https://ipv4.wtfismyip.com/text
0.80 https://wtfismyip.com/text
0.85 http://tnx.nl/ip
0.97 https://alma.ch/myip.cgi
0.99 https://l2.io/ip
1.09 https://ident.me/
1.19 https://eth0.me/
1.20 https://ip.tyk.nu/
1.65 https://tnx.nl/ip

Seconds: The average measured several times, except for the first time referring to the name server
Japanese rental server: Web server adopts Apache, the following PHP was used:

<?php
echo $_SERVER['REMOTE_ADDR']."\n";

Especially https is the fastest! Great!! :grin:

4 Likes

That’s awesome. Thanks for the comparison.

With v2, you can just use these directives:

templates
respond "{{.RemoteIP}}"

And there is no need for files, if all you want is a plaintext IP address in the response.

(FYI, later I might be removing template actions like {{.RemoteIP}} in favor of a single action that just accesses existing placeholders, like {{placeholder "http.request.remote.host"}} or something. So just be aware of that, before we leave beta.)

2 Likes

Oh? I tried it, but Caddy 2.0b13 returned this:

{{.RemoteIP}}

I understand it’s beta. :upside_down_face:

Oh, my bad, you need to add the Content-Type: text/plain header:

header Content-Type text/plain

The templates handler won’t evaluate responses that aren’t text or HTML (by default), so it needs a Content-Type.

Hmm!? I edited the Caddyfile, but my Caddy still returns {{.RemoteIP}}. …
Do I need anything else?:Or is it extra?

example.net.eu.org {
    tls balloon@example.net.eu.org
    templates
    header Content-Type text/plain
    respond "{{.RemoteIP}}"
    encode zstd gzip
}

I’ve also tried header Content-Type text/html, seeing the font change,
but the result doesn’t change.

Ah, it’s because header sets "deferred": true in the underlying JSON, so the header isn’t added until after the response starts being written (after templates does its thing).

I will change it so that it is not deferred when adding or setting, but is deferred for deleting headers… that should fix it up. But yeah, that Caddyfile is right, pending this change.

I understood. I wait for this change.
I think this method is useful not only for IP addresses.

Could you try with this commit: header: caddyfile: Defer header operations for deletions or manually · caddyserver/caddy@9639fe7 · GitHub

(I’ve tested that it works for me.)

1 Like

Thanks for the renovation @matt.
It required me to build from source for the first time, but the steps were simple. :computer:

I left the environment alone and just restarted Caddy:

sudo systemctl restart caddy

result:
スクリーンショット 2020-02-05 12.59.04
There was an IP address! We are happy :grin:

3 Likes

I have received a message regarding this.
Please let me know if you installed on your Caddy server.

Caddy IP address reply servers all over the world! :earth_asia::smile:

1 Like

This topic was automatically closed after 14 days. New replies are no longer allowed.