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

155 lines
4.1 KiB
Go
Raw Normal View History

2022-01-16 17:06:29 -05:00
package tbserve
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
2022-01-22 00:32:39 -05:00
"net/http"
2022-01-16 17:06:29 -05:00
"path/filepath"
"strings"
tbget "i2pgit.org/idk/i2p.plugins.tor-manager/get"
TBSupervise "i2pgit.org/idk/i2p.plugins.tor-manager/supervise"
2022-01-16 17:06:29 -05:00
)
type Client struct {
hostname string
TBD *tbget.TBDownloader
TBS *TBSupervise.Supervisor
}
func NewClient(hostname string, lang string, os string, arch string) (*Client, error) {
m := &Client{
2022-01-21 23:15:55 -05:00
hostname: hostname,
TBD: tbget.NewTBDownloader(lang, os, arch),
}
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 00:32:39 -05:00
switch path {
2022-01-22 01:09:18 -05:00
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)
2022-01-22 00:32:39 -05:00
case "/start-tor":
2022-01-22 01:09:18 -05:00
log.Println("Starting Tor")
go m.TBS.RunTorWithLang()
http.Redirect(rw, rq, "/", http.StatusFound)
2022-01-22 00:32:39 -05:00
case "/stop-tor":
2022-01-22 01:09:18 -05:00
log.Println("Stopping Tor")
go m.TBS.StopTor()
http.Redirect(rw, rq, "/", http.StatusFound)
2022-01-22 00:32:39 -05:00
default:
b, e := m.Page()
if e != nil {
2022-01-22 01:09:18 -05:00
log.Printf("Serve Error: %s", e)
2022-01-22 00:32:39 -05:00
}
2022-01-22 01:09:18 -05:00
//rw.WriteHeader("Content-Type", "text/html")
rw.Header().Set("Content-Type", "text/html")
2022-01-22 00:32:39 -05:00
rw.Write([]byte(b))
}
}
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-22 00:42:10 -05:00
return http.ListenAndServe("127.0.0.1:7695", m)
}
func (m *Client) generateMirrorJSON() (map[string]interface{}, error) {
if !strings.HasSuffix(m.hostname, "/") {
m.hostname += "/"
2022-01-16 17:06:29 -05:00
}
path := filepath.Join(tbget.DOWNLOAD_PATH, "downloads.json")
preBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("GenerateMirrorJSON: %s", err)
}
binpath, _, err := m.TBD.GetUpdaterForLangFromJsonBytes(preBytes, "en-US")
2022-01-16 17:06:29 -05:00
if err != nil {
return nil, fmt.Errorf("GenerateMirrorJSON: %s", err)
}
urlparts := strings.Split(binpath, "/")
replaceString := GenerateReplaceString(urlparts[:len(urlparts)-1])
fmt.Printf("Replacing: %s with %s\n", replaceString, m.hostname)
jsonBytes := []byte(strings.Replace(string(preBytes), replaceString, m.hostname, -1))
2022-01-16 17:06:29 -05:00
var JSON map[string]interface{}
if err := json.Unmarshal(jsonBytes, &JSON); err != nil {
panic(err)
}
return JSON, nil
}
func (m *Client) GenerateMirrorJSON(lang string) (string, error) {
JSON, err := m.generateMirrorJSON()
2022-01-16 17:06:29 -05:00
if err != nil {
return "", err
}
path := filepath.Join(tbget.DOWNLOAD_PATH, "downloads.json")
preBytes, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("GenerateMirrorJSONBytes: %s", err)
}
binpath, _, err := m.TBD.GetUpdaterForLangFromJsonBytes(preBytes, "en-US")
2022-01-16 17:06:29 -05:00
if err != nil {
return "", fmt.Errorf("GenerateMirrorJSONBytes: %s", err)
}
urlparts := strings.Split(binpath, "/")
replaceString := GenerateReplaceString(urlparts[:len(urlparts)-1])
if platform, ok := JSON["downloads"]; ok {
rtp := m.TBD.GetRuntimePair()
2022-01-16 17:06:29 -05:00
for k, v := range platform.(map[string]interface{}) {
if k != rtp {
delete(platform.(map[string]interface{}), k)
}
for k2 := range v.(map[string]interface{}) {
if k2 != lang {
delete(v.(map[string]interface{}), k2)
}
}
}
bytes, err := json.MarshalIndent(JSON, "", " ")
if err != nil {
return "", err
}
return strings.Replace(string(bytes), replaceString, m.hostname, -1), nil
2022-01-16 17:06:29 -05:00
}
return "", fmt.Errorf("GenerateMirrorJSONBytes: %s", "No downloads found")
}
func GenerateReplaceString(urlparts []string) string {
replaceString := ""
for _, val := range urlparts {
if val == "https" {
replaceString += val + "//"
} else {
replaceString += val + "/"
}
}
if !strings.HasSuffix(replaceString, "/") {
replaceString += "/"
}
return replaceString
}