Просмотр исходного кода

Load client cert and key only once at startup

Lu Stadler лет назад: 7
Родитель
Сommit
2f4d1d2ddf
1 измененных файлов с 12 добавлено и 10 удалено
  1. 12 10
      go/fake-http/fake-http.go

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

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
	var responsesPath string
61
	var responsesPath string
53
	if flag.NArg() == 1 {
62
	if flag.NArg() == 1 {
54
		responsesPath = flag.Arg(0)
63
		responsesPath = flag.Arg(0)
62
71
63
		var resp *http.Response
72
		var resp *http.Response
64
		if flags.proxyURL != "" {
73
		if flags.proxyURL != "" {
65
			resp = respondWithProxy(flags.proxyURL, w, req)
74
			resp = respondWithProxy(flags.proxyURL, &cert, w, req)
66
		} else {
75
		} else {
67
			resp = respondWithStub(responses, w, req)
76
			resp = respondWithStub(responses, w, req)
68
		}
77
		}
154
	return resp.AsHTTP()
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
	proxyTransport := &http.Transport{
167
	proxyTransport := &http.Transport{
159
		TLSClientConfig: &tls.Config{
168
		TLSClientConfig: &tls.Config{
160
			GetClientCertificate: func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) {
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
			InsecureSkipVerify: true,
172
			InsecureSkipVerify: true,
171
		},
173
		},