From 2604a638dbbaf50e20f3cda4a1ab69f5f6619711 Mon Sep 17 00:00:00 2001 From: faceair Date: Sat, 1 Dec 2018 20:50:52 +0800 Subject: [PATCH] support cert file download --- git/git.go | 31 ++++++++++++++++++++++++------- git/git_test.go | 2 +- main.go | 21 ++++++++------------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/git/git.go b/git/git.go index 639c589..dd1225b 100644 --- a/git/git.go +++ b/git/git.go @@ -1,6 +1,7 @@ package git import ( + "bytes" "errors" "fmt" "io" @@ -22,15 +23,21 @@ var repoRegex = regexp.MustCompile(`content="(.+?)\s+git\s+(.+)?"`) // NewServer create a Server instance // The gopath should be a valid folder and will store git repositories later -func NewServer(gopath string) *Server { +func NewServer(gopath, certpath string) *Server { err := os.Setenv("GOPATH", gopath) if err != nil { panic(err) } + certfile, err := ioutil.ReadFile(certpath) + if err != nil { + panic(err) + } + g := &Server{ - gopath: gopath, - queue: make(chan *cloneTask, 1024), + gopath: gopath, + certfile: certfile, + queue: make(chan *cloneTask, 1024), } go g.cloneLoop() return g @@ -38,16 +45,26 @@ func NewServer(gopath string) *Server { // Server implement interface of betproxy.Client type Server struct { - gopath string - queue chan *cloneTask - upTime sync.Map + gopath string + certfile []byte + queue chan *cloneTask + upTime sync.Map } // Do receive client requests and return git repository information func (g *Server) Do(req *http.Request) (*http.Response, error) { match := urlRegex.FindStringSubmatch(req.URL.String()) if match == nil { - return HTTPRedirect("https://github.com/faceair/gotit", req), nil + switch req.URL.Path { + case "/ssl": + res := betproxy.NewResponse(http.StatusOK, http.Header{ + "Content-Disposition": []string{"attachment; filename=gotit.crt"}, + }, bytes.NewReader(g.certfile), req) + res.ContentLength = int64(len(g.certfile)) + return res, nil + default: + return HTTPRedirect("https://github.com/faceair/gotit", req), nil + } } repoPath := match[1] diff --git a/git/git_test.go b/git/git_test.go index dc44a6d..0482559 100644 --- a/git/git_test.go +++ b/git/git_test.go @@ -17,7 +17,7 @@ func NewTestServer() *Server { log.Fatal(err) } - return NewServer(dir) + return NewServer(dir, "./git.go") } func TestURLNotMatch(t *testing.T) { diff --git a/main.go b/main.go index 6b8cfb6..fa9bbf1 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,10 @@ func main() { os.Exit(0) } - cacert, cakey, err := loadCA(capath) + certpath := path.Join(capath, "gotit.cert.pem") + keypath := path.Join(capath, "gotit.key.pem") + + cacert, cakey, err := loadCA(certpath, keypath) if err != nil { panic(err) } @@ -49,17 +52,14 @@ func main() { if err != nil { panic(err) } - service.SetClient(git.NewServer(gopath)) + service.SetClient(git.NewServer(gopath, certpath)) log.Fatal(service.Listen()) } -func loadCA(capath string) (*x509.Certificate, *rsa.PrivateKey, error) { - certpath := path.Join(capath, "gotit.cert.pem") - keypath := path.Join(capath, "gotit.key.pem") - +func loadCA(certpath, keypath string) (*x509.Certificate, *rsa.PrivateKey, error) { if _, err := os.Stat(certpath); os.IsNotExist(err) { - err := generateCA(capath, certpath, keypath) + err := generateCA(certpath, keypath) if err != nil { return nil, nil, err } @@ -93,12 +93,7 @@ func loadCA(capath string) (*x509.Certificate, *rsa.PrivateKey, error) { return rawCert, rawKey, nil } -func generateCA(capath, certpath, keypath string) error { - err := os.MkdirAll(capath, os.ModePerm) - if err != nil { - return err - } - +func generateCA(certpath, keypath string) error { cacert, cakey, err := mitm.NewAuthority("gotit", "faceair", 10*365*24*time.Hour) if err != nil { return err