2021-02-02 12:05:47 +01:00
|
|
|
// Package netx contains code to perform network measurements.
|
|
|
|
//
|
|
|
|
// This library contains replacements for commonly used standard library
|
|
|
|
// interfaces that facilitate seamless network measurements. By using
|
|
|
|
// such replacements, as opposed to standard library interfaces, we can:
|
|
|
|
//
|
|
|
|
// * save the timing of HTTP events (e.g. received response headers)
|
|
|
|
// * save the timing and result of every Connect, Read, Write, Close operation
|
|
|
|
// * save the timing and result of the TLS handshake (including certificates)
|
|
|
|
//
|
|
|
|
// By default, this library uses the system resolver. In addition, it
|
|
|
|
// is possible to configure alternative DNS transports and remote
|
|
|
|
// servers. We support DNS over UDP, DNS over TCP, DNS over TLS (DoT),
|
|
|
|
// and DNS over HTTPS (DoH). When using an alternative transport, we
|
|
|
|
// are also able to intercept and save DNS messages, as well as any
|
|
|
|
// other interaction with the remote server (e.g., the result of the
|
|
|
|
// TLS handshake for DoT and DoH).
|
|
|
|
//
|
|
|
|
// We described the design and implementation of the most recent version of
|
2021-02-26 10:16:34 +01:00
|
|
|
// this package at <https://github.com/ooni/probe-engine/issues/359>. Such
|
2021-02-02 12:05:47 +01:00
|
|
|
// issue also links to a previous design document.
|
|
|
|
package netx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2021-06-22 13:00:29 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/bytecounter"
|
2021-02-02 12:05:47 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/quicdialer"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
2021-06-08 11:24:13 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
2021-02-02 12:05:47 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
refactor: start pivoting netx (#396)
What do I mean by pivoting? Netx is currently organized by row:
```
| dialer | quicdialer | resolver | ...
saving | | | | ...
errorwrapping | | | | ...
logging | | | | ...
mocking/sys | | | | ...
```
Every row needs to implement saving, errorwrapping, logging, mocking (or
adapting to the system or to some underlying library).
This causes cross package dependencies and, in turn, complexity. For
example, we need the `trace` package for supporting saving.
And `dialer`, `quickdialer`, et al. need to depend on such a package.
The same goes for errorwrapping.
This arrangement further complicates testing. For example, I am
currently working on https://github.com/ooni/probe/issues/1505 and
I realize it need to repeat integration tests in multiple places.
Let's say instead we pivot the above matrix as follows:
```
| saving | errorwrapping | logging | ...
dialer | | | | ...
quicdialer | | | | ...
logging | | | | ...
mocking/sys | | | | ...
...
```
In this way, now every row contains everything related to a specific
action to perform. We can now share code without relying on extra
support packages. What's more, we can write tests and, judding from
the way in which things are made, it seems we only need integration
testing in `errorwrapping` because it's where data quality matters
whereas, in all other cases, unit testing is fine.
I am going, therefore, to proceed with these changes and "pivot"
`netx`. Hopefully, it won't be too painful.
2021-06-23 15:53:12 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains configuration for creating a new transport. When any
|
|
|
|
// field of Config is nil/empty, we will use a suitable default.
|
|
|
|
//
|
|
|
|
// We use different savers for different kind of events such that the
|
|
|
|
// user of this library can choose what to save.
|
|
|
|
type Config struct {
|
2022-01-07 18:33:37 +01:00
|
|
|
BaseResolver model.Resolver // default: system resolver
|
2021-02-02 12:05:47 +01:00
|
|
|
BogonIsError bool // default: bogon is not error
|
|
|
|
ByteCounter *bytecounter.Counter // default: no explicit byte counting
|
|
|
|
CacheResolutions bool // default: no caching
|
|
|
|
CertPool *x509.CertPool // default: use vendored gocertifi
|
|
|
|
ContextByteCounting bool // default: no implicit byte counting
|
|
|
|
DNSCache map[string][]string // default: cache is empty
|
|
|
|
DialSaver *trace.Saver // default: not saving dials
|
2022-01-07 18:33:37 +01:00
|
|
|
Dialer model.Dialer // default: dialer.DNSDialer
|
|
|
|
FullResolver model.Resolver // default: base resolver + goodies
|
|
|
|
QUICDialer model.QUICDialer // default: quicdialer.DNSDialer
|
2021-02-02 12:05:47 +01:00
|
|
|
HTTP3Enabled bool // default: disabled
|
|
|
|
HTTPSaver *trace.Saver // default: not saving HTTP
|
2022-01-03 13:53:23 +01:00
|
|
|
Logger model.DebugLogger // default: no logging
|
2021-02-02 12:05:47 +01:00
|
|
|
NoTLSVerify bool // default: perform TLS verify
|
|
|
|
ProxyURL *url.URL // default: no proxy
|
|
|
|
ReadWriteSaver *trace.Saver // default: not saving read/write
|
|
|
|
ResolveSaver *trace.Saver // default: not saving resolves
|
|
|
|
TLSConfig *tls.Config // default: attempt using h2
|
2022-01-07 18:33:37 +01:00
|
|
|
TLSDialer model.TLSDialer // default: dialer.TLSDialer
|
2021-02-02 12:05:47 +01:00
|
|
|
TLSSaver *trace.Saver // default: not saving TLS
|
|
|
|
}
|
|
|
|
|
|
|
|
type tlsHandshaker interface {
|
|
|
|
Handshake(ctx context.Context, conn net.Conn, config *tls.Config) (
|
|
|
|
net.Conn, tls.ConnectionState, error)
|
|
|
|
}
|
|
|
|
|
2021-06-25 12:39:45 +02:00
|
|
|
var defaultCertPool *x509.CertPool = netxlite.NewDefaultCertPool()
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
// NewResolver creates a new resolver from the specified config
|
2022-01-07 18:33:37 +01:00
|
|
|
func NewResolver(config Config) model.Resolver {
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.BaseResolver == nil {
|
2021-06-25 11:07:26 +02:00
|
|
|
config.BaseResolver = &netxlite.ResolverSystem{}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
var r model.Resolver = config.BaseResolver
|
|
|
|
r = &netxlite.AddressResolver{
|
|
|
|
Resolver: r,
|
2021-09-09 20:21:43 +02:00
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.CacheResolutions {
|
|
|
|
r = &resolver.CacheResolver{Resolver: r}
|
|
|
|
}
|
|
|
|
if config.DNSCache != nil {
|
|
|
|
cache := &resolver.CacheResolver{Resolver: r, ReadOnly: true}
|
|
|
|
for key, values := range config.DNSCache {
|
|
|
|
cache.Set(key, values)
|
|
|
|
}
|
|
|
|
r = cache
|
|
|
|
}
|
|
|
|
if config.BogonIsError {
|
|
|
|
r = resolver.BogonResolver{Resolver: r}
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
r = &netxlite.ErrorWrapperResolver{Resolver: r}
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.Logger != nil {
|
2021-09-05 18:03:50 +02:00
|
|
|
r = &netxlite.ResolverLogger{
|
|
|
|
Logger: config.Logger,
|
2022-01-07 18:33:37 +01:00
|
|
|
Resolver: r,
|
2021-09-05 18:03:50 +02:00
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
if config.ResolveSaver != nil {
|
|
|
|
r = resolver.SaverResolver{Resolver: r, Saver: config.ResolveSaver}
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
return &netxlite.ResolverIDNA{Resolver: r}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDialer creates a new Dialer from the specified config
|
2022-01-07 18:33:37 +01:00
|
|
|
func NewDialer(config Config) model.Dialer {
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.FullResolver == nil {
|
|
|
|
config.FullResolver = NewResolver(config)
|
|
|
|
}
|
2021-06-09 09:42:31 +02:00
|
|
|
return dialer.New(&dialer.Config{
|
|
|
|
ContextByteCounting: config.ContextByteCounting,
|
|
|
|
DialSaver: config.DialSaver,
|
|
|
|
Logger: config.Logger,
|
|
|
|
ProxyURL: config.ProxyURL,
|
|
|
|
ReadWriteSaver: config.ReadWriteSaver,
|
|
|
|
}, config.FullResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewQUICDialer creates a new DNS Dialer for QUIC, with the resolver from the specified config
|
2022-01-07 18:33:37 +01:00
|
|
|
func NewQUICDialer(config Config) model.QUICDialer {
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.FullResolver == nil {
|
|
|
|
config.FullResolver = NewResolver(config)
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
var ql model.QUICListener = &netxlite.QUICListenerStdlib{}
|
2022-01-07 17:31:21 +01:00
|
|
|
ql = &netxlite.ErrorWrapperQUICListener{QUICListener: ql}
|
2021-06-25 16:20:08 +02:00
|
|
|
if config.ReadWriteSaver != nil {
|
|
|
|
ql = &quicdialer.QUICListenerSaver{
|
|
|
|
QUICListener: ql,
|
|
|
|
Saver: config.ReadWriteSaver,
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
var d model.QUICDialer = &netxlite.QUICDialerQUICGo{
|
2021-06-25 16:20:08 +02:00
|
|
|
QUICListener: ql,
|
|
|
|
}
|
2022-01-07 17:31:21 +01:00
|
|
|
d = &netxlite.ErrorWrapperQUICDialer{
|
2022-01-07 18:33:37 +01:00
|
|
|
QUICDialer: d,
|
2022-01-07 17:31:21 +01:00
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.TLSSaver != nil {
|
2022-01-07 18:33:37 +01:00
|
|
|
d = quicdialer.HandshakeSaver{Saver: config.TLSSaver, QUICDialer: d}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
d = &netxlite.QUICDialerResolver{
|
2022-01-07 18:33:37 +01:00
|
|
|
Resolver: config.FullResolver,
|
|
|
|
Dialer: d,
|
2021-09-05 18:03:50 +02:00
|
|
|
}
|
2021-06-30 15:19:10 +02:00
|
|
|
return d
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTLSDialer creates a new TLSDialer from the specified config
|
2022-01-07 18:33:37 +01:00
|
|
|
func NewTLSDialer(config Config) model.TLSDialer {
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.Dialer == nil {
|
|
|
|
config.Dialer = NewDialer(config)
|
|
|
|
}
|
2021-06-25 20:51:59 +02:00
|
|
|
var h tlsHandshaker = &netxlite.TLSHandshakerConfigurable{}
|
2022-01-07 17:31:21 +01:00
|
|
|
h = &netxlite.ErrorWrapperTLSHandshaker{TLSHandshaker: h}
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.Logger != nil {
|
2022-01-03 13:53:23 +01:00
|
|
|
h = &netxlite.TLSHandshakerLogger{DebugLogger: config.Logger, TLSHandshaker: h}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
if config.TLSSaver != nil {
|
2021-06-08 11:24:13 +02:00
|
|
|
h = tlsdialer.SaverTLSHandshaker{TLSHandshaker: h, Saver: config.TLSSaver}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
if config.TLSConfig == nil {
|
|
|
|
config.TLSConfig = &tls.Config{NextProtos: []string{"h2", "http/1.1"}}
|
|
|
|
}
|
|
|
|
if config.CertPool == nil {
|
|
|
|
config.CertPool = defaultCertPool
|
|
|
|
}
|
|
|
|
config.TLSConfig.RootCAs = config.CertPool
|
|
|
|
config.TLSConfig.InsecureSkipVerify = config.NoTLSVerify
|
2021-09-06 14:12:30 +02:00
|
|
|
return &netxlite.TLSDialerLegacy{
|
2021-02-02 12:05:47 +01:00
|
|
|
Config: config.TLSConfig,
|
2022-01-07 18:33:37 +01:00
|
|
|
Dialer: config.Dialer,
|
2021-02-02 12:05:47 +01:00
|
|
|
TLSHandshaker: h,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHTTPTransport creates a new HTTPRoundTripper. You can further extend the returned
|
|
|
|
// HTTPRoundTripper before wrapping it into an http.Client.
|
2022-01-07 18:33:37 +01:00
|
|
|
func NewHTTPTransport(config Config) model.HTTPTransport {
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.Dialer == nil {
|
|
|
|
config.Dialer = NewDialer(config)
|
|
|
|
}
|
|
|
|
if config.TLSDialer == nil {
|
|
|
|
config.TLSDialer = NewTLSDialer(config)
|
|
|
|
}
|
|
|
|
if config.QUICDialer == nil {
|
|
|
|
config.QUICDialer = NewQUICDialer(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
tInfo := allTransportsInfo[config.HTTP3Enabled]
|
|
|
|
txp := tInfo.Factory(httptransport.Config{
|
|
|
|
Dialer: config.Dialer, QUICDialer: config.QUICDialer, TLSDialer: config.TLSDialer,
|
|
|
|
TLSConfig: config.TLSConfig})
|
|
|
|
|
|
|
|
if config.ByteCounter != nil {
|
|
|
|
txp = httptransport.ByteCountingTransport{
|
2022-01-07 18:33:37 +01:00
|
|
|
Counter: config.ByteCounter, HTTPTransport: txp}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
if config.Logger != nil {
|
2021-06-26 18:11:47 +02:00
|
|
|
txp = &netxlite.HTTPTransportLogger{Logger: config.Logger, HTTPTransport: txp}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
if config.HTTPSaver != nil {
|
|
|
|
txp = httptransport.SaverMetadataHTTPTransport{
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
HTTPTransport: txp, Saver: config.HTTPSaver}
|
2021-02-02 12:05:47 +01:00
|
|
|
txp = httptransport.SaverBodyHTTPTransport{
|
2022-01-07 18:33:37 +01:00
|
|
|
HTTPTransport: txp, Saver: config.HTTPSaver}
|
2021-02-02 12:05:47 +01:00
|
|
|
txp = httptransport.SaverTransactionHTTPTransport{
|
2022-01-07 18:33:37 +01:00
|
|
|
HTTPTransport: txp, Saver: config.HTTPSaver}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
return txp
|
|
|
|
}
|
|
|
|
|
|
|
|
// httpTransportInfo contains the constructing function as well as the transport name
|
|
|
|
type httpTransportInfo struct {
|
2022-01-07 18:33:37 +01:00
|
|
|
Factory func(httptransport.Config) model.HTTPTransport
|
2021-02-02 12:05:47 +01:00
|
|
|
TransportName string
|
|
|
|
}
|
|
|
|
|
|
|
|
var allTransportsInfo = map[bool]httpTransportInfo{
|
|
|
|
false: {
|
|
|
|
Factory: httptransport.NewSystemTransport,
|
|
|
|
TransportName: "tcp",
|
|
|
|
},
|
|
|
|
true: {
|
|
|
|
Factory: httptransport.NewHTTP3Transport,
|
|
|
|
TransportName: "quic",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSClient creates a new DNS client. The config argument is used to
|
|
|
|
// create the underlying Dialer and/or HTTP transport, if needed. The URL
|
|
|
|
// argument describes the kind of client that we want to make:
|
|
|
|
//
|
|
|
|
// - if the URL is `doh://powerdns`, `doh://google` or `doh://cloudflare` or the URL
|
|
|
|
// starts with `https://`, then we create a DoH client.
|
|
|
|
//
|
|
|
|
// - if the URL is `` or `system:///`, then we create a system client,
|
|
|
|
// i.e. a client using the system resolver.
|
|
|
|
//
|
|
|
|
// - if the URL starts with `udp://`, then we create a client using
|
|
|
|
// a resolver that uses the specified UDP endpoint.
|
|
|
|
//
|
|
|
|
// We return error if the URL does not parse or the URL scheme does not
|
|
|
|
// fall into one of the cases described above.
|
|
|
|
//
|
|
|
|
// If config.ResolveSaver is not nil and we're creating an underlying
|
|
|
|
// resolver where this is possible, we will also save events.
|
2022-01-10 11:53:06 +01:00
|
|
|
func NewDNSClient(config Config, URL string) (model.Resolver, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return NewDNSClientWithOverrides(config, URL, "", "", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSClientWithOverrides creates a new DNS client, similar to NewDNSClient,
|
|
|
|
// with the option to override the default Hostname and SNI.
|
|
|
|
func NewDNSClientWithOverrides(config Config, URL, hostOverride, SNIOverride,
|
2022-01-10 11:53:06 +01:00
|
|
|
TLSVersion string) (model.Resolver, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
switch URL {
|
|
|
|
case "doh://powerdns":
|
|
|
|
URL = "https://doh.powerdns.org/"
|
|
|
|
case "doh://google":
|
|
|
|
URL = "https://dns.google/dns-query"
|
|
|
|
case "doh://cloudflare":
|
|
|
|
URL = "https://cloudflare-dns.com/dns-query"
|
|
|
|
case "":
|
|
|
|
URL = "system:///"
|
|
|
|
}
|
|
|
|
resolverURL, err := url.Parse(URL)
|
|
|
|
if err != nil {
|
2022-01-10 11:53:06 +01:00
|
|
|
return nil, err
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
config.TLSConfig = &tls.Config{ServerName: SNIOverride}
|
2021-06-25 12:39:45 +02:00
|
|
|
if err := netxlite.ConfigureTLSVersion(config.TLSConfig, TLSVersion); err != nil {
|
2022-01-10 11:53:06 +01:00
|
|
|
return nil, err
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
switch resolverURL.Scheme {
|
|
|
|
case "system":
|
2022-01-10 11:53:06 +01:00
|
|
|
return &netxlite.ResolverSystem{}, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
case "https":
|
|
|
|
config.TLSConfig.NextProtos = []string{"h2", "http/1.1"}
|
2022-01-10 11:53:06 +01:00
|
|
|
httpClient := &http.Client{Transport: NewHTTPTransport(config)}
|
2022-01-07 20:02:19 +01:00
|
|
|
var txp model.DNSTransport = netxlite.NewDNSOverHTTPSWithHostOverride(
|
2022-01-10 11:53:06 +01:00
|
|
|
httpClient, URL, hostOverride)
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.ResolveSaver != nil {
|
|
|
|
txp = resolver.SaverDNSTransport{
|
2022-01-07 20:02:19 +01:00
|
|
|
DNSTransport: txp,
|
2021-02-02 12:05:47 +01:00
|
|
|
Saver: config.ResolveSaver,
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
return netxlite.NewSerialResolver(txp), nil
|
2021-02-02 12:05:47 +01:00
|
|
|
case "udp":
|
|
|
|
dialer := NewDialer(config)
|
|
|
|
endpoint, err := makeValidEndpoint(resolverURL)
|
|
|
|
if err != nil {
|
2022-01-10 11:53:06 +01:00
|
|
|
return nil, err
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
var txp model.DNSTransport = netxlite.NewDNSOverUDP(
|
2022-01-07 18:33:37 +01:00
|
|
|
dialer, endpoint)
|
2021-02-02 12:05:47 +01:00
|
|
|
if config.ResolveSaver != nil {
|
|
|
|
txp = resolver.SaverDNSTransport{
|
2022-01-07 20:02:19 +01:00
|
|
|
DNSTransport: txp,
|
2021-02-02 12:05:47 +01:00
|
|
|
Saver: config.ResolveSaver,
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
return netxlite.NewSerialResolver(txp), nil
|
2021-02-02 12:05:47 +01:00
|
|
|
case "dot":
|
|
|
|
config.TLSConfig.NextProtos = []string{"dot"}
|
|
|
|
tlsDialer := NewTLSDialer(config)
|
|
|
|
endpoint, err := makeValidEndpoint(resolverURL)
|
|
|
|
if err != nil {
|
2022-01-10 11:53:06 +01:00
|
|
|
return nil, err
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
var txp model.DNSTransport = netxlite.NewDNSOverTLS(
|
2021-02-02 12:05:47 +01:00
|
|
|
tlsDialer.DialTLSContext, endpoint)
|
|
|
|
if config.ResolveSaver != nil {
|
|
|
|
txp = resolver.SaverDNSTransport{
|
2022-01-07 20:02:19 +01:00
|
|
|
DNSTransport: txp,
|
2021-02-02 12:05:47 +01:00
|
|
|
Saver: config.ResolveSaver,
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
return netxlite.NewSerialResolver(txp), nil
|
2021-02-02 12:05:47 +01:00
|
|
|
case "tcp":
|
|
|
|
dialer := NewDialer(config)
|
|
|
|
endpoint, err := makeValidEndpoint(resolverURL)
|
|
|
|
if err != nil {
|
2022-01-10 11:53:06 +01:00
|
|
|
return nil, err
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
var txp model.DNSTransport = netxlite.NewDNSOverTCP(
|
2021-02-02 12:05:47 +01:00
|
|
|
dialer.DialContext, endpoint)
|
|
|
|
if config.ResolveSaver != nil {
|
|
|
|
txp = resolver.SaverDNSTransport{
|
2022-01-07 20:02:19 +01:00
|
|
|
DNSTransport: txp,
|
2021-02-02 12:05:47 +01:00
|
|
|
Saver: config.ResolveSaver,
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
return netxlite.NewSerialResolver(txp), nil
|
2021-02-02 12:05:47 +01:00
|
|
|
default:
|
2022-01-10 11:53:06 +01:00
|
|
|
return nil, errors.New("unsupported resolver scheme")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeValidEndpoint makes a valid endpoint for DoT and Do53 given the
|
|
|
|
// input URL representing such endpoint. Specifically, we are
|
|
|
|
// concerned with the case where the port is missing. In such a
|
|
|
|
// case, we ensure that we are using the default port 853 for DoT
|
|
|
|
// and default port 53 for TCP and UDP.
|
|
|
|
func makeValidEndpoint(URL *url.URL) (string, error) {
|
|
|
|
// Implementation note: when we're using a quoted IPv6
|
|
|
|
// address, URL.Host contains the quotes but instead the
|
|
|
|
// return value from URL.Hostname() does not.
|
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// - Host: [2620:fe::9]
|
|
|
|
// - Hostname(): 2620:fe::9
|
|
|
|
//
|
|
|
|
// We need to keep this in mind when trying to determine
|
|
|
|
// whether there is also a port or not.
|
|
|
|
//
|
|
|
|
// So the first step is to check whether URL.Host is already
|
|
|
|
// a whatever valid TCP/UDP endpoint and, if so, use it.
|
|
|
|
if _, _, err := net.SplitHostPort(URL.Host); err == nil {
|
|
|
|
return URL.Host, nil
|
|
|
|
}
|
|
|
|
// The second step is to assume that appending the default port
|
|
|
|
// to a host parsed by url.Parse should be giving us a valid
|
|
|
|
// endpoint. The possibilities in fact are:
|
|
|
|
//
|
|
|
|
// 1. domain w/o port
|
|
|
|
// 2. IPv4 w/o port
|
|
|
|
// 3. square bracket quoted IPv6 w/o port
|
|
|
|
// 4. other
|
|
|
|
//
|
|
|
|
// In the first three cases, appending a port leads us to a
|
|
|
|
// good endpoint. The fourth case does not.
|
|
|
|
//
|
|
|
|
// For this reason we check again whether we can split it using
|
|
|
|
// net.SplitHostPort. If we cannot, we were in case four.
|
|
|
|
host := URL.Host
|
|
|
|
if URL.Scheme == "dot" {
|
|
|
|
host += ":853"
|
|
|
|
} else {
|
|
|
|
host += ":53"
|
|
|
|
}
|
|
|
|
if _, _, err := net.SplitHostPort(host); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// Otherwise it's one of the three valid cases above.
|
|
|
|
return host, nil
|
|
|
|
}
|