在GoLang中执行ReverseProxy时确认TLS证书

时间:2016-02-14 10:29:18

标签: ssl go reverse-proxy

在GoLang中,我使用NewSingleHostReverseProxy来预制反向代理,但是我需要确认主机站点的SSL证书,以确保我拥有正确的安全证书...任何想法我应该怎么做?我应该使用处理程序或传输吗?我是GoLang的新手,仍然围绕着它。感谢

proxy := httputil.NewSingleHostReverseProxy(&url.URL{
         Scheme: "https",
         Host:   "sha256.badssl.com",
})

http.ListenAndServe("127.0.0.1:80", proxy)

2 个答案:

答案 0 :(得分:2)

要访问证书,您将可以访问ConnectionState。最简单的方法是提供您自己的DialTLS版本。在那里,您使用net.Dial连接到服务器,进行TLS握手,然后您可以自由验证。

package main

import (
    "crypto/tls"
    "log"
    "net"
    "net/http"
    "net/http/httputil"
    "net/url"
)

func main() {
    proxy := httputil.NewSingleHostReverseProxy(&url.URL{
        Scheme: "https",
        Host:   "sha256.badssl.com",
    })

    // Set a custom DialTLS to access the TLS connection state
    proxy.Transport = &http.Transport{DialTLS: dialTLS}

    // Change req.Host so badssl.com host check is passed
    director := proxy.Director
    proxy.Director = func(req *http.Request) {
        director(req)
        req.Host = req.URL.Host
    }

    log.Fatal(http.ListenAndServe("127.0.0.1:3000", proxy))
}

func dialTLS(network, addr string) (net.Conn, error) {
    conn, err := net.Dial(network, addr)
    if err != nil {
        return nil, err
    }

    host, _, err := net.SplitHostPort(addr)
    if err != nil {
        return nil, err
    }
    cfg := &tls.Config{ServerName: host}

    tlsConn := tls.Client(conn, cfg)
    if err := tlsConn.Handshake(); err != nil {
        conn.Close()
        return nil, err
    }

    cs := tlsConn.ConnectionState()
    cert := cs.PeerCertificates[0]

    // Verify here
    cert.VerifyHostname(host)
    log.Println(cert.Subject)

    return tlsConn, nil
}

答案 1 :(得分:0)

要将SSL调整为反向主机,可以设置传输 选项。因此,如果您想跳过验证,则可以这样设置。

proxy := httputil.NewSingleHostReverseProxy(&url.URL{
         Scheme: "https",
         Host:   "sha256.badssl.com",
})

proxy.Transport = &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

http.ListenAndServe("127.0.0.1:80", proxy)