dissertation-2-code/tcp/listener.go

43 lines
698 B
Go
Raw Normal View History

2020-11-01 16:50:02 +00:00
package tcp
import (
2021-03-30 20:57:53 +01:00
"context"
2020-11-03 20:56:51 +00:00
"log"
2020-11-01 16:50:02 +00:00
"mpbl3p/proxy"
"net"
)
2021-03-30 20:57:53 +01:00
func NewListener(ctx context.Context, p *proxy.Proxy, local string, v func() proxy.MacVerifier, g func() proxy.MacGenerator) error {
2020-11-01 16:50:02 +00:00
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)
}
2021-01-25 19:38:39 +00:00
if err := conn.SetWriteBuffer(0); err != nil {
2021-01-23 20:52:37 +00:00
panic(err)
}
2020-11-03 20:56:51 +00:00
2021-01-23 18:24:04 +00:00
f := NewFlowConn(conn)
2020-11-02 17:24:15 +00:00
2020-11-26 22:10:37 +00:00
log.Printf("received new tcp connection: %v\n", f)
2020-11-02 17:24:15 +00:00
2021-03-30 20:57:53 +01:00
p.AddConsumer(ctx, &f, g())
p.AddProducer(ctx, &f, v())
2020-11-01 16:50:02 +00:00
}
}()
return nil
}