Skip to content

Commit

Permalink
Merge pull request #704 from Darkren/feature/vpn-server-hello-timeout
Browse files Browse the repository at this point in the history
Add handshake timeout
  • Loading branch information
jdknives committed Mar 1, 2021
2 parents 19d88de + 30bcba2 commit 1059150
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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

0 comments on commit 1059150

Please sign in to comment.