summaryrefslogtreecommitdiff
path: root/src/server.go
blob: ca130d082033d190a314a8e84b3aa9f6676edb45 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package eduvpn

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func getFileUrl(url string) ([]byte, error) {
	// Do a Get request to the specified url
	resp, reqErr := http.Get(url)
	if reqErr != nil {
		return nil, &HTTPResourceError{URL: url, Err: reqErr}
	}
	// Close the response body at the end
	defer resp.Body.Close()

	// Check if http response code is ok
	if resp.StatusCode != http.StatusOK {
		return nil, &HTTPStatusError{URL: url, Status: resp.StatusCode}
	}
	// Read the body
	body, readErr := ioutil.ReadAll(resp.Body)
	if readErr != nil {
		return nil, &HTTPReadError{URL: url, Err: readErr}
	}
	return body, nil
}

type DiscoFileError struct {
	URL string
	Err error
}

func (e *DiscoFileError) Error() string {
	return fmt.Sprintf("failed obtaining disco file %s with error %v", e.URL, e.Err)
}

type DiscoSigFileError struct {
	URL string
	Err error
}

func (e *DiscoSigFileError) Error() string {
	return fmt.Sprintf("failed obtaining disco signature file %s with error %v", e.URL, e.Err)
}

type DiscoVerifyError struct {
	File    string
	Sigfile string
	Err     error
}

func (e *DiscoVerifyError) Error() string {
	return fmt.Sprintf("failed verifying file %s with signature %s due to error %v", e.File, e.Sigfile, e.Err)
}

// Helper function that gets a disco json
func getDiscoFile(jsonFile string) (string, error) {
	// Get json data
	discoURL := "https://disco.eduvpn.org/v2/"
	fileURL := discoURL + jsonFile
	fileBody, fileErr := getFileUrl(fileURL)

	if fileErr != nil {
		return "", &DiscoFileError{fileURL, fileErr}
	}

	// Get signature
	sigFile := jsonFile + ".minisig"
	sigURL := discoURL + sigFile
	sigBody, sigFileErr := getFileUrl(sigURL)

	if sigFileErr != nil {
		return "", &DiscoSigFileError{URL: sigURL, Err: sigFileErr}
	}

	// Verify signature
	// TODO: Handle this by keeping track of the previous sign time
	// Wrappers must do this?
	var previousSigTime uint64 = 0
	forcePrehash := false
	verifySuccess, verifyErr := Verify(string(sigBody), fileBody, jsonFile, previousSigTime, forcePrehash)

	if !verifySuccess || verifyErr != nil {
		return "", &DiscoVerifyError{File: jsonFile, Sigfile: sigFile, Err: verifyErr}
	}

	return string(fileBody), nil
}

type GetListError struct {
	File string
	Err  error
}

func (e *GetListError) Error() string {
	return fmt.Sprintf("failed getting disco list file %s with error %v", e.File, e.Err)
}

// Get the organization list
func GetOrganizationsList() (string, error) {
	file := "organization_list.json"
	body, err := getDiscoFile(file)
	if err != nil {
		return "", &GetListError{File: file, Err: err}
	}
	return body, nil
}

// Get the server list
func GetServersList() (string, error) {
	file := "server_list.json"
	body, err := getDiscoFile("server_list.json")
	if err != nil {
		return "", &GetListError{File: file, Err: err}
	}
	return body, nil
}