chore: merge probe-engine into probe-cli (#201)
This is how I did it: 1. `git clone https://github.com/ooni/probe-engine internal/engine` 2. ``` (cd internal/engine && git describe --tags) v0.23.0 ``` 3. `nvim go.mod` (merging `go.mod` with `internal/engine/go.mod` 4. `rm -rf internal/.git internal/engine/go.{mod,sum}` 5. `git add internal/engine` 6. `find . -type f -name \*.go -exec sed -i 's@/ooni/probe-engine@/ooni/probe-cli/v3/internal/engine@g' {} \;` 7. `go build ./...` (passes) 8. `go test -race ./...` (temporary failure on RiseupVPN) 9. `go mod tidy` 10. this commit message Once this piece of work is done, we can build a new version of `ooniprobe` that is using `internal/engine` directly. We need to do more work to ensure all the other functionality in `probe-engine` (e.g. making mobile packages) are still WAI. Part of https://github.com/ooni/probe/issues/1335
This commit is contained in:
parent
b1ce300c8d
commit
d57c78bc71
535 changed files with 66182 additions and 23 deletions
74
internal/engine/experiment/dash/download.go
Normal file
74
internal/engine/experiment/dash/download.go
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
package dash
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type downloadDeps interface {
|
||||
HTTPClient() *http.Client
|
||||
NewHTTPRequest(method string, url string, body io.Reader) (*http.Request, error)
|
||||
ReadAll(r io.Reader) ([]byte, error)
|
||||
Scheme() string
|
||||
UserAgent() string
|
||||
}
|
||||
|
||||
type downloadConfig struct {
|
||||
authorization string
|
||||
begin time.Time
|
||||
currentRate int64
|
||||
deps downloadDeps
|
||||
elapsedTarget int64
|
||||
fqdn string
|
||||
}
|
||||
|
||||
type downloadResult struct {
|
||||
elapsed float64
|
||||
received int64
|
||||
requestTicks float64
|
||||
serverURL string
|
||||
timestamp int64
|
||||
}
|
||||
|
||||
func download(ctx context.Context, config downloadConfig) (downloadResult, error) {
|
||||
nbytes := (config.currentRate * 1000 * config.elapsedTarget) >> 3
|
||||
var URL url.URL
|
||||
URL.Scheme = config.deps.Scheme()
|
||||
URL.Host = config.fqdn
|
||||
URL.Path = fmt.Sprintf("%s%d", downloadPath, nbytes)
|
||||
req, err := config.deps.NewHTTPRequest("GET", URL.String(), nil)
|
||||
var result downloadResult
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.serverURL = URL.String()
|
||||
req.Header.Set("User-Agent", config.deps.UserAgent())
|
||||
req.Header.Set("Authorization", config.authorization)
|
||||
savedTicks := time.Now()
|
||||
resp, err := config.deps.HTTPClient().Do(req.WithContext(ctx))
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
return result, errHTTPRequestFailed
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err := config.deps.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
// Implementation note: MK contains a comment that says that Neubot uses
|
||||
// the elapsed time since when we start receiving the response but it
|
||||
// turns out that Neubot and MK do the same. So, we do what they do. At
|
||||
// the same time, we are currently not able to include the overhead that
|
||||
// is caused by HTTP headers etc. So, we're a bit less precise.
|
||||
result.elapsed = time.Now().Sub(savedTicks).Seconds()
|
||||
result.received = int64(len(data))
|
||||
result.requestTicks = savedTicks.Sub(config.begin).Seconds()
|
||||
result.timestamp = time.Now().Unix()
|
||||
return result, nil
|
||||
}
|
||||
Loading…
Reference in a new issue