From ff8608c8cfe9318dd2ff90344552090f05830ae6 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Sun, 28 Feb 2021 20:33:12 +0300 Subject: [PATCH 1/4] Add handshake timeout --- internal/vpn/client.go | 6 ++++-- internal/vpn/net.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/internal/vpn/client.go b/internal/vpn/client.go index 675bbac26..1669e5d63 100644 --- a/internal/vpn/client.go +++ b/internal/vpn/client.go @@ -638,14 +638,16 @@ func (c *Client) shakeHands(conn net.Conn) (TUNIP, TUNGateway net.IP, err error) Passcode: c.cfg.Passcode, } + const handshakeTimeout = 5 * time.Second + fmt.Printf("Sending client hello: %v\n", cHello) - if err := WriteJSON(conn, &cHello); err != nil { + if err := WriteJSONWithTimeout(conn, &cHello, handshakeTimeout); err != nil { return nil, nil, fmt.Errorf("error sending client hello: %w", err) } var sHello ServerHello - if err := ReadJSON(conn, &sHello); err != nil { + if err := ReadJSONWithTimeout(conn, &sHello, handshakeTimeout); err != nil { return nil, nil, fmt.Errorf("error reading server hello: %w", err) } diff --git a/internal/vpn/net.go b/internal/vpn/net.go index 1a992ff7a..d2f32a4d0 100644 --- a/internal/vpn/net.go +++ b/internal/vpn/net.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net" + "time" "github.com/skycoin/dmsg/cipher" "github.com/skycoin/dmsg/noise" @@ -12,6 +13,23 @@ import ( "github.com/skycoin/skywire/pkg/app/appnet" ) +// WriteJSONWithTimeout marshals `data` and sends it over the `conn` with the specified write `timeout`. +func WriteJSONWithTimeout(conn net.Conn, data interface{}, timeout time.Duration) error { + if err := conn.SetWriteDeadline(time.Now().Add(timeout)); err != nil { + return fmt.Errorf("failed to set write deadline: %w", err) + } + + if err := WriteJSON(conn, data); err != nil { + return err + } + + if err := conn.SetWriteDeadline(time.Time{}); err != nil { + return fmt.Errorf("failed to remove write deadline: %w", err) + } + + return nil +} + // WriteJSON marshals `data` and sends it over the `conn`. func WriteJSON(conn net.Conn, data interface{}) error { dataBytes, err := json.Marshal(data) @@ -31,6 +49,24 @@ func WriteJSON(conn net.Conn, data interface{}) error { return nil } +// ReadJSONWithTimeout reads portion of data from the `conn` and unmarshals it into `data` with the +// specified read `timeout`. +func ReadJSONWithTimeout(conn net.Conn, data interface{}, timeout time.Duration) error { + if err := conn.SetReadDeadline(time.Now().Add(timeout)); err != nil { + return fmt.Errorf("failed to set read deadline: %w", err) + } + + if err := ReadJSON(conn, data); err != nil { + return err + } + + if err := conn.SetReadDeadline(time.Time{}); err != nil { + return fmt.Errorf("failed to remove read deadline: %w", err) + } + + return nil +} + // ReadJSON reads portion of data from the `conn` and unmarshals it into `data`. func ReadJSON(conn net.Conn, data interface{}) error { const bufSize = 1024 @@ -40,6 +76,7 @@ func ReadJSON(conn net.Conn, data interface{}) error { for { n, err := conn.Read(buf) if err != nil { + fmt.Printf("FAILED TO READ JSON: %v\n", err) return fmt.Errorf("error reading data: %w", err) } From bb5ebee283f72230795e0dce289c7e7726a8a6c5 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Mon, 1 Mar 2021 20:55:42 +0300 Subject: [PATCH 2/4] Add debug log --- pkg/router/router.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/router/router.go b/pkg/router/router.go index c3d8354ee..b90db664b 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -337,6 +337,8 @@ func (r *router) serveTransportManager(ctx context.Context) { return } + r.logger.Infof("GOT PACKET: %s %v", packet.Type().String(), packet) + if err := r.handleTransportPacket(ctx, packet); err != nil { if err == transport.ErrNotServing { r.logger.WithError(err).Warnf("Stopped serving Transport.") From f498f453216e1ceff46757f8613c7a742b6d15ad Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Mon, 1 Mar 2021 21:19:56 +0300 Subject: [PATCH 3/4] Remove debug log --- pkg/router/router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/router/router.go b/pkg/router/router.go index b90db664b..a5bc7636b 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -337,7 +337,7 @@ func (r *router) serveTransportManager(ctx context.Context) { return } - r.logger.Infof("GOT PACKET: %s %v", packet.Type().String(), packet) + //r.logger.Infof("GOT PACKET: %s %v", packet.Type().String(), packet) if err := r.handleTransportPacket(ctx, packet); err != nil { if err == transport.ErrNotServing { From 30bcba23c2ef08268bfebdf95f2a3ba5a7a4ef06 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Mon, 1 Mar 2021 22:30:57 +0300 Subject: [PATCH 4/4] Remove debug logs --- internal/vpn/net.go | 1 - pkg/router/router.go | 2 -- 2 files changed, 3 deletions(-) diff --git a/internal/vpn/net.go b/internal/vpn/net.go index d2f32a4d0..8dd9b89c3 100644 --- a/internal/vpn/net.go +++ b/internal/vpn/net.go @@ -76,7 +76,6 @@ func ReadJSON(conn net.Conn, data interface{}) error { for { n, err := conn.Read(buf) if err != nil { - fmt.Printf("FAILED TO READ JSON: %v\n", err) return fmt.Errorf("error reading data: %w", err) } diff --git a/pkg/router/router.go b/pkg/router/router.go index a5bc7636b..c3d8354ee 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -337,8 +337,6 @@ func (r *router) serveTransportManager(ctx context.Context) { return } - //r.logger.Infof("GOT PACKET: %s %v", packet.Type().String(), packet) - if err := r.handleTransportPacket(ctx, packet); err != nil { if err == transport.ErrNotServing { r.logger.WithError(err).Warnf("Stopped serving Transport.")