2018-06-26 16:15:48 +08:00
|
|
|
package proxy
|
2017-07-13 21:55:41 +08:00
|
|
|
|
2018-01-17 00:26:38 +08:00
|
|
|
import (
|
2022-01-08 15:05:55 +08:00
|
|
|
"context"
|
2018-08-20 22:23:00 +08:00
|
|
|
"errors"
|
2018-01-17 00:26:38 +08:00
|
|
|
"net"
|
2022-01-28 23:35:29 +08:00
|
|
|
"net/netip"
|
2020-05-03 20:02:11 +08:00
|
|
|
"time"
|
2018-06-26 16:15:48 +08:00
|
|
|
|
2022-01-08 15:05:55 +08:00
|
|
|
"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.
|
2018-08-15 00:54:17 +08:00
|
|
|
type Direct struct {
|
2020-05-04 16:51:41 +08:00
|
|
|
iface *net.Interface // interface specified by user
|
|
|
|
ip net.IP
|
|
|
|
dialTimeout time.Duration
|
|
|
|
relayTimeout time.Duration
|
2018-08-15 00:54:17 +08:00
|
|
|
}
|
|
|
|
|
2021-06-29 19:55:05 +08:00
|
|
|
func init() {
|
|
|
|
RegisterDialer("direct", NewDirectDialer)
|
|
|
|
}
|
|
|
|
|
2020-05-04 16:51:41 +08:00
|
|
|
// 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()
|
2020-05-04 16:51:41 +08:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2020-05-04 16:51:41 +08:00
|
|
|
return d, nil
|
2018-08-15 00:54:17 +08:00
|
|
|
}
|
2017-07-13 21:55:41 +08:00
|
|
|
|
2021-06-29 19:55:05 +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
|
2021-06-29 19:55:05 +08:00
|
|
|
}
|
|
|
|
|
2020-05-04 16:51:41 +08:00
|
|
|
// Addr returns forwarder's address.
|
2018-08-15 00:54:17 +08:00
|
|
|
func (d *Direct) Addr() string { return "DIRECT" }
|
2017-07-13 21:55:41 +08:00
|
|
|
|
2018-08-15 00:54:17 +08:00
|
|
|
// Dial connects to the address addr on the network net
|
2019-09-18 19:40:14 +08:00
|
|
|
func (d *Direct) Dial(network, addr string) (c net.Conn, err error) {
|
2018-08-20 22:23:00 +08:00
|
|
|
if d.iface == nil || d.ip != nil {
|
2020-05-04 16:51:41 +08:00
|
|
|
c, err = d.dial(network, addr, d.ip)
|
2018-08-20 22:23:00 +08:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-08-20 00:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range d.IFaceIPs() {
|
2020-05-04 16:51:41 +08:00
|
|
|
c, err = d.dial(network, addr, ip)
|
2018-08-19 01:49:52 +08:00
|
|
|
if err == nil {
|
2018-08-20 00:17:16 +08:00
|
|
|
d.ip = ip
|
2018-08-19 01:49:52 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-08-20 00:17:16 +08:00
|
|
|
|
2018-08-30 07:35:37 +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")
|
|
|
|
}
|
|
|
|
|
2019-09-18 19:40:14 +08:00
|
|
|
return c, err
|
2018-08-19 01:49:52 +08:00
|
|
|
}
|
|
|
|
|
2020-05-04 16:51:41 +08:00
|
|
|
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
|
2018-08-19 01:49:52 +08:00
|
|
|
switch network {
|
|
|
|
case "tcp":
|
2018-08-20 00:17:16 +08:00
|
|
|
la = &net.TCPAddr{IP: localIP}
|
2018-08-19 01:49:52 +08:00
|
|
|
case "udp":
|
2018-08-20 00:17:16 +08:00
|
|
|
la = &net.UDPAddr{IP: localIP}
|
2018-08-19 01:49:52 +08:00
|
|
|
}
|
|
|
|
|
2020-05-04 16:51:41 +08:00
|
|
|
dialer := &net.Dialer{LocalAddr: la, Timeout: d.dialTimeout}
|
2021-12-28 20:11:28 +08:00
|
|
|
if d.iface != nil {
|
2022-02-24 18:41:03 +08:00
|
|
|
dialer.Control = sockopt.Control(sockopt.Bind(d.iface))
|
2021-12-28 20:11:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-19 01:49:52 +08:00
|
|
|
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 {
|
2020-05-04 16:51:41 +08:00
|
|
|
c.SetDeadline(time.Now().Add(d.relayTimeout))
|
|
|
|
}
|
|
|
|
|
2017-07-13 21:55:41 +08:00
|
|
|
return c, err
|
|
|
|
}
|
2017-08-23 17:45:57 +08:00
|
|
|
|
2020-05-04 16:51:41 +08:00
|
|
|
// DialUDP connects to the given address.
|
2022-03-11 19:08:07 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-08 15:05:55 +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))
|
2022-01-08 15:05:55 +08:00
|
|
|
}
|
|
|
|
|
2022-03-11 19:08:07 +08:00
|
|
|
return lc.ListenPacket(context.Background(), network, la)
|
2018-01-17 00:26:38 +08:00
|
|
|
}
|
|
|
|
|
2020-05-04 16:51:41 +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()
|
2018-08-19 01:49:52 +08:00
|
|
|
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()
|
2018-08-19 01:49:52 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-05-03 18:19:33 +08:00
|
|
|
|
|
|
|
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:
|
2022-05-03 18:19:33 +08:00
|
|
|
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
|
|
|
|
`)
|
|
|
|
}
|