From cd5b671f7eccc6929236a3000ee5f5048e38aa10 Mon Sep 17 00:00:00 2001 From: ZhouYixun <291028775@qq.com> Date: Sun, 6 Mar 2022 17:34:30 +0800 Subject: [PATCH 1/5] init --- .idea/.gitignore | 8 ++++++++ .idea/inspectionProfiles/Project_Default.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/sonic-go-mitmproxy.iml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ 5 files changed, 36 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/sonic-go-mitmproxy.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..99ef0e1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sonic-go-mitmproxy.iml b/.idea/sonic-go-mitmproxy.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/sonic-go-mitmproxy.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From 3725ee6bb89fc44f352a652c125865ba946a200f Mon Sep 17 00:00:00 2001 From: ZhouYixun <291028775@qq.com> Date: Sun, 6 Mar 2022 17:36:01 +0800 Subject: [PATCH 2/5] save_init --- .gitignore | 2 +- .idea/.gitignore | 8 -------- .idea/inspectionProfiles/Project_Default.xml | 6 ------ .idea/modules.xml | 8 -------- .idea/sonic-go-mitmproxy.iml | 8 -------- .idea/vcs.xml | 6 ------ 6 files changed, 1 insertion(+), 37 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/sonic-go-mitmproxy.iml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 341ce9e..a41b35c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,5 @@ .env /go-mitmproxy /dummycert - +/.idea dist/ diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 73f69e0..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index 03d9549..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 99ef0e1..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/sonic-go-mitmproxy.iml b/.idea/sonic-go-mitmproxy.iml deleted file mode 100644 index c956989..0000000 --- a/.idea/sonic-go-mitmproxy.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 8522ebedb0cbd52fae1bef2423697a9eba5ca161 Mon Sep 17 00:00:00 2001 From: ZhouYixun <291028775@qq.com> Date: Tue, 8 Mar 2022 21:27:04 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E8=AF=81=E4=B9=A6=E7=94=9F=E6=88=90=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E3=80=81=E5=A2=9E=E5=8A=A0cer=E6=96=87=E4=BB=B6=E7=94=9F?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ README_CN.md | 2 ++ cert/cert.go | 31 ++++++++++++++++++++++++++++--- cmd/go-mitmproxy/main.go | 6 ++++-- proxy/middle.go | 4 ++-- proxy/proxy.go | 4 ++-- 6 files changed, 40 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 32bca22..58c0d1e 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ Usage of go-mitmproxy: show version -web_addr string web interface listen addr (default ":9081") + -cert_path string + path of generate cert files ``` ## Usage as package diff --git a/README_CN.md b/README_CN.md index c6fa7ea..a0787f9 100644 --- a/README_CN.md +++ b/README_CN.md @@ -54,6 +54,8 @@ Usage of go-mitmproxy: show version -web_addr string web interface listen addr (default ":9081") + -cert_path string + path of generate cert files ``` ## 作为包引入 diff --git a/cert/cert.go b/cert/cert.go index 6934008..0d0f704 100644 --- a/cert/cert.go +++ b/cert/cert.go @@ -67,10 +67,23 @@ func NewCA(path string) (*CA, error) { return nil, err } log.Debug("create root ca") - 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) { if path == "" { homeDir, err := os.UserHomeDir() @@ -117,6 +130,10 @@ func (ca *CA) caCertFile() string { 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 { caFile := ca.caFile() stat, err := os.Stat(caFile) @@ -234,7 +251,6 @@ func (ca *CA) save() error { return err } defer file.Close() - return ca.saveTo(file) } @@ -244,8 +260,17 @@ func (ca *CA) saveCert() error { return err } 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) { diff --git a/cmd/go-mitmproxy/main.go b/cmd/go-mitmproxy/main.go index 7dfdd6a..5b5b127 100644 --- a/cmd/go-mitmproxy/main.go +++ b/cmd/go-mitmproxy/main.go @@ -16,7 +16,8 @@ import ( ) type Config struct { - version bool + version bool + certPath string addr string webAddr string @@ -38,6 +39,7 @@ func loadConfig() *Config { flag.StringVar(&config.dump, "dump", "", "dump filename") 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.certPath, "cert_path", "", "path of generate cert files") flag.Parse() return config @@ -63,7 +65,7 @@ func main() { SslInsecure: config.ssl_insecure, } - p, err := proxy.NewProxy(opts) + p, err := proxy.NewProxy(opts,config.certPath) if err != nil { log.Fatal(err) } diff --git a/proxy/middle.go b/proxy/middle.go index 620c450..392b47e 100644 --- a/proxy/middle.go +++ b/proxy/middle.go @@ -73,8 +73,8 @@ type Middle struct { Server *http.Server } -func NewMiddle(proxy *Proxy) (Interceptor, error) { - ca, err := cert.NewCA("") +func NewMiddle(proxy *Proxy,path string) (Interceptor, error) { + ca, err := cert.NewCA(path) if err != nil { return nil, err } diff --git a/proxy/proxy.go b/proxy/proxy.go index 5d66941..abb4183 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -30,7 +30,7 @@ type Proxy struct { Addons []addon.Addon } -func NewProxy(opts *Options) (*Proxy, error) { +func NewProxy(opts *Options,path string) (*Proxy, error) { proxy := new(Proxy) 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 { return nil, err } From 87d9c93f0b8f2babb043d44ef8abefea9bb10414 Mon Sep 17 00:00:00 2001 From: ZhouYixun <291028775@qq.com> Date: Tue, 8 Mar 2022 23:35:25 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=8C=89=E9=9C=80=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cert/cert.go | 24 +++++++----------------- proxy/middle.go | 4 ++-- proxy/proxy.go | 5 +++-- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/cert/cert.go b/cert/cert.go index 0d0f704..731a9cc 100644 --- a/cert/cert.go +++ b/cert/cert.go @@ -70,20 +70,6 @@ func NewCA(path string) (*CA, error) { 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) { if path == "" { homeDir, err := os.UserHomeDir() @@ -261,15 +247,19 @@ func (ca *CA) saveCert() error { } defer file.Close() err = ca.saveCertTo(file) + if err != nil { + return err + } cerFile, err := os.Create(ca.caCertCerFile()) if err != nil { return err } defer cerFile.Close() - - CopyFile(cerFile.Name(), file.Name()) - + err = ca.saveCertTo(cerFile) + if err != nil { + return err + } return err } diff --git a/proxy/middle.go b/proxy/middle.go index 392b47e..b387be8 100644 --- a/proxy/middle.go +++ b/proxy/middle.go @@ -73,8 +73,8 @@ type Middle struct { Server *http.Server } -func NewMiddle(proxy *Proxy,path string) (Interceptor, error) { - ca, err := cert.NewCA(path) +func NewMiddle(proxy *Proxy,caPath string) (Interceptor, error) { + ca, err := cert.NewCA(caPath) if err != nil { return nil, err } diff --git a/proxy/proxy.go b/proxy/proxy.go index abb4183..5e7ff1a 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -19,6 +19,7 @@ type Options struct { Addr string StreamLargeBodies int64 SslInsecure bool + CaRootPath string } type Proxy struct { @@ -30,7 +31,7 @@ type Proxy struct { Addons []addon.Addon } -func NewProxy(opts *Options,path string) (*Proxy, error) { +func NewProxy(opts *Options) (*Proxy, error) { proxy := new(Proxy) proxy.Version = "0.1.5" @@ -65,7 +66,7 @@ func NewProxy(opts *Options,path string) (*Proxy, error) { }, } - interceptor, err := NewMiddle(proxy,path) + interceptor, err := NewMiddle(proxy, opts.CaRootPath) if err != nil { return nil, err } From 281d1addf6618e8f611ecbe4cfb0343d7a707171 Mon Sep 17 00:00:00 2001 From: ZhouYixun <291028775@qq.com> Date: Tue, 8 Mar 2022 23:39:30 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E8=B7=AF=E5=BE=84=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/go-mitmproxy/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/go-mitmproxy/main.go b/cmd/go-mitmproxy/main.go index 5b5b127..e92bd62 100644 --- a/cmd/go-mitmproxy/main.go +++ b/cmd/go-mitmproxy/main.go @@ -63,9 +63,10 @@ func main() { Addr: config.addr, StreamLargeBodies: 1024 * 1024 * 5, SslInsecure: config.ssl_insecure, + CaRootPath: config.certPath, } - p, err := proxy.NewProxy(opts,config.certPath) + p, err := proxy.NewProxy(opts) if err != nil { log.Fatal(err) }