cwtch/peer/send/peer_send_channel.go

92 lines
2.8 KiB
Go
Raw Normal View History

package send
2018-03-09 20:44:13 +00:00
import (
"errors"
"git.mascherari.press/cwtch/protocol"
"git.mascherari.press/cwtch/protocol/spam"
"github.com/golang/protobuf/proto"
"github.com/s-rah/go-ricochet/channels"
"github.com/s-rah/go-ricochet/utils"
"github.com/s-rah/go-ricochet/wire/control"
)
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".
2018-03-14 22:23:35 +00:00
func (cplc *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.
2018-03-14 22:23:35 +00:00
func (cplc *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.
2018-03-14 22:23:35 +00:00
func (cplc *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
2018-03-14 22:23:35 +00:00
func (cplc *CwtchPeerSendChannel) 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 *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
2018-03-14 22:23:35 +00:00
func (cplc *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.
2018-03-14 22:23:35 +00:00
func (cplc *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
2018-03-09 20:44:13 +00:00
func (cplc *CwtchPeerSendChannel) OpenOutbound(channel *channels.Channel) ([]byte, error) {
2018-03-09 21:02:42 +00:00
cplc.spamGuard.Difficulty = 2
2018-03-09 20:44:13 +00:00
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 the open channel request and sets the spamguard challenge
2018-03-09 20:44:13 +00:00
func (cplc *CwtchPeerSendChannel) OpenOutboundResult(err error, crm *Protocol_Data_Control.ChannelResult) {
if err == nil {
if crm.GetOpened() {
cplc.channel.Pending = false
ce, _ := proto.GetExtension(crm, protocol.E_ServerNonce)
2018-03-09 21:02:42 +00:00
cplc.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.
2018-03-09 20:44:13 +00:00
func (cplc *CwtchPeerSendChannel) SendGroupMessage(gm *protocol.GroupMessage) {
2018-03-09 21:02:42 +00:00
sgsolve := cplc.spamGuard.SolveChallenge(cplc.challenge, gm.GetCiphertext())
gm.Spamguard = sgsolve[:]
2018-03-09 20:44:13 +00:00
csp := &protocol.CwtchServerPacket{
GroupMessage: gm,
}
packet, _ := proto.Marshal(csp)
cplc.channel.SendMessage(packet)
2018-03-30 21:16:51 +00:00
cplc.channel.CloseChannel()
2018-03-09 20:44:13 +00:00
}
2018-03-09 21:02:42 +00:00
// Packet should never be
2018-03-14 22:23:35 +00:00
func (cplc *CwtchPeerSendChannel) Packet(data []byte) {
2018-03-09 20:44:13 +00:00
// If we receive a packet on this channel, close the connection
2018-03-14 22:23:35 +00:00
cplc.channel.CloseChannel()
2018-03-09 20:44:13 +00:00
}