cwtch/protocol/connections/peerapp.go

100 lines
2.7 KiB
Go
Raw Normal View History

2019-07-17 19:10:52 +00:00
package connections
import (
model2 "cwtch.im/cwtch/protocol/model"
"encoding/json"
2021-04-09 01:22:08 +00:00
"git.openprivacy.ca/cwtch.im/tapir"
"git.openprivacy.ca/cwtch.im/tapir/applications"
"git.openprivacy.ca/openprivacy/log"
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)
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)
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
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 model2.PeerMessage
err := json.Unmarshal(message, &peerMessage)
if err == nil {
if pa.IsAllowed(pa.connection.Hostname()) {
pa.MessageHandler(pa.connection.Hostname(), peerMessage.ID, peerMessage.Context, peerMessage.Data)
}
} 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 model2.PeerMessage) error {
serialized, err := json.Marshal(message)
if err == nil {
return pa.connection.Send(serialized)
}
return err
2019-07-17 19:10:52 +00:00
}