2022-09-17 20:07:00 +01:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2022-11-08 21:59:13 +00:00
|
|
|
"strconv"
|
2022-09-17 20:07:00 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Arceliar/phony"
|
|
|
|
)
|
|
|
|
|
|
|
|
type linkTCP struct {
|
|
|
|
phony.Inbox
|
|
|
|
*links
|
2023-04-06 21:45:49 +01:00
|
|
|
listenconfig *net.ListenConfig
|
|
|
|
_listeners map[*Listener]context.CancelFunc
|
2022-09-17 20:07:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *links) newLinkTCP() *linkTCP {
|
|
|
|
lt := &linkTCP{
|
|
|
|
links: l,
|
2023-04-06 21:45:49 +01:00
|
|
|
listenconfig: &net.ListenConfig{
|
2022-09-17 20:07:00 +01:00
|
|
|
KeepAlive: -1,
|
|
|
|
},
|
|
|
|
_listeners: map[*Listener]context.CancelFunc{},
|
|
|
|
}
|
2023-04-06 21:45:49 +01:00
|
|
|
lt.listenconfig.Control = lt.tcpContext
|
2022-09-17 20:07:00 +01:00
|
|
|
return lt
|
|
|
|
}
|
|
|
|
|
2022-11-26 16:18:15 +00:00
|
|
|
type tcpDialer struct {
|
|
|
|
info linkInfo
|
|
|
|
dialer *net.Dialer
|
|
|
|
addr *net.TCPAddr
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:45:49 +01:00
|
|
|
func (l *linkTCP) dialersFor(url *url.URL, info linkInfo) ([]*tcpDialer, error) {
|
2022-11-08 21:59:13 +00:00
|
|
|
host, p, err := net.SplitHostPort(url.Host)
|
2022-09-17 20:07:00 +01:00
|
|
|
if err != nil {
|
2022-11-26 16:18:15 +00:00
|
|
|
return nil, err
|
2022-09-17 20:07:00 +01:00
|
|
|
}
|
2022-11-08 21:59:13 +00:00
|
|
|
port, err := strconv.Atoi(p)
|
2022-09-17 20:07:00 +01:00
|
|
|
if err != nil {
|
2022-11-26 16:18:15 +00:00
|
|
|
return nil, err
|
2022-09-17 20:07:00 +01:00
|
|
|
}
|
2022-11-08 21:59:13 +00:00
|
|
|
ips, err := net.LookupIP(host)
|
2022-09-17 20:07:00 +01:00
|
|
|
if err != nil {
|
2022-11-26 16:18:15 +00:00
|
|
|
return nil, err
|
2022-09-17 20:07:00 +01:00
|
|
|
}
|
2022-11-26 16:18:15 +00:00
|
|
|
dialers := make([]*tcpDialer, 0, len(ips))
|
2022-11-08 21:59:13 +00:00
|
|
|
for _, ip := range ips {
|
|
|
|
addr := &net.TCPAddr{
|
|
|
|
IP: ip,
|
|
|
|
Port: port,
|
|
|
|
}
|
2023-04-06 21:45:49 +01:00
|
|
|
dialer, err := l.dialerFor(addr, info.sintf)
|
2022-11-08 21:59:13 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-26 16:18:15 +00:00
|
|
|
dialers = append(dialers, &tcpDialer{
|
|
|
|
info: info,
|
|
|
|
dialer: dialer,
|
|
|
|
addr: addr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return dialers, nil
|
|
|
|
}
|
|
|
|
|
2023-08-12 18:12:58 +01:00
|
|
|
func (l *linkTCP) dial(ctx context.Context, url *url.URL, info linkInfo, options linkOptions) (net.Conn, error) {
|
2023-04-06 21:45:49 +01:00
|
|
|
dialers, err := l.dialersFor(url, info)
|
2022-11-26 16:18:15 +00:00
|
|
|
if err != nil {
|
2023-04-06 21:45:49 +01:00
|
|
|
return nil, err
|
2022-11-26 16:18:15 +00:00
|
|
|
}
|
|
|
|
if len(dialers) == 0 {
|
2023-04-06 21:45:49 +01:00
|
|
|
return nil, nil
|
2022-11-26 16:18:15 +00:00
|
|
|
}
|
|
|
|
for _, d := range dialers {
|
|
|
|
var conn net.Conn
|
2023-08-12 18:12:58 +01:00
|
|
|
conn, err = d.dialer.DialContext(ctx, "tcp", d.addr.String())
|
2022-11-08 21:59:13 +00:00
|
|
|
if err != nil {
|
2022-11-26 16:18:15 +00:00
|
|
|
l.core.log.Warnf("Failed to connect to %s: %s", d.addr, err)
|
2022-11-08 21:59:13 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-04-06 21:45:49 +01:00
|
|
|
return conn, nil
|
2022-11-08 21:59:13 +00:00
|
|
|
}
|
2023-04-06 21:45:49 +01:00
|
|
|
return nil, err
|
2022-09-17 20:07:00 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 21:45:49 +01:00
|
|
|
func (l *linkTCP) listen(ctx context.Context, url *url.URL, sintf string) (net.Listener, error) {
|
2022-09-17 20:07:00 +01:00
|
|
|
hostport := url.Host
|
|
|
|
if sintf != "" {
|
|
|
|
if host, port, err := net.SplitHostPort(hostport); err == nil {
|
|
|
|
hostport = fmt.Sprintf("[%s%%%s]:%s", host, sintf, port)
|
|
|
|
}
|
|
|
|
}
|
2023-04-06 21:45:49 +01:00
|
|
|
return l.listenconfig.Listen(ctx, "tcp", hostport)
|
2022-09-17 20:07:00 +01:00
|
|
|
}
|
|
|
|
|
2022-10-21 19:49:15 +01:00
|
|
|
func (l *linkTCP) dialerFor(dst *net.TCPAddr, sintf string) (*net.Dialer, error) {
|
2022-09-17 20:07:00 +01:00
|
|
|
if dst.IP.IsLinkLocalUnicast() {
|
2022-10-21 19:49:15 +01:00
|
|
|
if sintf != "" {
|
|
|
|
dst.Zone = sintf
|
|
|
|
}
|
2022-09-17 20:07:00 +01:00
|
|
|
if dst.Zone == "" {
|
|
|
|
return nil, fmt.Errorf("link-local address requires a zone")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dialer := &net.Dialer{
|
|
|
|
Timeout: time.Second * 5,
|
|
|
|
KeepAlive: -1,
|
|
|
|
Control: l.tcpContext,
|
|
|
|
}
|
|
|
|
if sintf != "" {
|
|
|
|
dialer.Control = l.getControl(sintf)
|
|
|
|
ief, err := net.InterfaceByName(sintf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("interface %q not found", sintf)
|
|
|
|
}
|
|
|
|
if ief.Flags&net.FlagUp == 0 {
|
|
|
|
return nil, fmt.Errorf("interface %q is not up", sintf)
|
|
|
|
}
|
|
|
|
addrs, err := ief.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("interface %q addresses not available: %w", sintf, err)
|
|
|
|
}
|
|
|
|
for addrindex, addr := range addrs {
|
|
|
|
src, _, err := net.ParseCIDR(addr.String())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !src.IsGlobalUnicast() && !src.IsLinkLocalUnicast() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bothglobal := src.IsGlobalUnicast() == dst.IP.IsGlobalUnicast()
|
|
|
|
bothlinklocal := src.IsLinkLocalUnicast() == dst.IP.IsLinkLocalUnicast()
|
|
|
|
if !bothglobal && !bothlinklocal {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (src.To4() != nil) != (dst.IP.To4() != nil) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if bothglobal || bothlinklocal || addrindex == len(addrs)-1 {
|
|
|
|
dialer.LocalAddr = &net.TCPAddr{
|
|
|
|
IP: src,
|
|
|
|
Port: 0,
|
|
|
|
Zone: sintf,
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dialer.LocalAddr == nil {
|
|
|
|
return nil, fmt.Errorf("no suitable source address found on interface %q", sintf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dialer, nil
|
|
|
|
}
|