mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-20 01:06:45 +00:00
Add quic transporter
This commit is contained in:
52
relay/client/dialer/quic/conn.go
Normal file
52
relay/client/dialer/quic/conn.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package quic
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/quic-go/quic-go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Conn struct {
|
||||
quic.Stream
|
||||
qConn quic.Connection
|
||||
}
|
||||
|
||||
func NewConn(stream quic.Stream, qConn quic.Connection) net.Conn {
|
||||
return &Conn{
|
||||
Stream: stream,
|
||||
qConn: qConn,
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Conn) Write(b []byte) (n int, err error) {
|
||||
log.Debugf("writing: %d, %x\n", len(b), b)
|
||||
n, err = q.Stream.Write(b)
|
||||
if n != len(b) {
|
||||
log.Errorf("failed to write out the full message")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (q *Conn) Close() error {
|
||||
err := q.Stream.Close()
|
||||
if err != nil {
|
||||
log.Errorf("failed to close stream: %s", err)
|
||||
return err
|
||||
}
|
||||
err = q.qConn.CloseWithError(0, "")
|
||||
if err != nil {
|
||||
log.Errorf("failed to close connection: %s", err)
|
||||
return err
|
||||
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Conn) LocalAddr() net.Addr {
|
||||
return c.qConn.LocalAddr()
|
||||
}
|
||||
|
||||
func (c *Conn) RemoteAddr() net.Addr {
|
||||
return c.qConn.RemoteAddr()
|
||||
}
|
||||
32
relay/client/dialer/quic/quic.go
Normal file
32
relay/client/dialer/quic/quic.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package quic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
|
||||
"github.com/quic-go/quic-go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Dial(address string) (net.Conn, error) {
|
||||
tlsConf := &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
NextProtos: []string{"quic-echo-example"},
|
||||
}
|
||||
qConn, err := quic.DialAddr(context.Background(), address, tlsConf, &quic.Config{
|
||||
EnableDatagrams: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf("dial quic address %s failed: %s", address, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stream, err := qConn.OpenStreamSync(context.Background())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
conn := NewConn(stream, qConn)
|
||||
return conn, nil
|
||||
}
|
||||
Reference in New Issue
Block a user