go-httpbin icon indicating copy to clipboard operation
go-httpbin copied to clipboard

Add /server-ip endpoint to return server's local IP address

Open paimus opened this issue 1 month ago • 1 comments

This change introduces a new /server-ip endpoint that returns the IP address of the server (or Pod in Kubernetes) handling the request, as opposed to the existing /ip endpoint which returns the client's IP address.

Changes:

  • Add getServerIP() helper in helpers.go to detect server's primary IP
    • Prefers non-loopback IPv4 addresses
    • Falls back to IPv6 if no IPv4 found
    • Uses UDP dial as last resort for IP detection
  • Add ServerIP handler in handlers.go
  • Register /server-ip route in httpbin.go
  • Add serverIPResponse struct in responses.go

This is useful for debugging and service discovery in containerized environments where the server's IP may need to be identified.

paimus avatar Nov 18 '25 06:11 paimus

I appreciate the contribution, but as noted in https://github.com/mccutchen/go-httpbin/issues/113#issuecomment-1466225358 I am reluctant to expose this potentially sensitive info about internal networks by default.

I'd consider a change that only exposed this endpoint if it was explicitly enabled, along the lines of the USE_REAL_HOSTNAME option added in https://github.com/mccutchen/go-httpbin/pull/81.

That being said, I'm not sure about the implementation here. Why is IPv4 preferred over IPv6? Also I don't think making a throwaway UDP connection (why to port 80? does the port just not matter here?) is a reasonable thing to do.

mccutchen avatar Nov 19 '25 03:11 mccutchen