0

I am trying to implement a HTTP server in Golang that receives requests from an Amazon ELB that uses the proxy protocol. Now I'd like to know what the original IP address is and so I was thinking about using this package.

Now this package talks raws HTTP as far as I can tell but my server implements a higher level HTTP server with a router.

I am having trouble translating between them. My question is this: how do I use this library and still use a router like gorilla/mux? Now there's nothing special about this package, it just talks at a lower level than HTTP.

Example:

// Listen on TCP port 2000 on all interfaces.
l, err := net.Listen("tcp", ":2000")
// proxyproxy is the library that maintains the source ip address for me
proxyList := &proxyproto.Listener{Listener: list}
if err != nil {
    log.Fatal(err)
}
defer proxyList.Close()

for {
    // Wait for a connection.
    conn, err := proxyList.Accept()
    if err != nil {
        log.Fatal(err)
    }
    // Handle the connection in a new goroutine.
    // The loop then returns to accepting, so that
    // multiple connections may be served concurrently.
    go func(c net.Conn) {

        // how do I connect my router

    }(conn)
}

1 Answer 1

2

The usual way of finding out the actual client IP over HTTP is by using some HTTP headers such as:

  • X-Forwarded-For
  • X-Real-IP

Actually, Amazon ELB seems to support the X-Forwarded-For header:

http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html

If you are using Gorilla, they have a middleware that seems to take care of that for you:

https://godoc.org/github.com/gorilla/handlers#ProxyHeaders

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry I should have been for specific. I am using a TCP ELB and I only have access i those headers if using an HTTP or HTTPS ELB
That is why that package I referred to was created. So that the client up could be pulled out of a tcp connection and passed along if the proxy protocol is used

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.