5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 06:01:37 +00:00

Merge branch 'develop' into dependabot/go_modules/golang.org/x/net-0.7.0

This commit is contained in:
Neil 2023-02-26 21:27:56 +00:00 committed by GitHub
commit a6f742ee93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 38 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net" "net"
"net/url" "net/url"
"strconv"
"strings" "strings"
"time" "time"
@ -30,29 +31,71 @@ func (l *links) newLinkTCP() *linkTCP {
return lt return lt
} }
type tcpDialer struct {
info linkInfo
dialer *net.Dialer
addr *net.TCPAddr
}
func (l *linkTCP) dialersFor(url *url.URL, options linkOptions, sintf string) ([]*tcpDialer, error) {
host, p, err := net.SplitHostPort(url.Host)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(p)
if err != nil {
return nil, err
}
ips, err := net.LookupIP(host)
if err != nil {
return nil, err
}
dialers := make([]*tcpDialer, 0, len(ips))
for _, ip := range ips {
addr := &net.TCPAddr{
IP: ip,
Port: port,
}
dialer, err := l.dialerFor(addr, sintf)
if err != nil {
continue
}
info := linkInfoFor("tcp", sintf, tcpIDFor(dialer.LocalAddr, addr))
if l.links.isConnectedTo(info) {
return nil, nil
}
dialers = append(dialers, &tcpDialer{
info: info,
dialer: dialer,
addr: addr,
})
}
return dialers, nil
}
func (l *linkTCP) dial(url *url.URL, options linkOptions, sintf string) error { func (l *linkTCP) dial(url *url.URL, options linkOptions, sintf string) error {
addr, err := net.ResolveTCPAddr("tcp", url.Host) dialers, err := l.dialersFor(url, options, sintf)
if err != nil { if err != nil {
return err return err
} }
dialer, err := l.dialerFor(addr, sintf) if len(dialers) == 0 {
if err != nil {
return err
}
info := linkInfoFor("tcp", sintf, tcpIDFor(dialer.LocalAddr, addr))
if l.links.isConnectedTo(info) {
return nil return nil
} }
conn, err := dialer.DialContext(l.core.ctx, "tcp", addr.String()) for _, d := range dialers {
if err != nil { var conn net.Conn
return err conn, err = d.dialer.DialContext(l.core.ctx, "tcp", d.addr.String())
if err != nil {
l.core.log.Warnf("Failed to connect to %s: %s", d.addr, err)
continue
}
name := strings.TrimRight(strings.SplitN(url.String(), "?", 2)[0], "/")
dial := &linkDial{
url: url,
sintf: sintf,
}
return l.handler(dial, name, d.info, conn, options, false, false)
} }
name := strings.TrimRight(strings.SplitN(url.String(), "?", 2)[0], "/") return fmt.Errorf("failed to connect via %d address(es), last error: %w", len(dialers), err)
dial := &linkDial{
url: url,
sintf: sintf,
}
return l.handler(dial, name, info, conn, options, false, false)
} }
func (l *linkTCP) listen(url *url.URL, sintf string) (*Listener, error) { func (l *linkTCP) listen(url *url.URL, sintf string) (*Listener, error) {

View File

@ -47,34 +47,33 @@ func (l *links) newLinkTLS(tcp *linkTCP) *linkTLS {
} }
func (l *linkTLS) dial(url *url.URL, options linkOptions, sintf, sni string) error { func (l *linkTLS) dial(url *url.URL, options linkOptions, sintf, sni string) error {
addr, err := net.ResolveTCPAddr("tcp", url.Host) dialers, err := l.tcp.dialersFor(url, options, sintf)
if err != nil { if err != nil {
return err return err
} }
dialer, err := l.tcp.dialerFor(addr, sintf) if len(dialers) == 0 {
if err != nil {
return err
}
info := linkInfoFor("tls", sintf, tcpIDFor(dialer.LocalAddr, addr))
if l.links.isConnectedTo(info) {
return nil return nil
} }
tlsconfig := l.config.Clone() for _, d := range dialers {
tlsconfig.ServerName = sni tlsconfig := l.config.Clone()
tlsdialer := &tls.Dialer{ tlsconfig.ServerName = sni
NetDialer: dialer, tlsdialer := &tls.Dialer{
Config: tlsconfig, NetDialer: d.dialer,
Config: tlsconfig,
}
var conn net.Conn
conn, err = tlsdialer.DialContext(l.core.ctx, "tcp", d.addr.String())
if err != nil {
continue
}
name := strings.TrimRight(strings.SplitN(url.String(), "?", 2)[0], "/")
dial := &linkDial{
url: url,
sintf: sintf,
}
return l.handler(dial, name, d.info, conn, options, false, false)
} }
conn, err := tlsdialer.DialContext(l.core.ctx, "tcp", addr.String()) return fmt.Errorf("failed to connect via %d address(es), last error: %w", len(dialers), err)
if err != nil {
return err
}
name := strings.TrimRight(strings.SplitN(url.String(), "?", 2)[0], "/")
dial := &linkDial{
url: url,
sintf: sintf,
}
return l.handler(dial, name, info, conn, options, false, false)
} }
func (l *linkTLS) listen(url *url.URL, sintf string) (*Listener, error) { func (l *linkTLS) listen(url *url.URL, sintf string) (*Listener, error) {