2018-03-11 18:49:10 +00:00
|
|
|
package peer
|
2018-03-09 20:44:13 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-04 21:44:21 +00:00
|
|
|
"cwtch.im/cwtch/event"
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/model"
|
2019-01-04 21:44:21 +00:00
|
|
|
"cwtch.im/cwtch/protocol/connections"
|
2019-05-15 20:12:11 +00:00
|
|
|
"encoding/base32"
|
2018-06-09 07:49:18 +00:00
|
|
|
"encoding/base64"
|
2019-02-03 01:18:33 +00:00
|
|
|
"encoding/json"
|
2018-03-30 21:16:51 +00:00
|
|
|
"errors"
|
2020-02-10 22:09:24 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/log"
|
2018-06-09 07:49:18 +00:00
|
|
|
"strings"
|
2018-03-09 20:44:13 +00:00
|
|
|
"sync"
|
2019-01-22 19:11:25 +00:00
|
|
|
"time"
|
2018-03-09 20:44:13 +00:00
|
|
|
)
|
|
|
|
|
2019-10-18 23:56:10 +00:00
|
|
|
var autoHandleableEvents = map[event.Type]bool{event.EncryptedGroupMessage: true, event.PeerStateChange: true,
|
|
|
|
event.ServerStateChange: true, event.NewGroupInvite: true, event.NewMessageFromPeer: true,
|
|
|
|
event.PeerAcknowledgement: true, event.PeerError: true, event.SendMessageToGroupError: true}
|
2019-09-19 23:14:35 +00:00
|
|
|
|
2018-10-06 03:50:55 +00:00
|
|
|
// cwtchPeer manages incoming and outgoing connections and all processing for a Cwtch cwtchPeer
|
2018-06-19 22:28:44 +00:00
|
|
|
type cwtchPeer struct {
|
2019-01-04 21:44:21 +00:00
|
|
|
Profile *model.Profile
|
|
|
|
mutex sync.Mutex
|
|
|
|
shutdown bool
|
|
|
|
|
2019-08-14 20:56:45 +00:00
|
|
|
queue event.Queue
|
2019-06-05 20:40:55 +00:00
|
|
|
eventBus event.Manager
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 18:53:10 +00:00
|
|
|
// CwtchPeer provides us with a way of testing systems built on top of cwtch without having to
|
2018-06-19 22:28:44 +00:00
|
|
|
// directly implement a cwtchPeer.
|
2018-09-21 18:53:10 +00:00
|
|
|
type CwtchPeer interface {
|
2019-06-21 21:50:43 +00:00
|
|
|
Init(event.Manager)
|
2019-09-19 23:14:35 +00:00
|
|
|
AutoHandleEvents(events []event.Type)
|
2019-07-17 19:10:52 +00:00
|
|
|
PeerWithOnion(string)
|
2018-06-19 22:28:44 +00:00
|
|
|
InviteOnionToGroup(string, string) error
|
2019-01-15 20:59:54 +00:00
|
|
|
SendMessageToPeer(string, string) string
|
2018-06-19 22:28:44 +00:00
|
|
|
|
|
|
|
TrustPeer(string) error
|
|
|
|
BlockPeer(string) error
|
2019-08-07 18:49:44 +00:00
|
|
|
UnblockPeer(string) error
|
2020-02-03 18:46:15 +00:00
|
|
|
ProcessInvite(string, string) (string, error)
|
2018-06-19 22:28:44 +00:00
|
|
|
AcceptInvite(string) error
|
|
|
|
RejectInvite(string)
|
2019-08-07 05:27:11 +00:00
|
|
|
DeleteContact(string)
|
|
|
|
DeleteGroup(string)
|
2018-06-19 22:28:44 +00:00
|
|
|
|
|
|
|
JoinServer(string)
|
|
|
|
SendMessageToGroup(string, string) error
|
2019-02-19 20:18:25 +00:00
|
|
|
SendMessageToGroupTracked(string, string) (string, error)
|
2018-06-19 22:28:44 +00:00
|
|
|
|
2020-02-03 18:46:15 +00:00
|
|
|
GetName() string
|
|
|
|
SetName(string)
|
|
|
|
GetOnion() string
|
|
|
|
|
|
|
|
GetPeerState(string) (connections.ConnectionState, bool)
|
2018-06-19 22:28:44 +00:00
|
|
|
|
|
|
|
StartGroup(string) (string, []byte, error)
|
|
|
|
|
2019-09-19 23:14:35 +00:00
|
|
|
ImportGroup(string) error
|
2018-06-19 22:28:44 +00:00
|
|
|
ExportGroup(string) (string, error)
|
|
|
|
|
|
|
|
GetGroup(string) *model.Group
|
2020-02-03 18:46:15 +00:00
|
|
|
GetGroupState(string) (connections.ConnectionState, bool)
|
2018-06-19 22:28:44 +00:00
|
|
|
GetGroups() []string
|
2019-05-15 20:12:11 +00:00
|
|
|
AddContact(nick, onion string, trusted bool)
|
2018-06-19 22:28:44 +00:00
|
|
|
GetContacts() []string
|
|
|
|
GetContact(string) *model.PublicProfile
|
|
|
|
|
2019-10-31 21:39:31 +00:00
|
|
|
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)
|
|
|
|
|
2018-11-10 22:14:12 +00:00
|
|
|
Listen()
|
2019-05-15 20:12:11 +00:00
|
|
|
StartPeersConnections()
|
|
|
|
StartGroupConnections()
|
2018-06-19 22:28:44 +00:00
|
|
|
Shutdown()
|
|
|
|
}
|
|
|
|
|
2018-06-24 18:59:42 +00:00
|
|
|
// NewCwtchPeer creates and returns a new cwtchPeer with the given name.
|
2018-10-06 03:50:55 +00:00
|
|
|
func NewCwtchPeer(name string) CwtchPeer {
|
2018-06-23 23:56:51 +00:00
|
|
|
cp := new(cwtchPeer)
|
2018-05-01 20:44:45 +00:00
|
|
|
cp.Profile = model.GenerateNewProfile(name)
|
2018-11-10 22:14:12 +00:00
|
|
|
cp.shutdown = false
|
2018-10-06 03:50:55 +00:00
|
|
|
return cp
|
2018-10-15 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 03:50:55 +00:00
|
|
|
// FromProfile generates a new peer from a profile.
|
|
|
|
func FromProfile(profile *model.Profile) CwtchPeer {
|
|
|
|
cp := new(cwtchPeer)
|
|
|
|
cp.Profile = profile
|
2019-09-19 23:14:35 +00:00
|
|
|
cp.shutdown = false
|
2018-10-06 03:50:55 +00:00
|
|
|
return cp
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 03:50:55 +00:00
|
|
|
// Init instantiates a cwtchPeer
|
2019-06-21 21:50:43 +00:00
|
|
|
func (cp *cwtchPeer) Init(eventBus event.Manager) {
|
2019-08-14 20:56:45 +00:00
|
|
|
cp.queue = event.NewQueue()
|
2019-01-04 21:44:21 +00:00
|
|
|
go cp.eventHandler()
|
|
|
|
|
|
|
|
cp.eventBus = eventBus
|
2019-10-18 23:56:10 +00:00
|
|
|
cp.AutoHandleEvents([]event.Type{event.EncryptedGroupMessage, event.NewMessageFromPeer, event.PeerAcknowledgement, event.PeerError, event.SendMessageToGroupError})
|
2019-09-19 23:14:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AutoHandleEvents sets an event (if able) to be handled by this peer
|
|
|
|
func (cp *cwtchPeer) AutoHandleEvents(events []event.Type) {
|
|
|
|
for _, ev := range events {
|
|
|
|
if _, exists := autoHandleableEvents[ev]; exists {
|
|
|
|
cp.eventBus.Subscribe(ev, cp.queue)
|
|
|
|
} else {
|
|
|
|
log.Errorf("Peer asked to autohandle event it cannot: %v\n", ev)
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 07:49:18 +00:00
|
|
|
// ImportGroup intializes a group from an imported source rather than a peer invite
|
2019-09-19 23:14:35 +00:00
|
|
|
func (cp *cwtchPeer) ImportGroup(exportedInvite string) (err error) {
|
2018-10-05 03:18:34 +00:00
|
|
|
if strings.HasPrefix(exportedInvite, "torv3") {
|
2019-09-19 23:14:35 +00:00
|
|
|
data, err := base64.StdEncoding.DecodeString(exportedInvite[5:])
|
2018-06-09 07:49:18 +00:00
|
|
|
if err == nil {
|
2019-09-19 23:14:35 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.NewGroupInvite, map[event.Field]string{
|
|
|
|
event.GroupInvite: string(data),
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
log.Errorf("error decoding group invite: %v", err)
|
2018-06-09 07:49:18 +00:00
|
|
|
}
|
2019-09-19 23:14:35 +00:00
|
|
|
return nil
|
2018-06-09 07:49:18 +00:00
|
|
|
}
|
2019-09-19 23:14:35 +00:00
|
|
|
return errors.New("unsupported exported group type")
|
2018-06-09 07:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExportGroup serializes a group invite so it can be given offline
|
2018-06-19 22:28:44 +00:00
|
|
|
func (cp *cwtchPeer) ExportGroup(groupID string) (string, error) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2019-10-31 21:39:31 +00:00
|
|
|
group := cp.Profile.GetGroup(groupID)
|
2018-06-09 07:49:18 +00:00
|
|
|
if group != nil {
|
2018-10-03 05:46:00 +00:00
|
|
|
invite, err := group.Invite(group.GetInitialMessage())
|
2018-06-09 07:49:18 +00:00
|
|
|
if err == nil {
|
2019-09-19 23:14:35 +00:00
|
|
|
exportedInvite := "torv3" + base64.StdEncoding.EncodeToString(invite)
|
2018-06-09 07:49:18 +00:00
|
|
|
return exportedInvite, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.New("group id could not be found")
|
|
|
|
}
|
|
|
|
|
2018-06-19 22:28:44 +00:00
|
|
|
// StartGroup create a new group linked to the given server and returns the group ID, an invite or an error.
|
|
|
|
func (cp *cwtchPeer) StartGroup(server string) (string, []byte, error) {
|
2019-02-03 01:18:33 +00:00
|
|
|
return cp.StartGroupWithMessage(server, []byte{})
|
2018-06-19 22:28:44 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 18:02:46 +00:00
|
|
|
// StartGroupWithMessage create a new group linked to the given server and returns the group ID, an invite or an error.
|
2019-02-03 01:18:33 +00:00
|
|
|
func (cp *cwtchPeer) StartGroupWithMessage(server string, initialMessage []byte) (groupID string, invite []byte, err error) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-02-03 01:18:33 +00:00
|
|
|
groupID, invite, err = cp.Profile.StartGroupWithMessage(server, initialMessage)
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-02-03 01:18:33 +00:00
|
|
|
if err == nil {
|
|
|
|
group := cp.GetGroup(groupID)
|
|
|
|
jsobj, err := json.Marshal(group)
|
2019-02-03 03:24:42 +00:00
|
|
|
if err == nil {
|
2019-02-03 01:18:33 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.GroupCreated, map[event.Field]string{
|
|
|
|
event.Data: string(jsobj),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Errorf("error creating group: %v", err)
|
|
|
|
}
|
|
|
|
return
|
2018-09-21 18:02:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 22:28:44 +00:00
|
|
|
// GetGroups returns an unordered list of all group IDs.
|
|
|
|
func (cp *cwtchPeer) GetGroups() []string {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2018-06-19 22:28:44 +00:00
|
|
|
return cp.Profile.GetGroups()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGroup returns a pointer to a specific group, nil if no group exists.
|
|
|
|
func (cp *cwtchPeer) GetGroup(groupID string) *model.Group {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2019-10-31 21:39:31 +00:00
|
|
|
return cp.Profile.GetGroup(groupID)
|
2018-06-19 22:28:44 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 20:12:11 +00:00
|
|
|
func (cp *cwtchPeer) AddContact(nick, onion string, trusted bool) {
|
|
|
|
decodedPub, _ := base32.StdEncoding.DecodeString(strings.ToUpper(onion))
|
|
|
|
pp := &model.PublicProfile{Name: nick, Ed25519PublicKey: decodedPub, Trusted: trusted, Blocked: false, Onion: onion, Attributes: map[string]string{"nick": nick}}
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-02-03 01:18:33 +00:00
|
|
|
cp.Profile.AddContact(onion, pp)
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-02-04 19:39:58 +00:00
|
|
|
pd, _ := json.Marshal(pp)
|
2019-02-03 01:18:33 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.PeerCreated, map[event.Field]string{
|
2019-02-04 19:39:58 +00:00
|
|
|
event.Data: string(pd),
|
2019-02-03 01:18:33 +00:00
|
|
|
event.RemotePeer: onion,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2018-06-19 22:28:44 +00:00
|
|
|
// GetContacts returns an unordered list of onions
|
|
|
|
func (cp *cwtchPeer) GetContacts() []string {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2018-06-19 22:28:44 +00:00
|
|
|
return cp.Profile.GetContacts()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContact returns a given contact, nil is no such contact exists
|
|
|
|
func (cp *cwtchPeer) GetContact(onion string) *model.PublicProfile {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2018-06-19 22:28:44 +00:00
|
|
|
contact, _ := cp.Profile.GetContact(onion)
|
|
|
|
return contact
|
|
|
|
}
|
|
|
|
|
2020-02-03 18:46:15 +00:00
|
|
|
func (cp *cwtchPeer) GetName() string {
|
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
|
|
|
return cp.Profile.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *cwtchPeer) SetName(newName string) {
|
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
|
|
|
cp.Profile.Name = newName
|
2018-06-19 22:28:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 18:46:15 +00:00
|
|
|
func (cp *cwtchPeer) GetOnion() string {
|
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
|
|
|
return cp.Profile.Onion
|
|
|
|
}
|
|
|
|
func (cp *cwtchPeer) GetPeerState(onion string) (connections.ConnectionState, bool) {
|
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
|
|
|
if peer, ok := cp.Profile.Contacts[onion]; ok {
|
|
|
|
return connections.ConnectionStateToType[peer.State], true
|
|
|
|
}
|
|
|
|
return connections.DISCONNECTED, false
|
2019-05-15 20:12:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 18:46:15 +00:00
|
|
|
func (cp *cwtchPeer) GetGroupState(groupid string) (connections.ConnectionState, bool) {
|
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
|
|
|
if group, ok := cp.Profile.Groups[groupid]; ok {
|
|
|
|
return connections.ConnectionStateToType[group.State], true
|
|
|
|
}
|
|
|
|
return connections.DISCONNECTED, false
|
2019-05-15 20:12:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 22:28:44 +00:00
|
|
|
// PeerWithOnion is the entry point for cwtchPeer relationships
|
2019-07-17 19:10:52 +00:00
|
|
|
func (cp *cwtchPeer) PeerWithOnion(onion string) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-11-05 22:15:56 +00:00
|
|
|
if _, exists := cp.Profile.GetContact(onion); !exists {
|
|
|
|
cp.AddContact(onion, onion, false)
|
|
|
|
}
|
2020-02-03 18:46:15 +00:00
|
|
|
defer cp.mutex.Unlock()
|
2019-01-21 20:08:03 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.PeerRequest, map[event.Field]string{event.RemotePeer: onion}))
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 05:27:11 +00:00
|
|
|
// DeleteContact deletes a peer from the profile, storage, and handling
|
|
|
|
func (cp *cwtchPeer) DeleteContact(onion string) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-08-07 05:27:11 +00:00
|
|
|
cp.Profile.DeleteContact(onion)
|
2020-02-03 18:46:15 +00:00
|
|
|
defer cp.mutex.Unlock()
|
2019-08-07 05:27:11 +00:00
|
|
|
cp.eventBus.Publish(event.NewEventList(event.DeleteContact, event.RemotePeer, onion))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteGroup deletes a Group from the profile, storage, and handling
|
|
|
|
func (cp *cwtchPeer) DeleteGroup(groupID string) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-08-07 05:27:11 +00:00
|
|
|
cp.Profile.DeleteGroup(groupID)
|
2020-02-03 18:46:15 +00:00
|
|
|
defer cp.mutex.Unlock()
|
2019-08-07 05:27:11 +00:00
|
|
|
cp.eventBus.Publish(event.NewEventList(event.DeleteGroup, event.GroupID, groupID))
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:44:13 +00:00
|
|
|
// InviteOnionToGroup kicks off the invite process
|
2018-06-19 22:28:44 +00:00
|
|
|
func (cp *cwtchPeer) InviteOnionToGroup(onion string, groupid string) error {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-10-31 21:39:31 +00:00
|
|
|
group := cp.Profile.GetGroup(groupid)
|
2020-02-03 18:46:15 +00:00
|
|
|
defer cp.mutex.Unlock()
|
2019-01-04 21:44:21 +00:00
|
|
|
if group == nil {
|
|
|
|
return errors.New("invalid group id")
|
2018-03-15 20:53:22 +00:00
|
|
|
}
|
2018-03-09 20:44:13 +00:00
|
|
|
|
2019-01-04 21:44:21 +00:00
|
|
|
invite, err := group.Invite(group.InitialMessage)
|
|
|
|
if err == nil {
|
2019-01-21 20:08:03 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.InvitePeerToGroup, map[event.Field]string{event.RemotePeer: onion, event.GroupInvite: string(invite)}))
|
2019-01-04 21:44:21 +00:00
|
|
|
}
|
|
|
|
return err
|
2018-03-30 21:16:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// JoinServer manages a new server connection with the given onion address
|
2018-06-19 22:28:44 +00:00
|
|
|
func (cp *cwtchPeer) JoinServer(onion string) {
|
2019-01-21 20:08:03 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.JoinServer, map[event.Field]string{event.GroupServer: onion}))
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 20:09:25 +00:00
|
|
|
// SendMessageToGroup attempts to sent the given message to the given group id.
|
2019-02-19 20:18:25 +00:00
|
|
|
// TODO: Deprecate in favour of SendMessageToGroupTracked
|
2018-06-19 22:28:44 +00:00
|
|
|
func (cp *cwtchPeer) SendMessageToGroup(groupid string, message string) error {
|
2019-02-19 20:18:25 +00:00
|
|
|
_, err := cp.SendMessageToGroupTracked(groupid, message)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendMessageToGroup attempts to sent the given message to the given group id.
|
|
|
|
// It returns the signature of the message which can be used to identify it in any UX layer.
|
|
|
|
func (cp *cwtchPeer) SendMessageToGroupTracked(groupid string, message string) (string, error) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-10-31 21:39:31 +00:00
|
|
|
group := cp.Profile.GetGroup(groupid)
|
2020-02-03 18:46:15 +00:00
|
|
|
defer cp.mutex.Unlock()
|
|
|
|
|
2018-05-03 04:12:45 +00:00
|
|
|
if group == nil {
|
2019-02-19 20:18:25 +00:00
|
|
|
return "", errors.New("invalid group id")
|
2018-05-09 19:09:00 +00:00
|
|
|
}
|
2018-06-22 18:11:23 +00:00
|
|
|
ct, sig, err := cp.Profile.EncryptMessageToGroup(message, groupid)
|
2019-01-04 21:44:21 +00:00
|
|
|
|
|
|
|
if err == nil {
|
2019-01-21 20:08:03 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.SendMessageToGroup, map[event.Field]string{event.GroupServer: group.GroupServer, event.Ciphertext: string(ct), event.Signature: string(sig)}))
|
2018-03-30 21:16:51 +00:00
|
|
|
}
|
2019-01-04 21:44:21 +00:00
|
|
|
|
2019-02-19 20:18:25 +00:00
|
|
|
return string(sig), err
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 20:59:54 +00:00
|
|
|
func (cp *cwtchPeer) SendMessageToPeer(onion string, message string) string {
|
2019-01-21 20:08:03 +00:00
|
|
|
event := event.NewEvent(event.SendMessageToPeer, map[event.Field]string{event.RemotePeer: onion, event.Data: message})
|
2019-01-15 20:59:54 +00:00
|
|
|
cp.eventBus.Publish(event)
|
2019-10-18 23:56:10 +00:00
|
|
|
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-10-18 23:56:10 +00:00
|
|
|
cp.Profile.AddSentMessageToContactTimeline(onion, message, time.Now(), event.EventID)
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-10-18 23:56:10 +00:00
|
|
|
|
2019-01-15 20:59:54 +00:00
|
|
|
return event.EventID
|
2019-01-04 21:44:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// TrustPeer sets an existing peer relationship to trusted
|
2018-06-19 22:28:44 +00:00
|
|
|
func (cp *cwtchPeer) TrustPeer(peer string) error {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2018-05-30 17:41:02 +00:00
|
|
|
err := cp.Profile.TrustPeer(peer)
|
|
|
|
if err == nil {
|
|
|
|
cp.PeerWithOnion(peer)
|
2018-05-03 19:23:02 +00:00
|
|
|
}
|
2018-05-30 17:41:02 +00:00
|
|
|
return err
|
2018-05-03 19:23:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// BlockPeer blocks an existing peer relationship.
|
2018-06-19 22:28:44 +00:00
|
|
|
func (cp *cwtchPeer) BlockPeer(peer string) error {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2018-05-30 17:41:02 +00:00
|
|
|
err := cp.Profile.BlockPeer(peer)
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-01-21 20:08:03 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.BlockPeer, map[event.Field]string{event.RemotePeer: peer}))
|
2018-05-30 17:41:02 +00:00
|
|
|
return err
|
2018-05-03 19:23:02 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 18:49:44 +00:00
|
|
|
// UnblockPeer blocks an existing peer relationship.
|
|
|
|
func (cp *cwtchPeer) UnblockPeer(peer string) error {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-08-07 18:49:44 +00:00
|
|
|
err := cp.Profile.UnblockPeer(peer)
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-08-07 18:49:44 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.UnblockPeer, map[event.Field]string{event.RemotePeer: peer}))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-03 18:46:15 +00:00
|
|
|
// ProcessInvite adds a new group invite to the profile. returns the new group ID
|
|
|
|
func (cp *cwtchPeer) ProcessInvite(invite string, remotePeer string) (string, error) {
|
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
|
|
|
return cp.Profile.ProcessInvite(invite, remotePeer)
|
|
|
|
}
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// AcceptInvite accepts a given existing group invite
|
2018-06-19 22:28:44 +00:00
|
|
|
func (cp *cwtchPeer) AcceptInvite(groupID string) error {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-04-23 20:30:50 +00:00
|
|
|
err := cp.Profile.AcceptInvite(groupID)
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-04-23 20:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-14 01:57:42 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.AcceptGroupInvite, map[event.Field]string{event.GroupID: groupID}))
|
2019-04-23 20:30:50 +00:00
|
|
|
cp.JoinServer(cp.Profile.Groups[groupID].GroupServer)
|
|
|
|
|
|
|
|
return nil
|
2018-05-03 04:12:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// RejectInvite rejects a given group invite.
|
2018-06-19 22:28:44 +00:00
|
|
|
func (cp *cwtchPeer) RejectInvite(groupID string) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2018-05-30 17:41:02 +00:00
|
|
|
cp.Profile.RejectInvite(groupID)
|
|
|
|
}
|
|
|
|
|
2019-05-15 20:12:11 +00:00
|
|
|
// Listen makes the peer open a listening port to accept incoming connections (and be detactably online)
|
2018-11-10 22:14:12 +00:00
|
|
|
func (cp *cwtchPeer) Listen() {
|
2019-07-19 17:27:50 +00:00
|
|
|
log.Debugf("cwtchPeer Listen sending ProtocolEngineStartListen\n")
|
2019-10-31 23:05:01 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.ProtocolEngineStartListen, map[event.Field]string{event.Onion: cp.Profile.Onion}))
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 20:12:11 +00:00
|
|
|
// StartGroupConnections attempts to connect to all group servers (thus initiating reconnect attempts in the conectionsmanager)
|
|
|
|
func (cp *cwtchPeer) StartPeersConnections() {
|
|
|
|
for _, contact := range cp.GetContacts() {
|
|
|
|
cp.PeerWithOnion(contact)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartPeerConnections attempts to connect to all peers (thus initiating reconnect attempts in the conectionsmanager)
|
|
|
|
func (cp *cwtchPeer) StartGroupConnections() {
|
|
|
|
joinedServers := map[string]bool{}
|
|
|
|
for _, groupID := range cp.GetGroups() {
|
|
|
|
// Only send a join server packet if we haven't joined this server yet...
|
|
|
|
group := cp.GetGroup(groupID)
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-05-15 20:12:11 +00:00
|
|
|
if joined := joinedServers[groupID]; group.Accepted && !joined {
|
2019-06-21 21:50:43 +00:00
|
|
|
log.Infof("Join Server %v (%v)\n", group.GroupServer, joined)
|
2019-05-15 20:12:11 +00:00
|
|
|
cp.JoinServer(group.GroupServer)
|
|
|
|
joinedServers[group.GroupServer] = true
|
|
|
|
}
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-05-15 20:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 21:39:31 +00:00
|
|
|
// SetAttribute sets an attribute for this profile and emits an event
|
|
|
|
func (cp *cwtchPeer) SetAttribute(key string, val string) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-10-31 21:39:31 +00:00
|
|
|
cp.Profile.SetAttribute(key, val)
|
2020-02-03 18:46:15 +00:00
|
|
|
defer cp.mutex.Unlock()
|
2019-10-31 21:39:31 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{
|
|
|
|
event.Key: key,
|
|
|
|
event.Data: val,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAttribute gets an attribute for the profile
|
|
|
|
func (cp *cwtchPeer) GetAttribute(key string) (string, bool) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2019-10-31 21:39:31 +00:00
|
|
|
return cp.Profile.GetAttribute(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContactAttribute sets an attribute for the indicated contact and emits an event
|
|
|
|
func (cp *cwtchPeer) SetContactAttribute(onion string, key string, val string) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2019-10-31 21:39:31 +00:00
|
|
|
if contact, ok := cp.Profile.GetContact(onion); ok {
|
|
|
|
contact.SetAttribute(key, val)
|
|
|
|
cp.eventBus.Publish(event.NewEvent(event.SetPeerAttribute, map[event.Field]string{
|
|
|
|
event.RemotePeer: onion,
|
|
|
|
event.Key: key,
|
|
|
|
event.Data: val,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContactAttribute gets an attribute for the indicated contact
|
|
|
|
func (cp *cwtchPeer) GetContactAttribute(onion string, key string) (string, bool) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2019-10-31 21:39:31 +00:00
|
|
|
if contact, ok := cp.Profile.GetContact(onion); ok {
|
|
|
|
return contact.GetAttribute(key)
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetGroupAttribute sets an attribute for the indicated group and emits an event
|
|
|
|
func (cp *cwtchPeer) SetGroupAttribute(gid string, key string, val string) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2019-10-31 21:39:31 +00:00
|
|
|
if group := cp.Profile.GetGroup(gid); group != nil {
|
|
|
|
group.SetAttribute(key, val)
|
|
|
|
cp.eventBus.Publish(event.NewEvent(event.SetGroupAttribute, map[event.Field]string{
|
|
|
|
event.GroupID: gid,
|
|
|
|
event.Key: key,
|
|
|
|
event.Data: val,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGroupAttribute gets an attribute for the indicated group
|
|
|
|
func (cp *cwtchPeer) GetGroupAttribute(gid string, key string) (string, bool) {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2019-10-31 21:39:31 +00:00
|
|
|
if group := cp.Profile.GetGroup(gid); group != nil {
|
|
|
|
return group.GetAttribute(key)
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2018-06-15 16:21:07 +00:00
|
|
|
// Shutdown kills all connections and cleans up all goroutines for the peer
|
2018-06-19 22:28:44 +00:00
|
|
|
func (cp *cwtchPeer) Shutdown() {
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
|
|
|
defer cp.mutex.Unlock()
|
2018-11-10 22:14:12 +00:00
|
|
|
cp.shutdown = true
|
2019-01-04 21:44:21 +00:00
|
|
|
cp.queue.Shutdown()
|
2018-05-30 18:42:17 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 21:44:21 +00:00
|
|
|
// eventHandler process events from other subsystems
|
|
|
|
func (cp *cwtchPeer) eventHandler() {
|
|
|
|
for {
|
|
|
|
ev := cp.queue.Next()
|
|
|
|
switch ev.EventType {
|
2019-09-19 23:14:35 +00:00
|
|
|
/***** Default auto handled events *****/
|
|
|
|
|
2019-01-04 21:44:21 +00:00
|
|
|
case event.EncryptedGroupMessage:
|
2019-10-18 23:56:10 +00:00
|
|
|
// If successful, a side effect is the message is added to the group's timeline
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-02-03 03:24:42 +00:00
|
|
|
ok, groupID, message, seen := cp.Profile.AttemptDecryption([]byte(ev.Data[event.Ciphertext]), []byte(ev.Data[event.Signature]))
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-02-03 03:24:42 +00:00
|
|
|
if ok && !seen {
|
2019-02-04 01:55:13 +00:00
|
|
|
cp.eventBus.Publish(event.NewEvent(event.NewMessageFromGroup, map[event.Field]string{event.TimestampReceived: message.Received.Format(time.RFC3339Nano), event.TimestampSent: message.Timestamp.Format(time.RFC3339Nano), event.Data: message.Message, event.GroupID: groupID, event.Signature: string(message.Signature), event.PreviousSignature: string(message.PreviousMessageSig), event.RemotePeer: message.PeerID}))
|
2019-01-04 21:44:21 +00:00
|
|
|
}
|
2019-09-19 23:14:35 +00:00
|
|
|
|
2019-10-18 23:56:10 +00:00
|
|
|
case event.NewMessageFromPeer: //event.TimestampReceived, event.RemotePeer, event.Data
|
|
|
|
ts, _ := time.Parse(time.RFC3339Nano, ev.Data[event.TimestampReceived])
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-10-18 23:56:10 +00:00
|
|
|
cp.Profile.AddMessageToContactTimeline(ev.Data[event.RemotePeer], ev.Data[event.Data], ts)
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-10-18 23:56:10 +00:00
|
|
|
|
|
|
|
case event.PeerAcknowledgement:
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-10-18 23:56:10 +00:00
|
|
|
cp.Profile.AckSentMessageToPeer(ev.Data[event.RemotePeer], ev.Data[event.EventID])
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-10-18 23:56:10 +00:00
|
|
|
|
|
|
|
case event.SendMessageToGroupError:
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-10-18 23:56:10 +00:00
|
|
|
cp.Profile.AddGroupSentMessageError(ev.Data[event.GroupServer], ev.Data[event.Signature], ev.Data[event.Error])
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-10-18 23:56:10 +00:00
|
|
|
|
|
|
|
case event.SendMessageToPeerError:
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-10-18 23:56:10 +00:00
|
|
|
cp.Profile.ErrorSentMessageToPeer(ev.Data[event.RemotePeer], ev.Data[event.EventID], ev.Data[event.Error])
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-10-18 23:56:10 +00:00
|
|
|
|
2019-09-19 23:14:35 +00:00
|
|
|
/***** Non default but requestable handlable events *****/
|
|
|
|
|
2019-01-04 21:44:21 +00:00
|
|
|
case event.NewGroupInvite:
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-09-19 23:14:35 +00:00
|
|
|
cp.Profile.ProcessInvite(ev.Data[event.GroupInvite], ev.Data[event.RemotePeer])
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-05-15 20:12:11 +00:00
|
|
|
case event.PeerStateChange:
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-05-15 20:12:11 +00:00
|
|
|
if _, exists := cp.Profile.Contacts[ev.Data[event.RemotePeer]]; exists {
|
|
|
|
cp.Profile.Contacts[ev.Data[event.RemotePeer]].State = ev.Data[event.ConnectionState]
|
|
|
|
}
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-05-15 20:12:11 +00:00
|
|
|
case event.ServerStateChange:
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Lock()
|
2019-05-15 20:12:11 +00:00
|
|
|
for _, group := range cp.Profile.Groups {
|
|
|
|
if group.GroupServer == ev.Data[event.GroupServer] {
|
|
|
|
group.State = ev.Data[event.ConnectionState]
|
|
|
|
}
|
|
|
|
}
|
2020-02-03 18:46:15 +00:00
|
|
|
cp.mutex.Unlock()
|
2019-01-04 21:44:21 +00:00
|
|
|
default:
|
|
|
|
if ev.EventType != "" {
|
2019-01-13 23:48:17 +00:00
|
|
|
log.Errorf("peer event handler received an event it was not subscribed for: %v", ev.EventType)
|
2019-01-04 21:44:21 +00:00
|
|
|
}
|
2019-01-21 20:08:03 +00:00
|
|
|
return
|
2019-01-04 21:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 19:15:03 +00:00
|
|
|
}
|