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")
|
||||
}
|
||||
|
||||
|
||||
|
||||
csp := &protocol.CwtchServerPacket{
|
||||
GroupMessages: []*protocol.GroupMessage{
|
||||
{Ciphertext: []byte("hello"),Spamguard: []byte{},},
|
||||
{Ciphertext: []byte("hello"), Spamguard: []byte{}},
|
||||
},
|
||||
}
|
||||
packet, _ := proto.Marshal(csp)
|
||||
|
|
|
@ -69,10 +69,8 @@ func TestPeerListenChannel(t *testing.T) {
|
|||
t.Errorf("once opened channel should no longer be pending")
|
||||
}
|
||||
|
||||
|
||||
|
||||
csp := &protocol.CwtchServerPacket{
|
||||
GroupMessage: &protocol.GroupMessage{Ciphertext: []byte("hello"),Spamguard: []byte{},},
|
||||
GroupMessage: &protocol.GroupMessage{Ciphertext: []byte("hello"), Spamguard: []byte{}},
|
||||
}
|
||||
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.
|
||||
func (cplc *CwtchPeerSendChannel) SendGroupMessage(gm *protocol.GroupMessage) {
|
||||
sgsolve := cplc.spamGuard.SolveChallenge(cplc.challenge, gm.GetCiphertext())
|
||||
|
@ -83,7 +82,7 @@ func (cplc *CwtchPeerSendChannel) SendGroupMessage(gm *protocol.GroupMessage) {
|
|||
cplc.channel.SendMessage(packet)
|
||||
}
|
||||
|
||||
// Packet should never be called
|
||||
// Packet should never be
|
||||
func (cc *CwtchPeerSendChannel) Packet(data []byte) {
|
||||
// If we receive a packet on this channel, close the connection
|
||||
cc.channel.CloseChannel()
|
||||
|
|
|
@ -2,10 +2,10 @@ package listen
|
|||
|
||||
import (
|
||||
"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/wire/control"
|
||||
"git.mascherari.press/cwtch/protocol/spam"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -50,7 +50,6 @@ func TestPeerSendChannelOpenInbound(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func TestPeerSendChannelClosesOnPacket(t *testing.T) {
|
||||
pfc := new(CwtchPeerSendChannel)
|
||||
channel := new(channels.Channel)
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
challenge := sg.GenerateChallenge(3)
|
||||
control := new(Protocol_Data_Control.Packet)
|
||||
proto.Unmarshal(challenge[:], control)
|
||||
|
@ -99,8 +96,7 @@ func TestPeerSendChannel(t *testing.T) {
|
|||
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)
|
||||
if !success {
|
||||
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 (*GroupMessage) ProtoMessage() {}
|
||||
|
||||
|
||||
func (m *GroupMessage) GetCiphertext() []byte {
|
||||
if m != nil {
|
||||
return m.Ciphertext
|
||||
|
|
|
@ -15,6 +15,8 @@ type SpamGuard struct {
|
|||
nonce [24]byte
|
||||
}
|
||||
|
||||
|
||||
//GenerateChallenge returns a channel result packet with a spamguard challenge nonce
|
||||
func (sg *SpamGuard) GenerateChallenge(channelID int32) []byte {
|
||||
|
||||
cr := &Protocol_Data_Control.ChannelResult{
|
||||
|
@ -38,6 +40,9 @@ func (sg *SpamGuard) GenerateChallenge(channelID int32) []byte {
|
|||
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 {
|
||||
solved := false
|
||||
var spamguard [24]byte
|
||||
|
@ -67,8 +72,11 @@ func (sg *SpamGuard) SolveChallenge(challenge []byte, message []byte) []byte {
|
|||
return spamguard[:]
|
||||
}
|
||||
|
||||
// ValidateChallenge returns true if the message and spamguard pass the challenge
|
||||
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))
|
||||
copy(solve[0:], sg.nonce[:])
|
||||
copy(solve[len(sg.nonce):], message[:])
|
||||
|
|
Loading…
Reference in New Issue