From 296dc22b8ea80d5f73604a7ea0252892d0db156b Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 6 Apr 2021 14:22:36 -0700 Subject: [PATCH] API for Block/Allow Unknown Connections This was previously an application level setting handled by the UI. This commit pushes back that functionality to the profile. --- peer/cwtch_peer.go | 13 +++++++++++++ protocol/connections/engine.go | 10 ++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index b23a4a9..2546595 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -43,6 +43,17 @@ type cwtchPeer struct { eventBus event.Manager } +// BlockUnknownConnections will auto disconnect from connections if authentication doesn't resolve a hostname +// known to peer. +func (cp *cwtchPeer) BlockUnknownConnections() { + cp.eventBus.Publish(event.NewEvent(event.BlockUnknownPeers, map[event.Field]string{})) +} + +// AllowUnknownConnections will permit connections from unknown contacts. +func (cp *cwtchPeer) AllowUnknownConnections() { + cp.eventBus.Publish(event.NewEvent(event.AllowUnknownPeers, map[event.Field]string{})) +} + // ReadContacts is a meta-interface intended to restrict callers to read-only access to contacts type ReadContacts interface { GetContacts() []string @@ -65,6 +76,8 @@ type AccessPeeringState interface { // ModifyPeeringState is a meta-interface intended to restrict callers to modify-only access to connection peers type ModifyPeeringState interface { + BlockUnknownConnections() + AllowUnknownConnections() PeerWithOnion(string) JoinServer(string) error } diff --git a/protocol/connections/engine.go b/protocol/connections/engine.go index 24b5272..0117cd6 100644 --- a/protocol/connections/engine.go +++ b/protocol/connections/engine.go @@ -144,9 +144,9 @@ func (e *engine) eventHandler() { case event.DeleteGroup: // TODO: There isn't a way here to determine if other Groups are using a server connection... case event.SendMessageToGroup: - ciphertext,_ := base64.StdEncoding.DecodeString(ev.Data[event.Ciphertext]) - signature,_ := base64.StdEncoding.DecodeString(ev.Data[event.Signature]) - err := e.sendMessageToGroup(ev.Data[event.GroupServer],ciphertext, signature) + ciphertext, _ := base64.StdEncoding.DecodeString(ev.Data[event.Ciphertext]) + signature, _ := base64.StdEncoding.DecodeString(ev.Data[event.Signature]) + err := e.sendMessageToGroup(ev.Data[event.GroupServer], ciphertext, signature) if err != nil { e.eventManager.Publish(event.NewEvent(event.SendMessageToGroupError, map[event.Field]string{event.GroupServer: ev.Data[event.GroupServer], event.EventID: ev.EventID, event.Error: err.Error()})) } @@ -178,8 +178,10 @@ func (e *engine) eventHandler() { e.peerDisconnected(ev.Data[event.RemotePeer]) } case event.AllowUnknownPeers: + log.Debugf("%v now allows unknown connections", e.identity.Hostname()) e.blockUnknownContacts = false case event.BlockUnknownPeers: + log.Debugf("%v now forbids unknown connections", e.identity.Hostname()) e.blockUnknownContacts = true case event.ProtocolEngineStartListen: go e.listenFn() @@ -433,7 +435,7 @@ func (e *engine) deleteConnection(id string) { func (e *engine) receiveGroupMessage(server string, gm *groups.EncryptedGroupMessage) { // Publish Event so that a Profile Engine can deal with it. // Note: This technically means that *multiple* Profile Engines could listen to the same ProtocolEngine! - e.eventManager.Publish(event.NewEvent(event.EncryptedGroupMessage, map[event.Field]string{event.Ciphertext: base64.StdEncoding.EncodeToString(gm.Ciphertext), event.Signature: base64.StdEncoding.EncodeToString(gm.Signature)})) + e.eventManager.Publish(event.NewEvent(event.EncryptedGroupMessage, map[event.Field]string{event.Ciphertext: base64.StdEncoding.EncodeToString(gm.Ciphertext), event.Signature: base64.StdEncoding.EncodeToString(gm.Signature)})) } // sendMessageToGroup attempts to sent the given message to the given group id.