forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxoonirun.go
76 lines (63 loc) · 1.84 KB
/
xoonirun.go
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
package oonimkall
//
// eXperimental OONI Run code.
//
import (
"errors"
"fmt"
"net/http"
"net/url"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// OONIRunFetch fetches a given OONI run descriptor.
//
// The ID argument is the unique identifier of the OONI Run link. For example, in:
//
// https://api.ooni.io/api/_/ooni_run/fetch/297500125102
//
// The OONI Run link ID is 297500125102.
//
// Warning: this API is currently experimental and we only expose it to facilitate
// developing OONI Run v2. Do not use this API in production.
func (sess *Session) OONIRunFetch(ctx *Context, ID int64) (string, error) {
sess.mtx.Lock()
defer sess.mtx.Unlock()
// TODO(bassosimone): this code should be changed to use the probeservices.Client
// rather than using an hardcoded URL once we switch to production code. Until then,
// we are going to use the test backend server.
// For example: https://ams-pg-test.ooni.org/api/_/ooni_run/fetch/297500125102
URL := &url.URL{
Scheme: "https",
Opaque: "",
User: nil,
Host: "ams-pg-test.ooni.org",
Path: fmt.Sprintf("/api/_/ooni_run/fetch/%d", ID),
RawPath: "",
OmitHost: false,
ForceQuery: false,
RawQuery: "",
Fragment: "",
RawFragment: "",
}
return sess.ooniRunFetchWithURLLocked(ctx, URL)
}
func (sess *Session) ooniRunFetchWithURLLocked(ctx *Context, URL *url.URL) (string, error) {
clnt := sess.sessp.DefaultHTTPClient()
req, err := http.NewRequestWithContext(ctx.ctx, "GET", URL.String(), nil)
if err != nil {
return "", err
}
resp, err := clnt.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errors.New("xoonirun: HTTP request failed")
}
rawResp, err := netxlite.ReadAllContext(ctx.ctx, resp.Body)
if err != nil {
return "", err
}
return string(rawResp), nil
}