From ce9aa9ab67b962b7250c4b0db84e4151bca29475 Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Tue, 16 Nov 2021 20:36:25 +0100 Subject: parse motd from remote url --- branding/motd-cli/README.md | 2 ++ branding/motd-cli/main.go | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/branding/motd-cli/README.md b/branding/motd-cli/README.md index 926a982..5d00f10 100644 --- a/branding/motd-cli/README.md +++ b/branding/motd-cli/README.md @@ -54,6 +54,8 @@ Urgency: normal ✓ Languages: 2 ✓ ``` +Use `motd-cli -url https://example.com/motd.json` to validate a remote file. + Notes: I'm considering adding an explicit layer of verification of the motd payload. Please comment on [#554](https://0xacab.org/leap/bitmask-vpn/-/issues/554) if you have an opinion diff --git a/branding/motd-cli/main.go b/branding/motd-cli/main.go index 1c9191c..6828cc9 100644 --- a/branding/motd-cli/main.go +++ b/branding/motd-cli/main.go @@ -4,7 +4,9 @@ import ( "encoding/json" "flag" "fmt" + "io" "io/ioutil" + "net/http" "os" ) @@ -86,15 +88,22 @@ type LocalizedText struct { } func main() { - // TODO pass url flag too, to fetch and validate remote file file := flag.String("file", "", "file to validate") + url := flag.String("url", "", "url to validate") flag.Parse() + f := *file - if f == "" { - f = defaultFile - } + u := *url - fmt.Println("file:", f) + if u != "" { + fmt.Println("url:", u) + f = downloadToTempFile(u) + } else { + if f == "" { + f = defaultFile + } + fmt.Println("file:", f) + } m := parseFile(f) fmt.Printf("count: %v\n", m.Length()) fmt.Println() @@ -110,6 +119,24 @@ func main() { } } +func downloadToTempFile(url string) string { + resp, err := http.Get(url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + + out, err := ioutil.TempFile("/tmp/", "motd-linter") + if err != nil { + panic(err) + } + defer out.Close() + + _, _ = io.Copy(out, resp.Body) + fmt.Println("File downloaded to", out.Name()) + return out.Name() +} + func parseFile(f string) Messages { jsonFile, err := os.Open(f) if err != nil { -- cgit v1.2.3