mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-19 11:37:51 +01:00
added basic blacklisting
This commit is contained in:
63
Blacklist.go
Normal file
63
Blacklist.go
Normal file
@@ -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")
|
||||
})
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user