|
|
@@ -29,6 +29,7 @@ import (
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
|
|
+ "github.com/ethereum/go-ethereum/p2p/netutil"
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
)
|
|
|
|
|
|
@@ -126,8 +127,16 @@ func makeEndpoint(addr *net.UDPAddr, tcpPort uint16) rpcEndpoint {
|
|
|
return rpcEndpoint{IP: ip, UDP: uint16(addr.Port), TCP: tcpPort}
|
|
|
}
|
|
|
|
|
|
-func nodeFromRPC(rn rpcNode) (*Node, error) {
|
|
|
- // TODO: don't accept localhost, LAN addresses from internet hosts
|
|
|
+func (t *udp) nodeFromRPC(sender *net.UDPAddr, rn rpcNode) (*Node, error) {
|
|
|
+ if rn.UDP <= 1024 {
|
|
|
+ return nil, errors.New("low port")
|
|
|
+ }
|
|
|
+ if err := netutil.CheckRelayIP(sender.IP, rn.IP); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if t.netrestrict != nil && !t.netrestrict.Contains(rn.IP) {
|
|
|
+ return nil, errors.New("not contained in netrestrict whitelist")
|
|
|
+ }
|
|
|
n := NewNode(rn.ID, rn.IP, rn.UDP, rn.TCP)
|
|
|
err := n.validateComplete()
|
|
|
return n, err
|
|
|
@@ -151,6 +160,7 @@ type conn interface {
|
|
|
// udp implements the RPC protocol.
|
|
|
type udp struct {
|
|
|
conn conn
|
|
|
+ netrestrict *netutil.Netlist
|
|
|
priv *ecdsa.PrivateKey
|
|
|
ourEndpoint rpcEndpoint
|
|
|
|
|
|
@@ -201,7 +211,7 @@ type reply struct {
|
|
|
}
|
|
|
|
|
|
// ListenUDP returns a new table that listens for UDP packets on laddr.
|
|
|
-func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, nodeDBPath string) (*Table, error) {
|
|
|
+func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, error) {
|
|
|
addr, err := net.ResolveUDPAddr("udp", laddr)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
@@ -210,7 +220,7 @@ func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, nodeDBP
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- tab, _, err := newUDP(priv, conn, natm, nodeDBPath)
|
|
|
+ tab, _, err := newUDP(priv, conn, natm, nodeDBPath, netrestrict)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
@@ -218,13 +228,14 @@ func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, nodeDBP
|
|
|
return tab, nil
|
|
|
}
|
|
|
|
|
|
-func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, nodeDBPath string) (*Table, *udp, error) {
|
|
|
+func newUDP(priv *ecdsa.PrivateKey, c conn, natm nat.Interface, nodeDBPath string, netrestrict *netutil.Netlist) (*Table, *udp, error) {
|
|
|
udp := &udp{
|
|
|
- conn: c,
|
|
|
- priv: priv,
|
|
|
- closing: make(chan struct{}),
|
|
|
- gotreply: make(chan reply),
|
|
|
- addpending: make(chan *pending),
|
|
|
+ conn: c,
|
|
|
+ priv: priv,
|
|
|
+ netrestrict: netrestrict,
|
|
|
+ closing: make(chan struct{}),
|
|
|
+ gotreply: make(chan reply),
|
|
|
+ addpending: make(chan *pending),
|
|
|
}
|
|
|
realaddr := c.LocalAddr().(*net.UDPAddr)
|
|
|
if natm != nil {
|
|
|
@@ -281,9 +292,12 @@ func (t *udp) findnode(toid NodeID, toaddr *net.UDPAddr, target NodeID) ([]*Node
|
|
|
reply := r.(*neighbors)
|
|
|
for _, rn := range reply.Nodes {
|
|
|
nreceived++
|
|
|
- if n, err := nodeFromRPC(rn); err == nil {
|
|
|
- nodes = append(nodes, n)
|
|
|
+ n, err := t.nodeFromRPC(toaddr, rn)
|
|
|
+ if err != nil {
|
|
|
+ glog.V(logger.Detail).Infof("invalid neighbor node (%v) from %v: %v", rn.IP, toaddr, err)
|
|
|
+ continue
|
|
|
}
|
|
|
+ nodes = append(nodes, n)
|
|
|
}
|
|
|
return nreceived >= bucketSize
|
|
|
})
|
|
|
@@ -479,13 +493,6 @@ func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) ([]byte,
|
|
|
return packet, nil
|
|
|
}
|
|
|
|
|
|
-func isTemporaryError(err error) bool {
|
|
|
- tempErr, ok := err.(interface {
|
|
|
- Temporary() bool
|
|
|
- })
|
|
|
- return ok && tempErr.Temporary() || isPacketTooBig(err)
|
|
|
-}
|
|
|
-
|
|
|
// readLoop runs in its own goroutine. it handles incoming UDP packets.
|
|
|
func (t *udp) readLoop() {
|
|
|
defer t.conn.Close()
|
|
|
@@ -495,7 +502,7 @@ func (t *udp) readLoop() {
|
|
|
buf := make([]byte, 1280)
|
|
|
for {
|
|
|
nbytes, from, err := t.conn.ReadFromUDP(buf)
|
|
|
- if isTemporaryError(err) {
|
|
|
+ if netutil.IsTemporaryError(err) {
|
|
|
// Ignore temporary read errors.
|
|
|
glog.V(logger.Debug).Infof("Temporary read error: %v", err)
|
|
|
continue
|
|
|
@@ -602,6 +609,9 @@ func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte
|
|
|
// Send neighbors in chunks with at most maxNeighbors per packet
|
|
|
// to stay below the 1280 byte limit.
|
|
|
for i, n := range closest {
|
|
|
+ if netutil.CheckRelayIP(from.IP, n.IP) != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
p.Nodes = append(p.Nodes, nodeToRPC(n))
|
|
|
if len(p.Nodes) == maxNeighbors || i == len(closest)-1 {
|
|
|
t.send(from, neighborsPacket, p)
|