forked from cwtch.im/cwtch
Review Board Comments
This commit is contained in:
parent
bc226173e4
commit
71dd298a91
|
@ -169,3 +169,10 @@ const (
|
|||
const (
|
||||
AppErrLoaded0 = "Loaded 0 profiles"
|
||||
)
|
||||
|
||||
// Defining Protocol Contexts
|
||||
const (
|
||||
ContextAck = "im.cwtch.acknowledgement"
|
||||
ContextInvite = "im.cwtch.invite"
|
||||
ContextRaw = "im.cwtch.raw"
|
||||
)
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"cwtch.im/cwtch/protocol"
|
||||
"cwtch.im/tapir"
|
||||
"cwtch.im/tapir/networks/tor"
|
||||
"errors"
|
||||
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
||||
"git.openprivacy.ca/openprivacy/libricochet-go/identity"
|
||||
"github.com/golang/protobuf/proto"
|
||||
|
@ -100,7 +101,7 @@ func (e *engine) eventHandler() {
|
|||
case event.PeerRequest:
|
||||
e.peerWithOnion(ev.Data[event.RemotePeer])
|
||||
case event.InvitePeerToGroup:
|
||||
e.sendMessageToPeer(ev.EventID, ev.Data[event.RemotePeer], "im.cwtch.invite", []byte(ev.Data[event.GroupInvite]))
|
||||
e.sendMessageToPeer(ev.EventID, ev.Data[event.RemotePeer], event.ContextInvite, []byte(ev.Data[event.GroupInvite]))
|
||||
case event.JoinServer:
|
||||
e.joinServer(ev.Data[event.GroupServer])
|
||||
case event.SendMessageToGroup:
|
||||
|
@ -109,7 +110,7 @@ func (e *engine) eventHandler() {
|
|||
// TODO: remove this passthrough once the UI is integrated.
|
||||
context, ok := ev.Data[event.EventContext]
|
||||
if !ok {
|
||||
context = "im.cwtch.raw"
|
||||
context = event.ContextRaw
|
||||
}
|
||||
err := e.sendMessageToPeer(ev.EventID, ev.Data[event.RemotePeer], context, []byte(ev.Data[event.GroupInvite]))
|
||||
if err != nil {
|
||||
|
@ -129,14 +130,19 @@ func (e *engine) eventHandler() {
|
|||
}
|
||||
}
|
||||
|
||||
// Listen sets up an onion listener to process incoming cwtch messages
|
||||
func (e *engine) listenFn() {
|
||||
func (e *engine) createPeerTemplate() *PeerApp {
|
||||
peerAppTemplate := new(PeerApp)
|
||||
peerAppTemplate.MessageHandler = e.handlePeerMessage
|
||||
peerAppTemplate.OnAcknowledgement = e.peerAck
|
||||
peerAppTemplate.OnAuth = e.peerAuthed
|
||||
peerAppTemplate.OnConnecting = e.peerConnecting
|
||||
peerAppTemplate.OnClose = e.peerDisconnected
|
||||
err := e.service.Listen(peerAppTemplate)
|
||||
return peerAppTemplate
|
||||
}
|
||||
|
||||
// Listen sets up an onion listener to process incoming cwtch messages
|
||||
func (e *engine) listenFn() {
|
||||
err := e.service.Listen(e.createPeerTemplate())
|
||||
e.eventManager.Publish(event.NewEvent(event.ProtocolEngineStopped, map[event.Field]string{event.Identity: e.identity.Hostname(), event.Error: err.Error()}))
|
||||
return
|
||||
}
|
||||
|
@ -150,12 +156,7 @@ func (e *engine) Shutdown() {
|
|||
|
||||
// peerWithOnion is the entry point for cwtchPeer relationships
|
||||
func (e *engine) peerWithOnion(onion string) {
|
||||
peerAppTemplate := new(PeerApp)
|
||||
peerAppTemplate.MessageHandler = e.handlePeerMessage
|
||||
peerAppTemplate.OnAcknowledgement = e.peerAck
|
||||
peerAppTemplate.OnAuth = e.peerAuthed
|
||||
peerAppTemplate.OnClose = e.peerDisconnected
|
||||
e.service.Connect(onion, peerAppTemplate)
|
||||
e.service.Connect(onion, e.createPeerTemplate())
|
||||
}
|
||||
|
||||
func (e *engine) peerAuthed(onion string) {
|
||||
|
@ -165,6 +166,13 @@ func (e *engine) peerAuthed(onion string) {
|
|||
}))
|
||||
}
|
||||
|
||||
func (e *engine) peerConnecting(onion string) {
|
||||
e.eventManager.Publish(event.NewEvent(event.PeerStateChange, map[event.Field]string{
|
||||
event.RemotePeer: string(onion),
|
||||
event.ConnectionState: ConnectionStateName[CONNECTING],
|
||||
}))
|
||||
}
|
||||
|
||||
func (e *engine) peerAck(eventID string) {
|
||||
e.eventManager.Publish(event.NewEvent(event.PeerAcknowledgement, map[event.Field]string{
|
||||
event.EventID: eventID,
|
||||
|
@ -187,7 +195,7 @@ func (e *engine) sendMessageToPeer(eventID string, onion string, context string,
|
|||
peerApp.SendMessage(PeerMessage{eventID, context, message})
|
||||
return nil
|
||||
}
|
||||
panic("this should never happen")
|
||||
return errors.New("failed type assertion conn.App != PeerApp")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
@ -229,6 +237,7 @@ func (e *engine) handlePeerMessage(hostname string, message []byte) {
|
|||
marshal, _ := proto.Marshal(cpp.GetGroupChatInvite())
|
||||
e.eventManager.Publish(event.NewEvent(event.NewGroupInvite, map[event.Field]string{event.TimestampReceived: time.Now().Format(time.RFC3339Nano), event.RemotePeer: hostname, event.GroupInvite: string(marshal)}))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
e.eventManager.Publish(event.NewEvent(event.NewMessageFromPeer, map[event.Field]string{event.TimestampReceived: time.Now().Format(time.RFC3339Nano), event.RemotePeer: hostname, event.Data: string(message)}))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package connections
|
||||
|
||||
import (
|
||||
"cwtch.im/cwtch/event"
|
||||
"cwtch.im/tapir"
|
||||
"cwtch.im/tapir/applications"
|
||||
"encoding/json"
|
||||
|
@ -12,9 +13,10 @@ type PeerApp struct {
|
|||
applications.AuthApp
|
||||
connection *tapir.Connection
|
||||
MessageHandler func(string, []byte)
|
||||
OnAcknowledgement func(string)
|
||||
OnAuth func(string)
|
||||
OnClose func(string)
|
||||
OnAcknowledgement func(string)
|
||||
OnConnecting func(string)
|
||||
}
|
||||
|
||||
// PeerMessage is an encapsulation that can be used by higher level applications
|
||||
|
@ -28,13 +30,21 @@ type PeerMessage struct {
|
|||
func (pa PeerApp) NewInstance() tapir.Application {
|
||||
newApp := new(PeerApp)
|
||||
newApp.MessageHandler = pa.MessageHandler
|
||||
newApp.OnAcknowledgement = pa.OnAcknowledgement
|
||||
newApp.OnAuth = pa.OnAuth
|
||||
newApp.OnClose = pa.OnClose
|
||||
newApp.OnConnecting = pa.OnConnecting
|
||||
return newApp
|
||||
}
|
||||
|
||||
// Init is run when the connection is first started.
|
||||
func (pa *PeerApp) Init(connection *tapir.Connection) {
|
||||
|
||||
// We don't want to issue an event for unknown peers (we only know inbound peer hostnames once we have authenticated)
|
||||
if connection.Outbound {
|
||||
pa.OnConnecting(connection.Hostname)
|
||||
}
|
||||
|
||||
// First run the Authentication App
|
||||
pa.AuthApp.Init(connection)
|
||||
|
||||
|
@ -58,7 +68,7 @@ func (pa PeerApp) listen() {
|
|||
var peerMessage PeerMessage
|
||||
err := json.Unmarshal(message, &peerMessage)
|
||||
if err == nil {
|
||||
if peerMessage.Context == "im.cwtch.acknowledgement" {
|
||||
if peerMessage.Context == event.ContextAck {
|
||||
pa.OnAcknowledgement(peerMessage.ID)
|
||||
} else {
|
||||
pa.MessageHandler(pa.connection.Hostname, peerMessage.Data)
|
||||
|
|
Loading…
Reference in New Issue