embed the content in the binary and unpack it if content is not available

This commit is contained in:
idk
2023-01-27 22:36:17 +00:00
parent 2a676d60eb
commit 1563e66dd5
22 changed files with 27 additions and 6 deletions

4
go.mod
View File

@ -1,6 +1,6 @@
module i2pgit.org/idk/reseed-tools
go 1.13
go 1.16
require (
github.com/cretz/bine v0.2.0
@ -8,6 +8,7 @@ require (
github.com/eyedeekay/go-i2pd v0.0.0-20220213070306-9807541b2dfc
github.com/eyedeekay/i2pkeys v0.33.0
github.com/eyedeekay/sam3 v0.33.5
github.com/eyedeekay/unembed v0.0.0-20230123014222-9916b121855b
github.com/go-acme/lego/v4 v4.3.1
github.com/gorilla/handlers v1.5.1
github.com/justinas/alice v1.2.0
@ -30,4 +31,3 @@ replace github.com/libp2p/go-libp2p-gostream => github.com/libp2p/go-libp2p-gost
replace github.com/libp2p/go-libp2p-http => github.com/libp2p/go-libp2p-http v0.2.0
//replace github.com/eyedeekay/go-i2pd v0.0.0-20220213070306-9807541b2dfc => ./go-i2pd

4
go.sum
View File

@ -151,6 +151,8 @@ github.com/eyedeekay/sam3 v0.32.2/go.mod h1:Y3igFVzN4ybqkkpfUWULGhw7WRp8lieq0ORX
github.com/eyedeekay/sam3 v0.32.32/go.mod h1:qRA9KIIVxbrHlkj+ZB+OoxFGFgdKeGp1vSgPw26eOVU=
github.com/eyedeekay/sam3 v0.33.5 h1:mY2MmEG4W35AOpG/G7DOdAhFZWRwFxlm+NmIoub1Xnw=
github.com/eyedeekay/sam3 v0.33.5/go.mod h1:sPtlI4cRm7wD0UywOzLPvvdY1G++vBSK3n+jiIGqWlU=
github.com/eyedeekay/unembed v0.0.0-20230123014222-9916b121855b h1:QyCSwbHpkJtKGvIvHsvvlbDkf7/3a8qUlaa4rEr8myQ=
github.com/eyedeekay/unembed v0.0.0-20230123014222-9916b121855b/go.mod h1:A6dZU88muI132XMrmdM0+cc2yIuwmhwgRfyrU54DjPc=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
@ -219,7 +221,6 @@ github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:x
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
@ -1082,7 +1083,6 @@ google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 116 KiB

After

Width:  |  Height:  |  Size: 116 KiB

View File

@ -1,6 +1,8 @@
package reseed
import (
"embed"
_ "embed"
"io/ioutil"
"log"
"net/http"
@ -8,10 +10,14 @@ import (
"path/filepath"
"strings"
"github.com/eyedeekay/unembed"
"gitlab.com/golang-commonmark/markdown"
"golang.org/x/text/language"
)
//go:embed content
var f embed.FS
var SupportedLanguages = []language.Tag{
language.English,
language.Russian,
@ -30,7 +36,17 @@ var SupportedLanguages = []language.Tag{
var CachedLanguagePages = map[string]string{}
var CachedDataPages = map[string][]byte{}
var BaseContentPath, ContentPathError = ContentPath()
func StableContentPath() (string, error) {
var BaseContentPath, ContentPathError = ContentPath()
if _, err := os.Stat(BaseContentPath); os.IsNotExist(err) {
if err := unembed.Unembed(f, BaseContentPath); err != nil {
return "", err
} else {
return BaseContentPath, nil
}
}
return BaseContentPath, ContentPathError
}
var matcher = language.NewMatcher(SupportedLanguages)
@ -61,6 +77,7 @@ func ContentPath() (string, error) {
}
func (srv *Server) HandleARealBrowser(w http.ResponseWriter, r *http.Request) {
_, ContentPathError := StableContentPath()
if ContentPathError != nil {
http.Error(w, "403 Forbidden", http.StatusForbidden)
return
@ -115,6 +132,7 @@ func (srv *Server) HandleARealBrowser(w http.ResponseWriter, r *http.Request) {
}
func HandleAFile(w http.ResponseWriter, dirPath, file string) {
BaseContentPath, _ := StableContentPath()
file = filepath.Join(dirPath, file)
if _, prs := CachedDataPages[file]; !prs {
path := filepath.Join(BaseContentPath, file)
@ -132,6 +150,7 @@ func HandleAFile(w http.ResponseWriter, dirPath, file string) {
func HandleALocalizedFile(w http.ResponseWriter, dirPath string) {
if _, prs := CachedLanguagePages[dirPath]; !prs {
BaseContentPath, _ := StableContentPath()
dir := filepath.Join(BaseContentPath, "lang", dirPath)
files, err := ioutil.ReadDir(dir)
if err != nil {

View File

@ -40,6 +40,7 @@ func PingWriteContent(url string) error {
path := strings.Replace(url, "http://", "", 1)
path = strings.Replace(path, "https://", "", 1)
path = strings.Replace(path, "/", "", -1)
BaseContentPath, _ := StableContentPath()
path = filepath.Join(BaseContentPath, path+"-"+date+".ping")
if _, err := os.Stat(path); err != nil {
result, err := Ping(url)
@ -85,6 +86,7 @@ func PingEverybody() []string {
func GetPingFiles() ([]string, error) {
var files []string
date := time.Now().Format("2006-01-02")
BaseContentPath, _ := StableContentPath()
err := filepath.Walk(BaseContentPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@ -105,7 +107,7 @@ func ReadOut(w http.ResponseWriter) {
if err == nil {
fmt.Fprintf(w, "<h3>Reseed Server Statuses</h3>")
fmt.Fprintf(w, "<div><p>This feature is experimental and may not always provide accurate results.</p></div>")
fmt.Fprintf(w, "</div><p><ul>")
fmt.Fprintf(w, "<div><p><ul>")
for _, file := range pinglist {
ping, err := ioutil.ReadFile(file)
host := strings.Replace(file, ".ping", "", 1)