2014-10-23 18:57:54 +03:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-02-05 04:07:58 +02:00
|
|
|
"crypto/ecdsa"
|
2014-11-21 22:48:49 +02:00
|
|
|
"errors"
|
2014-10-23 18:57:54 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-02-05 04:07:58 +02:00
|
|
|
"runtime"
|
2014-10-23 18:57:54 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-03-04 13:03:43 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2014-11-21 22:48:49 +02:00
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
2015-02-05 04:07:58 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2015-02-11 18:19:31 +02:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
2014-10-23 18:57:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-02-13 15:49:49 +02:00
|
|
|
handshakeTimeout = 5 * time.Second
|
2015-02-11 18:19:31 +02:00
|
|
|
defaultDialTimeout = 10 * time.Second
|
|
|
|
refreshPeersInterval = 30 * time.Second
|
2014-10-23 18:57:54 +03:00
|
|
|
)
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
var srvlog = logger.NewLogger("P2P Server")
|
2015-02-19 18:09:33 +02:00
|
|
|
var srvjslog = logger.NewJsonLogger()
|
2014-10-23 18:57:54 +03:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// MakeName creates a node name that follows the ethereum convention
|
|
|
|
// for such names. It adds the operation system name and Go runtime version
|
|
|
|
// the name.
|
|
|
|
func MakeName(name, version string) string {
|
|
|
|
return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Server manages all peer connections.
|
|
|
|
//
|
|
|
|
// The fields of Server are used as configuration parameters.
|
|
|
|
// You should set them before starting the Server. Fields may not be
|
|
|
|
// modified while the server is running.
|
2014-10-23 18:57:54 +03:00
|
|
|
type Server struct {
|
2015-02-05 04:07:58 +02:00
|
|
|
// This field must be set to a valid secp256k1 private key.
|
|
|
|
PrivateKey *ecdsa.PrivateKey
|
2014-11-21 22:48:49 +02:00
|
|
|
|
|
|
|
// MaxPeers is the maximum number of peers that can be
|
|
|
|
// connected. It must be greater than zero.
|
|
|
|
MaxPeers int
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// Name sets the node name of this server.
|
|
|
|
// Use MakeName to create a name that follows existing conventions.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Bootstrap nodes are used to establish connectivity
|
|
|
|
// with the rest of the network.
|
2015-02-07 01:38:36 +02:00
|
|
|
BootstrapNodes []*discover.Node
|
2015-02-05 04:07:58 +02:00
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Protocols should contain the protocols supported
|
|
|
|
// by the server. Matching protocols are launched for
|
|
|
|
// each peer.
|
|
|
|
Protocols []Protocol
|
|
|
|
|
|
|
|
// If Blacklist is set to a non-nil value, the given Blacklist
|
|
|
|
// is used to verify peer connections.
|
|
|
|
Blacklist Blacklist
|
|
|
|
|
|
|
|
// If ListenAddr is set to a non-nil address, the server
|
|
|
|
// will listen for incoming connections.
|
|
|
|
//
|
|
|
|
// If the port is zero, the operating system will pick a port. The
|
|
|
|
// ListenAddr field will be updated with the actual address when
|
|
|
|
// the server is started.
|
|
|
|
ListenAddr string
|
|
|
|
|
|
|
|
// If set to a non-nil value, the given NAT port mapper
|
|
|
|
// is used to make the listening port available to the
|
|
|
|
// Internet.
|
2015-02-11 18:19:31 +02:00
|
|
|
NAT nat.Interface
|
2014-11-21 22:48:49 +02:00
|
|
|
|
|
|
|
// If Dialer is set to a non-nil value, the given Dialer
|
|
|
|
// is used to dial outbound peer connections.
|
|
|
|
Dialer *net.Dialer
|
|
|
|
|
|
|
|
// If NoDial is true, the server will not dial any peers.
|
|
|
|
NoDial bool
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// Hooks for testing. These are useful because we can inhibit
|
2014-11-21 22:48:49 +02:00
|
|
|
// the whole protocol stack.
|
2015-02-19 02:52:03 +02:00
|
|
|
setupFunc
|
2015-02-05 04:07:58 +02:00
|
|
|
newPeerHook
|
2014-10-23 18:57:54 +03:00
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
ourHandshake *protoHandshake
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
lock sync.RWMutex
|
|
|
|
running bool
|
|
|
|
listener net.Listener
|
|
|
|
peers map[discover.NodeID]*Peer
|
|
|
|
|
|
|
|
ntab *discover.Table
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
loopWG sync.WaitGroup // {dial,listen,nat}Loop
|
|
|
|
peerWG sync.WaitGroup // active peer goroutines
|
|
|
|
peerConnect chan *discover.Node
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
type setupFunc func(net.Conn, *ecdsa.PrivateKey, *protoHandshake, *discover.Node) (*conn, error)
|
2015-02-05 04:07:58 +02:00
|
|
|
type newPeerHook func(*Peer)
|
2014-10-23 18:57:54 +03:00
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Peers returns all connected peers.
|
|
|
|
func (srv *Server) Peers() (peers []*Peer) {
|
|
|
|
srv.lock.RLock()
|
|
|
|
defer srv.lock.RUnlock()
|
|
|
|
for _, peer := range srv.peers {
|
2014-10-23 18:57:54 +03:00
|
|
|
if peer != nil {
|
|
|
|
peers = append(peers, peer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// PeerCount returns the number of connected peers.
|
|
|
|
func (srv *Server) PeerCount() int {
|
|
|
|
srv.lock.RLock()
|
2015-02-05 04:07:58 +02:00
|
|
|
n := len(srv.peers)
|
|
|
|
srv.lock.RUnlock()
|
|
|
|
return n
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// SuggestPeer creates a connection to the given Node if it
|
|
|
|
// is not already connected.
|
2015-02-07 01:15:04 +02:00
|
|
|
func (srv *Server) SuggestPeer(n *discover.Node) {
|
|
|
|
srv.peerConnect <- n
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Broadcast sends an RLP-encoded message to all connected peers.
|
|
|
|
// This method is deprecated and will be removed later.
|
|
|
|
func (srv *Server) Broadcast(protocol string, code uint64, data ...interface{}) {
|
2014-11-04 14:21:44 +02:00
|
|
|
var payload []byte
|
|
|
|
if data != nil {
|
2015-03-04 13:03:43 +02:00
|
|
|
payload = ethutil.Encode(data)
|
2014-11-04 14:21:44 +02:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
srv.lock.RLock()
|
|
|
|
defer srv.lock.RUnlock()
|
|
|
|
for _, peer := range srv.peers {
|
2014-10-23 18:57:54 +03:00
|
|
|
if peer != nil {
|
2014-11-04 14:21:44 +02:00
|
|
|
var msg = Msg{Code: code}
|
|
|
|
if data != nil {
|
|
|
|
msg.Payload = bytes.NewReader(payload)
|
|
|
|
msg.Size = uint32(len(payload))
|
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
peer.writeProtoMsg(protocol, msg)
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// Start starts running the server.
|
|
|
|
// Servers can be re-used and started again after stopping.
|
|
|
|
func (srv *Server) Start() (err error) {
|
|
|
|
srv.lock.Lock()
|
|
|
|
defer srv.lock.Unlock()
|
|
|
|
if srv.running {
|
|
|
|
return errors.New("server already running")
|
|
|
|
}
|
|
|
|
srvlog.Infoln("Starting Server")
|
|
|
|
|
2015-02-19 18:08:18 +02:00
|
|
|
// static fields
|
2015-02-05 04:07:58 +02:00
|
|
|
if srv.PrivateKey == nil {
|
|
|
|
return fmt.Errorf("Server.PrivateKey must be set to a non-nil key")
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
if srv.MaxPeers <= 0 {
|
|
|
|
return fmt.Errorf("Server.MaxPeers must be > 0")
|
|
|
|
}
|
|
|
|
srv.quit = make(chan struct{})
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.peers = make(map[discover.NodeID]*Peer)
|
|
|
|
srv.peerConnect = make(chan *discover.Node)
|
2015-02-19 02:52:03 +02:00
|
|
|
if srv.setupFunc == nil {
|
|
|
|
srv.setupFunc = setupConn
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
if srv.Blacklist == nil {
|
|
|
|
srv.Blacklist = NewBlacklist()
|
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
|
2015-02-19 18:08:18 +02:00
|
|
|
// node table
|
2015-02-19 02:52:03 +02:00
|
|
|
ntab, err := discover.ListenUDP(srv.PrivateKey, srv.ListenAddr, srv.NAT)
|
2015-02-05 04:07:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-19 02:52:03 +02:00
|
|
|
srv.ntab = ntab
|
|
|
|
|
2015-02-19 18:08:18 +02:00
|
|
|
// handshake
|
2015-02-19 02:52:03 +02:00
|
|
|
srv.ourHandshake = &protoHandshake{Version: baseProtocolVersion, Name: srv.Name, ID: ntab.Self()}
|
|
|
|
for _, p := range srv.Protocols {
|
|
|
|
srv.ourHandshake.Caps = append(srv.ourHandshake.Caps, p.cap())
|
|
|
|
}
|
|
|
|
|
2015-02-19 18:08:18 +02:00
|
|
|
// listen/dial
|
|
|
|
if srv.ListenAddr != "" {
|
|
|
|
if err := srv.startListening(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if srv.Dialer == nil {
|
|
|
|
srv.Dialer = &net.Dialer{Timeout: defaultDialTimeout}
|
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
if !srv.NoDial {
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.loopWG.Add(1)
|
2014-11-21 22:48:49 +02:00
|
|
|
go srv.dialLoop()
|
|
|
|
}
|
|
|
|
if srv.NoDial && srv.ListenAddr == "" {
|
|
|
|
srvlog.Warnln("I will be kind-of useless, neither dialing nor listening.")
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.running = true
|
|
|
|
return nil
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
func (srv *Server) startListening() error {
|
|
|
|
listener, err := net.Listen("tcp", srv.ListenAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-11 18:19:31 +02:00
|
|
|
laddr := listener.Addr().(*net.TCPAddr)
|
|
|
|
srv.ListenAddr = laddr.String()
|
2014-11-21 22:48:49 +02:00
|
|
|
srv.listener = listener
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.loopWG.Add(1)
|
2014-11-21 22:48:49 +02:00
|
|
|
go srv.listenLoop()
|
2015-02-11 18:19:31 +02:00
|
|
|
if !laddr.IP.IsLoopback() && srv.NAT != nil {
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.loopWG.Add(1)
|
2015-02-11 18:19:31 +02:00
|
|
|
go func() {
|
|
|
|
nat.Map(srv.NAT, srv.quit, "tcp", laddr.Port, laddr.Port, "ethereum p2p")
|
|
|
|
srv.loopWG.Done()
|
|
|
|
}()
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop terminates the server and all active peer connections.
|
|
|
|
// It blocks until all active connections have been closed.
|
|
|
|
func (srv *Server) Stop() {
|
|
|
|
srv.lock.Lock()
|
|
|
|
if !srv.running {
|
|
|
|
srv.lock.Unlock()
|
|
|
|
return
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
srv.running = false
|
|
|
|
srv.lock.Unlock()
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
srvlog.Infoln("Stopping Server")
|
|
|
|
srv.ntab.Close()
|
2014-11-21 22:48:49 +02:00
|
|
|
if srv.listener != nil {
|
|
|
|
// this unblocks listener Accept
|
|
|
|
srv.listener.Close()
|
|
|
|
}
|
|
|
|
close(srv.quit)
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.loopWG.Wait()
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// No new peers can be added at this point because dialLoop and
|
|
|
|
// listenLoop are down. It is safe to call peerWG.Wait because
|
|
|
|
// peerWG.Add is not called outside of those loops.
|
|
|
|
for _, peer := range srv.peers {
|
|
|
|
peer.Disconnect(DiscQuitting)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.peerWG.Wait()
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2014-10-23 18:57:54 +03:00
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
// main loop for adding connections via listening
|
|
|
|
func (srv *Server) listenLoop() {
|
2015-02-05 04:07:58 +02:00
|
|
|
defer srv.loopWG.Done()
|
2014-11-21 22:48:49 +02:00
|
|
|
srvlog.Infoln("Listening on", srv.listener.Addr())
|
2014-10-23 18:57:54 +03:00
|
|
|
for {
|
2015-02-05 04:07:58 +02:00
|
|
|
conn, err := srv.listener.Accept()
|
|
|
|
if err != nil {
|
2014-11-21 22:48:49 +02:00
|
|
|
return
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
srvlog.Debugf("Accepted conn %v\n", conn.RemoteAddr())
|
|
|
|
srv.peerWG.Add(1)
|
|
|
|
go srv.startPeer(conn, nil)
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
func (srv *Server) dialLoop() {
|
2015-02-05 04:07:58 +02:00
|
|
|
defer srv.loopWG.Done()
|
|
|
|
refresh := time.NewTicker(refreshPeersInterval)
|
|
|
|
defer refresh.Stop()
|
|
|
|
|
|
|
|
srv.ntab.Bootstrap(srv.BootstrapNodes)
|
|
|
|
go srv.findPeers()
|
|
|
|
|
|
|
|
dialed := make(chan *discover.Node)
|
|
|
|
dialing := make(map[discover.NodeID]bool)
|
|
|
|
|
|
|
|
// TODO: limit number of active dials
|
|
|
|
// TODO: ensure only one findPeers goroutine is running
|
|
|
|
// TODO: pause findPeers when we're at capacity
|
|
|
|
|
2014-10-23 18:57:54 +03:00
|
|
|
for {
|
|
|
|
select {
|
2015-02-05 04:07:58 +02:00
|
|
|
case <-refresh.C:
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
go srv.findPeers()
|
|
|
|
|
|
|
|
case dest := <-srv.peerConnect:
|
2015-02-13 15:50:14 +02:00
|
|
|
// avoid dialing nodes that are already connected.
|
|
|
|
// there is another check for this in addPeer,
|
|
|
|
// which runs after the handshake.
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.lock.Lock()
|
|
|
|
_, isconnected := srv.peers[dest.ID]
|
|
|
|
srv.lock.Unlock()
|
2015-02-13 15:50:14 +02:00
|
|
|
if isconnected || dialing[dest.ID] || dest.ID == srv.ntab.Self() {
|
2015-02-05 04:07:58 +02:00
|
|
|
continue
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
|
|
|
|
dialing[dest.ID] = true
|
|
|
|
srv.peerWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
srv.dialNode(dest)
|
|
|
|
// at this point, the peer has been added
|
|
|
|
// or discarded. either way, we're not dialing it anymore.
|
|
|
|
dialed <- dest
|
|
|
|
}()
|
|
|
|
|
|
|
|
case dest := <-dialed:
|
|
|
|
delete(dialing, dest.ID)
|
|
|
|
|
|
|
|
case <-srv.quit:
|
|
|
|
// TODO: maybe wait for active dials
|
2014-10-23 18:57:54 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (srv *Server) dialNode(dest *discover.Node) {
|
2015-02-06 15:40:53 +02:00
|
|
|
addr := &net.TCPAddr{IP: dest.IP, Port: dest.TCPPort}
|
|
|
|
srvlog.Debugf("Dialing %v\n", dest)
|
|
|
|
conn, err := srv.Dialer.Dial("tcp", addr.String())
|
2014-10-23 18:57:54 +03:00
|
|
|
if err != nil {
|
2015-01-19 12:21:46 +02:00
|
|
|
srvlog.DebugDetailf("dial error: %v", err)
|
2014-11-04 14:21:44 +02:00
|
|
|
return
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.startPeer(conn, dest)
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (srv *Server) findPeers() {
|
|
|
|
far := srv.ntab.Self()
|
|
|
|
for i := range far {
|
|
|
|
far[i] = ^far[i]
|
|
|
|
}
|
|
|
|
closeToSelf := srv.ntab.Lookup(srv.ntab.Self())
|
|
|
|
farFromSelf := srv.ntab.Lookup(far)
|
|
|
|
|
|
|
|
for i := 0; i < len(closeToSelf) || i < len(farFromSelf); i++ {
|
|
|
|
if i < len(closeToSelf) {
|
|
|
|
srv.peerConnect <- closeToSelf[i]
|
|
|
|
}
|
|
|
|
if i < len(farFromSelf) {
|
|
|
|
srv.peerConnect <- farFromSelf[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 02:52:03 +02:00
|
|
|
func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
|
2015-02-13 15:49:49 +02:00
|
|
|
// TODO: handle/store session token
|
2015-02-27 05:06:55 +02:00
|
|
|
// TODO: reenable deadlines
|
|
|
|
// fd.SetDeadline(time.Now().Add(handshakeTimeout))
|
2015-02-19 02:52:03 +02:00
|
|
|
conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest)
|
2015-02-05 04:07:58 +02:00
|
|
|
if err != nil {
|
2015-02-19 02:52:03 +02:00
|
|
|
fd.Close()
|
|
|
|
srvlog.Debugf("Handshake with %v failed: %v", fd.RemoteAddr(), err)
|
2015-02-05 04:07:58 +02:00
|
|
|
return
|
|
|
|
}
|
2015-02-27 05:06:55 +02:00
|
|
|
p := newPeer(fd, conn, srv.Protocols)
|
2015-02-19 02:52:03 +02:00
|
|
|
if ok, reason := srv.addPeer(conn.ID, p); !ok {
|
2015-02-13 15:47:05 +02:00
|
|
|
srvlog.DebugDetailf("Not adding %v (%v)\n", p, reason)
|
|
|
|
p.politeDisconnect(reason)
|
2015-02-05 04:07:58 +02:00
|
|
|
return
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2015-02-19 18:09:33 +02:00
|
|
|
|
2015-02-13 15:47:05 +02:00
|
|
|
srvlog.Debugf("Added %v\n", p)
|
2015-02-19 18:09:33 +02:00
|
|
|
srvjslog.LogJson(&logger.P2PConnected{
|
|
|
|
RemoteId: fmt.Sprintf("%x", conn.ID[:]),
|
2015-02-27 05:06:55 +02:00
|
|
|
RemoteAddress: fd.RemoteAddr().String(),
|
2015-02-19 18:09:33 +02:00
|
|
|
RemoteVersionString: conn.Name,
|
|
|
|
NumConnections: srv.PeerCount(),
|
|
|
|
})
|
2015-02-05 04:07:58 +02:00
|
|
|
|
2015-02-07 01:13:22 +02:00
|
|
|
if srv.newPeerHook != nil {
|
|
|
|
srv.newPeerHook(p)
|
|
|
|
}
|
2015-02-13 15:47:05 +02:00
|
|
|
discreason := p.run()
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.removePeer(p)
|
2015-02-19 18:09:33 +02:00
|
|
|
|
2015-02-13 15:47:05 +02:00
|
|
|
srvlog.Debugf("Removed %v (%v)\n", p, discreason)
|
2015-02-19 18:09:33 +02:00
|
|
|
srvjslog.LogJson(&logger.P2PDisconnected{
|
|
|
|
RemoteId: fmt.Sprintf("%x", conn.ID[:]),
|
|
|
|
NumConnections: srv.PeerCount(),
|
|
|
|
})
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (srv *Server) addPeer(id discover.NodeID, p *Peer) (bool, DiscReason) {
|
2014-11-21 22:48:49 +02:00
|
|
|
srv.lock.Lock()
|
|
|
|
defer srv.lock.Unlock()
|
2015-02-05 04:07:58 +02:00
|
|
|
switch {
|
|
|
|
case !srv.running:
|
|
|
|
return false, DiscQuitting
|
|
|
|
case len(srv.peers) >= srv.MaxPeers:
|
|
|
|
return false, DiscTooManyPeers
|
|
|
|
case srv.peers[id] != nil:
|
|
|
|
return false, DiscAlreadyConnected
|
|
|
|
case srv.Blacklist.Exists(id[:]):
|
|
|
|
return false, DiscUselessPeer
|
|
|
|
case id == srv.ntab.Self():
|
|
|
|
return false, DiscSelf
|
|
|
|
}
|
|
|
|
srv.peers[id] = p
|
|
|
|
return true, 0
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func (srv *Server) removePeer(p *Peer) {
|
|
|
|
srv.lock.Lock()
|
2015-02-19 02:52:03 +02:00
|
|
|
delete(srv.peers, p.ID())
|
2015-02-05 04:07:58 +02:00
|
|
|
srv.lock.Unlock()
|
|
|
|
srv.peerWG.Done()
|
2014-11-04 14:21:44 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
type Blacklist interface {
|
|
|
|
Get([]byte) (bool, error)
|
|
|
|
Put([]byte) error
|
|
|
|
Delete([]byte) error
|
|
|
|
Exists(pubkey []byte) (ok bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlacklistMap struct {
|
|
|
|
blacklist map[string]bool
|
|
|
|
lock sync.RWMutex
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
func NewBlacklist() *BlacklistMap {
|
|
|
|
return &BlacklistMap{
|
|
|
|
blacklist: make(map[string]bool),
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2014-10-23 18:57:54 +03:00
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
func (self *BlacklistMap) Get(pubkey []byte) (bool, error) {
|
|
|
|
self.lock.RLock()
|
|
|
|
defer self.lock.RUnlock()
|
|
|
|
v, ok := self.blacklist[string(pubkey)]
|
|
|
|
var err error
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("not found")
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BlacklistMap) Exists(pubkey []byte) (ok bool) {
|
|
|
|
self.lock.RLock()
|
|
|
|
defer self.lock.RUnlock()
|
|
|
|
_, ok = self.blacklist[string(pubkey)]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BlacklistMap) Put(pubkey []byte) error {
|
2015-02-15 03:12:14 +02:00
|
|
|
self.lock.Lock()
|
|
|
|
defer self.lock.Unlock()
|
2014-11-21 22:48:49 +02:00
|
|
|
self.blacklist[string(pubkey)] = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BlacklistMap) Delete(pubkey []byte) error {
|
2015-02-15 03:12:14 +02:00
|
|
|
self.lock.Lock()
|
|
|
|
defer self.lock.Unlock()
|
2014-11-21 22:48:49 +02:00
|
|
|
delete(self.blacklist, string(pubkey))
|
2014-10-23 18:57:54 +03:00
|
|
|
return nil
|
|
|
|
}
|