From 681e0e8e65e371d100f519fe8b4bc4fe6eaec26b Mon Sep 17 00:00:00 2001 From: idk Date: Fri, 25 Feb 2022 00:17:12 -0500 Subject: [PATCH] various nits to running local services --- get/get.go | 2 +- onion/onion.go | 22 ++++++++++++++++++++-- serve/serve.go | 4 ++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/get/get.go b/get/get.go index ced2ca6..bfdc6f9 100644 --- a/get/get.go +++ b/get/get.go @@ -103,7 +103,7 @@ func NewTBDownloader(lang string, os, arch string, content *embed.FS) *TBDownloa // ServeHTTP serves the DOWNLOAD_PATH as a mirror func (t *TBDownloader) ServeHTTP(w http.ResponseWriter, r *http.Request) { - r.URL.Path = strings.Replace(r.URL.Path, "..", "", -1) + r.URL.Path = path.Clean(r.URL.Path) ext := filepath.Ext(r.URL.Path) if ext == ".json" { w.Header().Set("Content-Type", "application/json") diff --git a/onion/onion.go b/onion/onion.go index 29702d2..76750a0 100644 --- a/onion/onion.go +++ b/onion/onion.go @@ -6,6 +6,8 @@ import ( "log" "net" "net/http" + "path" + "path/filepath" "time" "github.com/cretz/bine/tor" @@ -13,10 +15,21 @@ import ( type I2POnionService struct { OnionService net.Listener + ServeDir string +} + +func NewOnionService(dir string) *I2POnionService { + return &I2POnionService{ServeDir: dir} } func (ios *I2POnionService) ServeHTTP(w http.ResponseWriter, r *http.Request) { - fmt.Println("ServeHTTP") + path := path.Clean(r.URL.Path) + if path == "/" { + path = "/index.html" + } + path = filepath.Join(ios.ServeDir, path) + + http.ServeFile(w, r, path) } func (ios *I2POnionService) Listen(net, addr string) (net.Listener, error) { @@ -24,7 +37,7 @@ func (ios *I2POnionService) Listen(net, addr string) (net.Listener, error) { return ios.OnionService, nil } fmt.Println("Starting and registering onion service, please wait a couple of minutes...") - tb, err := tor.Start(nil, nil) + tb, err := tor.Start(context.Background(), nil) if err != nil { log.Panicf("Unable to start Tor: %v", err) } @@ -40,6 +53,11 @@ func (ios *I2POnionService) Listen(net, addr string) (net.Listener, error) { return ios.OnionService, nil } +func (ios *I2POnionService) Serve(l net.Listener) error { + ios.OnionService = l + return http.Serve(ios.OnionService, ios) +} + func (ios *I2POnionService) ListenAndServe() error { var err error ios.OnionService, err = ios.Listen("", "") diff --git a/serve/serve.go b/serve/serve.go index dacaef7..5b5887f 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -7,9 +7,9 @@ import ( "io/ioutil" "log" "net/http" + "path" "path/filepath" "strconv" - "strings" "github.com/justinas/nosurf" cp "github.com/otiai10/copy" @@ -99,7 +99,7 @@ func (m *Client) GetAddress() string { // ServeHTTP handles HTTP requests. func (m *Client) ServeHTTP(rw http.ResponseWriter, rq *http.Request) { - path := strings.Replace(rq.URL.Path, "..", "", -1) + path := path.Clean(rq.URL.Path) rq.URL.Path = path log.Printf("ServeHTTP: '%s'", path) fileextension := filepath.Ext(path)