~welt/murse

ref: signing murse/client.go -rw-r--r-- 2.4 KiB
408fd8d8welt Fix garbage JSON when wrapping revisions. 2 months ago
                                                                                
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
120
121
122
123
package main

import (
	"crypto/ecdsa"
	"crypto/tls"
	"encoding/json"
	"errors"
	"io"
	"net/http"
	"strconv"
	"strings"
)

// If it returns a Reader, it means it has not been verified.
type Client struct {
	key  *ecdsa.PublicKey
	repo string
	h    *http.Client
}

func NewClient(repo string, key *ecdsa.PublicKey) *Client {
	return &Client{
		repo: repo,
		h:    &http.Client{},
		key:  key,
	}
}

func (c *Client) DisableHTTP2() {
	c.h.Transport = &http.Transport{
		TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
	}
}

func (c *Client) get(url string) (io.ReadCloser, error) {
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		return nil, err
	}

	req.Header.Add("User-Agent", "murse/"+VERSION)

	resp, err := c.h.Do(req)
	if err != nil {
		return nil, err
	}

	if resp.StatusCode != 200 {
		return resp.Body, errors.New("got status code " + strconv.Itoa(resp.StatusCode))
	}

	return resp.Body, nil
}

func (c *Client) getRead(url string) ([]byte, error) {
	var bytes []byte
	body, err := c.get(url)
	if err != nil {
		return bytes, err
	}

	return io.ReadAll(body)
}

func (c *Client) GetRevision(v int) ([]Change, error) {
	object, err := c.getRead(c.repo + "revisions/" + strconv.Itoa(v))
	if err != nil {
		return nil, err
	}

	if c.key != nil {
		sig, err := c.getRead(c.repo + "revisions/" + strconv.Itoa(v) + ".sig")
		if err != nil {
			return nil, err
		}

		ok := verifyContents(c.key, object, sig)
		if !ok {
			return nil, errors.New("failed cryptographic check")
		}
	}

	var wrappedRevision WrappedRevision

	err = json.Unmarshal(object, &wrappedRevision)
	if err != nil {
		if c.key == nil {
			var unwrappedRevision []Change

			err = json.Unmarshal(object, &unwrappedRevision)
			if err != nil {
				return nil, err
			}

			return unwrappedRevision, nil
		}

		return nil, err
	}

	if wrappedRevision.No != v {
		return nil, errors.New("invalid revision sent by server")
	}

	return wrappedRevision.Changes, err
}

func (c *Client) GetObject(object string) (io.ReadCloser, error) {
	return c.get(c.repo + "objects/" + object)
}

func (c *Client) GetObjectSig(object string) ([]byte, error) {
	return c.getRead(c.repo + "objects/" + object + ".sig")
}

func (c *Client) GetLatestRevision() (int, error) {
	b, err := c.getRead(c.repo + "revisions/latest")
	if err != nil {
		return -1, err
	}

	return strconv.Atoi(strings.TrimSpace(string(b)))
}