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-13 12:29:28 -07:00
|
|
|
func (noRouter) MapPort(protocol string, intport, extport uint16, desc string, duration time.Duration) error {
|
2020-06-12 14:40:41 -07:00
|
|
|
if intport != extport {
|
|
|
|
return fmt.Errorf("cannot map port %d to %d", intport, extport)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-13 12:29:28 -07:00
|
|
|
func (noRouter) UnmapPort(protocol string, extport 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-13 12:29:28 -07:00
|
|
|
func (noRouter) IsMapped(uint16, string) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
return conn.LocalAddr().(*net.UDPAddr).IP, nil
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|