From 2a066b95e071534e23c289bc5d49b1cbb2f66a39 Mon Sep 17 00:00:00 2001 From: Akilan Selvacoumar Date: Sat, 12 Feb 2022 00:15:46 +0400 Subject: [PATCH] added basic blacklisting --- Blacklist.go | 63 +++++++++++++++++++++++++++++++++++++++++++++ Config Default.yaml | 1 + Config.go | 1 + Network.go | 6 ++++- Peer ID.go | 8 +++++- Peernet.go | 5 ++++ 6 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 Blacklist.go diff --git a/Blacklist.go b/Blacklist.go new file mode 100644 index 0000000..7bb8cf4 --- /dev/null +++ b/Blacklist.go @@ -0,0 +1,63 @@ +package core + +import ( + "encoding/hex" + "fmt" + "github.com/PeernetOfficial/core/store" + "sync" +) + +//type BlackList struct { +// BacklistNodes []*BlackListNode +//} + +type BlackListNode struct { + peer *PeerInfo + reason string +} + +// BlackListNodeDB blacklist nodes databse +type BlackListNodeDB struct { + Database store.Store // The database storing the blockchain. + sync.RWMutex +} + +func InitBlackListStoreDB(DatabaseDirectory string) (blackListNodeDB *BlackListNodeDB, err error) { + if DatabaseDirectory == "" { + return + } + + blackListNodeDB = &BlackListNodeDB{} + + if blackListNodeDB.Database, err = store.NewPogrebStore(DatabaseDirectory); err != nil { + return nil, err + } + return blackListNodeDB, err +} + +// AddBlackList Adds blacklisted peer +func (backend *Backend) AddBlackList(peerinfo *PeerInfo, reason string) { + // Store the blacklisted information in the database + backend.Blacklist.Database.Set(peerinfo.PublicKey.SerializeCompressed(), []byte(reason)) + + // Remove the list of nodes if it's present + backend.PeerlistRemove(peerinfo) +} + +// CheckNodeBlackList Checks if the node is blacklisted +func (backend *Backend) CheckNodeBlackList(PublicKey []byte) bool { + _, found := backend.Blacklist.Database.Get(PublicKey) + return found +} + +// RemoveNodeBlackList Deletes node from the blacklist +func (backend *Backend) RemoveNodeBlackList(PublicKey []byte) { + backend.Blacklist.Database.Delete(PublicKey) +} + +func (backend *Backend) ListAllNodesInBlackList() { + backend.Blacklist.Database.Iterate(func(key []byte, value []byte) { + fmt.Println("\nPeer ID: " + hex.EncodeToString(key) + "\n" + "Reason: " + string(value) + "\n" + + "---------------------------------------------------------------------------\n") + }) +} \ No newline at end of file diff --git a/Config Default.yaml b/Config Default.yaml index 655d680..b3d40f7 100644 --- a/Config Default.yaml +++ b/Config Default.yaml @@ -4,6 +4,7 @@ BlockchainMain: "data/blockchain main/" # Blockchain main stores the end BlockchainGlobal: "data/blockchain global/" # Blockchain global caches blockchain data from global users. Empty to disable. WarehouseMain: "data/warehouse main/" # Warehouse main stores the actual data of files shared by the end-user. SearchIndex: "data/search index/" # Local search index of blockchain records. Empty to disable. +blacklist: "data/black list" # Local record of blacklisted nodes GeoIPDatabase: "data/GeoLite2-City.mmdb" # GeoLite2 City database to provide GeoIP information. # Listen defines all IP:Port combinations to listen on. If empty, it will listen on all IPs automatically on available ports. diff --git a/Config.go b/Config.go index 17b4994..8001adb 100644 --- a/Config.go +++ b/Config.go @@ -28,6 +28,7 @@ type Config struct { BlockchainGlobal string `yaml:"BlockchainGlobal"` // Blockchain global caches blockchain data from global users. Empty to disable. WarehouseMain string `yaml:"WarehouseMain"` // Warehouse main stores the actual data of files shared by the end-user. SearchIndex string `yaml:"SearchIndex"` // Local search index of blockchain records. Empty to disable. + BlackList string `yaml: "Blacklist"` // Blacklist records of nodes. Empty to disable. GeoIPDatabase string `yaml:"GeoIPDatabase"` // GeoLite2 City database to provide GeoIP information. // Target for the log messages: 0 = Log file, 1 = Stdout, 2 = Log file + Stdout, 3 = None diff --git a/Network.go b/Network.go index 5a7f2e3..ae26fce 100644 --- a/Network.go +++ b/Network.go @@ -162,9 +162,13 @@ func (nets *Networks) packetWorker() { nets.backend.Filters.PacketIn(decoded, senderPublicKey, connection) + //if senderPublicKey.SerializeUncompressed() != nil // A peer structure will always be returned, even if the peer won't be added to the peer list. peer, added := nets.backend.PeerlistAdd(senderPublicKey, connection) - if !added { + + if peer == nil { + continue + } else if !added { connection = peer.registerConnection(connection) } diff --git a/Peer ID.go b/Peer ID.go index be76c27..7c721f7 100644 --- a/Peer ID.go +++ b/Peer ID.go @@ -115,6 +115,12 @@ type peerAddress struct { // PeerlistAdd adds a new peer to the peer list. It does not validate the peer info. If the peer is already added, it does nothing. Connections must be live. func (backend *Backend) PeerlistAdd(PublicKey *btcec.PublicKey, connections ...*Connection) (peer *PeerInfo, added bool) { + + // Adds node to the peer list + if backend.CheckNodeBlackList(PublicKey.SerializeCompressed()) { + return nil, false + } + if len(connections) == 0 { return nil, false } @@ -130,7 +136,7 @@ func (backend *Backend) PeerlistAdd(PublicKey *btcec.PublicKey, connections ...* peer = &PeerInfo{Backend: backend, PublicKey: PublicKey, connectionActive: connections, connectionLatest: connections[0], NodeID: protocol.PublicKey2NodeID(PublicKey), messageSequence: rand.Uint32()} _, peer.IsRootPeer = rootPeers[publicKeyCompressed] - + backend.peerList[publicKeyCompressed] = peer // also add to mirrored nodeList diff --git a/Peernet.go b/Peernet.go index 2cf05bb..c9149fd 100644 --- a/Peernet.go +++ b/Peernet.go @@ -70,6 +70,10 @@ func Init(UserAgent string, ConfigFilename string, Filters *Filters, ConfigOut i backend.userBlockchainUpdateSearchIndex() } + if backend.Blacklist, err = InitBlackListStoreDB(backend.Config.BlackList); err != nil { + backend.LogError("Init", "blacklist '%s' init: %s", backend.Config.BlackList, err.Error()) + } + return backend, ExitSuccess, nil } @@ -94,6 +98,7 @@ type Backend struct { userAgent string // User Agent GlobalBlockchainCache *BlockchainCache // Caches blockchains of other peers. SearchIndex *search.SearchIndexStore // Search index of blockchain records. + Blacklist *BlackListNodeDB // Blacklist nodes records networks *Networks // All connected networks. dhtStore store.Store // dhtStore contains all key-value data served via DHT UserBlockchain *blockchain.Blockchain // UserBlockchain is the user's blockchain and exports functions to directly read and write it