-
-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[client,relay] Add QUIC support (#2962)
- Loading branch information
Showing
25 changed files
with
943 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrClosedByServer = errors.New("closed by server") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package quic | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/quic-go/quic-go" | ||
log "github.com/sirupsen/logrus" | ||
|
||
netErr "github.com/netbirdio/netbird/relay/client/dialer/net" | ||
) | ||
|
||
const ( | ||
Network = "quic" | ||
) | ||
|
||
type Addr struct { | ||
addr string | ||
} | ||
|
||
func (a Addr) Network() string { | ||
return Network | ||
} | ||
|
||
func (a Addr) String() string { | ||
return a.addr | ||
} | ||
|
||
type Conn struct { | ||
session quic.Connection | ||
ctx context.Context | ||
} | ||
|
||
func NewConn(session quic.Connection) net.Conn { | ||
return &Conn{ | ||
session: session, | ||
ctx: context.Background(), | ||
} | ||
} | ||
|
||
func (c *Conn) Read(b []byte) (n int, err error) { | ||
dgram, err := c.session.ReceiveDatagram(c.ctx) | ||
if err != nil { | ||
return 0, c.remoteCloseErrHandling(err) | ||
} | ||
|
||
n = copy(b, dgram) | ||
return n, nil | ||
} | ||
|
||
func (c *Conn) Write(b []byte) (int, error) { | ||
err := c.session.SendDatagram(b) | ||
if err != nil { | ||
err = c.remoteCloseErrHandling(err) | ||
log.Errorf("failed to write to QUIC stream: %v", err) | ||
return 0, err | ||
} | ||
return len(b), nil | ||
} | ||
|
||
func (c *Conn) RemoteAddr() net.Addr { | ||
return c.session.RemoteAddr() | ||
} | ||
|
||
func (c *Conn) LocalAddr() net.Addr { | ||
if c.session != nil { | ||
return c.session.LocalAddr() | ||
} | ||
return Addr{addr: "unknown"} | ||
} | ||
|
||
func (c *Conn) SetReadDeadline(t time.Time) error { | ||
return fmt.Errorf("SetReadDeadline is not implemented") | ||
} | ||
|
||
func (c *Conn) SetWriteDeadline(t time.Time) error { | ||
return fmt.Errorf("SetWriteDeadline is not implemented") | ||
} | ||
|
||
func (c *Conn) SetDeadline(t time.Time) error { | ||
return nil | ||
} | ||
|
||
func (c *Conn) Close() error { | ||
return c.session.CloseWithError(0, "normal closure") | ||
} | ||
|
||
func (c *Conn) remoteCloseErrHandling(err error) error { | ||
var appErr *quic.ApplicationError | ||
if errors.As(err, &appErr) && appErr.ErrorCode == 0x0 { | ||
return netErr.ErrClosedByServer | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package quic | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"strings" | ||
"time" | ||
|
||
"github.com/quic-go/quic-go" | ||
log "github.com/sirupsen/logrus" | ||
|
||
quictls "github.com/netbirdio/netbird/relay/tls" | ||
nbnet "github.com/netbirdio/netbird/util/net" | ||
) | ||
|
||
type Dialer struct { | ||
} | ||
|
||
func (d Dialer) Protocol() string { | ||
return Network | ||
} | ||
|
||
func (d Dialer) Dial(ctx context.Context, address string) (net.Conn, error) { | ||
quicURL, err := prepareURL(address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
quicConfig := &quic.Config{ | ||
KeepAlivePeriod: 30 * time.Second, | ||
MaxIdleTimeout: 4 * time.Minute, | ||
EnableDatagrams: true, | ||
} | ||
|
||
udpConn, err := nbnet.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0}) | ||
if err != nil { | ||
log.Errorf("failed to listen on UDP: %s", err) | ||
return nil, err | ||
} | ||
|
||
udpAddr, err := net.ResolveUDPAddr("udp", quicURL) | ||
if err != nil { | ||
log.Errorf("failed to resolve UDP address: %s", err) | ||
return nil, err | ||
} | ||
|
||
session, err := quic.Dial(ctx, udpConn, udpAddr, quictls.ClientQUICTLSConfig(), quicConfig) | ||
if err != nil { | ||
if errors.Is(err, context.Canceled) { | ||
return nil, err | ||
} | ||
log.Errorf("failed to dial to Relay server via QUIC '%s': %s", quicURL, err) | ||
return nil, err | ||
} | ||
|
||
conn := NewConn(session) | ||
return conn, nil | ||
} | ||
|
||
func prepareURL(address string) (string, error) { | ||
if !strings.HasPrefix(address, "rel://") && !strings.HasPrefix(address, "rels://") { | ||
return "", fmt.Errorf("unsupported scheme: %s", address) | ||
} | ||
|
||
if strings.HasPrefix(address, "rels://") { | ||
return address[7:], nil | ||
} | ||
return address[6:], nil | ||
} |
Oops, something went wrong.