增加自定义证书生成路径、增加cer文件生成

addon-dailer
ZhouYixun 3 years ago
parent 3725ee6bb8
commit 8522ebedb0

@ -54,6 +54,8 @@ Usage of go-mitmproxy:
show version show version
-web_addr string -web_addr string
web interface listen addr (default ":9081") web interface listen addr (default ":9081")
-cert_path string
path of generate cert files
``` ```
## Usage as package ## Usage as package

@ -54,6 +54,8 @@ Usage of go-mitmproxy:
show version show version
-web_addr string -web_addr string
web interface listen addr (default ":9081") web interface listen addr (default ":9081")
-cert_path string
path of generate cert files
``` ```
## 作为包引入 ## 作为包引入

@ -67,10 +67,23 @@ func NewCA(path string) (*CA, error) {
return nil, err return nil, err
} }
log.Debug("create root ca") log.Debug("create root ca")
return ca, nil return ca, nil
} }
func CopyFile(dstName, srcName string) (written int64, err error) {
src, err := os.Open(srcName)
if err != nil {
return
}
defer src.Close()
dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return
}
defer dst.Close()
return io.Copy(dst, src)
}
func getStorePath(path string) (string, error) { func getStorePath(path string) (string, error) {
if path == "" { if path == "" {
homeDir, err := os.UserHomeDir() homeDir, err := os.UserHomeDir()
@ -117,6 +130,10 @@ func (ca *CA) caCertFile() string {
return filepath.Join(ca.StorePath, "mitmproxy-ca-cert.pem") return filepath.Join(ca.StorePath, "mitmproxy-ca-cert.pem")
} }
func (ca *CA) caCertCerFile() string {
return filepath.Join(ca.StorePath, "mitmproxy-ca-cert.cer")
}
func (ca *CA) load() error { func (ca *CA) load() error {
caFile := ca.caFile() caFile := ca.caFile()
stat, err := os.Stat(caFile) stat, err := os.Stat(caFile)
@ -234,7 +251,6 @@ func (ca *CA) save() error {
return err return err
} }
defer file.Close() defer file.Close()
return ca.saveTo(file) return ca.saveTo(file)
} }
@ -244,8 +260,17 @@ func (ca *CA) saveCert() error {
return err return err
} }
defer file.Close() defer file.Close()
err = ca.saveCertTo(file)
cerFile, err := os.Create(ca.caCertCerFile())
if err != nil {
return err
}
defer cerFile.Close()
CopyFile(cerFile.Name(), file.Name())
return ca.saveCertTo(file) return err
} }
func (ca *CA) GetCert(commonName string) (*tls.Certificate, error) { func (ca *CA) GetCert(commonName string) (*tls.Certificate, error) {

@ -16,7 +16,8 @@ import (
) )
type Config struct { type Config struct {
version bool version bool
certPath string
addr string addr string
webAddr string webAddr string
@ -38,6 +39,7 @@ func loadConfig() *Config {
flag.StringVar(&config.dump, "dump", "", "dump filename") flag.StringVar(&config.dump, "dump", "", "dump filename")
flag.IntVar(&config.dumpLevel, "dump_level", 0, "dump level: 0 - header, 1 - header + body") flag.IntVar(&config.dumpLevel, "dump_level", 0, "dump level: 0 - header, 1 - header + body")
flag.StringVar(&config.mapperDir, "mapper_dir", "", "mapper files dirpath") flag.StringVar(&config.mapperDir, "mapper_dir", "", "mapper files dirpath")
flag.StringVar(&config.certPath, "cert_path", "", "path of generate cert files")
flag.Parse() flag.Parse()
return config return config
@ -63,7 +65,7 @@ func main() {
SslInsecure: config.ssl_insecure, SslInsecure: config.ssl_insecure,
} }
p, err := proxy.NewProxy(opts) p, err := proxy.NewProxy(opts,config.certPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

@ -73,8 +73,8 @@ type Middle struct {
Server *http.Server Server *http.Server
} }
func NewMiddle(proxy *Proxy) (Interceptor, error) { func NewMiddle(proxy *Proxy,path string) (Interceptor, error) {
ca, err := cert.NewCA("") ca, err := cert.NewCA(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -30,7 +30,7 @@ type Proxy struct {
Addons []addon.Addon Addons []addon.Addon
} }
func NewProxy(opts *Options) (*Proxy, error) { func NewProxy(opts *Options,path string) (*Proxy, error) {
proxy := new(Proxy) proxy := new(Proxy)
proxy.Version = "0.1.5" proxy.Version = "0.1.5"
@ -65,7 +65,7 @@ func NewProxy(opts *Options) (*Proxy, error) {
}, },
} }
interceptor, err := NewMiddle(proxy) interceptor, err := NewMiddle(proxy,path)
if err != nil { if err != nil {
return nil, err return nil, err
} }

Loading…
Cancel
Save