lib/connections: switch statement to get the QUIC network (#7816)

This commit is contained in:
greatroar 2021-07-10 13:53:51 +02:00 committed by GitHub
parent a56f70ab94
commit 1e21042138
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 5 deletions

View File

@ -13,7 +13,6 @@ import (
"crypto/tls" "crypto/tls"
"net" "net"
"net/url" "net/url"
"strings"
"time" "time"
"github.com/lucas-clemente/quic-go" "github.com/lucas-clemente/quic-go"
@ -46,7 +45,7 @@ type quicDialer struct {
func (d *quicDialer) Dial(ctx context.Context, _ protocol.DeviceID, uri *url.URL) (internalConn, error) { func (d *quicDialer) Dial(ctx context.Context, _ protocol.DeviceID, uri *url.URL) (internalConn, error) {
uri = fixupPort(uri, config.DefaultQUICPort) uri = fixupPort(uri, config.DefaultQUICPort)
network := strings.ReplaceAll(uri.Scheme, "quic", "udp") network := quicNetwork(uri)
addr, err := net.ResolveUDPAddr(network, uri.Host) addr, err := net.ResolveUDPAddr(network, uri.Host)
if err != nil { if err != nil {

View File

@ -13,7 +13,6 @@ import (
"crypto/tls" "crypto/tls"
"net" "net"
"net/url" "net/url"
"strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"syscall" "syscall"
@ -81,7 +80,7 @@ func (t *quicListener) OnExternalAddressChanged(address *stun.Host, via string)
} }
func (t *quicListener) serve(ctx context.Context) error { func (t *quicListener) serve(ctx context.Context) error {
network := strings.ReplaceAll(t.uri.Scheme, "quic", "udp") network := quicNetwork(t.uri)
udpAddr, err := net.ResolveUDPAddr(network, t.uri.Host) udpAddr, err := net.ResolveUDPAddr(network, t.uri.Host)
if err != nil { if err != nil {
@ -204,7 +203,7 @@ func (t *quicListener) LANAddresses() []*url.URL {
uri := maybeReplacePort(t.uri, t.laddr) uri := maybeReplacePort(t.uri, t.laddr)
t.mut.Unlock() t.mut.Unlock()
addrs := []*url.URL{uri} addrs := []*url.URL{uri}
network := strings.ReplaceAll(uri.Scheme, "quic", "udp") network := quicNetwork(uri)
addrs = append(addrs, getURLsForAllAdaptersIfUnspecified(network, uri)...) addrs = append(addrs, getURLsForAllAdaptersIfUnspecified(network, uri)...)
return addrs return addrs
} }

View File

@ -11,6 +11,7 @@ package connections
import ( import (
"crypto/tls" "crypto/tls"
"net" "net"
"net/url"
"github.com/lucas-clemente/quic-go" "github.com/lucas-clemente/quic-go"
"github.com/syncthing/syncthing/lib/util" "github.com/syncthing/syncthing/lib/util"
@ -23,6 +24,17 @@ var (
} }
) )
func quicNetwork(uri *url.URL) string {
switch uri.Scheme {
case "quic4":
return "udp4"
case "quic6":
return "udp6"
default:
return "udp"
}
}
type quicTlsConn struct { type quicTlsConn struct {
quic.Session quic.Session
quic.Stream quic.Stream