From b268a44287552a22e092c67bca38cf02b1a279db Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 29 Mar 2021 11:53:02 -0700 Subject: [PATCH] Break apart CwtchPeer interface to better support testing and analysis --- peer/cwtch_peer.go | 143 +++++++++++++++++++++++++++++++++------------ 1 file changed, 106 insertions(+), 37 deletions(-) diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index dcbdc3c..b23a4a9 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -43,58 +43,127 @@ type cwtchPeer struct { eventBus event.Manager } -// CwtchPeer provides us with a way of testing systems built on top of cwtch without having to -// directly implement a cwtchPeer. -type CwtchPeer interface { - Init(event.Manager) - AutoHandleEvents(events []event.Type) - PeerWithOnion(string) - InviteOnionToGroup(string, string) error - SendMessageToPeer(string, string) string - SendGetValToPeer(string, string, string) - StoreMessage(onion string, messageTxt string, sent time.Time) +// ReadContacts is a meta-interface intended to restrict callers to read-only access to contacts +type ReadContacts interface { + GetContacts() []string + GetContact(string) *model.PublicProfile + GetContactAttribute(string, string) (string, bool) +} +// ModifyContacts is a meta-interface intended to restrict callers to modify-only access to contacts +type ModifyContacts interface { + AddContact(nick, onion string, authorization model.Authorization) SetContactAuthorization(string, model.Authorization) error - ProcessInvite(string, string) (string, string, error) - AcceptInvite(string) error - RejectInvite(string) + SetContactAttribute(string, string, string) DeleteContact(string) - DeleteGroup(string) - - AddServer(string) error - JoinServer(string) error - GetServers() []string - SendMessageToGroupTracked(string, string) (string, error) - - GetName() string - SetName(string) - GetOnion() string +} +// AccessPeeringState provides access to functions relating to the underlying connections of a peer. +type AccessPeeringState interface { GetPeerState(string) (connections.ConnectionState, bool) +} - StartGroup(string) (string, []byte, error) +// ModifyPeeringState is a meta-interface intended to restrict callers to modify-only access to connection peers +type ModifyPeeringState interface { + PeerWithOnion(string) + JoinServer(string) error +} - ImportGroup(string) error - ExportGroup(string) (string, error) +// ModifyContactsAndPeers is a meta-interface intended to restrict a call to reading and modifying contacts +// and peers. +type ModifyContactsAndPeers interface { + ReadContacts + ModifyContacts + ModifyPeeringState +} +// ReadServers provides access to the servers +type ReadServers interface { + GetServers() []string +} + +// ReadGroups provides read-only access to group state +type ReadGroups interface { GetGroup(string) *model.Group GetGroupState(string) (connections.ConnectionState, bool) GetGroups() []string - AddContact(nick, onion string, authorization model.Authorization) - GetContacts() []string - GetContact(string) *model.PublicProfile - - SetAttribute(string, string) - GetAttribute(string) (string, bool) - SetContactAttribute(string, string, string) - GetContactAttribute(string, string) (string, bool) - SetGroupAttribute(string, string, string) GetGroupAttribute(string, string) (string, bool) + ExportGroup(string) (string, error) +} +// ModifyGroups provides write-only access add/edit/remove new groups +type ModifyGroups interface { + ImportGroup(string) error + StartGroup(string) (string, []byte, error) + AcceptInvite(string) error + RejectInvite(string) + DeleteGroup(string) + SetGroupAttribute(string, string, string) +} + +// ModifyServers provides write-only access to servers +type ModifyServers interface { + AddServer(string) error +} + +// SendMessages enables a caller to sender messages to a contact +// Note: +type SendMessages interface { + SendGetValToPeer(string, string, string) + SendMessageToPeer(string, string) string + + // TODO This should probably not be exposed + StoreMessage(onion string, messageTxt string, sent time.Time) + + // TODO Extract once groups are stable + InviteOnionToGroup(string, string) error +} + +// SendMessagesToGroup enables a caller to sender messages to a group +type SendMessagesToGroup interface { + SendMessageToGroupTracked(string, string) (string, error) +} + +// CwtchPeer provides us with a way of testing systems built on top of cwtch without having to +// directly implement a cwtchPeer. +type CwtchPeer interface { + + // Core Cwtch Peer Functions that should not be exposed to + // most functions + Init(event.Manager) + AutoHandleEvents(events []event.Type) Listen() StartPeersConnections() StartServerConnections() Shutdown() + + // Relating to local attributes + GetOnion() string + SetAttribute(string, string) + GetAttribute(string) (string, bool) + + ReadContacts + ModifyContacts + + AccessPeeringState + ModifyPeeringState + + ReadGroups + ModifyGroups + + ReadServers + ModifyServers + + SendMessages + SendMessagesToGroup + + // Deprecated + // TODO Should be removed + GetName() string + SetName(string) + + // TODO Should not be exposed... + ProcessInvite(string, string) (string, string, error) } // NewCwtchPeer creates and returns a new cwtchPeer with the given name. @@ -604,8 +673,8 @@ func (cp *cwtchPeer) eventHandler() { case event.EncryptedGroupMessage: // If successful, a side effect is the message is added to the group's timeline cp.mutex.Lock() - ciphertext,_ := base64.StdEncoding.DecodeString(ev.Data[event.Ciphertext]) - signature,_ := base64.StdEncoding.DecodeString(ev.Data[event.Signature]) + ciphertext, _ := base64.StdEncoding.DecodeString(ev.Data[event.Ciphertext]) + signature, _ := base64.StdEncoding.DecodeString(ev.Data[event.Signature]) ok, groupID, message, seen := cp.Profile.AttemptDecryption(ciphertext, signature) cp.mutex.Unlock() if ok && !seen {