Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handshake timeout #704

Merged
merged 4 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions internal/vpn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
36 changes: 36 additions & 0 deletions internal/vpn/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@ import (
"fmt"
"io"
"net"
"time"

"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/noise"

"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)
Expand All @@ -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
Expand Down