Skip to content

Commit 99d6804

Browse files
godoc
1 parent 50f0838 commit 99d6804

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

ca.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,24 @@ s39uFDUnxsMb2Nl3JcNJHYBTm9ubjAZSo/3NuB0z/Gm+ssOcExTD//vW7BxxSAcs
101101
/xlPPTPbY5qoMAT7kK71kd4Ypnqbcs3UPpAHtcPkjWpuWOlebK0J7UYToj4f
102102
-----END RSA PRIVATE KEY-----`)
103103

104+
// CaSigner is a certificate signer by CA certificate. It supports caching.
104105
type CaSigner struct {
105-
Ca *tls.Certificate
106+
// Ca specifies CA certificate. You must set before using.
107+
Ca *tls.Certificate
108+
106109
mu sync.RWMutex
107110
certMap map[string]*tls.Certificate
108111
certList []string
109112
certIndex int
110113
certMax int
111114
}
112115

116+
// NewCaSigner returns a new CaSigner without caching.
113117
func NewCaSigner() *CaSigner {
114118
return NewCaSignerCache(0)
115119
}
116120

121+
// NewCaSignerCache returns a new CaSigner with caching given max.
117122
func NewCaSignerCache(max int) *CaSigner {
118123
if max < 0 {
119124
max = 0
@@ -126,16 +131,17 @@ func NewCaSignerCache(max int) *CaSigner {
126131
}
127132
}
128133

134+
// SignHost generates TLS certificate given single host, signed by CA certificate.
129135
func (c *CaSigner) SignHost(host string) (cert *tls.Certificate) {
130136
if host == "" {
131137
return
132138
}
133139
if c.certMax <= 0 {
134-
crt, err := signHosts(*c.Ca, []string{host})
140+
crt, err := SignHosts(*c.Ca, []string{host})
135141
if err != nil {
136142
return nil
137143
}
138-
cert = &crt
144+
cert = crt
139145
return
140146
}
141147
func() {
@@ -152,11 +158,11 @@ func (c *CaSigner) SignHost(host string) (cert *tls.Certificate) {
152158
if cert != nil {
153159
return
154160
}
155-
crt, err := signHosts(*c.Ca, []string{host})
161+
crt, err := SignHosts(*c.Ca, []string{host})
156162
if err != nil {
157163
return nil
158164
}
159-
cert = &crt
165+
cert = crt
160166
if len(c.certMap) >= c.certMax {
161167
delete(c.certMap, c.certList[c.certIndex])
162168
}
@@ -169,7 +175,8 @@ func (c *CaSigner) SignHost(host string) (cert *tls.Certificate) {
169175
return
170176
}
171177

172-
func signHosts(ca tls.Certificate, hosts []string) (cert tls.Certificate, error error) {
178+
// SignHosts generates TLS certificate given hosts, signed by CA certificate.
179+
func SignHosts(ca tls.Certificate, hosts []string) (cert *tls.Certificate, error error) {
173180
var x509ca *x509.Certificate
174181
if x509ca, error = x509.ParseCertificate(ca.Certificate[0]); error != nil {
175182
return
@@ -205,7 +212,7 @@ func signHosts(ca tls.Certificate, hosts []string) (cert tls.Certificate, error
205212
if derBytes, error = x509.CreateCertificate(rnd, &template, x509ca, &certPriv.PublicKey, ca.PrivateKey); error != nil {
206213
return
207214
}
208-
return tls.Certificate{
215+
return &tls.Certificate{
209216
Certificate: [][]byte{derBytes, ca.Certificate[0]},
210217
PrivateKey: certPriv,
211218
}, nil

httpproxy.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const (
3333

3434
// Proxy defines parameters for running an HTTP Proxy. It implements
3535
// http.Handler interface for ListenAndServe function. If you need, you must
36-
// fill Proxy struct before handling requests.
36+
// set Proxy struct before handling requests.
3737
type Proxy struct {
3838
// Session number of last proxy request.
3939
SessionNo int64
@@ -74,9 +74,11 @@ type Proxy struct {
7474
OnResponse func(ctx *Context, req *http.Request, resp *http.Response)
7575

7676
// If ConnectAction is ConnectMitm, it sets chunked to Transfer-Encoding.
77-
// By default, it is true.
77+
// By default, true.
7878
MitmChunked bool
7979

80+
// HTTP Authentication type. If it's not specified (""), uses "Basic".
81+
// By default, "".
8082
AuthType string
8183

8284
signer *CaSigner

0 commit comments

Comments
 (0)