various nits to running local services

This commit is contained in:
idk
2022-02-25 00:17:12 -05:00
parent 7f2c89fd7d
commit 681e0e8e65
3 changed files with 23 additions and 5 deletions

View File

@ -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")

View File

@ -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("", "")

View File

@ -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)