-
Notifications
You must be signed in to change notification settings - Fork 0
/
https.go
150 lines (136 loc) · 4.17 KB
/
https.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io"
"math/big"
"os"
"strings"
"time"
"github.com/elazarl/goproxy"
"github.com/urfave/cli/v2"
"golang.org/x/net/idna"
)
func createRootCAInfo(rootCertificateReader, rootKeyReader io.Reader) (*x509.Certificate, *rsa.PrivateKey, error) {
rootCertB, err := io.ReadAll(rootCertificateReader)
if err != nil {
return nil, nil, fmt.Errorf(": %w", err)
}
rootCertBlock, _ := pem.Decode(rootCertB)
if err != nil {
return nil, nil, fmt.Errorf(": %w", err)
}
rootCertificate, err := x509.ParseCertificate(rootCertBlock.Bytes)
if err != nil {
return nil, nil, fmt.Errorf(": %w", err)
}
k, err := io.ReadAll(rootKeyReader)
if err != nil {
return nil, nil, fmt.Errorf(": %w", err)
}
rootKBlock, _ := pem.Decode(k)
rootKey, err := x509.ParsePKCS1PrivateKey(rootKBlock.Bytes)
if err != nil {
return nil, nil, fmt.Errorf(": %w", err)
}
return rootCertificate, rootKey, nil
}
func createCertificate(host string, rootCertificate *x509.Certificate, rootKey *rsa.PrivateKey) (tls.Certificate, error) {
var c tls.Certificate
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serial, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return c, fmt.Errorf(": %w", err)
}
notBefore := time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
notAfter := time.Now().AddDate(2, 0, 0)
cnHosts := strings.Split(host, ":")
hostName := cnHosts[0]
hostName, err = idna.ToASCII(hostName)
if err != nil {
return c, err
}
hostName = strings.ToLower(hostName)
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return c, fmt.Errorf(": %w", err)
}
template := x509.Certificate{
SerialNumber: serial,
NotBefore: notBefore,
NotAfter: notAfter,
Issuer: pkix.Name{
Organization: rootCertificate.Issuer.Organization,
OrganizationalUnit: rootCertificate.Issuer.OrganizationalUnit,
CommonName: rootCertificate.Issuer.CommonName,
},
Subject: pkix.Name{
CommonName: "*." + hostName,
},
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
AuthorityKeyId: rootCertificate.SubjectKeyId,
DNSNames: []string{hostName, "*." + hostName},
}
certB, err := x509.CreateCertificate(rand.Reader, &template, rootCertificate, &priv.PublicKey, rootKey)
if err != nil {
return c, fmt.Errorf(": %w", err)
}
var certPem, keyPem bytes.Buffer
if err := pem.Encode(&certPem, &pem.Block{Type: "CERTIFICATE", Bytes: certB}); err != nil {
return c, fmt.Errorf(": %w", err)
}
marshaledKey := x509.MarshalPKCS1PrivateKey(priv)
if err := pem.Encode(&keyPem, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: marshaledKey}); err != nil {
return c, fmt.Errorf(": %w", err)
}
certPemB, err := io.ReadAll(&certPem)
if err != nil {
return c, fmt.Errorf(": %w", err)
}
keyPemB, err := io.ReadAll(&keyPem)
if err != nil {
return c, fmt.Errorf(": %w", err)
}
return tls.X509KeyPair(certPemB, keyPemB)
}
func enableHttpsProxy(c *cli.Context, proxy *goproxy.ProxyHttpServer) error {
certFile, err := os.Open(c.String("cert"))
if err != nil {
return fmt.Errorf("failed to open cert file: %w", err)
}
keyFile, err := os.Open(c.String("key"))
if err != nil {
return fmt.Errorf("failed to open key file: %w", err)
}
defer keyFile.Close()
defer certFile.Close()
rootCert, rootKey, err := createRootCAInfo(certFile, keyFile)
if err != nil {
return fmt.Errorf("failed to create rootca info: %w", err)
}
httpsHandler := func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
certificate, err := createCertificate(host, rootCert, rootKey)
if err != nil {
fmt.Println(err)
return nil, ""
}
customConnectAction := &goproxy.ConnectAction{
Action: goproxy.ConnectMitm,
TLSConfig: func(host string, ctx *goproxy.ProxyCtx) (*tls.Config, error) {
var config tls.Config
config.Certificates = append(config.Certificates, certificate)
return &config, nil
},
}
return customConnectAction, host
}
proxy.OnRequest().HandleConnect(goproxy.FuncHttpsHandler(httpsHandler))
return nil
}