ooni-probe-cli/internal/engine/experiment/webconnectivity/endpoints.go
Simone Basso d57c78bc71
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
2021-02-02 12:05:47 +01:00

78 lines
2.0 KiB
Go

package webconnectivity
import (
"net"
"net/url"
"github.com/ooni/probe-cli/v3/internal/engine/internal/runtimex"
)
// EndpointInfo describes a TCP/TLS endpoint.
type EndpointInfo struct {
String string // String representation
URLGetterURL string // URL for urlgetter
}
// EndpointsList is a list of EndpointInfo
type EndpointsList []EndpointInfo
// Endpoints returns a list of endpoints for TCP connect
func (el EndpointsList) Endpoints() (out []string) {
out = []string{}
for _, ei := range el {
out = append(out, ei.String)
}
return
}
// URLs returns a list of URLs for TCP urlgetter
func (el EndpointsList) URLs() (out []string) {
out = []string{}
for _, ei := range el {
out = append(out, ei.URLGetterURL)
}
return
}
// NewEndpoints creates a list of TCP/TLS endpoints to test from the
// target URL and the list of resolved IP addresses.
func NewEndpoints(URL *url.URL, addrs []string) (out EndpointsList) {
out = EndpointsList{}
port := NewEndpointPort(URL)
for _, addr := range addrs {
endpoint := net.JoinHostPort(addr, port.Port)
out = append(out, EndpointInfo{
String: endpoint,
URLGetterURL: (&url.URL{Scheme: port.URLGetterScheme, Host: endpoint}).String(),
})
}
return
}
// EndpointPort is the port to be used by a TCP/TLS endpoint.
type EndpointPort struct {
URLGetterScheme string
Port string
}
// NewEndpointPort creates an EndpointPort from the given URL. This function
// panic if the scheme is not `http` or `https` as well as if the host is not
// valid. The latter should not happen if you used url.Parse.
func NewEndpointPort(URL *url.URL) (out EndpointPort) {
if URL.Scheme != "http" && URL.Scheme != "https" {
panic("passed an unexpected scheme")
}
switch URL.Scheme {
case "http":
out.URLGetterScheme, out.Port = "tcpconnect", "80"
case "https":
out.URLGetterScheme, out.Port = "tlshandshake", "443"
}
if URL.Host != URL.Hostname() {
_, port, err := net.SplitHostPort(URL.Host)
runtimex.PanicOnError(err, "SplitHostPort should not fail here")
out.Port = port
}
return
}