Pārlūkot izejas kodu

Load client cert and key only once at startup

Lu Stadler 7 gadi atpakaļ
vecāks
revīzija
2f4d1d2ddf
1 mainītis faili ar 12 papildinājumiem un 10 dzēšanām
  1. 12 10
      go/fake-http/fake-http.go

+ 12 - 10
go/fake-http/fake-http.go

@ -49,6 +49,15 @@ func main() {
49 49
		}
50 50
	}
51 51
52
	cert := tls.Certificate{}
53
	if flags.proxyClientCert != "" && flags.proxyClientKey != "" {
54
		var err error
55
		cert, err = tls.LoadX509KeyPair(flags.proxyClientCert, flags.proxyClientKey)
56
		if err != nil {
57
			log.Fatalf("Error: parsing client cert and key: %s", err)
58
		}
59
	}
60
52 61
	var responsesPath string
53 62
	if flag.NArg() == 1 {
54 63
		responsesPath = flag.Arg(0)
@ -62,7 +71,7 @@ func main() {
62 71
63 72
		var resp *http.Response
64 73
		if flags.proxyURL != "" {
65
			resp = respondWithProxy(flags.proxyURL, w, req)
74
			resp = respondWithProxy(flags.proxyURL, &cert, w, req)
66 75
		} else {
67 76
			resp = respondWithStub(responses, w, req)
68 77
		}
@ -154,18 +163,11 @@ func respondWithStub(responses Responses, w http.ResponseWriter, req *http.Reque
154 163
	return resp.AsHTTP()
155 164
}
156 165
157
func respondWithProxy(proxyURL string, w http.ResponseWriter, req *http.Request) *http.Response {
166
func respondWithProxy(proxyURL string, cert *tls.Certificate, w http.ResponseWriter, req *http.Request) *http.Response {
158 167
	proxyTransport := &http.Transport{
159 168
		TLSClientConfig: &tls.Config{
160 169
			GetClientCertificate: func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) {
161
				if flags.proxyClientCert != "" && flags.proxyClientKey != "" {
162
					cert, err := tls.LoadX509KeyPair(flags.proxyClientCert, flags.proxyClientKey)
163
					if err != nil {
164
						return nil, err
165
					}
166
					return &cert, nil
167
				}
168
				return &tls.Certificate{}, nil
170
				return cert, nil
169 171
			},
170 172
			InsecureSkipVerify: true,
171 173
		},