Browse Source

Defining Event Fields as Constants

common_fields
Sarah Jamie Lewis 1 year ago
parent
commit
dc6cd56098
4 changed files with 36 additions and 21 deletions
  1. 1
    0
      .gitignore
  2. 13
    0
      event/common.go
  3. 10
    9
      peer/cwtch_peer.go
  4. 12
    12
      protocol/connections/engine.go

+ 1
- 0
.gitignore View File

@@ -8,3 +8,4 @@
server/app/messages
.reviewboardrc
/vendor/
/testing/tor/

+ 13
- 0
event/common.go View File

@@ -31,6 +31,19 @@ type Field string

// Defining Common Field Types
const (
RemotePeer = Field("RemotePeer")
Ciphertext = Field("Ciphertext")
Signature = Field("Signature")
TimestampSent = Field("TimestampSent")
TimestampReceived = Field("TimestampReceived")

Identity = Field("Identity")

GroupID = Field("GroupID")
GroupServer = Field("GroupServer")
GroupInvite = Field("GroupInvite")

Data = Field("Data")

Error = Field("Error")
)

+ 10
- 9
peer/cwtch_peer.go View File

@@ -178,7 +178,7 @@ func (cp *cwtchPeer) GetProfile() *model.Profile {

// PeerWithOnion is the entry point for cwtchPeer relationships
func (cp *cwtchPeer) PeerWithOnion(onion string) *connections.PeerPeerConnection {
cp.eventBus.Publish(event.NewEvent(event.PeerRequest, map[event.Field]string{"Onion": onion}))
cp.eventBus.Publish(event.NewEvent(event.PeerRequest, map[event.Field]string{event.RemotePeer: onion}))
return nil
}

@@ -191,14 +191,14 @@ func (cp *cwtchPeer) InviteOnionToGroup(onion string, groupid string) error {

invite, err := group.Invite(group.InitialMessage)
if err == nil {
cp.eventBus.Publish(event.NewEvent(event.InvitePeerToGroup, map[event.Field]string{"Onion": onion, "Invite": string(invite)}))
cp.eventBus.Publish(event.NewEvent(event.InvitePeerToGroup, map[event.Field]string{event.RemotePeer: onion, event.GroupInvite: string(invite)}))
}
return err
}

// JoinServer manages a new server connection with the given onion address
func (cp *cwtchPeer) JoinServer(onion string) {
cp.eventBus.Publish(event.NewEvent(event.JoinServer, map[event.Field]string{"Onion": onion}))
cp.eventBus.Publish(event.NewEvent(event.JoinServer, map[event.Field]string{event.GroupServer: onion}))
}

// SendMessageToGroup attemps to sent the given message to the given group id.
@@ -210,14 +210,14 @@ func (cp *cwtchPeer) SendMessageToGroup(groupid string, message string) error {
ct, sig, err := cp.Profile.EncryptMessageToGroup(message, groupid)

if err == nil {
cp.eventBus.Publish(event.NewEvent(event.SendMessageToGroup, map[event.Field]string{"Server": group.GroupServer, "Ciphertext": string(ct), "Signature": string(sig)}))
cp.eventBus.Publish(event.NewEvent(event.SendMessageToGroup, map[event.Field]string{event.GroupServer: group.GroupServer, event.Ciphertext: string(ct), event.Signature: string(sig)}))
}

return err
}

func (cp *cwtchPeer) SendMessageToPeer(onion string, message string) string {
event := event.NewEvent(event.SendMessageToPeer, map[event.Field]string{"Peer": onion, "Message": message})
event := event.NewEvent(event.SendMessageToPeer, map[event.Field]string{event.RemotePeer: onion, event.Data: message})
cp.eventBus.Publish(event)
return event.EventID
}
@@ -244,7 +244,7 @@ func (cp *cwtchPeer) TrustPeer(peer string) error {
// BlockPeer blocks an existing peer relationship.
func (cp *cwtchPeer) BlockPeer(peer string) error {
err := cp.Profile.BlockPeer(peer)
cp.eventBus.Publish(event.NewEvent(event.BlockPeer, map[event.Field]string{"Onion": peer}))
cp.eventBus.Publish(event.NewEvent(event.BlockPeer, map[event.Field]string{event.RemotePeer: peer}))
return err
}

@@ -280,19 +280,20 @@ func (cp *cwtchPeer) eventHandler() {
ev := cp.queue.Next()
switch ev.EventType {
case event.EncryptedGroupMessage:
ok, groupID, message := cp.Profile.AttemptDecryption([]byte(ev.Data["Ciphertext"]), []byte(ev.Data["Signature"]))
ok, groupID, message := cp.Profile.AttemptDecryption([]byte(ev.Data[event.Ciphertext]), []byte(ev.Data[event.Signature]))
log.Debugf("ok,gid,msg = %v,%v,%v", ok, groupID, message)
if ok {
cp.eventBus.Publish(event.NewEvent(event.NewMessageFromGroup, map[event.Field]string{event.TimestampReceived: message.Received.String(), event.TimestampSent: message.Timestamp.String(), "Data": message.Message, "GroupID": groupID, "Onion": message.PeerID}))
cp.eventBus.Publish(event.NewEvent(event.NewMessageFromGroup, map[event.Field]string{event.TimestampReceived: message.Received.String(), event.TimestampSent: message.Timestamp.String(), event.Data: message.Message, event.GroupID: groupID, event.RemotePeer: message.PeerID}))
}
case event.NewGroupInvite:
var groupInvite protocol.GroupChatInvite
proto.Unmarshal([]byte(ev.Data["GroupInvite"]), &groupInvite)
cp.Profile.ProcessInvite(&groupInvite, ev.Data["Onion"])
cp.Profile.ProcessInvite(&groupInvite, ev.Data[event.RemotePeer])
default:
if ev.EventType != "" {
log.Errorf("peer event handler received an event it was not subscribed for: %v", ev.EventType)
}
return
}
}
}

+ 12
- 12
protocol/connections/engine.go View File

@@ -75,22 +75,22 @@ func (e *Engine) eventHandler() {
case event.StatusRequest:
e.eventManager.Publish(event.Event{EventType: event.ProtocolEngineStatus, EventID: ev.EventID})
case event.PeerRequest:
e.PeerWithOnion(ev.Data["Onion"])
e.PeerWithOnion(ev.Data[event.RemotePeer])
case event.InvitePeerToGroup:
e.InviteOnionToGroup(ev.Data["Onion"], []byte(ev.Data["Invite"]))
e.InviteOnionToGroup(ev.Data[event.RemotePeer], []byte(ev.Data[event.GroupInvite]))
case event.JoinServer:
e.JoinServer(ev.Data["Onion"])
e.JoinServer(ev.Data[event.GroupServer])
case event.SendMessageToGroup:
e.SendMessageToGroup(ev.Data["Server"], []byte(ev.Data["Ciphertext"]), []byte(ev.Data["Signature"]))
e.SendMessageToGroup(ev.Data[event.GroupServer], []byte(ev.Data[event.Ciphertext]), []byte(ev.Data[event.Signature]))
case event.SendMessageToPeer:
log.Debugf("Sending Message to Peer.....")
ppc := e.connectionsManager.GetPeerPeerConnectionForOnion(ev.Data["Peer"])
ppc := e.connectionsManager.GetPeerPeerConnectionForOnion(ev.Data[event.RemotePeer])
if ppc != nil {
// TODO this will block.
ppc.SendPacket([]byte(ev.Data["Message"]))
ppc.SendPacket([]byte(ev.Data[event.Data]))
}
case event.BlockPeer:
e.blocked.Store(ev.Data["Peer"], true)
e.blocked.Store(ev.Data[event.RemotePeer], true)
case event.ProtocolEngineStartListen:
go e.listenFn()
default:
@@ -110,7 +110,7 @@ func (e *Engine) listenFn() {
ra := new(application.RicochetApplication)
onionService, err := e.ACN.Listen(e.privateKey, application.RicochetPort)
if err != nil /*&& fmt.Sprintf("%v", err) != "550 Unspecified Tor error: Onion address collision"*/ {
e.eventManager.Publish(event.NewEvent(event.ProtocolEngineStopped, map[event.Field]string{"Identity": e.Identity.Hostname(), "Error": err.Error()}))
e.eventManager.Publish(event.NewEvent(event.ProtocolEngineStopped, map[event.Field]string{event.Identity: e.Identity.Hostname(), event.Error: err.Error()}))
return
}

@@ -141,7 +141,7 @@ func (e *Engine) listenFn() {
e.started = true
e.app = ra
ra.Run(onionService)
e.eventManager.Publish(event.NewEvent(event.ProtocolEngineStopped, map[event.Field]string{"Identity": e.Identity.Hostname()}))
e.eventManager.Publish(event.NewEvent(event.ProtocolEngineStopped, map[event.Field]string{event.Identity: e.Identity.Hostname()}))
return
}

@@ -198,7 +198,7 @@ func (e *Engine) InviteOnionToGroup(onion string, invite []byte) error {
func (e *Engine) ReceiveGroupMessage(server string, gm *protocol.GroupMessage) {
// 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{"Ciphertext": string(gm.GetCiphertext()), "Signature": string(gm.GetSignature())}))
e.eventManager.Publish(event.NewEvent(event.EncryptedGroupMessage, map[event.Field]string{event.Ciphertext: string(gm.GetCiphertext()), event.Signature: string(gm.GetSignature())}))
}

// JoinServer manages a new server connection with the given onion address
@@ -254,12 +254,12 @@ func (cph *CwtchPeerHandler) HandleGroupInvite(gci *protocol.GroupChatInvite) {
log.Debugf("Received GroupID from %v %v\n", cph.Onion, gci.String())
marshal, err := proto.Marshal(gci)
if err == nil {
cph.EventBus.Publish(event.NewEvent(event.NewGroupInvite, map[event.Field]string{event.TimestampReceived: time.Now().String(), "Onion": cph.Onion, "GroupInvite": string(marshal)}))
cph.EventBus.Publish(event.NewEvent(event.NewGroupInvite, map[event.Field]string{event.TimestampReceived: time.Now().String(), event.RemotePeer: cph.Onion, event.GroupInvite: string(marshal)}))
}
}

// HandlePacket handles the Cwtch cwtchPeer Data Channel
func (cph *CwtchPeerHandler) HandlePacket(data []byte) []byte {
cph.EventBus.Publish(event.NewEvent(event.NewMessageFromPeer, map[event.Field]string{event.TimestampReceived: time.Now().String(), "Onion": cph.Onion, "Data": string(data)}))
cph.EventBus.Publish(event.NewEvent(event.NewMessageFromPeer, map[event.Field]string{event.TimestampReceived: time.Now().String(), event.RemotePeer: cph.Onion, event.Data: string(data)}))
return []byte{} // TODO remove this
}

Loading…
Cancel
Save