gecko/nat/no_router.go

61 lines
1.1 KiB
Go
Raw Normal View History

2020-06-15 12:16:24 -07:00
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
2020-06-12 14:40:41 -07:00
package nat
import (
"fmt"
"net"
"time"
)
const googleDNSServer = "8.8.8.8:80"
type noRouter struct {
2020-06-12 14:40:41 -07:00
ip net.IP
}
2020-06-15 12:16:24 -07:00
func (noRouter) MapPort(_ string, intPort, extPort uint16, _ string, _ time.Duration) error {
if intPort != extPort {
return fmt.Errorf("cannot map port %d to %d", intPort, extPort)
2020-06-12 14:40:41 -07:00
}
return nil
}
2020-06-15 12:16:24 -07:00
func (noRouter) UnmapPort(string, uint16, uint16) error {
2020-06-12 14:40:41 -07:00
return nil
}
func (r noRouter) ExternalIP() (net.IP, error) {
2020-06-12 14:40:41 -07:00
return r.ip, nil
}
func (noRouter) GetPortMappingEntry(uint16, string) (string, uint16, string, error) {
return "", 0, "", nil
}
2020-06-12 14:40:41 -07:00
func getOutboundIP() (net.IP, error) {
conn, err := net.Dial("udp", googleDNSServer)
if err != nil {
return nil, err
}
if udpAddr, ok := conn.LocalAddr().(*net.UDPAddr); ok {
return udpAddr.IP, conn.Close()
}
2020-06-17 14:13:35 -07:00
conn.Close()
return nil, fmt.Errorf("getting outbound IP failed")
2020-06-12 14:40:41 -07:00
}
2020-06-17 19:33:41 -07:00
// NewNoRouter returns a router that assumes the network is public
func NewNoRouter() Router {
2020-06-12 14:40:41 -07:00
ip, err := getOutboundIP()
if err != nil {
return nil
}
return &noRouter{
2020-06-12 14:40:41 -07:00
ip: ip,
}
}