summaryrefslogtreecommitdiff
path: root/pkg/motd/fetch.go
blob: fd76d7c8e9472a84a330a216f912320b15a18c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package motd

import (
	"io/ioutil"
	"log"
	"net/http"
	"os"

	"0xacab.org/leap/bitmask-vpn/pkg/config"
)

const riseupMOTD = "https://static.riseup.net/vpn/motd.json"

func FetchLatest() []Message {
	empty := []Message{}
	if os.Getenv("SKIP_MOTD") == "1" {
		return empty
	}
	url := ""
	if os.Getenv("DEBUG") == "1" {
		url = os.Getenv("MOTD_URL")
		if url == "" {
			url = riseupMOTD
		}
	} else {
		switch config.Provider {
		case "riseup.net":
			url = riseupMOTD
		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)
}