99b28c1d95
* refactor: start building an Android package Part of https://github.com/ooni/probe/issues/1335. This seems also a good moment to move some packages out of the engine, e.g., oonimkall. This package, for example, is a consumer of the engine, so it makes sense it's not _inside_ it. * fix: committed some stuff I didn't need to commit * fix: oonimkall needs to be public to build The side effect is that we will probably need to bump the major version number every time we change one of these APIs. (We can also of course choose to violate the basic guidelines of Go software, but I believe this is bad form.) I have no problem in bumping the major quite frequently and in any case this monorepo solution is convinving me more than continuing to keep a split between engine and cli. The need to embed assets to make the probe more reliable trumps the negative effects of having to ~frequently bump major because we expose a public API. * fix: let's not forget about libooniffi Honestly, I don't know what to do with this library. I added it to provide a drop in replacement for MK but I have no idea whether it's used and useful. I would not feel comfortable exposing it, unlike oonimkall, since we're not using it. It may be that the right thing to do here is just to delete the package and reduce the amount of code we're maintaining? * woops, we're still missing the publish android script * fix(publish-android.bash): add proper API key * ouch fix another place where the name changed
78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
package webconnectivity
|
|
|
|
import (
|
|
"net"
|
|
"net/url"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/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
|
|
}
|