gecko/nat/no_router.go

53 lines
928 B
Go
Raw Normal View History

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
}
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
}
func (noRouter) UnmapPort(protocol string, extport 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
}
defer conn.Close()
return conn.LocalAddr().(*net.UDPAddr).IP, nil
}
func NewNoRouter() *noRouter {
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,
}
}