dissertation-2-code/tcp/listener.go
Jake Hillion fad829803a
Some checks failed
continuous-integration/drone/push Build is failing
initial context propagation
2021-03-30 20:57:53 +01:00

43 lines
698 B
Go

package tcp
import (
"context"
"log"
"mpbl3p/proxy"
"net"
)
func NewListener(ctx context.Context, p *proxy.Proxy, local string, v func() proxy.MacVerifier, g func() proxy.MacGenerator) 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)
}
f := NewFlowConn(conn)
log.Printf("received new tcp connection: %v\n", f)
p.AddConsumer(ctx, &f, g())
p.AddProducer(ctx, &f, v())
}
}()
return nil
}