diff options
author | kali kaneko (leap communications) <kali@leap.se> | 2021-11-24 16:18:51 +0100 |
---|---|---|
committer | kali kaneko (leap communications) <kali@leap.se> | 2021-11-24 20:00:11 +0100 |
commit | 4fca210846b28caf1372edb5ec0abe8193d3ff8b (patch) | |
tree | bde7c869b580d1527c7c09bd85d25dabf01a92e7 /pkg/motd/fetch.go | |
parent | 7d10b4ce74fe9e8a78689d95824fc3bfc37eb469 (diff) |
[feat] hook motd during bootstrap
some refactor, plus fix docs
Diffstat (limited to 'pkg/motd/fetch.go')
-rw-r--r-- | pkg/motd/fetch.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/pkg/motd/fetch.go b/pkg/motd/fetch.go new file mode 100644 index 0000000..4f22388 --- /dev/null +++ b/pkg/motd/fetch.go @@ -0,0 +1,54 @@ +package motd + +import ( + "io/ioutil" + "log" + "net/http" + "os" + + "0xacab.org/leap/bitmask-vpn/pkg/config" +) + +func FetchLatest() []Message { + empty := []Message{} + if os.Getenv("SKIP_MOTD") == "1" { + return empty + } + url := "" + switch config.Provider { + case "riseup.net": + url = "https://downloads.leap.se/motd/riseup/motd.json" + default: + return empty + } + log.Println("Fetching MOTD for", config.Provider) + b, err := fetchURL(url) + if err != nil { + log.Println("WARN Error fetching json from", url) + return empty + } + allMsg, err := getFromJSON(b) + if err != nil { + log.Println("WARN Error parsing json from", url) + return empty + } + valid := empty[:] + if allMsg.Length() != 0 { + log.Printf("There are %d pending messages\n", allMsg.Length()) + } + for _, msg := range allMsg.Messages { + if msg.IsValid() { + valid = append(valid, msg) + } + } + return valid +} + +func fetchURL(url string) ([]byte, error) { + resp, err := http.Get(url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + return ioutil.ReadAll(resp.Body) +} |