从Go HTTP服务器获取源IP地址

huangapple go评论106阅读模式
英文:

Get Source IP Address from Go HTTP Server

问题

我正在尝试在Golang中实现一个HTTP服务器,该服务器接收来自使用代理协议的Amazon ELB的请求。现在我想知道原始的IP地址,所以我考虑使用这个包:go-proxyproto

根据我所了解,这个包主要处理原始的HTTP请求,但是我的服务器实现了一个更高级别的HTTP服务器,并带有一个路由器。

我在它们之间遇到了一些问题。我的问题是:我如何同时使用go-proxyproto库和像gorilla/mux这样的路由器?现在,这个包并没有什么特别之处,它只是在比HTTP更低的层级上进行通信。

示例代码:

// 在所有接口上监听TCP端口2000。
l, err := net.Listen("tcp", ":2000")
// proxyproto是一个为我维护源IP地址的库
proxyList := &proxyproto.Listener{Listener: l}
if err != nil {
    log.Fatal(err)
}
defer proxyList.Close()

for {
    // 等待连接。
    conn, err := proxyList.Accept()
    if err != nil {
        log.Fatal(err)
    }
    // 在一个新的goroutine中处理连接。
    // 然后循环返回到接受连接的状态,以便可以同时处理多个连接。
    go func(c net.Conn) {
        
        // 如何连接我的路由器?

    }(conn)
}

希望这可以帮助到你!

英文:

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

得分: 1

通常在HTTP中查找实际客户端IP的方法是使用一些HTTP头,例如:

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

实际上,Amazon ELB似乎支持X-Forwarded-For头:

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

如果你正在使用Gorilla,他们有一个中间件似乎可以为你处理这个问题:

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

英文:

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

huangapple
  • 本文由 发表于 2017年5月10日 09:01:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/43882010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定