Skip to content

Conversation

@pimatrix
Copy link

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.

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.
@mccutchen
Copy link
Owner

I appreciate the contribution, but as noted in #113 (comment) 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 #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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants