2018-03-09 20:44:13 +00:00
|
|
|
package listen
|
|
|
|
|
|
|
|
import (
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/protocol"
|
2018-03-09 20:44:13 +00:00
|
|
|
"errors"
|
2018-06-23 16:15:36 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/wire/control"
|
2018-06-29 19:20:07 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-03-09 20:44:13 +00:00
|
|
|
)
|
|
|
|
|
2018-03-09 20:49:49 +00:00
|
|
|
// CwtchPeerListenChannel is the peer implementation of im.cwtch.server.listen
|
2018-03-09 20:44:13 +00:00
|
|
|
type CwtchPeerListenChannel struct {
|
|
|
|
channel *channels.Channel
|
|
|
|
Handler CwtchPeerSendChannelHandler
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:49:49 +00:00
|
|
|
// CwtchPeerSendChannelHandler is implemented by peers who want to listen to new messages
|
2018-03-09 20:44:13 +00:00
|
|
|
type CwtchPeerSendChannelHandler interface {
|
|
|
|
HandleGroupMessage(*protocol.GroupMessage)
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:49:49 +00:00
|
|
|
// Type returns the type string for this channel, e.g. "im.ricochet.server.listen".
|
2018-03-14 22:23:35 +00:00
|
|
|
func (cplc *CwtchPeerListenChannel) Type() string {
|
2018-03-09 20:44:13 +00:00
|
|
|
return "im.cwtch.server.listen"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closed is called when the channel is closed for any reason.
|
2018-03-14 22:23:35 +00:00
|
|
|
func (cplc *CwtchPeerListenChannel) Closed(err error) {
|
2018-03-09 20:44:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:49:49 +00:00
|
|
|
// OnlyClientCanOpen - for Cwtch server channels can only be opened by peers
|
2018-03-14 22:23:35 +00:00
|
|
|
func (cplc *CwtchPeerListenChannel) OnlyClientCanOpen() bool {
|
2018-03-09 20:44:13 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Singleton - for Cwtch channels there can only be one instance per direction
|
2018-03-14 22:23:35 +00:00
|
|
|
func (cplc *CwtchPeerListenChannel) Singleton() bool {
|
2018-03-09 20:44:13 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bidirectional - for Cwtch channels are not bidrectional
|
2018-03-14 22:23:35 +00:00
|
|
|
func (cplc *CwtchPeerListenChannel) Bidirectional() bool {
|
2018-03-09 20:44:13 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:49:49 +00:00
|
|
|
// RequiresAuthentication - Cwtch channels require no auth channels
|
2018-03-14 22:23:35 +00:00
|
|
|
func (cplc *CwtchPeerListenChannel) RequiresAuthentication() string {
|
2018-03-09 20:44:13 +00:00
|
|
|
return "none"
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:49:49 +00:00
|
|
|
// OpenInbound - peers should never respond to open inbound requests from servers
|
2018-03-14 22:23:35 +00:00
|
|
|
func (cplc *CwtchPeerListenChannel) OpenInbound(channel *channels.Channel, raw *Protocol_Data_Control.OpenChannel) ([]byte, error) {
|
2018-03-09 20:44:13 +00:00
|
|
|
return nil, errors.New("client does not receive inbound listen channels")
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:49:49 +00:00
|
|
|
// OpenOutbound sets up a new server listen channel
|
2018-03-09 20:44:13 +00:00
|
|
|
func (cplc *CwtchPeerListenChannel) OpenOutbound(channel *channels.Channel) ([]byte, error) {
|
|
|
|
cplc.channel = channel
|
|
|
|
messageBuilder := new(utils.MessageBuilder)
|
|
|
|
return messageBuilder.OpenChannel(channel.ID, cplc.Type()), nil
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:49:49 +00:00
|
|
|
// OpenOutboundResult confirms a previous open channel request
|
2018-03-09 20:44:13 +00:00
|
|
|
func (cplc *CwtchPeerListenChannel) OpenOutboundResult(err error, crm *Protocol_Data_Control.ChannelResult) {
|
|
|
|
if err == nil {
|
|
|
|
if crm.GetOpened() {
|
|
|
|
cplc.channel.Pending = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:49:49 +00:00
|
|
|
// Packet is called for each server packet received on this channel.
|
2018-03-09 20:44:13 +00:00
|
|
|
func (cplc *CwtchPeerListenChannel) Packet(data []byte) {
|
|
|
|
csp := &protocol.CwtchServerPacket{}
|
|
|
|
err := proto.Unmarshal(data, csp)
|
|
|
|
if err == nil {
|
|
|
|
if csp.GetGroupMessage() != nil {
|
|
|
|
gm := csp.GetGroupMessage()
|
2018-05-16 21:33:13 +00:00
|
|
|
// We create a new go routine here to avoid leaking any information about processing time
|
|
|
|
// TODO Server can probably try to use this to DoS a peer
|
2018-05-16 21:31:06 +00:00
|
|
|
go cplc.Handler.HandleGroupMessage(gm)
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|