feat: port-filtering experiment (#891)

Part of https://github.com/ooni/probe/issues/2005
This commit is contained in:
DecFox
2022-09-14 23:24:43 +05:30
committed by GitHub
parent cb632ea0f3
commit d6a362d96f
15 changed files with 452 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
# ooporthelper
This directory contains the source code of the Port-
Filtering test helper written in go
+79
View File
@@ -0,0 +1,79 @@
// command ooporthelper implements the Port Filtering test helper
package main
import (
"context"
"flag"
"net"
"sync"
"time"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/portfiltering"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
var (
srvCtx context.Context
srvCancel context.CancelFunc
srvWg = new(sync.WaitGroup)
srvTestChan = make(chan string, len(TestPorts)) // buffered channel for testing
srvTest bool
)
func init() {
srvCtx, srvCancel = context.WithCancel(context.Background())
}
func shutdown(ctx context.Context, l net.Listener) {
<-ctx.Done()
l.Close()
}
// TODO(DecFox): Add the ability of an echo service to generate some traffic
func handleConnetion(ctx context.Context, conn net.Conn) {
defer conn.Close()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
<-ctx.Done()
}
func listenTCP(ctx context.Context, port string) {
defer srvWg.Done()
address := net.JoinHostPort("127.0.0.1", port)
listener, err := net.Listen("tcp", address)
runtimex.PanicOnError(err, "net.Listen failed")
go shutdown(ctx, listener)
srvTestChan <- port // send to channel to imply server will start listening on port
for {
conn, err := listener.Accept()
if err != nil {
log.Infof("listener unable to accept connections on port%s", port)
return
}
go handleConnetion(ctx, conn)
}
}
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])
defer srvCancel()
ports := portfiltering.Ports
if srvTest {
ports = TestPorts
}
for _, port := range ports {
srvWg.Add(1)
ctx, cancel := context.WithCancel(srvCtx)
defer cancel()
go listenTCP(ctx, port)
}
<-srvCtx.Done()
srvWg.Wait() // wait for listeners on all ports to close
}
+31
View File
@@ -0,0 +1,31 @@
package main
import (
"context"
"net"
"testing"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestMainWorkingAsIntended(t *testing.T) {
srvTest = true // toggle to imply that we are running in test mode
go main()
dialer := netxlite.NewDialerWithoutResolver(model.DiscardLogger)
for _, port := range TestPorts {
<-srvTestChan
addr := net.JoinHostPort("127.0.0.1", port)
ctx := context.Background()
conn, err := dialer.DialContext(ctx, "tcp", addr)
if err != nil {
t.Fatal(err)
}
if conn == nil {
t.Fatal("expected non-nil conn")
}
conn.Close()
}
srvCancel() // shutdown server
srvWg.Wait() // wait for listeners on all ports to close
}
+12
View File
@@ -0,0 +1,12 @@
package main
//
// List of ports we want to use for running integration tests
//
// Ports for testing the testhelper
// Note: we must only use unprivileged ports here to ensure tests run successfully
var TestPorts = []string{
"8080", // tcp
"5050", // tcp
}