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"
|
|
|
|
|
2020-06-13 12:29:28 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-13 12:29:28 -07:00
|
|
|
func (r noRouter) ExternalIP() (net.IP, error) {
|
2020-06-12 14:40:41 -07:00
|
|
|
return r.ip, nil
|
|
|
|
}
|
|
|
|
|
2020-06-14 00:51:31 -07:00
|
|
|
func (noRouter) GetPortMappingEntry(uint16, string) (string, uint16, string, error) {
|
|
|
|
return "", 0, "", nil
|
2020-06-13 12:29:28 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-16 08:29:50 -07:00
|
|
|
if udpAddr, ok := conn.LocalAddr().(*net.UDPAddr); ok {
|
|
|
|
return udpAddr.IP, conn.Close()
|
|
|
|
}
|
|
|
|
|
2020-06-17 14:13:35 -07:00
|
|
|
conn.Close()
|
2020-06-16 08:29:50 -07:00
|
|
|
return nil, fmt.Errorf("getting outbound IP failed")
|
2020-06-12 14:40:41 -07:00
|
|
|
}
|
|
|
|
|
2020-06-13 12:29:28 -07:00
|
|
|
func NewNoRouter() *noRouter {
|
2020-06-12 14:40:41 -07:00
|
|
|
ip, err := getOutboundIP()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-13 12:29:28 -07:00
|
|
|
return &noRouter{
|
2020-06-12 14:40:41 -07:00
|
|
|
ip: ip,
|
|
|
|
}
|
|
|
|
}
|