From ceaeb327ff4e9ad7fb556190fbd506559018524b Mon Sep 17 00:00:00 2001 From: lqqyt2423 <974923609@qq.com> Date: Tue, 5 Jan 2021 00:43:27 +0800 Subject: [PATCH] add dummycert cmd --- Makefile | 8 +++++- cert/cert.go | 9 +++++- cmd/dummycert/main.go | 65 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 cmd/dummycert/main.go diff --git a/Makefile b/Makefile index 60414d6..531c595 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,16 @@ +all: mitmproxy dummycert + .PHONY: mitmproxy mitmproxy: go build -o mitmproxy cmd/mitmproxy/main.go +.PHONY: dummycert +dummycert: + go build -o dummycert cmd/dummycert/main.go + .PHONY: clean clean: - rm -f mitmproxy + rm -f mitmproxy dummycert .PHONY: test test: diff --git a/cert/cert.go b/cert/cert.go index 3ce0c81..a567414 100644 --- a/cert/cert.go +++ b/cert/cert.go @@ -12,6 +12,7 @@ import ( "io" "io/ioutil" "math/big" + "net" "os" "path/filepath" "time" @@ -278,7 +279,13 @@ func (ca *CA) DummyCert(commonName string) (*tls.Certificate, error) { NotAfter: time.Now().Add(time.Hour * 24 * 365), SignatureAlgorithm: x509.SHA256WithRSA, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, - DNSNames: []string{commonName}, + } + + ip := net.ParseIP(commonName) + if ip != nil { + template.IPAddresses = []net.IP{ip} + } else { + template.DNSNames = []string{commonName} } certBytes, err := x509.CreateCertificate(rand.Reader, template, &ca.RootCert, &ca.PrivateKey.PublicKey, &ca.PrivateKey) diff --git a/cmd/dummycert/main.go b/cmd/dummycert/main.go new file mode 100644 index 0000000..6a223e2 --- /dev/null +++ b/cmd/dummycert/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "crypto/x509" + "encoding/pem" + "flag" + "fmt" + "os" + + "github.com/lqqyt2423/go-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") + } + + ca, err := cert.NewCA("") + if err != nil { + panic(err) + } + + cert, err := ca.DummyCert(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) + } +}