glider/proxy/direct.go

124 lines
2.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 (
"errors"
2018-01-17 00:26:38 +08:00
"net"
"time"
"github.com/nadoo/glider/common/log"
2018-01-17 00:26:38 +08:00
)
2017-07-13 21:55:41 +08:00
// Direct proxy
type Direct struct {
2018-08-20 00:17:16 +08:00
iface *net.Interface // interface specified by user
ip net.IP
}
// Default dialer
var Default = &Direct{}
// NewDirect returns a Direct dialer
func NewDirect(intface string) (*Direct, error) {
if intface == "" {
return &Direct{}, nil
}
2018-08-18 23:59:21 +08:00
ip := net.ParseIP(intface)
if ip != nil {
return &Direct{ip: ip}, nil
2018-08-18 23:59:21 +08:00
}
iface, err := net.InterfaceByName(intface)
if err != nil {
return nil, errors.New(err.Error() + ": " + intface)
2018-08-18 23:59:21 +08:00
}
return &Direct{iface: iface}, nil
}
2017-07-13 21:55:41 +08:00
// 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 = dial(network, addr, d.ip)
if err == nil {
return
}
2018-08-20 00:17:16 +08:00
}
for _, ip := range d.IFaceIPs() {
c, err = 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
}
2018-08-20 00:17:16 +08:00
func dial(network, addr string, localIP net.IP) (net.Conn, error) {
if network == "uot" {
network = "udp"
}
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: time.Second * 3}
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
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, net.Addr, error) {
// TODO: support specifying local interface
2020-04-14 00:17:46 +08:00
var la string
2018-08-20 00:17:16 +08:00
if d.ip != nil {
la = d.ip.String() + ":0"
}
pc, err := net.ListenPacket(network, la)
2018-01-17 00:26:38 +08:00
if err != nil {
log.F("ListenPacket error: %s", err)
2018-01-17 00:26:38 +08:00
return nil, nil, err
}
uAddr, err := net.ResolveUDPAddr("udp", addr)
2018-01-18 18:12:17 +08:00
return pc, uAddr, err
2018-01-17 00:26:38 +08:00
}
2018-08-20 00:17:16 +08:00
// IFaceIPs returns ip addresses according to the specified interface
func (d *Direct) IFaceIPs() (ips []net.IP) {
ipnets, err := d.iface.Addrs()
if err != nil {
return
}
for _, ipnet := range ipnets {
ips = append(ips, ipnet.(*net.IPNet).IP) //!ip.IsLinkLocalUnicast()
}
return
}