-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
66 lines (55 loc) · 1.27 KB
/
main.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
package main
import (
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"os"
"github.com/kardianos/mitmproxy/cert"
log "github.com/sirupsen/logrus"
)
// 生成假的/用于测试的服务器证书
type Config struct {
commonName string
}
func loadConfig() *Config {
config := new(Config)
flag.StringVar(&config.commonName, "commonName", "", "server commonName")
flag.Parse()
return config
}
func main() {
log.SetLevel(log.InfoLevel)
log.SetReportCaller(false)
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
config := loadConfig()
if config.commonName == "" {
log.Fatal("commonName required")
}
l := &cert.MemoryLoader{}
ca, err := cert.New(l)
if err != nil {
panic(err)
}
cert, err := ca.GenerateCert(config.commonName)
if err != nil {
panic(err)
}
os.Stdout.WriteString(fmt.Sprintf("%v-cert.pem\n", config.commonName))
err = pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Certificate[0]})
if err != nil {
panic(err)
}
os.Stdout.WriteString(fmt.Sprintf("\n%v-key.pem\n", config.commonName))
keyBytes, err := x509.MarshalPKCS8PrivateKey(&ca.PrivateKey)
if err != nil {
panic(err)
}
err = pem.Encode(os.Stdout, &pem.Block{Type: "PRIVATE KEY", Bytes: keyBytes})
if err != nil {
panic(err)
}
}