From 22b5cc74e14d9d62155e9601162dba6af6711f8a Mon Sep 17 00:00:00 2001 From: Jake Hillion Date: Mon, 2 Nov 2020 17:24:15 +0000 Subject: [PATCH] interface naming and write deadline --- config/builder.go | 6 +++++- config/config.go | 3 ++- tcp/flow.go | 8 +++++++- tcp/listener.go | 5 +++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/config/builder.go b/config/builder.go index e79379a..6f94d8b 100644 --- a/config/builder.go +++ b/config/builder.go @@ -26,7 +26,11 @@ func (c Configuration) Build() (*proxy.Proxy, error) { p := proxy.NewProxy(0) p.Generator = UselessMac{} - ss, err := tun.NewTun("nc%d", 1500) + if c.Host.InterfaceName == "" { + c.Host.InterfaceName = "nc%d" + } + + ss, err := tun.NewTun(c.Host.InterfaceName, 1500) if err != nil { return nil, err } diff --git a/config/config.go b/config/config.go index bf8e781..76de54c 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,8 @@ type Configuration struct { } type Host struct { - PrivateKey string `validate:"required"` + PrivateKey string `validate:"required"` + InterfaceName string } type Peer struct { diff --git a/tcp/flow.go b/tcp/flow.go index 5f20dd8..48db06f 100644 --- a/tcp/flow.go +++ b/tcp/flow.go @@ -8,6 +8,7 @@ import ( "mpbl3p/shared" "net" "sync" + "time" ) var ErrNotEnoughBytes = errors.New("not enough bytes") @@ -15,6 +16,7 @@ var ErrNotEnoughBytes = errors.New("not enough bytes") type Conn interface { Read(b []byte) (n int, err error) Write(b []byte) (n int, err error) + SetWriteDeadline(time.Time) error } type InitiatedFlow struct { @@ -97,7 +99,11 @@ func (f *Flow) consumeMarshalled(data []byte) error { binary.LittleEndian.PutUint32(prefixedData, uint32(len(data))) copy(prefixedData[4:], data) - _, err := f.conn.Write(prefixedData) + err := f.conn.SetWriteDeadline(time.Now().Add(5*time.Second)) + if err != nil { + return err + } + _, err = f.conn.Write(prefixedData) return err } diff --git a/tcp/listener.go b/tcp/listener.go index 225a54a..88ccb84 100644 --- a/tcp/listener.go +++ b/tcp/listener.go @@ -24,9 +24,10 @@ func NewListener(p *proxy.Proxy, local string, v proxy.MacVerifier) error { panic(err) } - fmt.Printf("received new connection `%v`\n", conn) - f := Flow{conn: conn, isAlive: true} + + fmt.Printf("received new connection: %v\n", f) + p.AddConsumer(&f) p.AddProducer(&f, v) }