add dummycert cmd

addon-dailer
lqqyt2423 4 years ago
parent c18165f94c
commit ceaeb327ff

@ -1,10 +1,16 @@
all: mitmproxy dummycert
.PHONY: mitmproxy .PHONY: mitmproxy
mitmproxy: mitmproxy:
go build -o mitmproxy cmd/mitmproxy/main.go go build -o mitmproxy cmd/mitmproxy/main.go
.PHONY: dummycert
dummycert:
go build -o dummycert cmd/dummycert/main.go
.PHONY: clean .PHONY: clean
clean: clean:
rm -f mitmproxy rm -f mitmproxy dummycert
.PHONY: test .PHONY: test
test: test:

@ -12,6 +12,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"net"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -278,7 +279,13 @@ func (ca *CA) DummyCert(commonName string) (*tls.Certificate, error) {
NotAfter: time.Now().Add(time.Hour * 24 * 365), NotAfter: time.Now().Add(time.Hour * 24 * 365),
SignatureAlgorithm: x509.SHA256WithRSA, SignatureAlgorithm: x509.SHA256WithRSA,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, 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) certBytes, err := x509.CreateCertificate(rand.Reader, template, &ca.RootCert, &ca.PrivateKey.PublicKey, &ca.PrivateKey)

@ -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)
}
}
Loading…
Cancel
Save