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)
}
frankgreco
  • 1,426
  • 1
  • 18
  • 31

1 Answers1

1

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

eugenioy
  • 11,825
  • 28
  • 35
  • 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 – frankgreco May 10 '17 at 03:01
  • 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 – frankgreco May 10 '17 at 03:03