dissertation-2-code/tcp/listener.go

42 lines
656 B
Go
Raw Normal View History

2020-11-01 16:50:02 +00:00
package tcp
import (
2020-11-03 20:56:51 +00:00
"log"
2020-11-01 16:50:02 +00:00
"mpbl3p/proxy"
"net"
)
2020-11-29 22:06:38 +00:00
func NewListener(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
2020-11-29 22:06:38 +00:00
p.AddConsumer(&f, g())
p.AddProducer(&f, v())
2020-11-01 16:50:02 +00:00
}
}()
return nil
}