2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-02-10 16:49:45 -06:00
|
|
|
package nat
|
|
|
|
|
|
|
|
import (
|
2024-02-29 03:56:46 -06:00
|
|
|
"errors"
|
2015-02-10 16:49:45 -06:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-11-18 02:49:18 -06:00
|
|
|
natpmp "github.com/jackpal/go-nat-pmp"
|
2015-02-10 16:49:45 -06:00
|
|
|
)
|
|
|
|
|
2024-04-15 07:58:17 -05:00
|
|
|
// pmp adapts the NAT-PMP protocol implementation so it conforms to
|
2015-02-10 16:49:45 -06:00
|
|
|
// the common interface.
|
|
|
|
type pmp struct {
|
|
|
|
gw net.IP
|
|
|
|
c *natpmp.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *pmp) String() string {
|
|
|
|
return fmt.Sprintf("NAT-PMP(%v)", n.gw)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *pmp) ExternalIP() (net.IP, error) {
|
|
|
|
response, err := n.c.GetExternalAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return response.ExternalIPAddress[:], nil
|
|
|
|
}
|
|
|
|
|
2023-07-14 07:58:27 -05:00
|
|
|
func (n *pmp) AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) (uint16, error) {
|
2015-02-10 16:49:45 -06:00
|
|
|
if lifetime <= 0 {
|
2024-02-29 03:56:46 -06:00
|
|
|
return 0, errors.New("lifetime must not be <= 0")
|
2015-02-10 16:49:45 -06:00
|
|
|
}
|
|
|
|
// Note order of port arguments is switched between our
|
|
|
|
// AddMapping and the client's AddPortMapping.
|
2022-12-07 09:22:04 -06:00
|
|
|
res, err := n.c.AddPortMapping(strings.ToLower(protocol), intport, extport, int(lifetime/time.Second))
|
|
|
|
if err != nil {
|
2023-07-14 07:58:27 -05:00
|
|
|
return 0, err
|
2022-12-07 09:22:04 -06:00
|
|
|
}
|
|
|
|
|
2023-07-14 07:58:27 -05:00
|
|
|
// NAT-PMP maps an alternative available port number if the requested port
|
|
|
|
// is already mapped to another address and returns success. Handling of
|
|
|
|
// alternate port numbers is done by the caller.
|
|
|
|
return res.MappedExternalPort, nil
|
2015-02-10 16:49:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *pmp) DeleteMapping(protocol string, extport, intport int) (err error) {
|
|
|
|
// To destroy a mapping, send an add-port with an internalPort of
|
|
|
|
// the internal port to destroy, an external port of zero and a
|
|
|
|
// time of zero.
|
|
|
|
_, err = n.c.AddPortMapping(strings.ToLower(protocol), intport, 0, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func discoverPMP() Interface {
|
|
|
|
// run external address lookups on all potential gateways
|
|
|
|
gws := potentialGateways()
|
|
|
|
found := make(chan *pmp, len(gws))
|
|
|
|
for i := range gws {
|
|
|
|
gw := gws[i]
|
|
|
|
go func() {
|
|
|
|
c := natpmp.NewClient(gw)
|
|
|
|
if _, err := c.GetExternalAddress(); err != nil {
|
|
|
|
found <- nil
|
|
|
|
} else {
|
|
|
|
found <- &pmp{gw, c}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
// return the one that responds first.
|
|
|
|
// discovery needs to be quick, so we stop caring about
|
|
|
|
// any responses after a very short timeout.
|
|
|
|
timeout := time.NewTimer(1 * time.Second)
|
|
|
|
defer timeout.Stop()
|
2017-01-06 08:52:03 -06:00
|
|
|
for range gws {
|
2015-02-10 16:49:45 -06:00
|
|
|
select {
|
|
|
|
case c := <-found:
|
|
|
|
if c != nil {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
case <-timeout.C:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: improve this. We currently assume that (on most networks)
|
|
|
|
// the router is X.X.X.1 in a local LAN range.
|
|
|
|
func potentialGateways() (gws []net.IP) {
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
ifaddrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return gws
|
|
|
|
}
|
|
|
|
for _, addr := range ifaddrs {
|
2018-07-30 04:30:09 -05:00
|
|
|
if x, ok := addr.(*net.IPNet); ok {
|
2022-11-25 09:30:07 -06:00
|
|
|
if x.IP.IsPrivate() {
|
2015-02-10 16:49:45 -06:00
|
|
|
ip := x.IP.Mask(x.Mask).To4()
|
|
|
|
if ip != nil {
|
|
|
|
ip[3] = ip[3] | 0x01
|
|
|
|
gws = append(gws, ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gws
|
|
|
|
}
|