Make it launch I2P profiles too because why shouldn't it be cool like that

This commit is contained in:
idk
2022-01-21 22:47:18 -05:00
parent 1726966e33
commit a193523d3e
4 changed files with 233 additions and 121 deletions

View File

@ -4,29 +4,55 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"strings"
tbget "i2pgit.org/idk/i2p.plugins.tor-manager/get"
TBSupervise "i2pgit.org/idk/i2p.plugins.tor-manager/supervise"
)
func generateMirrorJSON(hostname string) (map[string]interface{}, error) {
if !strings.HasSuffix(hostname, "/") {
hostname += "/"
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{
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
}
func (m *Client) generateMirrorJSON() (map[string]interface{}, error) {
if !strings.HasSuffix(m.hostname, "/") {
m.hostname += "/"
}
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 := tbget.GetUpdaterForLangFromJsonBytes(preBytes, "en-US")
binpath, _, err := m.TBD.GetUpdaterForLangFromJsonBytes(preBytes, "en-US")
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, hostname)
jsonBytes := []byte(strings.Replace(string(preBytes), replaceString, hostname, -1))
fmt.Printf("Replacing: %s with %s\n", replaceString, m.hostname)
jsonBytes := []byte(strings.Replace(string(preBytes), replaceString, m.hostname, -1))
var JSON map[string]interface{}
if err := json.Unmarshal(jsonBytes, &JSON); err != nil {
panic(err)
@ -34,8 +60,8 @@ func generateMirrorJSON(hostname string) (map[string]interface{}, error) {
return JSON, nil
}
func GenerateMirrorJSON(hostname, lang string) (string, error) {
JSON, err := generateMirrorJSON(hostname)
func (m *Client) GenerateMirrorJSON(lang string) (string, error) {
JSON, err := m.generateMirrorJSON()
if err != nil {
return "", err
}
@ -44,7 +70,7 @@ func GenerateMirrorJSON(hostname, lang string) (string, error) {
if err != nil {
return "", fmt.Errorf("GenerateMirrorJSONBytes: %s", err)
}
binpath, _, err := tbget.GetUpdaterForLangFromJsonBytes(preBytes, "en-US")
binpath, _, err := m.TBD.GetUpdaterForLangFromJsonBytes(preBytes, "en-US")
if err != nil {
return "", fmt.Errorf("GenerateMirrorJSONBytes: %s", err)
}
@ -52,7 +78,7 @@ func GenerateMirrorJSON(hostname, lang string) (string, error) {
replaceString := GenerateReplaceString(urlparts[:len(urlparts)-1])
if platform, ok := JSON["downloads"]; ok {
rtp := tbget.GetRuntimePair()
rtp := m.TBD.GetRuntimePair()
for k, v := range platform.(map[string]interface{}) {
if k != rtp {
delete(platform.(map[string]interface{}), k)
@ -68,7 +94,7 @@ func GenerateMirrorJSON(hostname, lang string) (string, error) {
if err != nil {
return "", err
}
return strings.Replace(string(bytes), replaceString, hostname, -1), nil
return strings.Replace(string(bytes), replaceString, m.hostname, -1), nil
}
return "", fmt.Errorf("GenerateMirrorJSONBytes: %s", "No downloads found")
}