cwtch/peer/send/peer_send_channel.go

97 lines
3.0 KiB
Go
Raw Normal View History

package send
2018-03-09 20:44:13 +00:00
import (
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/protocol"
"cwtch.im/cwtch/protocol/spam"
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
// CwtchPeerSendChannel is the peer implementation of im.cwtch.server.send
2018-03-09 20:44:13 +00:00
type CwtchPeerSendChannel struct {
2018-03-09 21:02:42 +00:00
channel *channels.Channel
2018-03-14 22:23:35 +00:00
spamGuard spam.Guard
2018-03-09 20:44:13 +00:00
challenge []byte
}
2018-03-09 20:49:49 +00:00
// Type returns the type string for this channel, e.g. "im.ricochet.server.send".
func (cpsc *CwtchPeerSendChannel) Type() string {
2018-03-09 20:44:13 +00:00
return "im.cwtch.server.send"
}
// Closed is called when the channel is closed for any reason.
func (cpsc *CwtchPeerSendChannel) Closed(err error) {
2018-03-09 20:44:13 +00:00
}
2018-03-09 20:49:49 +00:00
// OnlyClientCanOpen - for Cwtch server channels only peers may open.
func (cpsc *CwtchPeerSendChannel) OnlyClientCanOpen() bool {
2018-03-09 20:44:13 +00:00
return true
}
// Singleton - for Cwtch channels there can only be one instance per direction
func (cpsc *CwtchPeerSendChannel) Singleton() bool {
2018-03-09 20:44:13 +00:00
return true
}
// Bidirectional - for Cwtch channels are not bidrectional
func (cpsc *CwtchPeerSendChannel) 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
func (cpsc *CwtchPeerSendChannel) RequiresAuthentication() string {
2018-03-09 20:44:13 +00:00
return "none"
}
2018-03-09 20:49:49 +00:00
// OpenInbound should never be called on peers.
func (cpsc *CwtchPeerSendChannel) 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 21:02:42 +00:00
// OpenOutbound is used to set up a new send channel and initialize spamguard
func (cpsc *CwtchPeerSendChannel) OpenOutbound(channel *channels.Channel) ([]byte, error) {
cpsc.spamGuard.Difficulty = 2
cpsc.channel = channel
2018-03-09 20:44:13 +00:00
messageBuilder := new(utils.MessageBuilder)
return messageBuilder.OpenChannel(channel.ID, cpsc.Type()), nil
2018-03-09 20:44:13 +00:00
}
2018-03-09 20:49:49 +00:00
// OpenOutboundResult confirms the open channel request and sets the spamguard challenge
func (cpsc *CwtchPeerSendChannel) OpenOutboundResult(err error, crm *Protocol_Data_Control.ChannelResult) {
2018-03-09 20:44:13 +00:00
if err == nil {
if crm.GetOpened() {
cpsc.channel.Pending = false
2018-03-09 20:44:13 +00:00
ce, _ := proto.GetExtension(crm, protocol.E_ServerNonce)
cpsc.challenge = ce.([]byte)[:]
2018-03-09 20:44:13 +00:00
}
}
}
2018-03-09 20:49:49 +00:00
// SendGroupMessage performs the spamguard proof of work and sends a message.
func (cpsc *CwtchPeerSendChannel) SendGroupMessage(gm *protocol.GroupMessage) error {
if cpsc.channel.Pending == false {
sgsolve := cpsc.spamGuard.SolveChallenge(cpsc.challenge, gm.GetCiphertext())
gm.Spamguard = sgsolve[:]
csp := &protocol.CwtchServerPacket{
GroupMessage: gm,
}
packet, _ := proto.Marshal(csp)
cpsc.channel.SendMessage(packet)
cpsc.channel.CloseChannel()
} else {
return errors.New("channel isn't set up yet")
2018-03-09 20:44:13 +00:00
}
return nil
2018-03-09 20:44:13 +00:00
}
2018-03-09 21:02:42 +00:00
// Packet should never be
func (cpsc *CwtchPeerSendChannel) Packet(data []byte) {
2018-03-09 20:44:13 +00:00
// If we receive a packet on this channel, close the connection
cpsc.channel.CloseChannel()
2018-03-09 20:44:13 +00:00
}