Selve-serve a mirror
This commit is contained in:
@ -68,6 +68,9 @@ func GetUpdaterForLangFromJson(body io.ReadCloser, ietf string) (string, string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", fmt.Errorf("GetUpdaterForLangFromJson: %s", err)
|
return "", "", fmt.Errorf("GetUpdaterForLangFromJson: %s", err)
|
||||||
}
|
}
|
||||||
|
if err = ioutil.WriteFile(filepath.Join(DOWNLOAD_PATH, "downloads.json"), jsonBytes, 0644); err != nil {
|
||||||
|
return "", "", fmt.Errorf("GetUpdaterForLangFromJson: %s", err)
|
||||||
|
}
|
||||||
return GetUpdaterForLangFromJsonBytes(jsonBytes, ietf)
|
return GetUpdaterForLangFromJsonBytes(jsonBytes, ietf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
89
serve/serve.go
Normal file
89
serve/serve.go
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
package tbserve
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
tbget "i2pgit.org/idk/i2p.plugins.tor-manager/get"
|
||||||
|
)
|
||||||
|
|
||||||
|
func generateMirrorJSON(hostname string) (map[string]interface{}, error) {
|
||||||
|
if !strings.HasSuffix(hostname, "/") {
|
||||||
|
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")
|
||||||
|
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))
|
||||||
|
var JSON map[string]interface{}
|
||||||
|
if err := json.Unmarshal(jsonBytes, &JSON); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return JSON, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateMirrorJSON(hostname, lang string) (string, error) {
|
||||||
|
JSON, err := generateMirrorJSON(hostname)
|
||||||
|
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 := tbget.GetUpdaterForLangFromJsonBytes(preBytes, "en-US")
|
||||||
|
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 := tbget.GetRuntimePair()
|
||||||
|
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, hostname, -1), nil
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
15
serve/serve_test.go
Normal file
15
serve/serve_test.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package tbserve
|
||||||
|
|
||||||
|
import (
|
||||||
|
tbget "i2pgit.org/idk/i2p.plugins.tor-manager/get"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestServe(t *testing.T) {
|
||||||
|
tbget.DOWNLOAD_PATH = "../tor-browser"
|
||||||
|
bytes, err := GenerateMirrorJSON("http://localhost:8080", "en-US")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
t.Log(string(bytes))
|
||||||
|
}
|
1
tor-browser/downloads.json
Normal file
1
tor-browser/downloads.json
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user