2016-04-13 61 views
3

我想寫一個使用Golang的net/http包的HTTP服務器,反應將取決於服務器 HTTP連接的IP地址。如何使用golang net/http獲取服務器自己的地址?

換句話說,我在找的是CGI的「SERVER_ADDR」變量。

http.Request中最接近的字段是「主機」 - 但是因爲如果請求使用文字地址,它只會等於地址,所以我無法使用它(服務器可能按名稱使用)。

查看https://golang.org/src/net/http/server.go的源代碼,似乎到達服務器地址的唯一方法是在處理程序中Hijack()連接併爲同一連接上的後續請求實現後續HTTP解析,但看起來很不雅至少可以說...

這似乎是理想的解決辦法是在修改爲golang標準庫中的HTTP /請求和HTTP /服務器如下:

diff -u go-stock-library/request.go ./request.go 
--- go-stock-library/request.go 2016-04-13 17:31:48.000000000 +0200 
+++ ./request.go 2016-04-13 17:32:40.000000000 +0200 
@@ -227,6 +227,15 @@ 
    // This field is ignored by the HTTP client. 
    RemoteAddr string 

+ // LocalAddr allows HTTP servers and other software to record 
+ // the network address that the request was sent to, usually for 
+ // logging. This field is not filled in by ReadRequest and 
+ // has no defined format. The HTTP server in this package 
+ // sets LocalAddr to an "IP:port" address before invoking a 
+ // handler. 
+ // This field is ignored by the HTTP client. 
+ LocalAddr string 
+ 
    // RequestURI is the unmodified Request-URI of the 
    // Request-Line (RFC 2616, Section 5.1) as sent by the client 
    // to a server. Usually the URL field should be used instead. 
diff -u go-stock-library/server.go ./server.go 
--- go-stock-library/server.go 2016-04-13 17:29:19.000000000 +0200 
+++ ./server.go 2016-04-13 17:31:38.000000000 +0200 
@@ -161,6 +161,13 @@ 
    // This is the value of a Handler's (*Request).RemoteAddr. 
    remoteAddr string 

+ // serverAddr is rwc.LocalAddr().String(). It is not populated synchronously 
+ // inside the Listener's Accept goroutine, as some implementations block. 
+ // It is populated immediately inside the (*conn).serve goroutine. 
+ // This is the value of a Handler's (*Request).LocalAddr. 
+ localAddr string 
+ 
+ 
    // tlsState is the TLS connection state when using TLS. 
    // nil means not TLS. 
    tlsState *tls.ConnectionState 
@@ -736,6 +743,7 @@ 
    delete(req.Header, "Host") 

    req.RemoteAddr = c.remoteAddr 
+ req.LocalAddr = c.localAddr 
    req.TLS = c.tlsState 
    if body, ok := req.Body.(*body); ok { 
     body.doEarlyClose = true 
@@ -1382,6 +1390,7 @@ 
// Serve a new connection. 
func (c *conn) serve() { 
    c.remoteAddr = c.rwc.RemoteAddr().String() 
+ c.localAddr = c.rwc.LocalAddr().String() 
    defer func() { 
     if err := recover(); err != nil { 
      const size = 64 << 10 

,然後使用新的代碼中的LocalAddr以一種非常乾淨的方式。

我錯過了什麼,有沒有一個更清潔的方式來做到這一點?

回答

1

我個人不會修改標準庫中的任何東西,我可以通過其他方式獲得。解析出每個連接是否有一些優勢?

可能有一個更簡單的方法,但我有以下幾點。

func getMyInterfaceAddr() (net.IP, error) { 


    ifaces, err := net.Interfaces() 
    if err != nil { 
     return nil, err 
    } 
    addresses := []net.IP{} 
    for _, iface := range ifaces { 

     if iface.Flags&net.FlagUp == 0 { 
      continue // interface down 
     } 
     if iface.Flags&net.FlagLoopback != 0 { 
      continue // loopback interface 
     } 
     addrs, err := iface.Addrs() 
     if err != nil { 
      continue 
     } 

     for _, addr := range addrs { 
      var ip net.IP 
      switch v := addr.(type) { 
      case *net.IPNet: 
       ip = v.IP 
      case *net.IPAddr: 
       ip = v.IP 
      } 
      if ip == nil || ip.IsLoopback() { 
       continue 
      } 
      ip = ip.To4() 
      if ip == nil { 
       continue // not an ipv4 address 
      } 
      addresses = append(addresses, ip) 
     } 
    } 
    if len(addresses) == 0 { 
     return nil, fmt.Errorf("no address Found, net.InterfaceAddrs: %v", addresses) 
    } 
    //only need first 
    return addresses[0], nil 
} 
+0

對於我的用例,單個地址將不起作用 - 因爲它們對於每個連接可能不同。想想應用一大堆IPv6地址的單一接口。連接可以在任何連接上終止,我需要知道它是哪一個... –

相關問題