github.com/phillinzzz/newBsc@v1.1.6/p2p/nat/natpmp.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package nat
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/phillinzzz/newBsc/common/gopool"
    26  	natpmp "github.com/jackpal/go-nat-pmp"
    27  )
    28  
    29  // natPMPClient adapts the NAT-PMP protocol implementation so it conforms to
    30  // the common interface.
    31  type pmp struct {
    32  	gw net.IP
    33  	c  *natpmp.Client
    34  }
    35  
    36  func (n *pmp) String() string {
    37  	return fmt.Sprintf("NAT-PMP(%v)", n.gw)
    38  }
    39  
    40  func (n *pmp) ExternalIP() (net.IP, error) {
    41  	response, err := n.c.GetExternalAddress()
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return response.ExternalIPAddress[:], nil
    46  }
    47  
    48  func (n *pmp) AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) error {
    49  	if lifetime <= 0 {
    50  		return fmt.Errorf("lifetime must not be <= 0")
    51  	}
    52  	// Note order of port arguments is switched between our
    53  	// AddMapping and the client's AddPortMapping.
    54  	_, err := n.c.AddPortMapping(strings.ToLower(protocol), intport, extport, int(lifetime/time.Second))
    55  	return err
    56  }
    57  
    58  func (n *pmp) DeleteMapping(protocol string, extport, intport int) (err error) {
    59  	// To destroy a mapping, send an add-port with an internalPort of
    60  	// the internal port to destroy, an external port of zero and a
    61  	// time of zero.
    62  	_, err = n.c.AddPortMapping(strings.ToLower(protocol), intport, 0, 0)
    63  	return err
    64  }
    65  
    66  func discoverPMP() Interface {
    67  	// run external address lookups on all potential gateways
    68  	gws := potentialGateways()
    69  	found := make(chan *pmp, len(gws))
    70  	for i := range gws {
    71  		gw := gws[i]
    72  		gopool.Submit(func() {
    73  			c := natpmp.NewClient(gw)
    74  			if _, err := c.GetExternalAddress(); err != nil {
    75  				found <- nil
    76  			} else {
    77  				found <- &pmp{gw, c}
    78  			}
    79  		})
    80  	}
    81  	// return the one that responds first.
    82  	// discovery needs to be quick, so we stop caring about
    83  	// any responses after a very short timeout.
    84  	timeout := time.NewTimer(1 * time.Second)
    85  	defer timeout.Stop()
    86  	for range gws {
    87  		select {
    88  		case c := <-found:
    89  			if c != nil {
    90  				return c
    91  			}
    92  		case <-timeout.C:
    93  			return nil
    94  		}
    95  	}
    96  	return nil
    97  }
    98  
    99  var (
   100  	// LAN IP ranges
   101  	_, lan10, _  = net.ParseCIDR("10.0.0.0/8")
   102  	_, lan176, _ = net.ParseCIDR("172.16.0.0/12")
   103  	_, lan192, _ = net.ParseCIDR("192.168.0.0/16")
   104  )
   105  
   106  // TODO: improve this. We currently assume that (on most networks)
   107  // the router is X.X.X.1 in a local LAN range.
   108  func potentialGateways() (gws []net.IP) {
   109  	ifaces, err := net.Interfaces()
   110  	if err != nil {
   111  		return nil
   112  	}
   113  	for _, iface := range ifaces {
   114  		ifaddrs, err := iface.Addrs()
   115  		if err != nil {
   116  			return gws
   117  		}
   118  		for _, addr := range ifaddrs {
   119  			if x, ok := addr.(*net.IPNet); ok {
   120  				if lan10.Contains(x.IP) || lan176.Contains(x.IP) || lan192.Contains(x.IP) {
   121  					ip := x.IP.Mask(x.Mask).To4()
   122  					if ip != nil {
   123  						ip[3] = ip[3] | 0x01
   124  						gws = append(gws, ip)
   125  					}
   126  				}
   127  			}
   128  		}
   129  	}
   130  	return gws
   131  }