2022-01-16 17:06:29 -05:00
|
|
|
package tbserve
|
|
|
|
|
|
|
|
import (
|
2022-01-23 13:40:14 -05:00
|
|
|
"embed"
|
2022-01-21 22:47:18 -05:00
|
|
|
"log"
|
2022-01-22 00:32:39 -05:00
|
|
|
"net/http"
|
2022-01-16 17:06:29 -05:00
|
|
|
"path/filepath"
|
|
|
|
|
2022-01-23 11:42:23 -05:00
|
|
|
"github.com/justinas/nosurf"
|
2022-01-16 17:06:29 -05:00
|
|
|
tbget "i2pgit.org/idk/i2p.plugins.tor-manager/get"
|
2022-01-21 22:47:18 -05:00
|
|
|
TBSupervise "i2pgit.org/idk/i2p.plugins.tor-manager/supervise"
|
2022-01-16 17:06:29 -05:00
|
|
|
)
|
|
|
|
|
2022-01-21 22:47:18 -05:00
|
|
|
type Client struct {
|
|
|
|
hostname string
|
|
|
|
TBD *tbget.TBDownloader
|
|
|
|
TBS *TBSupervise.Supervisor
|
|
|
|
}
|
|
|
|
|
2022-01-23 13:40:14 -05:00
|
|
|
func NewClient(hostname string, lang string, os string, arch string, content *embed.FS) (*Client, error) {
|
2022-01-21 22:47:18 -05:00
|
|
|
m := &Client{
|
2022-01-21 23:15:55 -05:00
|
|
|
hostname: hostname,
|
2022-01-23 13:40:14 -05:00
|
|
|
TBD: tbget.NewTBDownloader(lang, os, arch, content),
|
2022-01-21 22:47:18 -05:00
|
|
|
}
|
2022-01-23 13:40:14 -05:00
|
|
|
m.TBD.MakeTBDirectory()
|
2022-01-21 22:47:18 -05:00
|
|
|
tgz, sig, err := m.TBD.DownloadUpdaterForLang(lang)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var home string
|
|
|
|
if home, err = m.TBD.CheckSignature(tgz, sig); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
} else {
|
|
|
|
log.Printf("Signature check passed: %s %s", tgz, sig)
|
|
|
|
}
|
|
|
|
m.TBS = TBSupervise.NewSupervisor(home, lang)
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2022-01-22 00:42:10 -05:00
|
|
|
func (m *Client) ServeHTTP(rw http.ResponseWriter, rq *http.Request) {
|
2022-01-22 00:32:39 -05:00
|
|
|
path := rq.URL.Path
|
2022-01-22 01:09:18 -05:00
|
|
|
log.Printf("ServeHTTP: '%s'", path)
|
2022-01-22 23:36:48 -05:00
|
|
|
fileextension := filepath.Ext(path)
|
|
|
|
switch fileextension {
|
|
|
|
case ".json":
|
|
|
|
m.serveJSON(rw, rq)
|
|
|
|
return
|
|
|
|
case ".css":
|
|
|
|
m.serveCSS(rw, rq)
|
|
|
|
return
|
|
|
|
case ".js":
|
|
|
|
m.serveJS(rw, rq)
|
|
|
|
return
|
|
|
|
case ".png":
|
|
|
|
m.servePNG(rw, rq)
|
|
|
|
return
|
|
|
|
case ".ico":
|
|
|
|
m.serveICO(rw, rq)
|
|
|
|
return
|
|
|
|
case ".svg":
|
|
|
|
m.serveSVG(rw, rq)
|
|
|
|
return
|
2022-01-22 00:32:39 -05:00
|
|
|
default:
|
2022-01-22 23:36:48 -05:00
|
|
|
switch path {
|
|
|
|
case "/launch-tor-browser":
|
|
|
|
log.Println("Starting Tor Browser")
|
|
|
|
go m.TBS.RunTBWithLang()
|
|
|
|
http.Redirect(rw, rq, "/", http.StatusFound)
|
|
|
|
case "/launch-i2p-browser":
|
|
|
|
log.Println("Starting I2P Browser")
|
|
|
|
go m.TBS.RunI2PBWithLang()
|
|
|
|
http.Redirect(rw, rq, "/", http.StatusFound)
|
|
|
|
case "/start-tor":
|
|
|
|
log.Println("Starting Tor")
|
|
|
|
go m.TBS.RunTorWithLang()
|
|
|
|
http.Redirect(rw, rq, "/", http.StatusFound)
|
|
|
|
case "/stop-tor":
|
|
|
|
log.Println("Stopping Tor")
|
|
|
|
go m.TBS.StopTor()
|
|
|
|
http.Redirect(rw, rq, "/", http.StatusFound)
|
|
|
|
default:
|
|
|
|
b, _ := m.Page()
|
|
|
|
rw.Header().Set("Content-Type", "text/html")
|
|
|
|
rw.Write([]byte(b))
|
2022-01-22 00:32:39 -05:00
|
|
|
}
|
|
|
|
}
|
2022-01-22 23:36:48 -05:00
|
|
|
|
2022-01-22 00:32:39 -05:00
|
|
|
}
|
|
|
|
|
2022-01-22 00:42:10 -05:00
|
|
|
func (m *Client) Serve() error {
|
|
|
|
//http.Handle("/", m)
|
2022-01-22 01:13:30 -05:00
|
|
|
go m.TBS.RunTorWithLang()
|
2022-01-23 11:42:23 -05:00
|
|
|
return http.ListenAndServe("127.0.0.1:7695", nosurf.New(m))
|
2022-01-22 00:42:10 -05:00
|
|
|
}
|