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 }