2021-02-02 12:05:47 +01:00
|
|
|
package dash
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type collectDeps interface {
|
|
|
|
HTTPClient() *http.Client
|
|
|
|
JSONMarshal(v interface{}) ([]byte, error)
|
|
|
|
Logger() model.Logger
|
|
|
|
NewHTTPRequest(method string, url string, body io.Reader) (*http.Request, error)
|
2021-06-15 11:57:40 +02:00
|
|
|
ReadAllContext(ctx context.Context, r io.Reader) ([]byte, error)
|
2021-02-02 12:05:47 +01:00
|
|
|
Scheme() string
|
|
|
|
UserAgent() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func collect(ctx context.Context, fqdn, authorization string,
|
|
|
|
results []clientResults, deps collectDeps) error {
|
|
|
|
data, err := deps.JSONMarshal(results)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
deps.Logger().Debugf("dash: body: %s", string(data))
|
|
|
|
var URL url.URL
|
|
|
|
URL.Scheme = deps.Scheme()
|
|
|
|
URL.Host = fqdn
|
|
|
|
URL.Path = collectPath
|
|
|
|
req, err := deps.NewHTTPRequest("POST", URL.String(), bytes.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("User-Agent", deps.UserAgent())
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Authorization", authorization)
|
|
|
|
resp, err := deps.HTTPClient().Do(req.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return errHTTPRequestFailed
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2021-06-15 11:57:40 +02:00
|
|
|
data, err = deps.ReadAllContext(ctx, resp.Body)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
deps.Logger().Debugf("dash: body: %s", string(data))
|
|
|
|
var serverResults []serverResults
|
|
|
|
return json.Unmarshal(data, &serverResults)
|
|
|
|
}
|