cwtch/protocol/connections/peerapp.go

127 lines
3.6 KiB
Go
Raw Normal View History

2019-07-17 19:10:52 +00:00
package connections
import (
2019-07-23 17:57:04 +00:00
"cwtch.im/cwtch/event"
2019-07-17 19:10:52 +00:00
"cwtch.im/tapir"
"cwtch.im/tapir/applications"
"encoding/json"
"git.openprivacy.ca/openprivacy/log"
"sync"
2019-07-17 19:10:52 +00:00
)
const cwtchCapability = tapir.Capability("cwtchCapability")
2019-07-17 19:10:52 +00:00
// PeerApp encapsulates the behaviour of a Cwtch Peer
type PeerApp struct {
applications.AuthApp
2019-08-08 18:39:38 +00:00
connection tapir.Connection
MessageHandler func(string, string, string, []byte)
RetValHandler func(string, []byte, []byte)
IsBlocked func(string) bool
IsAllowed func(string) bool
2019-07-29 20:21:58 +00:00
OnAcknowledgement func(string, string)
OnAuth func(string)
OnClose func(string)
2019-07-23 17:57:04 +00:00
OnConnecting func(string)
getValRequests sync.Map // [string]string eventID:Data
}
// PeerMessage is an encapsulation that can be used by higher level applications
type PeerMessage struct {
ID string // A unique Message ID (primarily used for acknowledgments)
Context string // A unique context identifier i.e. im.cwtch.chat
Data []byte // The serialized data packet.
2019-07-17 19:10:52 +00:00
}
type peerGetVal struct {
Scope, Path string
}
type peerRetVal struct {
Val string
Exists bool
}
2019-07-17 19:10:52 +00:00
// NewInstance should always return a new instantiation of the application.
func (pa *PeerApp) NewInstance() tapir.Application {
2019-07-17 19:10:52 +00:00
newApp := new(PeerApp)
newApp.MessageHandler = pa.MessageHandler
newApp.IsBlocked = pa.IsBlocked
newApp.IsAllowed = pa.IsAllowed
2019-07-23 17:57:04 +00:00
newApp.OnAcknowledgement = pa.OnAcknowledgement
2019-07-17 19:10:52 +00:00
newApp.OnAuth = pa.OnAuth
newApp.OnClose = pa.OnClose
2019-07-23 17:57:04 +00:00
newApp.OnConnecting = pa.OnConnecting
newApp.RetValHandler = pa.RetValHandler
2019-07-17 19:10:52 +00:00
return newApp
}
// Init is run when the connection is first started.
2019-08-08 18:39:38 +00:00
func (pa *PeerApp) Init(connection tapir.Connection) {
2019-07-17 19:10:52 +00:00
// First run the Authentication App
pa.AuthApp.Init(connection)
if connection.HasCapability(applications.AuthCapability) {
2019-07-17 19:10:52 +00:00
pa.connection = connection
connection.SetCapability(cwtchCapability)
2019-08-08 18:39:38 +00:00
if pa.IsBlocked(connection.Hostname()) {
pa.connection.Close()
2019-08-08 18:39:38 +00:00
pa.OnClose(connection.Hostname())
} else {
2019-08-08 18:39:38 +00:00
pa.OnAuth(connection.Hostname())
go pa.listen()
}
2019-07-17 19:10:52 +00:00
} else {
// The auth protocol wasn't completed, we can safely shutdown the connection
connection.Close()
2019-07-17 19:10:52 +00:00
}
}
func (pa *PeerApp) listen() {
2019-07-17 19:10:52 +00:00
for {
message := pa.connection.Expect()
if len(message) == 0 {
2020-07-14 00:46:05 +00:00
log.Debugf("0 byte read, socket has likely failed. Closing the listen goroutine")
2019-08-08 18:39:38 +00:00
pa.OnClose(pa.connection.Hostname())
2019-07-17 19:10:52 +00:00
return
}
var peerMessage PeerMessage
err := json.Unmarshal(message, &peerMessage)
if err == nil {
switch peerMessage.Context {
case event.ContextAck:
2019-08-08 18:39:38 +00:00
pa.OnAcknowledgement(pa.connection.Hostname(), peerMessage.ID)
case event.ContextRetVal:
req, ok := pa.getValRequests.Load(peerMessage.ID)
if ok {
reqStr := []byte(req.(string))
pa.RetValHandler(pa.connection.Hostname(), reqStr, peerMessage.Data)
pa.getValRequests.Delete(peerMessage.ID)
}
default:
if pa.IsAllowed(pa.connection.Hostname()) {
pa.MessageHandler(pa.connection.Hostname(), peerMessage.ID, peerMessage.Context, peerMessage.Data)
2019-07-30 23:47:12 +00:00
// Acknowledge the message
pa.SendMessage(PeerMessage{peerMessage.ID, event.ContextAck, []byte{}})
}
}
} else {
log.Errorf("Error unmarshalling PeerMessage package: %x %v", message, err)
}
2019-07-17 19:10:52 +00:00
}
}
// SendMessage sends the peer a preformatted message
// NOTE: This is a stub, we will likely want to extend this to better reflect the desired protocol
func (pa *PeerApp) SendMessage(message PeerMessage) {
if message.Context == event.ContextGetVal {
pa.getValRequests.Store(message.ID, string(message.Data))
}
serialized, _ := json.Marshal(message)
pa.connection.Send(serialized)
2019-07-17 19:10:52 +00:00
}