Deprecated SendMessageToPeer and SendMessageToGroupTracks
continuous-integration/drone/push Build is pending Details
continuous-integration/drone/pr Build is pending Details

New Generic SendMessage function that does basic handle checking and
error reporting
This commit is contained in:
Sarah Jamie Lewis 2021-09-08 11:45:06 -07:00
parent 62f0cfaad3
commit e038da335e
6 changed files with 45 additions and 5 deletions

9
.gitignore vendored
View File

@ -12,3 +12,12 @@ server/app/messages
/storage/*/testing/ /storage/*/testing/
/storage/testing/ /storage/testing/
/testing/storage/ /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

View File

@ -83,7 +83,7 @@ func (f *Functionality) ShareFile(filepath string, profile peer.CwtchPeer, handl
profile.ShareFile(key, string(serializedManifest)) profile.ShareFile(key, string(serializedManifest))
profile.SendMessageToPeer(handle, string(wrapperJSON)) profile.SendMessage(handle, string(wrapperJSON))
return nil return nil
} }

3
go.mod
View File

@ -15,3 +15,6 @@ require (
golang.org/x/tools v0.1.2 // indirect golang.org/x/tools v0.1.2 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // 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

View File

@ -11,6 +11,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
"strconv" "strconv"
"strings" "strings"
@ -51,6 +52,26 @@ type cwtchPeer struct {
eventBus event.Manager 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) { func (cp *cwtchPeer) UpdateMessageFlags(handle string, mIdx int, flags uint64) {
cp.mutex.Lock() cp.mutex.Lock()
defer cp.mutex.Unlock() defer cp.mutex.Unlock()
@ -137,9 +158,13 @@ type ModifyServers interface {
} }
// SendMessages enables a caller to sender messages to a contact // SendMessages enables a caller to sender messages to a contact
// Note:
type SendMessages interface { type SendMessages interface {
SendMessage(handle string, message string) error
SendGetValToPeer(string, string, string) SendGetValToPeer(string, string, string)
// Deprecated
SendMessageToPeer(string, string) string SendMessageToPeer(string, string) string
// TODO This should probably not be exposed // TODO This should probably not be exposed
@ -151,6 +176,8 @@ type SendMessages interface {
// SendMessagesToGroup enables a caller to sender messages to a group // SendMessagesToGroup enables a caller to sender messages to a group
type SendMessagesToGroup interface { type SendMessagesToGroup interface {
// Deprecated
SendMessageToGroupTracked(string, string) (string, error) SendMessageToGroupTracked(string, string) (string, error)
} }

View File

@ -524,8 +524,8 @@ func (e *engine) handlePeerMessage(hostname string, eventID string, context stri
} else if context == event.ContextRetVal { } else if context == event.ContextRetVal {
req, ok := e.getValRequests.Load(hostname + eventID) req, ok := e.getValRequests.Load(hostname + eventID)
if ok { if ok {
reqStr := req.(string) reqStr := req.([]byte)
e.handlePeerRetVal(hostname, []byte(reqStr), message) e.handlePeerRetVal(hostname, reqStr, message)
e.getValRequests.Delete(hostname + eventID) e.getValRequests.Delete(hostname + eventID)
} else { } else {
log.Errorf("could not find val request for %v %s",hostname, eventID) 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 { if ok {
return peerApp.SendMessage(message) 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) return fmt.Errorf("could not find peer app to send message to: %v", handle)
} }
log.Errorf("could not send peer message: %v", err) log.Errorf("could not send peer message: %v", err)

View File

@ -86,6 +86,7 @@ func TestFileSharing(t *testing.T) {
bob.AddContact("alice?", alice.GetOnion(), model.AuthApproved) bob.AddContact("alice?", alice.GetOnion(), model.AuthApproved)
alice.PeerWithOnion(bob.GetOnion()) alice.PeerWithOnion(bob.GetOnion())
bob.PeerWithOnion(alice.GetOnion())
fmt.Println("Waiting for alice and Bob to peer...") fmt.Println("Waiting for alice and Bob to peer...")
waitForPeerPeerConnection(t, alice, bob) waitForPeerPeerConnection(t, alice, bob)