glider/proxy/direct.go

152 lines
3.4 KiB
Go
Raw Normal View History

package proxy
2017-07-13 21:55:41 +08:00
2018-01-17 00:26:38 +08:00
import (
"context"
"errors"
2018-01-17 00:26:38 +08:00
"net"
2022-01-28 23:35:29 +08:00
"net/netip"
"time"
"github.com/nadoo/glider/pkg/sockopt"
2018-01-17 00:26:38 +08:00
)
2017-07-13 21:55:41 +08:00
2020-09-24 18:50:04 +08:00
// Direct proxy.
type Direct struct {
iface *net.Interface // interface specified by user
ip net.IP
dialTimeout time.Duration
relayTimeout time.Duration
}
func init() {
RegisterDialer("direct", NewDirectDialer)
}
// NewDirect returns a Direct dialer.
func NewDirect(intface string, dialTimeout, relayTimeout time.Duration) (*Direct, error) {
d := &Direct{dialTimeout: dialTimeout, relayTimeout: relayTimeout}
if intface != "" {
2022-01-28 23:35:29 +08:00
if addr, err := netip.ParseAddr(intface); err == nil {
d.ip = addr.AsSlice()
} else {
iface, err := net.InterfaceByName(intface)
if err != nil {
return nil, errors.New(err.Error() + ": " + intface)
}
d.iface = iface
}
2018-08-18 23:59:21 +08:00
}
return d, nil
}
2017-07-13 21:55:41 +08:00
// NewDirectDialer returns a direct dialer.
func NewDirectDialer(s string, d Dialer) (Dialer, error) {
2021-07-02 19:09:01 +08:00
if d == nil {
return NewDirect("", time.Duration(3)*time.Second, time.Duration(3)*time.Second)
}
return d, nil
}
// Addr returns forwarder's address.
func (d *Direct) Addr() string { return "DIRECT" }
2017-07-13 21:55:41 +08:00
// Dial connects to the address addr on the network net
func (d *Direct) Dial(network, addr string) (c net.Conn, err error) {
if d.iface == nil || d.ip != nil {
c, err = d.dial(network, addr, d.ip)
if err == nil {
return
}
2018-08-20 00:17:16 +08:00
}
for _, ip := range d.IFaceIPs() {
c, err = d.dial(network, addr, ip)
if err == nil {
2018-08-20 00:17:16 +08:00
d.ip = ip
break
}
}
2018-08-20 00:17:16 +08:00
// no ip available (so no dials made), maybe the interface link is down
if c == nil && err == nil {
err = errors.New("dial failed, maybe the interface link is down, please check it")
}
return c, err
}
func (d *Direct) dial(network, addr string, localIP net.IP) (net.Conn, error) {
2018-08-20 00:17:16 +08:00
var la net.Addr
switch network {
case "tcp":
2018-08-20 00:17:16 +08:00
la = &net.TCPAddr{IP: localIP}
case "udp":
2018-08-20 00:17:16 +08:00
la = &net.UDPAddr{IP: localIP}
}
dialer := &net.Dialer{LocalAddr: la, Timeout: d.dialTimeout}
if d.iface != nil {
2022-02-24 18:41:03 +08:00
dialer.Control = sockopt.Control(sockopt.Bind(d.iface))
}
c, err := dialer.Dial(network, addr)
2018-01-17 00:26:38 +08:00
if err != nil {
return nil, err
}
2017-07-13 21:55:41 +08:00
if c, ok := c.(*net.TCPConn); ok {
c.SetKeepAlive(true)
}
2018-01-17 00:26:38 +08:00
2020-05-05 01:30:57 +08:00
if d.relayTimeout > 0 {
c.SetDeadline(time.Now().Add(d.relayTimeout))
}
2017-07-13 21:55:41 +08:00
return c, err
}
// DialUDP connects to the given address.
func (d *Direct) DialUDP(network, addr string) (net.PacketConn, error) {
2020-04-14 00:17:46 +08:00
var la string
2018-08-20 00:17:16 +08:00
if d.ip != nil {
2020-12-03 23:47:26 +08:00
la = net.JoinHostPort(d.ip.String(), "0")
2018-08-20 00:17:16 +08:00
}
lc := &net.ListenConfig{}
if d.iface != nil {
2022-02-24 18:41:03 +08:00
lc.Control = sockopt.Control(sockopt.Bind(d.iface))
}
return lc.ListenPacket(context.Background(), network, la)
2018-01-17 00:26:38 +08:00
}
// IFaceIPs returns ip addresses according to the specified interface.
2018-08-20 00:17:16 +08:00
func (d *Direct) IFaceIPs() (ips []net.IP) {
2022-01-08 23:57:30 +08:00
ipNets, err := d.iface.Addrs()
if err != nil {
return
}
2022-01-08 23:57:30 +08:00
for _, ipNet := range ipNets {
ips = append(ips, ipNet.(*net.IPNet).IP) //!ip.IsLinkLocalUnicast()
}
return
}
func init() {
AddUsage("direct", `
Direct scheme:
direct://
2024-04-28 23:00:11 +08:00
Only needed when you want to specify the outgoing interface:
glider -verbose -listen :8443 -forward direct://#interface=eth0
Or load balance multiple interfaces directly:
glider -verbose -listen :8443 -forward direct://#interface=eth0 -forward direct://#interface=eth1 -strategy rr
Or you can use the high availability mode:
glider -verbose -listen :8443 -forward direct://#interface=eth0&priority=100 -forward direct://#interface=eth1&priority=200 -strategy ha
`)
}