2021-02-02 12:05:47 +01:00
|
|
|
// Command oohelperd contains the Web Connectivity test helper.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
2021-08-17 11:23:53 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/cmd/oohelperd/internal/webconnectivity"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/cmd/oohelperd/internal/websteps"
|
2021-09-30 02:06:27 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webstepsx"
|
2021-02-02 12:05:47 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
2021-09-27 08:13:30 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const maxAcceptableBody = 1 << 24
|
|
|
|
|
|
|
|
var (
|
|
|
|
dialer netx.Dialer
|
|
|
|
endpoint = flag.String("endpoint", ":8080", "Endpoint where to listen")
|
|
|
|
httpx *http.Client
|
|
|
|
resolver netx.Resolver
|
|
|
|
srvcancel context.CancelFunc
|
|
|
|
srvctx context.Context
|
|
|
|
srvwg = new(sync.WaitGroup)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
srvctx, srvcancel = context.WithCancel(context.Background())
|
|
|
|
dialer = netx.NewDialer(netx.Config{Logger: log.Log})
|
|
|
|
txp := netx.NewHTTPTransport(netx.Config{Logger: log.Log})
|
|
|
|
httpx = &http.Client{Transport: txp}
|
2021-09-27 08:13:30 +02:00
|
|
|
// fix: use 8.8.8.8:53/udp so we pin to a specific resolver.
|
|
|
|
var err error
|
|
|
|
resolver, err = netx.NewDNSClient(netx.Config{Logger: log.Log}, "udp://8.8.8.8:53")
|
|
|
|
runtimex.PanicOnError(err, "NewDNSClient failed")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func shutdown(srv *http.Server) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
srv.Shutdown(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
logmap := map[bool]log.Level{
|
|
|
|
true: log.DebugLevel,
|
|
|
|
false: log.InfoLevel,
|
|
|
|
}
|
|
|
|
debug := flag.Bool("debug", false, "Toggle debug mode")
|
|
|
|
flag.Parse()
|
|
|
|
log.SetLevel(logmap[*debug])
|
|
|
|
testableMain()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testableMain() {
|
|
|
|
mux := http.NewServeMux()
|
2021-08-17 11:23:53 +02:00
|
|
|
mux.Handle("/api/unstable/websteps", &websteps.Handler{Config: &websteps.Config{}})
|
2021-09-30 02:06:27 +02:00
|
|
|
mux.Handle("/api/v1/websteps", &webstepsx.THHandler{})
|
2021-08-17 11:23:53 +02:00
|
|
|
mux.Handle("/", webconnectivity.Handler{
|
2021-02-02 12:05:47 +01:00
|
|
|
Client: httpx,
|
|
|
|
Dialer: dialer,
|
|
|
|
MaxAcceptableBody: maxAcceptableBody,
|
|
|
|
Resolver: resolver,
|
|
|
|
})
|
|
|
|
srv := &http.Server{Addr: *endpoint, Handler: mux}
|
|
|
|
srvwg.Add(1)
|
|
|
|
go srv.ListenAndServe()
|
|
|
|
<-srvctx.Done()
|
|
|
|
shutdown(srv)
|
|
|
|
srvwg.Done()
|
|
|
|
}
|