mirror of
https://github.com/yggdrasil-network/yggdrasil-go
synced 2024-11-09 23:20:26 +03:00
Merge pull request #981 from yggdrasil-network/neilalexander/tryall
Try all addresses when connecting to a DNS name
This commit is contained in:
commit
4189053cfc
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -30,29 +31,71 @@ func (l *links) newLinkTCP() *linkTCP {
|
||||
return lt
|
||||
}
|
||||
|
||||
func (l *linkTCP) dial(url *url.URL, options linkOptions, sintf string) error {
|
||||
addr, err := net.ResolveTCPAddr("tcp", url.Host)
|
||||
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 err
|
||||
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 {
|
||||
return err
|
||||
continue
|
||||
}
|
||||
info := linkInfoFor("tcp", sintf, tcpIDFor(dialer.LocalAddr, addr))
|
||||
if l.links.isConnectedTo(info) {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
conn, err := dialer.DialContext(l.core.ctx, "tcp", addr.String())
|
||||
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 {
|
||||
dialers, err := l.dialersFor(url, options, sintf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(dialers) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, d := range dialers {
|
||||
var conn net.Conn
|
||||
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, info, conn, options, false, false)
|
||||
return l.handler(dial, name, d.info, conn, options, false, false)
|
||||
}
|
||||
return fmt.Errorf("failed to connect via %d address(es), last error: %w", len(dialers), err)
|
||||
}
|
||||
|
||||
func (l *linkTCP) listen(url *url.URL, sintf string) (*Listener, error) {
|
||||
|
@ -47,34 +47,33 @@ func (l *links) newLinkTLS(tcp *linkTCP) *linkTLS {
|
||||
}
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
dialer, err := l.tcp.dialerFor(addr, sintf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
info := linkInfoFor("tls", sintf, tcpIDFor(dialer.LocalAddr, addr))
|
||||
if l.links.isConnectedTo(info) {
|
||||
if len(dialers) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, d := range dialers {
|
||||
tlsconfig := l.config.Clone()
|
||||
tlsconfig.ServerName = sni
|
||||
tlsdialer := &tls.Dialer{
|
||||
NetDialer: dialer,
|
||||
NetDialer: d.dialer,
|
||||
Config: tlsconfig,
|
||||
}
|
||||
conn, err := tlsdialer.DialContext(l.core.ctx, "tcp", addr.String())
|
||||
var conn net.Conn
|
||||
conn, err = tlsdialer.DialContext(l.core.ctx, "tcp", d.addr.String())
|
||||
if err != nil {
|
||||
return err
|
||||
continue
|
||||
}
|
||||
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)
|
||||
return l.handler(dial, name, d.info, conn, options, false, false)
|
||||
}
|
||||
return fmt.Errorf("failed to connect via %d address(es), last error: %w", len(dialers), err)
|
||||
}
|
||||
|
||||
func (l *linkTLS) listen(url *url.URL, sintf string) (*Listener, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user