Add quick listener

This commit is contained in:
Zoltán Papp
2024-11-15 19:36:09 +01:00
parent b82b4a07fc
commit b23169de63
8 changed files with 130 additions and 106 deletions

View File

@@ -175,8 +175,7 @@ func (c *Client) Connect() error {
return nil
}
err := c.connect()
if err != nil {
if err := c.connect(); err != nil {
return err
}
@@ -266,8 +265,7 @@ func (c *Client) connect() error {
}
c.relayConn = conn
err = c.handShake()
if err != nil {
if err = c.handShake(); err != nil {
cErr := conn.Close()
if cErr != nil {
c.log.Errorf("failed to close connection: %s", cErr)
@@ -341,7 +339,7 @@ func (c *Client) readLoop(relayConn net.Conn) {
c.log.Infof("start to Relay read loop exit")
c.mu.Lock()
if c.serviceIsRunning && !internallyStoppedFlag.isSet() {
c.log.Debugf("failed to read message from relay server: %s", errExit)
c.log.Errorf("failed to read message from relay server: %s", errExit)
}
c.mu.Unlock()
break

View File

@@ -2,11 +2,11 @@ package quic
import (
"context"
"fmt"
"net"
"time"
"github.com/quic-go/quic-go"
log "github.com/sirupsen/logrus"
)
type QuicAddr struct {
@@ -36,22 +36,21 @@ func NewConn(session quic.Connection, serverAddress string) net.Conn {
}
func (c *Conn) Read(b []byte) (n int, err error) {
// Use the QUIC stream's Read method directly
dgram, err := c.session.ReceiveDatagram(c.ctx)
if err != nil {
return 0, fmt.Errorf("failed to read from QUIC stream: %v", err)
log.Errorf("failed to read from QUIC session: %v", err)
return 0, err
}
// Copy data to b, ensuring we dont exceed the size of b
n = copy(b, dgram)
return n, nil
}
func (c *Conn) Write(b []byte) (int, error) {
// Use the QUIC stream's Write method directly
err := c.session.SendDatagram(b)
if err != nil {
return 0, fmt.Errorf("failed to write to QUIC stream: %v", err)
log.Errorf("failed to write to QUIC stream: %v", err)
return 0, err
}
return len(b), nil
}

View File

@@ -9,6 +9,7 @@ import (
"time"
"github.com/quic-go/quic-go"
log "github.com/sirupsen/logrus"
)
const (
@@ -35,9 +36,12 @@ func Dial(address string) (net.Conn, error) {
EnableDatagrams: true,
}
// todo add support for custom dialer
session, err := quic.DialAddr(ctx, quicURL, tlsConf, quicConfig)
if err != nil {
return nil, fmt.Errorf("failed to dial QUIC server '%s': %v", quicURL, err)
log.Errorf("failed to dial to Relay server via QUIC '%s': %s", quicURL, err)
return nil, err
}
conn := NewConn(session, address)

View File

@@ -32,8 +32,6 @@ func Dial(address string) (net.Conn, error) {
}
parsedURL.Path = ws.URLPath
log.Infof("------ Dialing to Relay server: %s", wsURL)
wsConn, resp, err := websocket.Dial(context.Background(), parsedURL.String(), opts)
if err != nil {
log.Errorf("failed to dial to Relay server '%s': %s", wsURL, err)