2021-02-02 12:05:47 +01:00
|
|
|
package urlgetter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-06-25 12:39:45 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2022-06-02 00:50:55 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/tracex"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// The Configurer job is to construct a Configuration that can
|
|
|
|
// later be used by the measurer to perform measurements.
|
|
|
|
type Configurer struct {
|
|
|
|
Config Config
|
|
|
|
Logger model.Logger
|
|
|
|
ProxyURL *url.URL
|
2022-05-31 21:53:01 +02:00
|
|
|
Saver *tracex.Saver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The Configuration is the configuration for running a measurement.
|
|
|
|
type Configuration struct {
|
|
|
|
HTTPConfig netx.Config
|
2022-01-10 11:53:06 +01:00
|
|
|
DNSClient model.Resolver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloseIdleConnections will close idle connections, if needed.
|
|
|
|
func (c Configuration) CloseIdleConnections() {
|
|
|
|
c.DNSClient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfiguration builds a new measurement configuration.
|
|
|
|
func (c Configurer) NewConfiguration() (Configuration, error) {
|
|
|
|
// set up defaults
|
|
|
|
configuration := Configuration{
|
|
|
|
HTTPConfig: netx.Config{
|
|
|
|
BogonIsError: c.Config.RejectDNSBogons,
|
|
|
|
CacheResolutions: true,
|
|
|
|
ContextByteCounting: true,
|
|
|
|
HTTP3Enabled: c.Config.HTTP3Enabled,
|
|
|
|
Logger: c.Logger,
|
|
|
|
ReadWriteSaver: c.Saver,
|
2022-06-02 18:18:49 +02:00
|
|
|
Saver: c.Saver,
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
// fill DNS cache
|
|
|
|
if c.Config.DNSCache != "" {
|
|
|
|
entry := strings.Split(c.Config.DNSCache, " ")
|
|
|
|
if len(entry) < 2 {
|
|
|
|
return configuration, errors.New("invalid DNSCache string")
|
|
|
|
}
|
|
|
|
domainregex := regexp.MustCompile(`^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$`)
|
|
|
|
if !domainregex.MatchString(entry[0]) {
|
|
|
|
return configuration, errors.New("invalid domain in DNSCache")
|
|
|
|
}
|
|
|
|
var addresses []string
|
|
|
|
for i := 1; i < len(entry); i++ {
|
|
|
|
if net.ParseIP(entry[i]) == nil {
|
|
|
|
return configuration, errors.New("invalid IP in DNSCache")
|
|
|
|
}
|
|
|
|
addresses = append(addresses, entry[i])
|
|
|
|
}
|
|
|
|
configuration.HTTPConfig.DNSCache = map[string][]string{
|
|
|
|
entry[0]: addresses,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dnsclient, err := netx.NewDNSClientWithOverrides(
|
|
|
|
configuration.HTTPConfig, c.Config.ResolverURL,
|
|
|
|
c.Config.DNSHTTPHost, c.Config.DNSTLSServerName,
|
|
|
|
c.Config.DNSTLSVersion,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return configuration, err
|
|
|
|
}
|
|
|
|
configuration.DNSClient = dnsclient
|
2022-01-10 11:53:06 +01:00
|
|
|
configuration.HTTPConfig.BaseResolver = dnsclient
|
2021-02-02 12:05:47 +01:00
|
|
|
// configure TLS
|
|
|
|
configuration.HTTPConfig.TLSConfig = &tls.Config{
|
|
|
|
NextProtos: []string{"h2", "http/1.1"},
|
|
|
|
}
|
|
|
|
if c.Config.TLSServerName != "" {
|
|
|
|
configuration.HTTPConfig.TLSConfig.ServerName = c.Config.TLSServerName
|
|
|
|
}
|
2021-06-25 12:39:45 +02:00
|
|
|
err = netxlite.ConfigureTLSVersion(
|
2021-02-02 12:05:47 +01:00
|
|
|
configuration.HTTPConfig.TLSConfig, c.Config.TLSVersion,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return configuration, err
|
|
|
|
}
|
2022-06-02 18:18:49 +02:00
|
|
|
configuration.HTTPConfig.TLSConfig.InsecureSkipVerify = c.Config.NoTLSVerify
|
|
|
|
configuration.HTTPConfig.TLSConfig.RootCAs = c.Config.CertPool
|
2021-02-02 12:05:47 +01:00
|
|
|
// configure proxy
|
|
|
|
configuration.HTTPConfig.ProxyURL = c.ProxyURL
|
|
|
|
return configuration, nil
|
|
|
|
}
|