From e038da335ebce50dc7867821025c4353aca06594 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Wed, 8 Sep 2021 11:45:06 -0700 Subject: [PATCH] Deprecated SendMessageToPeer and SendMessageToGroupTracks New Generic SendMessage function that does basic handle checking and error reporting --- .gitignore | 9 ++++++ .../filesharing/filesharing_functionality.go | 2 +- go.mod | 3 ++ peer/cwtch_peer.go | 29 ++++++++++++++++++- protocol/connections/engine.go | 6 ++-- testing/file_sharing_integration_test.go | 1 + 6 files changed, 45 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index c85bdd3..441f3c4 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,12 @@ server/app/messages /storage/*/testing/ /storage/testing/ /testing/storage/ +ebusgraph.txt +messages/ +serverMonitorReport.txt +testing/cwtch.out.png +testing/cwtch.out.png.manifest +testing/tordir/ +tokens-bak.db +tokens.db +tokens1.db \ No newline at end of file diff --git a/functionality/filesharing/filesharing_functionality.go b/functionality/filesharing/filesharing_functionality.go index 1e1486c..022bded 100644 --- a/functionality/filesharing/filesharing_functionality.go +++ b/functionality/filesharing/filesharing_functionality.go @@ -83,7 +83,7 @@ func (f *Functionality) ShareFile(filepath string, profile peer.CwtchPeer, handl profile.ShareFile(key, string(serializedManifest)) - profile.SendMessageToPeer(handle, string(wrapperJSON)) + profile.SendMessage(handle, string(wrapperJSON)) return nil } diff --git a/go.mod b/go.mod index b3d34fb..a2a636a 100644 --- a/go.mod +++ b/go.mod @@ -15,3 +15,6 @@ require ( golang.org/x/tools v0.1.2 // indirect gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect ) + + +replace git.openprivacy.ca/cwtch.im/tapir => /home/sarah/workspace/src/cwtch.im/tapir \ No newline at end of file diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index 58c114c..34260cc 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -11,6 +11,7 @@ import ( "encoding/json" "errors" "fmt" + "git.openprivacy.ca/openprivacy/connectivity/tor" "git.openprivacy.ca/openprivacy/log" "strconv" "strings" @@ -51,6 +52,26 @@ type cwtchPeer struct { eventBus event.Manager } +// SendMessage is a higher level that merges sending messages to contacts and group handles +// If you try to send a message to a handle that doesn't exist, malformed or an incorrect type then +// this function will error +func (cp *cwtchPeer) SendMessage(handle string, message string) error { + // Group Handles are always 32 bytes in length, but we forgo any further testing here + // and delegate the group existence check to SendMessageToGroupTracked + if len(handle) == 32 { + _, err := cp.SendMessageToGroupTracked(handle, message) + return err + } else if tor.IsValidHostname(handle) { + // We assume we are sending to a Contact. + // (Servers are technically Contacts) + cp.SendMessageToPeer(handle, message) + // We assume this is always successful as it is always valid to attempt to + // Contact a valid hostname + return nil + } + return errors.New("malformed handle type") +} + func (cp *cwtchPeer) UpdateMessageFlags(handle string, mIdx int, flags uint64) { cp.mutex.Lock() defer cp.mutex.Unlock() @@ -137,9 +158,13 @@ type ModifyServers interface { } // SendMessages enables a caller to sender messages to a contact -// Note: type SendMessages interface { + + SendMessage(handle string, message string) error + SendGetValToPeer(string, string, string) + + // Deprecated SendMessageToPeer(string, string) string // TODO This should probably not be exposed @@ -151,6 +176,8 @@ type SendMessages interface { // SendMessagesToGroup enables a caller to sender messages to a group type SendMessagesToGroup interface { + + // Deprecated SendMessageToGroupTracked(string, string) (string, error) } diff --git a/protocol/connections/engine.go b/protocol/connections/engine.go index 0224421..c1f27c9 100644 --- a/protocol/connections/engine.go +++ b/protocol/connections/engine.go @@ -524,8 +524,8 @@ func (e *engine) handlePeerMessage(hostname string, eventID string, context stri } else if context == event.ContextRetVal { req, ok := e.getValRequests.Load(hostname + eventID) if ok { - reqStr := req.(string) - e.handlePeerRetVal(hostname, []byte(reqStr), message) + reqStr := req.([]byte) + e.handlePeerRetVal(hostname, reqStr, message) e.getValRequests.Delete(hostname + eventID) } else { log.Errorf("could not find val request for %v %s",hostname, eventID) @@ -615,7 +615,7 @@ func (e *engine) sendPeerMessage(handle string, message model3.PeerMessage) erro if ok { return peerApp.SendMessage(message) } - log.Errorf("could not send peer message: %v", err) + log.Errorf("could not derive peer app: %v", err) return fmt.Errorf("could not find peer app to send message to: %v", handle) } log.Errorf("could not send peer message: %v", err) diff --git a/testing/file_sharing_integration_test.go b/testing/file_sharing_integration_test.go index de91c95..5d9e1d3 100644 --- a/testing/file_sharing_integration_test.go +++ b/testing/file_sharing_integration_test.go @@ -86,6 +86,7 @@ func TestFileSharing(t *testing.T) { bob.AddContact("alice?", alice.GetOnion(), model.AuthApproved) alice.PeerWithOnion(bob.GetOnion()) + bob.PeerWithOnion(alice.GetOnion()) fmt.Println("Waiting for alice and Bob to peer...") waitForPeerPeerConnection(t, alice, bob)