103 lines
3.5 KiB
Go
103 lines
3.5 KiB
Go
|
package listen
|
||
|
|
||
|
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"
|
||
|
)
|
||
|
|
||
|
// CwtchChannel implements the ChannelHandler interface for a channel of
|
||
|
// type "im.ricochet.Cwtch". The channel may be inbound or outbound.
|
||
|
//
|
||
|
// CwtchChannel implements protocol-level sanity and state validation, but
|
||
|
// does not handle or acknowledge Cwtch messages. The application must provide
|
||
|
// a CwtchChannelHandler implementation to handle Cwtch events.
|
||
|
type CwtchPeerSendChannel struct {
|
||
|
channel *channels.Channel
|
||
|
spamGuard spam.SpamGuard
|
||
|
challenge []byte
|
||
|
}
|
||
|
|
||
|
// Type returns the type string for this channel, e.g. "im.ricochet.Cwtch".
|
||
|
func (cc *CwtchPeerSendChannel) Type() string {
|
||
|
return "im.cwtch.server.send"
|
||
|
}
|
||
|
|
||
|
// Closed is called when the channel is closed for any reason.
|
||
|
func (cc *CwtchPeerSendChannel) Closed(err error) {
|
||
|
|
||
|
}
|
||
|
|
||
|
// OnlyClientCanOpen - for Cwtch channels any side can open
|
||
|
func (cc *CwtchPeerSendChannel) OnlyClientCanOpen() bool {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// Singleton - for Cwtch channels there can only be one instance per direction
|
||
|
func (cc *CwtchPeerSendChannel) Singleton() bool {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// Bidirectional - for Cwtch channels are not bidrectional
|
||
|
func (cc *CwtchPeerSendChannel) Bidirectional() bool {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// RequiresAuthentication - Cwtch channels require hidden service auth
|
||
|
func (cc *CwtchPeerSendChannel) RequiresAuthentication() string {
|
||
|
return "none"
|
||
|
}
|
||
|
|
||
|
// OpenInbound is the first method called for an inbound channel request.
|
||
|
// If an error is returned, the channel is rejected. If a RawMessage is
|
||
|
// returned, it will be sent as the ChannelResult message.
|
||
|
func (cc *CwtchPeerSendChannel) OpenInbound(channel *channels.Channel, raw *Protocol_Data_Control.OpenChannel) ([]byte, error) {
|
||
|
return nil, errors.New("client does not receive inbound listen channels")
|
||
|
}
|
||
|
|
||
|
// OpenOutbound is the first method called for an outbound channel request.
|
||
|
// If an error is returned, the channel is not opened. If a RawMessage is
|
||
|
// returned, it will be sent as the OpenChannel message.
|
||
|
func (cplc *CwtchPeerSendChannel) OpenOutbound(channel *channels.Channel) ([]byte, error) {
|
||
|
cplc.spamGuard.Difficulty = 2
|
||
|
cplc.channel = channel
|
||
|
messageBuilder := new(utils.MessageBuilder)
|
||
|
return messageBuilder.OpenChannel(channel.ID, cplc.Type()), nil
|
||
|
}
|
||
|
|
||
|
// OpenOutboundResult is called when a response is received for an
|
||
|
// outbound OpenChannel request. If `err` is non-nil, the channel was
|
||
|
// rejected and Closed will be called immediately afterwards. `raw`
|
||
|
// contains the raw protocol message including any extension data.
|
||
|
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)
|
||
|
cplc.challenge = ce.([]byte)[:]
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
// SendGroupMessage
|
||
|
func (cplc *CwtchPeerSendChannel) SendGroupMessage(gm *protocol.GroupMessage) {
|
||
|
sgsolve := cplc.spamGuard.SolveChallenge(cplc.challenge, gm.GetCiphertext())
|
||
|
gm.Spamguard = sgsolve
|
||
|
csp := &protocol.CwtchServerPacket{
|
||
|
GroupMessage: gm,
|
||
|
}
|
||
|
packet, _ := proto.Marshal(csp)
|
||
|
cplc.channel.SendMessage(packet)
|
||
|
}
|
||
|
|
||
|
// Packet is called for each raw packet received on this channel.
|
||
|
func (cc *CwtchPeerSendChannel) Packet(data []byte) {
|
||
|
// If we receive a packet on this channel, close the connection
|
||
|
cc.channel.CloseChannel()
|
||
|
}
|