chore: hy client should not force UDP addr for quic Dial

This commit is contained in:
Toby
2022-10-24 22:47:12 -07:00
parent 3d54cb43af
commit 8b0a157e0b
2 changed files with 63 additions and 29 deletions

View File

@@ -25,7 +25,9 @@ import (
var ErrClosed = errors.New("closed")
type Client struct {
serverAddr string
serverAddr string
serverName string // QUIC SNI
sendBPS, recvBPS uint64
auth []byte
@@ -50,8 +52,18 @@ func NewClient(serverAddr string, auth []byte, tlsConfig *tls.Config, quicConfig
pktConnFunc pktconns.ClientPacketConnFunc, sendBPS uint64, recvBPS uint64, quicReconnectFunc func(err error),
) (*Client, error) {
quicConfig.DisablePathMTUDiscovery = quicConfig.DisablePathMTUDiscovery || pmtud.DisablePathMTUDiscovery
// QUIC wants server name, but our serverAddr is usually host:port,
// so we try to extract it from serverAddr.
serverName, _, err := net.SplitHostPort(serverAddr)
if err != nil {
// It's possible that we have some weird serverAddr combined with weird PacketConn implementation,
// that doesn't follow the standard host:port format. So it's ok if we run into error here.
// Server name should be set in tlsConfig in that case.
serverName = ""
}
c := &Client{
serverAddr: serverAddr,
serverName: serverName,
sendBPS: sendBPS,
recvBPS: recvBPS,
auth: auth,
@@ -75,16 +87,12 @@ func (c *Client) connect() error {
_ = c.pktConn.Close()
}
// New connection
pktConn, err := c.pktConnFunc(c.serverAddr)
pktConn, sAddr, err := c.pktConnFunc(c.serverAddr)
if err != nil {
return err
}
serverUDPAddr, err := net.ResolveUDPAddr("udp", c.serverAddr)
if err != nil {
_ = pktConn.Close()
return err
}
quicConn, err := quic.Dial(pktConn, serverUDPAddr, c.serverAddr, c.tlsConfig, c.quicConfig)
// Dial QUIC
quicConn, err := quic.Dial(pktConn, sAddr, c.serverName, c.tlsConfig, c.quicConfig)
if err != nil {
_ = pktConn.Close()
return err