Files
i2p.plugins.tor-updater/onion/onion.go

123 lines
2.9 KiB
Go
Raw Normal View History

2022-02-22 21:45:32 -05:00
package i2pdotonion
import (
"context"
2022-02-28 19:59:47 -05:00
"embed"
2022-02-22 21:45:32 -05:00
"fmt"
2022-02-28 19:59:47 -05:00
"io/fs"
"io/ioutil"
2022-02-22 21:45:32 -05:00
"log"
"net"
"net/http"
2022-02-25 00:30:41 -05:00
"os"
2022-02-25 00:17:12 -05:00
"path"
"path/filepath"
2022-02-28 19:59:47 -05:00
"strings"
2022-02-22 21:45:32 -05:00
"time"
"github.com/cretz/bine/tor"
)
2022-02-25 00:30:41 -05:00
//go:embded www/*
2022-02-28 19:59:47 -05:00
var content embed.FS
2022-02-25 00:30:41 -05:00
2022-02-22 21:45:32 -05:00
type I2POnionService struct {
OnionService net.Listener
2022-02-25 00:17:12 -05:00
ServeDir string
}
2022-02-28 19:59:47 -05:00
func NewOnionService(dir string) (*I2POnionService, error) {
ios := &I2POnionService{ServeDir: dir}
if err := ios.UnpackSite(); err != nil {
return nil, err
}
return ios, nil
2022-02-22 21:45:32 -05:00
}
func (ios *I2POnionService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2022-02-25 00:17:12 -05:00
path := path.Clean(r.URL.Path)
if path == "/" {
path = "/index.html"
}
path = filepath.Join(ios.ServeDir, path)
2022-02-25 00:30:41 -05:00
finfo, err := os.Stat(path)
if err != nil {
http.NotFound(w, r)
return
}
if finfo.IsDir() {
http.NotFound(w, r)
}
2022-02-25 00:17:12 -05:00
http.ServeFile(w, r, path)
2022-02-22 21:45:32 -05:00
}
2022-02-25 00:30:41 -05:00
func (ios *I2POnionService) StandardHTML() string {
return ""
}
2022-02-22 21:45:32 -05:00
func (ios *I2POnionService) Listen(net, addr string) (net.Listener, error) {
if ios.OnionService != nil {
return ios.OnionService, nil
}
fmt.Println("Starting and registering onion service, please wait a couple of minutes...")
2022-02-25 00:17:12 -05:00
tb, err := tor.Start(context.Background(), nil)
2022-02-22 21:45:32 -05:00
if err != nil {
log.Panicf("Unable to start Tor: %v", err)
}
defer tb.Close()
// Wait at most a few minutes to publish the service
listenCtx, listenCancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer listenCancel()
// Create a v3 onion service to listen on any port but show as 80
ios.OnionService, err = tb.Listen(listenCtx, &tor.ListenConf{Version3: true, RemotePorts: []int{80}})
if err != nil {
log.Panicf("Unable to create onion service: %v", err)
}
return ios.OnionService, nil
}
2022-02-25 00:17:12 -05:00
func (ios *I2POnionService) Serve(l net.Listener) error {
ios.OnionService = l
return http.Serve(ios.OnionService, ios)
}
2022-02-22 21:45:32 -05:00
func (ios *I2POnionService) ListenAndServe() error {
var err error
ios.OnionService, err = ios.Listen("", "")
if err != nil {
return err
}
return http.Serve(ios.OnionService, ios)
}
2022-02-28 19:59:47 -05:00
func (ios *I2POnionService) UnpackSite() error {
docroot := filepath.Join(ios.ServeDir, "www")
os.MkdirAll(docroot, 0755)
if dir, err := os.Stat(docroot); err == nil && dir.IsDir() {
return nil
}
//unpack the contents to the docroot
return fs.WalkDir(content, ".", func(embedpath string, d fs.DirEntry, err error) error {
fp := filepath.Join(docroot)
if err != nil {
log.Fatal(err)
}
fmt.Println(embedpath, filepath.Join(fp, strings.Replace(embedpath, "tor-browser/unpack/i2p.firefox", "", -1)))
if d.IsDir() {
os.MkdirAll(filepath.Join(fp, strings.Replace(embedpath, "tor-browser/unpack/i2p.firefox", "", -1)), 0755)
} else {
fullpath := path.Join(embedpath)
bytes, err := content.ReadFile(fullpath)
if err != nil {
return err
}
unpack := filepath.Join(fp, strings.Replace(embedpath, "tor-browser/unpack/i2p.firefox", "", -1))
if err := ioutil.WriteFile(unpack, bytes, 0644); err != nil {
return err
}
}
return nil
})
}