dissertation-2-code/tcp/listener.go
Jake Hillion 75f02802ad
Some checks failed
continuous-integration/drone/push Build is failing
Refactored for multiple macverifiers/generators
Tests not fixed yet.
2021-05-13 21:56:25 +01:00

64 lines
1.0 KiB
Go

package tcp
import (
"context"
"log"
"mpbl3p/proxy"
"net"
)
func NewListener(
ctx context.Context,
p *proxy.Proxy,
local string,
vs []func() proxy.MacVerifier,
gs []func() proxy.MacGenerator,
enableConsumers bool,
enableProducers bool,
) error {
laddr, err := net.ResolveTCPAddr("tcp", local)
if err != nil {
return err
}
listener, err := net.ListenTCP("tcp", laddr)
if err != nil {
return err
}
go func() {
for {
conn, err := listener.AcceptTCP()
if err != nil {
panic(err)
}
if err := conn.SetWriteBuffer(0); err != nil {
panic(err)
}
var verifiers = make([]proxy.MacVerifier, len(vs))
for i, v := range vs {
verifiers[i] = v()
}
var generators = make([]proxy.MacGenerator, len(gs))
for i, g := range gs {
generators[i] = g()
}
f := NewFlowConn(ctx, conn, verifiers, generators)
log.Printf("received new tcp connection: %v\n", f)
if enableConsumers {
p.AddConsumer(ctx, &f)
}
if enableProducers {
p.AddProducer(ctx, &f)
}
}
}()
return nil
}