From 849deb14dc141397ac5a86da71f598133b34644b Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 8 Jan 2019 10:58:01 -0800 Subject: [PATCH] Autoblock Known Blocked Peers --- peer/cwtch_peer.go | 11 ++++++++++- protocol/connections/engine.go | 36 +++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index 85868ad..b063abe 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -89,8 +89,17 @@ func (cp *cwtchPeer) Init(acn connectivity.ACN, eventBus *event.Manager) { cp.eventBus.Subscribe(event.EncryptedGroupMessage, cp.queue.EventChannel) cp.eventBus.Subscribe(event.NewGroupInvite, cp.queue.EventChannel) + // Calculate a list of Peers who have been Blocked. + blockedPeers := []string{} + for _, contact := range cp.Profile.GetContacts() { + c, _ := cp.Profile.GetContact(contact) + if c.Blocked { + blockedPeers = append(blockedPeers, c.Onion) + } + } + // TODO: Would be nice if ProtocolEngine did not need to explictly be given the Private Key. - cp.engine = connections.NewProtocolEngine(cp.Profile.Ed25519PrivateKey, acn, eventBus) + cp.engine = connections.NewProtocolEngine(cp.Profile.Ed25519PrivateKey, acn, eventBus, blockedPeers) cp.engine.Identity = identity.InitializeV3(cp.Profile.Name, &cp.Profile.Ed25519PrivateKey, &cp.Profile.Ed25519PublicKey) } diff --git a/protocol/connections/engine.go b/protocol/connections/engine.go index f9b5deb..5558eae 100644 --- a/protocol/connections/engine.go +++ b/protocol/connections/engine.go @@ -13,6 +13,7 @@ import ( "git.openprivacy.ca/openprivacy/libricochet-go/log" "github.com/golang/protobuf/proto" "golang.org/x/crypto/ed25519" + "sync" ) // Engine (ProtocolEngine) encapsulates the logic necessary to make and receive Cwtch connections. @@ -30,13 +31,16 @@ type Engine struct { // Engine State started bool + // Blocklist + blocked sync.Map + // Pointer to the Global Event Manager eventManager *event.Manager privateKey ed25519.PrivateKey } // NewProtocolEngine initializes a new engine that runs Cwtch using the given parameters -func NewProtocolEngine(privateKey ed25519.PrivateKey, acn connectivity.ACN, eventManager *event.Manager) *Engine { +func NewProtocolEngine(privateKey ed25519.PrivateKey, acn connectivity.ACN, eventManager *event.Manager, blockedPeers []string) *Engine { engine := new(Engine) engine.privateKey = privateKey engine.queue = event.NewEventQueue(100) @@ -47,6 +51,7 @@ func NewProtocolEngine(privateKey ed25519.PrivateKey, acn connectivity.ACN, even go engine.connectionsManager.AttemptReconnections() engine.eventManager = eventManager + engine.eventManager.Subscribe(event.ProtocolEngineStartListen, engine.queue.EventChannel) engine.eventManager.Subscribe(event.PeerRequest, engine.queue.EventChannel) engine.eventManager.Subscribe(event.InvitePeerToGroup, engine.queue.EventChannel) @@ -54,6 +59,10 @@ func NewProtocolEngine(privateKey ed25519.PrivateKey, acn connectivity.ACN, even engine.eventManager.Subscribe(event.SendMessageToGroup, engine.queue.EventChannel) engine.eventManager.Subscribe(event.SendMessageToPeer, engine.queue.EventChannel) + engine.eventManager.Subscribe(event.BlockPeer, engine.queue.EventChannel) + for _, peer := range blockedPeers { + engine.blocked.Store(peer, true) + } return engine } @@ -79,6 +88,8 @@ func (e *Engine) eventHandler() { // TODO this will block. ppc.SendPacket([]byte(ev.Data["Message"])) } + case event.BlockPeer: + e.blocked.Store(ev.Data["Peer"], true) case event.ProtocolEngineStartListen: go e.listenFn() default: @@ -133,19 +144,26 @@ func (e *Engine) listenFn() { return } -// LookupContact returns that a contact is known and allowed to communicate for all cases. +// LookupContact is a V2 API Call, we want to reject all V2 Peers +// TODO Deprecate func (e *Engine) LookupContact(hostname string, publicKey rsa.PublicKey) (allowed, known bool) { - return true, true + return false, false +} + +// ContactRequest is a V2 API Call needed to implement ContactRequestHandler Interface +// TODO Deprecate +func (e *Engine) ContactRequest(name string, message string) string { + return "Rejected" } // LookupContactV3 returns that a contact is known and allowed to communicate for all cases. func (e *Engine) LookupContactV3(hostname string, publicKey ed25519.PublicKey) (allowed, known bool) { - return true, true -} - -// ContactRequest needed to implement ContactRequestHandler Interface -func (e *Engine) ContactRequest(name string, message string) string { - return "Accepted" + // TODO: We want to autoblock those that are blocked, The known parameter has no use anymore and should be + // disregarded by peers, so we set it to false. + if _, blocked := e.blocked.Load(hostname); blocked { + return false, false + } + return true, false } // Shutdown tears down the eventHandler goroutine -- 2.25.1