transport.go 6.21 KB
Newer Older
1
package irma
2
3
4

import (
	"bytes"
5
	"crypto/sha256"
6
7
8
	"encoding/json"
	"io"
	"io/ioutil"
9
	"log"
10
	"net"
11
	"net/http"
12
	"path/filepath"
13
14
	"strings"
	"time"
15

16
	"github.com/go-errors/errors"
17
	"github.com/hashicorp/go-retryablehttp"
18
	"github.com/sirupsen/logrus"
19

20
	"github.com/privacybydesign/irmago/internal/disable_sigpipe"
21
	"github.com/privacybydesign/irmago/internal/fs"
22
23
)

Sietse Ringers's avatar
Sietse Ringers committed
24
// HTTPTransport sends and receives JSON messages to a HTTP server.
25
type HTTPTransport struct {
Sietse Ringers's avatar
Sietse Ringers committed
26
	Server  string
27
	client  *retryablehttp.Client
Sietse Ringers's avatar
Sietse Ringers committed
28
	headers map[string]string
29
30
}

31
32
// Logger is used for logging. If not set, init() will initialize it to logrus.StandardLogger().
var Logger *logrus.Logger
Sietse Ringers's avatar
Sietse Ringers committed
33

34
35
36
var transportlogger *log.Logger

func init() {
37
38
	if Logger == nil {
		Logger = logrus.StandardLogger()
39
40
41
	}
}

Sietse Ringers's avatar
Sietse Ringers committed
42
// NewHTTPTransport returns a new HTTPTransport.
43
func NewHTTPTransport(serverURL string) *HTTPTransport {
44
45
	if Logger.IsLevelEnabled(logrus.TraceLevel) {
		transportlogger = log.New(Logger.WriterLevel(logrus.TraceLevel), "transport: ", 0)
46
47
	} else {
		transportlogger = log.New(ioutil.Discard, "", 0)
48
49
	}

50
	url := serverURL
51
	if serverURL != "" && !strings.HasSuffix(url, "/") { // TODO fix this
52
53
		url += "/"
	}
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

	// Create a transport that dials with a SIGPIPE handler (which is only active on iOS)
	var innerTransport http.Transport

	innerTransport.Dial = func(network, addr string) (c net.Conn, err error) {
		c, err = net.Dial(network, addr)
		if err != nil {
			return c, err
		}
		if err = disable_sigpipe.DisableSigPipe(c); err != nil {
			return c, err
		}
		return c, nil
	}

69
70
71
72
	client := retryablehttp.NewClient()
	client.RetryMax = 3
	client.RetryWaitMin = 100 * time.Millisecond
	client.RetryWaitMax = 500 * time.Millisecond
73
	client.Logger = transportlogger
74
75
76
77
78
	client.HTTPClient = &http.Client{
		Timeout:   time.Second * 5,
		Transport: &innerTransport,
	}

Sietse Ringers's avatar
Sietse Ringers committed
79
80
81
	return &HTTPTransport{
		Server:  url,
		headers: map[string]string{},
82
		client:  client,
Sietse Ringers's avatar
Sietse Ringers committed
83
	}
84
85
}

86
// SetHeader sets a header to be sent in requests.
Sietse Ringers's avatar
Sietse Ringers committed
87
88
89
90
func (transport *HTTPTransport) SetHeader(name, val string) {
	transport.headers[name] = val
}

91
92
93
func (transport *HTTPTransport) request(
	url string, method string, reader io.Reader, isstr bool,
) (response *http.Response, err error) {
94
95
	var req retryablehttp.Request
	req.Request, err = http.NewRequest(method, transport.Server+url, reader)
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
	if err != nil {
		return nil, &SessionError{ErrorType: ErrorTransport, Err: err}
	}

	req.Header.Set("User-Agent", "irmago")
	if reader != nil {
		if isstr {
			req.Header.Set("Content-Type", "text/plain; charset=UTF-8")
		} else {
			req.Header.Set("Content-Type", "application/json; charset=UTF-8")
		}
	}
	for name, val := range transport.headers {
		req.Header.Set(name, val)
	}

112
	res, err := transport.client.Do(&req)
113
114
115
116
117
118
119
	if err != nil {
		return nil, &SessionError{ErrorType: ErrorTransport, Err: err}
	}
	return res, nil
}

func (transport *HTTPTransport) jsonRequest(url string, method string, result interface{}, object interface{}) error {
120
	if method != http.MethodPost && method != http.MethodGet && method != http.MethodDelete {
121
122
123
124
125
126
		panic("Unsupported HTTP method " + method)
	}
	if method == http.MethodGet && object != nil {
		panic("Cannot GET and also post an object")
	}

127
	var isstr bool
128
129
	var reader io.Reader
	if object != nil {
130
131
132
133
134
135
		var objstr string
		if objstr, isstr = object.(string); isstr {
			reader = bytes.NewBuffer([]byte(objstr))
		} else {
			marshaled, err := json.Marshal(object)
			if err != nil {
Tomas's avatar
Tomas committed
136
				return &SessionError{ErrorType: ErrorSerialization, Err: err}
137
			}
138
			Logger.Debugf("%s %s: %s\n", method, url, string(marshaled))
139
			reader = bytes.NewBuffer(marshaled)
140
		}
141
	} else {
142
		Logger.Debugf("%s %s\n", method, url)
143
144
	}

145
	res, err := transport.request(url, method, reader, isstr)
146
	if err != nil {
147
		return err
148
	}
149
150
151
152
	if method == http.MethodDelete {
		return nil
	}

153
154
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
Tomas's avatar
Tomas committed
155
		return &SessionError{ErrorType: ErrorServerResponse, Err: err, RemoteStatus: res.StatusCode}
156
157
	}
	if res.StatusCode != 200 {
Tomas's avatar
Tomas committed
158
		apierr := &RemoteError{}
Sietse Ringers's avatar
Sietse Ringers committed
159
160
		err = json.Unmarshal(body, apierr)
		if err != nil || apierr.ErrorName == "" { // Not an ApiErrorMessage
Tomas's avatar
Tomas committed
161
			return &SessionError{ErrorType: ErrorServerResponse, RemoteStatus: res.StatusCode}
162
		}
163
		Logger.Debugf("ERROR: %+v\n", apierr)
Tomas's avatar
Tomas committed
164
		return &SessionError{ErrorType: ErrorApi, RemoteStatus: res.StatusCode, RemoteError: apierr}
165
166
	}

167
	Logger.Debugf("RESPONSE: %s\n", string(body))
168
169
170
171
172
	if _, resultstr := result.(*string); resultstr {
		*result.(*string) = string(body)
	} else {
		err = json.Unmarshal(body, result)
		if err != nil {
Tomas's avatar
Tomas committed
173
			return &SessionError{ErrorType: ErrorServerResponse, Err: err, RemoteStatus: res.StatusCode}
174
		}
175
176
177
178
179
	}

	return nil
}

180
181
182
183
184
func (transport *HTTPTransport) GetBytes(url string) ([]byte, error) {
	res, err := transport.request(url, http.MethodGet, nil, false)
	if err != nil {
		return nil, &SessionError{ErrorType: ErrorTransport, Err: err}
	}
185
186

	if res.StatusCode != 200 {
Tomas's avatar
Tomas committed
187
		return nil, &SessionError{ErrorType: ErrorServerResponse, RemoteStatus: res.StatusCode}
188
	}
189
190
	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
Tomas's avatar
Tomas committed
191
		return nil, &SessionError{ErrorType: ErrorServerResponse, Err: err, RemoteStatus: res.StatusCode}
192
193
194
195
	}
	return b, nil
}

196
func (transport *HTTPTransport) GetSignedFile(url string, dest string, hash ConfigurationFileHash) error {
197
198
199
200
	b, err := transport.GetBytes(url)
	if err != nil {
		return err
	}
201
202
203
204
	sha := sha256.Sum256(b)
	if hash != nil && !bytes.Equal(hash, sha[:]) {
		return errors.Errorf("Signature over new file %s is not valid", dest)
	}
205
	if err = fs.EnsureDirectoryExists(filepath.Dir(dest)); err != nil {
206
207
		return err
	}
208
	return fs.SaveFile(dest, b)
209
210
}

211
212
213
214
func (transport *HTTPTransport) GetFile(url string, dest string) error {
	return transport.GetSignedFile(url, dest, nil)
}

Sietse Ringers's avatar
Sietse Ringers committed
215
// Post sends the object to the server and parses its response into result.
Sietse Ringers's avatar
Sietse Ringers committed
216
func (transport *HTTPTransport) Post(url string, result interface{}, object interface{}) error {
217
	return transport.jsonRequest(url, http.MethodPost, result, object)
218
219
}

Sietse Ringers's avatar
Sietse Ringers committed
220
// Get performs a GET request and parses the server's response into result.
Sietse Ringers's avatar
Sietse Ringers committed
221
func (transport *HTTPTransport) Get(url string, result interface{}) error {
222
	return transport.jsonRequest(url, http.MethodGet, result, nil)
223
224
}

Sietse Ringers's avatar
Sietse Ringers committed
225
// Delete performs a DELETE.
226
func (transport *HTTPTransport) Delete() {
227
	_ = transport.jsonRequest("", http.MethodDelete, nil, nil)
228
}