From eb0422cee1eb08d768aa9606ed532982cc40865f Mon Sep 17 00:00:00 2001 From: Zeal Date: Sun, 5 Jun 2016 23:13:29 +0800 Subject: [PATCH 1/2] improve self-signed host list --- transport/http_transport.go | 49 ++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/transport/http_transport.go b/transport/http_transport.go index 0ad00ea1..9fc35401 100644 --- a/transport/http_transport.go +++ b/transport/http_transport.go @@ -409,7 +409,16 @@ func (h *httpTransport) Listen(addr string, opts ...ListenOption) (Listener, err fn := func(addr string) (net.Listener, error) { if config == nil { - cert, err := mls.Certificate(addr) + hosts := []string{addr} + if h, _, e := net.SplitHostPort(addr); e == nil { + if h == "" { + hosts = getIPAddrList() + } else { + hosts = []string{h} + } + } + + cert, err := mls.Certificate(hosts...) if err != nil { return nil, err } @@ -447,3 +456,41 @@ func newHTTPTransport(opts ...Option) *httpTransport { } return &httpTransport{opts: options} } + +func getIPAddrList() []string { + ifaces, err := net.Interfaces() + + if err != nil { + return nil + } + + var ipAddrlist []string + + for _, i := range ifaces { + if addrs, err := i.Addrs(); err != nil { + continue + } else { + 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 { + continue + } + + ip = ip.To4() + if ip == nil { + continue + } + + ipAddrlist = append(ipAddrlist, ip.String()) + } + } + } + return ipAddrlist +} From d8ecd428682f8d9d8e7a9b672397b70b8a3f8992 Mon Sep 17 00:00:00 2001 From: Zeal Date: Mon, 6 Jun 2016 20:56:53 +0800 Subject: [PATCH 2/2] rename getIPAddrList() to getIPAddrs and improve code format --- transport/http_transport.go | 52 ++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/transport/http_transport.go b/transport/http_transport.go index 9fc35401..8a4814cf 100644 --- a/transport/http_transport.go +++ b/transport/http_transport.go @@ -411,8 +411,8 @@ func (h *httpTransport) Listen(addr string, opts ...ListenOption) (Listener, err if config == nil { hosts := []string{addr} if h, _, e := net.SplitHostPort(addr); e == nil { - if h == "" { - hosts = getIPAddrList() + if len(h) == 0 { + hosts = getIPAddrs() } else { hosts = []string{h} } @@ -457,40 +457,40 @@ func newHTTPTransport(opts ...Option) *httpTransport { return &httpTransport{opts: options} } -func getIPAddrList() []string { +func getIPAddrs() []string { ifaces, err := net.Interfaces() - if err != nil { return nil } - var ipAddrlist []string + var ipAddrs []string for _, i := range ifaces { - if addrs, err := i.Addrs(); err != nil { + addrs, err := i.Addrs() + if err != nil { continue - } else { - 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 { - continue - } - - ip = ip.To4() - if ip == nil { - continue - } - - ipAddrlist = append(ipAddrlist, ip.String()) + 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 { + continue + } + + ip = ip.To4() + if ip == nil { + continue + } + + ipAddrs = append(ipAddrs, ip.String()) } } - return ipAddrlist + return ipAddrs }