forked from cwtch.im/cwtch
fmt and spamguard
This commit is contained in:
parent
086e964514
commit
487ce6d2da
|
@ -69,11 +69,9 @@ func TestPeerFetchChannel(t *testing.T) {
|
||||||
t.Errorf("once opened channel should no longer be pending")
|
t.Errorf("once opened channel should no longer be pending")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
csp := &protocol.CwtchServerPacket{
|
csp := &protocol.CwtchServerPacket{
|
||||||
GroupMessages: []*protocol.GroupMessage{
|
GroupMessages: []*protocol.GroupMessage{
|
||||||
{Ciphertext: []byte("hello"),Spamguard: []byte{},},
|
{Ciphertext: []byte("hello"), Spamguard: []byte{}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
packet, _ := proto.Marshal(csp)
|
packet, _ := proto.Marshal(csp)
|
||||||
|
|
|
@ -69,10 +69,8 @@ func TestPeerListenChannel(t *testing.T) {
|
||||||
t.Errorf("once opened channel should no longer be pending")
|
t.Errorf("once opened channel should no longer be pending")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
csp := &protocol.CwtchServerPacket{
|
csp := &protocol.CwtchServerPacket{
|
||||||
GroupMessage: &protocol.GroupMessage{Ciphertext: []byte("hello"),Spamguard: []byte{},},
|
GroupMessage: &protocol.GroupMessage{Ciphertext: []byte("hello"), Spamguard: []byte{}},
|
||||||
}
|
}
|
||||||
packet, _ := proto.Marshal(csp)
|
packet, _ := proto.Marshal(csp)
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,6 @@ func (cplc *CwtchPeerSendChannel) OpenOutboundResult(err error, crm *Protocol_Da
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// SendGroupMessage performs the spamguard proof of work and sends a message.
|
// SendGroupMessage performs the spamguard proof of work and sends a message.
|
||||||
func (cplc *CwtchPeerSendChannel) SendGroupMessage(gm *protocol.GroupMessage) {
|
func (cplc *CwtchPeerSendChannel) SendGroupMessage(gm *protocol.GroupMessage) {
|
||||||
sgsolve := cplc.spamGuard.SolveChallenge(cplc.challenge, gm.GetCiphertext())
|
sgsolve := cplc.spamGuard.SolveChallenge(cplc.challenge, gm.GetCiphertext())
|
||||||
|
@ -83,7 +82,7 @@ func (cplc *CwtchPeerSendChannel) SendGroupMessage(gm *protocol.GroupMessage) {
|
||||||
cplc.channel.SendMessage(packet)
|
cplc.channel.SendMessage(packet)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Packet should never be called
|
// Packet should never be
|
||||||
func (cc *CwtchPeerSendChannel) Packet(data []byte) {
|
func (cc *CwtchPeerSendChannel) Packet(data []byte) {
|
||||||
// If we receive a packet on this channel, close the connection
|
// If we receive a packet on this channel, close the connection
|
||||||
cc.channel.CloseChannel()
|
cc.channel.CloseChannel()
|
||||||
|
|
|
@ -2,10 +2,10 @@ package listen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.mascherari.press/cwtch/protocol"
|
"git.mascherari.press/cwtch/protocol"
|
||||||
|
"git.mascherari.press/cwtch/protocol/spam"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/s-rah/go-ricochet/channels"
|
"github.com/s-rah/go-ricochet/channels"
|
||||||
"github.com/s-rah/go-ricochet/wire/control"
|
"github.com/s-rah/go-ricochet/wire/control"
|
||||||
"git.mascherari.press/cwtch/protocol/spam"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -50,12 +50,11 @@ func TestPeerSendChannelOpenInbound(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestPeerSendChannelClosesOnPacket(t *testing.T) {
|
func TestPeerSendChannelClosesOnPacket(t *testing.T) {
|
||||||
pfc := new(CwtchPeerSendChannel)
|
pfc := new(CwtchPeerSendChannel)
|
||||||
channel := new(channels.Channel)
|
channel := new(channels.Channel)
|
||||||
closed := false
|
closed := false
|
||||||
channel.CloseChannel = func () {
|
channel.CloseChannel = func() {
|
||||||
closed = true
|
closed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,8 +87,6 @@ func TestPeerSendChannel(t *testing.T) {
|
||||||
t.Errorf("expected result but also got non-nil error: result:%v, err: %v", result, err)
|
t.Errorf("expected result but also got non-nil error: result:%v, err: %v", result, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
challenge := sg.GenerateChallenge(3)
|
challenge := sg.GenerateChallenge(3)
|
||||||
control := new(Protocol_Data_Control.Packet)
|
control := new(Protocol_Data_Control.Packet)
|
||||||
proto.Unmarshal(challenge[:], control)
|
proto.Unmarshal(challenge[:], control)
|
||||||
|
@ -99,8 +96,7 @@ func TestPeerSendChannel(t *testing.T) {
|
||||||
t.Errorf("once opened channel should no longer be pending")
|
t.Errorf("once opened channel should no longer be pending")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gm := &protocol.GroupMessage{Ciphertext: []byte("hello")}
|
||||||
gm := &protocol.GroupMessage{Ciphertext: []byte("hello"),}
|
|
||||||
pfc.SendGroupMessage(gm)
|
pfc.SendGroupMessage(gm)
|
||||||
if !success {
|
if !success {
|
||||||
t.Errorf("send channel should have successfully sent a valid group message")
|
t.Errorf("send channel should have successfully sent a valid group message")
|
||||||
|
|
|
@ -63,7 +63,6 @@ func (m *GroupMessage) Reset() { *m = GroupMessage{} }
|
||||||
func (m *GroupMessage) String() string { return proto.CompactTextString(m) }
|
func (m *GroupMessage) String() string { return proto.CompactTextString(m) }
|
||||||
func (*GroupMessage) ProtoMessage() {}
|
func (*GroupMessage) ProtoMessage() {}
|
||||||
|
|
||||||
|
|
||||||
func (m *GroupMessage) GetCiphertext() []byte {
|
func (m *GroupMessage) GetCiphertext() []byte {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
return m.Ciphertext
|
return m.Ciphertext
|
||||||
|
|
|
@ -15,6 +15,8 @@ type SpamGuard struct {
|
||||||
nonce [24]byte
|
nonce [24]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//GenerateChallenge returns a channel result packet with a spamguard challenge nonce
|
||||||
func (sg *SpamGuard) GenerateChallenge(channelID int32) []byte {
|
func (sg *SpamGuard) GenerateChallenge(channelID int32) []byte {
|
||||||
|
|
||||||
cr := &Protocol_Data_Control.ChannelResult{
|
cr := &Protocol_Data_Control.ChannelResult{
|
||||||
|
@ -38,6 +40,9 @@ func (sg *SpamGuard) GenerateChallenge(channelID int32) []byte {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SolveChallenge takes in a challenge and a message and returns a solution
|
||||||
|
// The solution is a 24 byte nonce which when hashed with the challenge and the message
|
||||||
|
// produces a sha256 hash with Difficulty leading 0s
|
||||||
func (sg *SpamGuard) SolveChallenge(challenge []byte, message []byte) []byte {
|
func (sg *SpamGuard) SolveChallenge(challenge []byte, message []byte) []byte {
|
||||||
solved := false
|
solved := false
|
||||||
var spamguard [24]byte
|
var spamguard [24]byte
|
||||||
|
@ -67,8 +72,11 @@ func (sg *SpamGuard) SolveChallenge(challenge []byte, message []byte) []byte {
|
||||||
return spamguard[:]
|
return spamguard[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValidateChallenge returns true if the message and spamguard pass the challenge
|
||||||
func (sg *SpamGuard) ValidateChallenge(message []byte, spamguard []byte) bool {
|
func (sg *SpamGuard) ValidateChallenge(message []byte, spamguard []byte) bool {
|
||||||
//log.Printf("%v %v\n", sg.nonce[:], spamguard[:])
|
if len(spamguard) != 24 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
solve := make([]byte, len(sg.nonce)+len(message)+len(spamguard))
|
solve := make([]byte, len(sg.nonce)+len(message)+len(spamguard))
|
||||||
copy(solve[0:], sg.nonce[:])
|
copy(solve[0:], sg.nonce[:])
|
||||||
copy(solve[len(sg.nonce):], message[:])
|
copy(solve[len(sg.nonce):], message[:])
|
||||||
|
|
Loading…
Reference in New Issue