Skip to content

Commit

Permalink
refactor(netemx/scenario.go): start using QAEnvOptionNetStack (ooni#1226
Browse files Browse the repository at this point in the history
)

With the changes we implemented so far, we're not able to start
migrating part of scenario.go (probably the easiest part) to use
QAEnvOptionNetStack for configuring servers.

## Checklist

- [x] I have read the [contribution
guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request:
ooni/probe#1803
- [x] if you changed anything related to how experiments work and you
need to reflect these changes in the ooni/spec repository, please link
to the related ooni/spec pull request: N/A
- [x] if you changed code inside an experiment, make sure you bump its
version number: N/A
  • Loading branch information
bassosimone authored and Murphy-OrangeMud committed Feb 13, 2024
1 parent 867d427 commit 1d61256
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion internal/experiment/telegram/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func configureDNSWithDefaults(config *netem.DNSConfig) {
func newQAEnvironment(ipaddrs ...string) *netemx.QAEnv {
// create a single factory for handling all the requests
factory := &netemx.HTTPCleartextServerFactory{
Factory: netemx.HTTPHandlerFactoryFunc(func() http.Handler {
Factory: netemx.HTTPHandlerFactoryFunc(func(_ *netem.UNetStack) http.Handler {
// we create an empty mux, which should cause a 404 for each webpage, which seems what
// the servers used by telegram DC do as of 2023-07-11
return http.NewServeMux()
Expand Down
4 changes: 2 additions & 2 deletions internal/netemx/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type GeoIPHandlerFactoryUbuntu struct {
ProbeIP string
}

var _ QAEnvHTTPHandlerFactory = &GeoIPHandlerFactoryUbuntu{}
var _ HTTPHandlerFactory = &GeoIPHandlerFactoryUbuntu{}

// NewHandler implements QAEnvHTTPHandlerFactory.
func (f *GeoIPHandlerFactoryUbuntu) NewHandler(unet netem.UnderlyingNetwork) http.Handler {
func (f *GeoIPHandlerFactoryUbuntu) NewHandler(_ *netem.UNetStack) http.Handler {
return &testingx.GeoIPHandlerUbuntu{ProbeIP: f.ProbeIP}
}
10 changes: 5 additions & 5 deletions internal/netemx/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (

// HTTPHandlerFactory constructs an [http.Handler].
type HTTPHandlerFactory interface {
NewHandler() http.Handler
NewHandler(stack *netem.UNetStack) http.Handler
}

// HTTPHandlerFactoryFunc allows a func to become an [HTTPHandlerFactory].
type HTTPHandlerFactoryFunc func() http.Handler
type HTTPHandlerFactoryFunc func(stack *netem.UNetStack) http.Handler

var _ HTTPHandlerFactory = HTTPHandlerFactoryFunc(nil)

// NewHandler implements HTTPHandlerFactory.
func (fx HTTPHandlerFactoryFunc) NewHandler() http.Handler {
return fx()
func (fx HTTPHandlerFactoryFunc) NewHandler(stack *netem.UNetStack) http.Handler {
return fx(stack)
}

// HTTPCleartextServerFactory implements [NetStackServerFactory] for cleartext HTTP.
Expand Down Expand Up @@ -80,7 +80,7 @@ func (srv *httpCleartextServer) MustStart() {
srv.mu.Lock()

// create the handler
handler := srv.factory.NewHandler()
handler := srv.factory.NewHandler(srv.unet)

// create the listening address
ipAddr := net.ParseIP(srv.unet.IPAddress())
Expand Down
2 changes: 1 addition & 1 deletion internal/netemx/http3.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (srv *http3Server) MustStart() {
srv.mu.Lock()

// create the handler
handler := srv.factory.NewHandler()
handler := srv.factory.NewHandler(srv.unet)

// create the listening address
ipAddr := net.ParseIP(srv.unet.IPAddress())
Expand Down
4 changes: 2 additions & 2 deletions internal/netemx/http3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestHTTP3ServerFactory(t *testing.T) {

env := MustNewQAEnv(
QAEnvOptionNetStack("10.55.56.57", &HTTP3ServerFactory{
Factory: HTTPHandlerFactoryFunc(func() http.Handler {
Factory: HTTPHandlerFactoryFunc(func(_ *netem.UNetStack) http.Handler {
return ExampleWebPageHandler()
}),
Ports: []int{443},
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestHTTP3ServerFactory(t *testing.T) {

env := MustNewQAEnv(
QAEnvOptionNetStack("10.55.56.100", &HTTP3ServerFactory{
Factory: HTTPHandlerFactoryFunc(func() http.Handler {
Factory: HTTPHandlerFactoryFunc(func(_ *netem.UNetStack) http.Handler {
return ExampleWebPageHandler()
}),
Ports: []int{443},
Expand Down
3 changes: 2 additions & 1 deletion internal/netemx/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (

"github.com/apex/log"
"github.com/google/go-cmp/cmp"
"github.com/ooni/netem"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)

func TestHTTPCleartextServerFactory(t *testing.T) {
env := MustNewQAEnv(
QAEnvOptionNetStack(AddressWwwExampleCom, &HTTPCleartextServerFactory{
Factory: HTTPHandlerFactoryFunc(func() http.Handler {
Factory: HTTPHandlerFactoryFunc(func(_ *netem.UNetStack) http.Handler {
return ExampleWebPageHandler()
}),
Ports: []int{80},
Expand Down
2 changes: 1 addition & 1 deletion internal/netemx/https.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (srv *httpSecureServer) MustStart() {
srv.mu.Lock()

// create the handler
handler := srv.factory.NewHandler()
handler := srv.factory.NewHandler(srv.unet)

// create the listening address
ipAddr := net.ParseIP(srv.unet.IPAddress())
Expand Down
4 changes: 2 additions & 2 deletions internal/netemx/https_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestHTTPSecureServerFactory(t *testing.T) {
t.Run("when using the TLSConfig provided by netem", func(t *testing.T) {
env := MustNewQAEnv(
QAEnvOptionNetStack(AddressWwwExampleCom, &HTTPSecureServerFactory{
Factory: HTTPHandlerFactoryFunc(func() http.Handler {
Factory: HTTPHandlerFactoryFunc(func(_ *netem.UNetStack) http.Handler {
return ExampleWebPageHandler()
}),
Ports: []int{443},
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestHTTPSecureServerFactory(t *testing.T) {

env := MustNewQAEnv(
QAEnvOptionNetStack(AddressWwwExampleCom, &HTTPSecureServerFactory{
Factory: HTTPHandlerFactoryFunc(func() http.Handler {
Factory: HTTPHandlerFactoryFunc(func(_ *netem.UNetStack) http.Handler {
return ExampleWebPageHandler()
}),
Ports: []int{443},
Expand Down
4 changes: 2 additions & 2 deletions internal/netemx/ooapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
// OOAPIHandlerFactory is a [QAEnvHTTPHandlerFactory] that creates [OOAPIHandler] instances.
type OOAPIHandlerFactory struct{}

var _ QAEnvHTTPHandlerFactory = &OOAPIHandlerFactory{}
var _ HTTPHandlerFactory = &OOAPIHandlerFactory{}

// NewHandler implements QAEnvHTTPHandlerFactory.
func (*OOAPIHandlerFactory) NewHandler(unet netem.UnderlyingNetwork) http.Handler {
func (*OOAPIHandlerFactory) NewHandler(_ *netem.UNetStack) http.Handler {
return &OOAPIHandler{}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/netemx/oohelperd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
// test helper using a specific [netem.UnderlyingNetwork].
type OOHelperDFactory struct{}

var _ QAEnvHTTPHandlerFactory = &OOHelperDFactory{}
var _ HTTPHandlerFactory = &OOHelperDFactory{}

// NewHandler implements QAEnvHTTPHandlerFactory.NewHandler.
func (f *OOHelperDFactory) NewHandler(unet netem.UnderlyingNetwork) http.Handler {
func (f *OOHelperDFactory) NewHandler(unet *netem.UNetStack) http.Handler {
netx := netxlite.Netx{Underlying: &netxlite.NetemUnderlyingNetworkAdapter{UNet: unet}}
handler := oohelperd.NewHandler()

Expand Down
20 changes: 16 additions & 4 deletions internal/netemx/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,30 @@ func MustNewScenario(config []*ScenarioDomainAddresses) *QAEnv {

case ScenarioRoleOONIAPI:
for _, addr := range sad.Addresses {
opts = append(opts, QAEnvOptionHTTPServer(addr, &OOAPIHandlerFactory{}))
opts = append(opts, QAEnvOptionNetStack(addr, &HTTPSecureServerFactory{
Factory: &OOAPIHandlerFactory{},
Ports: []int{443},
TLSConfig: nil, // use netem's default
}))
}

case ScenarioRoleOONITestHelper:
for _, addr := range sad.Addresses {
opts = append(opts, QAEnvOptionHTTPServer(addr, &OOHelperDFactory{}))
opts = append(opts, QAEnvOptionNetStack(addr, &HTTPSecureServerFactory{
Factory: &OOHelperDFactory{},
Ports: []int{443},
TLSConfig: nil, // use netem's default
}))
}

case ScenarioRoleUbuntuGeoIP:
for _, addr := range sad.Addresses {
opts = append(opts, QAEnvOptionHTTPServer(addr, &GeoIPHandlerFactoryUbuntu{
ProbeIP: DefaultClientAddress,
opts = append(opts, QAEnvOptionNetStack(addr, &HTTPSecureServerFactory{
Factory: &GeoIPHandlerFactoryUbuntu{
ProbeIP: DefaultClientAddress,
},
Ports: []int{443},
TLSConfig: nil, // use netem's default
}))
}
}
Expand Down

0 comments on commit 1d61256

Please sign in to comment.