Skip to content

Commit

Permalink
refactor(oohelperd): move code out of package main (#1189)
Browse files Browse the repository at this point in the history
This diff refactors the oohelperd implementation to move code out of
package main. Specifically, we move most of the code from
`./internal/cmd/oohelperd` to `./internal/oohelperd`. Thanks to this
refactoring, we can reuse the implementation of the oohelperd for
writing netem based integration tests.

Part of ooni/probe#2461

---------

Co-authored-by: kelmenhorst <[email protected]>
  • Loading branch information
bassosimone and kelmenhorst committed Jul 21, 2023
1 parent 9414298 commit d4013c9
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 240 deletions.
118 changes: 0 additions & 118 deletions internal/cmd/oohelperd/handler.go

This file was deleted.

106 changes: 2 additions & 104 deletions internal/cmd/oohelperd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,20 @@ import (
"fmt"
"net"
"net/http"
"net/http/cookiejar"
"net/http/pprof"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/oohelperd"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/net/publicsuffix"
)

// maxAcceptableBodySize is the maximum acceptable body size for incoming
// API requests as well as when we're measuring webpages.
const maxAcceptableBodySize = 1 << 24

var (
// apiEndpoint is the endpoint where we serve ooniprobe requests
apiEndpoint = flag.String("api-endpoint", "127.0.0.1:8080", "API endpoint")
Expand Down Expand Up @@ -58,16 +50,6 @@ var (
versionFlag = flag.Bool("version", false, "Prints version information on the stdout")
)

// newResolver creates a new [model.Resolver] suitable for serving
// requests coming from ooniprobe clients.
func newResolver(logger model.Logger) model.Resolver {
// Implementation note: pin to a specific resolver so we don't depend upon the
// default resolver configured by the box. Also, use an encrypted transport thus
// we're less vulnerable to any policy implemented by the box's provider.
resolver := netxlite.NewParallelDNSOverHTTPSResolver(logger, "https://dns.google/dns-query")
return resolver
}

// shutdown calls srv.Shutdown with a reasonably long timeout. The srv.Shutdown
// function will immediately close any open listener and then will wait until
// all pending connections are closed or the context has expired. By giving pending
Expand All @@ -81,90 +63,6 @@ func shutdown(srv *http.Server, wg *sync.WaitGroup) {
srv.Shutdown(ctx)
}

// newCookieJar is the factory for constructing a new cookier jar.
func newCookieJar() *cookiejar.Jar {
// Implementation note: the [cookiejar.New] function always returns a
// nil error; hence, it's safe here to use [runtimex.Try1].
return runtimex.Try1(cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}))
}

// newHTTPClientWithTransportFactory creates a new HTTP client.
func newHTTPClientWithTransportFactory(
logger model.Logger,
txpFactory func(model.DebugLogger, model.Resolver) model.HTTPTransport,
) model.HTTPClient {
// If the DoH resolver we're using insists that a given domain maps to
// bogons, make sure we're going to fail the HTTP measurement.
//
// The TCP measurements scheduler in ipinfo.go will also refuse to
// schedule TCP measurements for bogons.
//
// While this seems theoretical, as of 2022-08-28, I see:
//
// % host polito.it
// polito.it has address 192.168.59.6
// polito.it has address 192.168.40.1
// polito.it mail is handled by 10 mx.polito.it.
//
// So, it's better to consider this as a possible corner case.
reso := netxlite.MaybeWrapWithBogonResolver(
true, // enabled
newResolver(logger),
)

// fix: We MUST set a cookie jar for measuring HTTP. See
// https://github.com/ooni/probe/issues/2488 for additional
// context and pointers to the relevant measurements.
client := &http.Client{
Transport: txpFactory(logger, reso),
CheckRedirect: nil,
Jar: newCookieJar(),
Timeout: 0,
}

return netxlite.WrapHTTPClient(client)
}

// newHandler constructs the [handler] used by [main].
func newHandler() *handler {
return &handler{
BaseLogger: log.Log,
Indexer: &atomic.Int64{},
MaxAcceptableBody: maxAcceptableBodySize,
Measure: measure,

NewHTTPClient: func(logger model.Logger) model.HTTPClient {
return newHTTPClientWithTransportFactory(
logger,
netxlite.NewHTTPTransportWithResolver,
)
},

NewHTTP3Client: func(logger model.Logger) model.HTTPClient {
return newHTTPClientWithTransportFactory(
logger,
netxlite.NewHTTP3TransportWithResolver,
)
},

NewDialer: func(logger model.Logger) model.Dialer {
return netxlite.NewDialerWithoutResolver(logger)
},
NewQUICDialer: func(logger model.Logger) model.QUICDialer {
return netxlite.NewQUICDialerWithoutResolver(
netxlite.NewQUICListener(),
logger,
)
},
NewResolver: newResolver,
NewTLSHandshaker: func(logger model.Logger) model.TLSHandshaker {
return netxlite.NewTLSHandshakerStdlib(logger)
},
}
}

func main() {
// parse command line options
flag.Parse()
Expand Down Expand Up @@ -194,7 +92,7 @@ func main() {
mux := http.NewServeMux()

// add the main oohelperd handler to the mux
mux.Handle("/", newHandler())
mux.Handle("/", oohelperd.NewHandler())

// create a listening server for serving ooniprobe requests
srv := &http.Server{Addr: *apiEndpoint, Handler: mux}
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/oohelperd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
"github.com/ooni/probe-cli/v3/internal/runtimex"
)

type (
ctrlRequest = model.THRequest
ctrlResponse = model.THResponse
)

func TestMainRunServerWorkingAsIntended(t *testing.T) {
// let the kernel pick a random free port
*apiEndpoint = "127.0.0.1:0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package oohelperd

//
// DNS measurements
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package oohelperd

import (
"context"
Expand Down
Loading

0 comments on commit d4013c9

Please sign in to comment.