Browse Source

Refactoring, fixing up some issues

pull/8/head
Sarah Jamie Lewis 4 years ago
parent
commit
b43aa55b44
  1. 1
      .gitignore
  2. 4
      model/group_test.go
  3. 2
      model/profile_test
  4. 51
      peer/connections/peerpeerconnection.go
  5. 8
      peer/connections/peerpeerconnection_test.go
  6. 2
      peer/connections/peerserverconnection_test.go
  7. 22
      peer/cwtch_peer.go
  8. 14
      peer/listen/peer_listen_channel.go
  9. 10
      peer/peer/peer_channel_test.go
  10. 20
      peer/send/peer_send_channel.go
  11. 2
      peer/send/peer_send_channel_test.go
  12. 1
      peer/test_profile
  13. 8
      protocol/spam/spamguard.go
  14. 4
      protocol/spam/spamguard_test.go
  15. 2
      server/send/server_send_channel.go
  16. 4
      server/send/server_send_channel_test.go
  17. 6
      server/server.go
  18. 8
      server/server_instance.go
  19. 2
      server/server_instance_test.go

1
.gitignore

@ -1 +1,2 @@
*.out
.idea

4
model/group_test.go

@ -6,8 +6,8 @@ import (
func TestGroup(t *testing.T) {
g := NewGroup("server.onion")
enc_message := g.EncryptMessage("Hello World")
ok, message := g.DecryptMessage(enc_message)
encMessage := g.EncryptMessage("Hello World")
ok, message := g.DecryptMessage(encMessage)
if !ok || message != "Hello World" {
t.Errorf("group encryption was invalid, or returned wrong message decrypted:%v message:%v", ok, message)
return

2
model/profile_test

@ -1 +1 @@
{"Name":"Sarah","Ed25519PublicKey":"msKO36i2l86zvEL175TKZEC0nD73g2v7nV5xmK0Vphk=","Contacts":{},"Ed25519PrivateKey":"gY02OGDQrUZmtgrGE8Vc75m+YpveuiBnq7S04R6hDheawo7fqLaXzrO8QvXvlMpkQLScPveDa/udXnGYrRWmGQ==","OnionPrivateKey":{"N":149473818603978520797265583326345889632722230289009522405338669742795723417758103856403611357040387558433860451515052572608570172648790617938713499325164649087791083575310232535396589977906388592625399123963785709861980287466698616117387601534139103746310242068634673611193675737864910207364356589966194353473,"E":65537,"D":86372179235129264119389774334631106709052761967206167714270952639877840698086567786777007829029700426291870169505394524080848260344686218492440609418252045928103604805029397268780872419743985198533279610220673486415102569608190490192041308974054759325240302029165831212246096675348836655257812290267217972353,"Primes":[12798859654138079143364344404787850219664210052105976906571980247193551352543964631210504198681240048855374604305718740175510657834512957821516486884913417,11678682526661756792661834016830595869008788319138001870469300096518673413667724031509428867627103613547399458382675739150139741094498852501694360147458169],"Precomputed":{"Dp":8019280314598958509909975651827265416025626094719436802334028486053813863764176261522451957069254926623865104850782437826372799222707868639469788073228857,"Dq":8119140840742831728447418732545517936185336731870022143557414147696473718258828483507523964582237435959024888579450875492602146630873502261046407938088241,"Qinv":1212466450879442551713910248017765796626002260907864986369164997419826127998002105201194295184151193998884696413793530693071388392214893165411485014542824,"CRTValues":[]}},"Groups":{}}
{"Name":"Sarah","Ed25519PublicKey":"/B8dEVHus01Da+YnNGXTkcO4kx0IOIiUXfUb8jCKtCA=","Contacts":{},"Ed25519PrivateKey":"4BKKduiLawCLymPakXQ8KR7rk3+mcKkXBjdvX6MUpiH8Hx0RUe6zTUNr5ic0ZdORw7iTHQg4iJRd9RvyMIq0IA==","OnionPrivateKey":{"N":122894509251387918537344449363582844899371582871503138947591748059687692350624757973436500610485234906492758491648918955960649482658707961407465134777548850040832459753903991632513446336436075872130848944818392546778020468818008658094247574580293640095581476556568537356529453655090205726813812773136632293627,"E":65537,"D":14834650086023617552663868332625904237284718435333648659755532277952749350531645640292600459733413084902638393997811890391758824134657349019553178833715134614120468834956286694945442257374600339998005616482726880381216386928218674308996477590581500573205360929206003857898285894073647530982811784307255428401,"Primes":[11399281077950092701265950373080119600146585644302247801593315654188006980066408020813759117244884225514463927040243360808856555139261087338484716581806083,10780899989307726072173333462686027108707159501939065538297724308089670254067834791777651673196745149447212427380713841130876160570417436683376001317203369],"Precomputed":{"Dp":6660379190943512057568637131482734023968339373050668672942173780157272430546147915421829868891482777109111687965958142302710456831905420699227257386674985,"Dq":717059722803795992318897120158817037198140108167178642315635141354698454880169856071513551786999894814233165554610855447907123974647139882857561221015449,"Qinv":3661289130630961871097580877408594138463407662738278774315778153391032147854364535225179585092033532454907736889755632942438448844816650105956852053425750,"CRTValues":[]}},"Groups":{}}

51
peer/connections/peerpeerconnection.go

@ -8,8 +8,8 @@ import (
"github.com/s-rah/go-ricochet/connection"
"github.com/s-rah/go-ricochet/identity"
//"github.com/s-rah/go-ricochet/utils"
"time"
"git.mascherari.press/cwtch/model"
"time"
)
type PeerPeerConnection struct {
@ -17,7 +17,7 @@ type PeerPeerConnection struct {
PeerHostname string
state ConnectionState
connection connection.Connection
profile *model.Profile
profile *model.Profile
}
func NewPeerPeerConnection(peerhostname string, profile *model.Profile) *PeerPeerConnection {
@ -34,25 +34,24 @@ func (ppc *PeerPeerConnection) GetState() ConnectionState {
}
func (ppc *PeerPeerConnection) ClientIdentity(ci *protocol.CwtchIdentity) {
ppc.profile.AddCwtchIdentity(ppc.PeerHostname, ci)
ppc.profile.AddCwtchIdentity(ppc.PeerHostname, ci)
}
func (ppc *PeerPeerConnection) HandleGroupInvite(gci *protocol.GroupChatInvite) {
ppc.profile.ProcessInvite(gci)
ppc.profile.ProcessInvite(gci)
}
func (ppc *PeerPeerConnection) SendGroupInvite(invite []byte) {
ppc.connection.Do(func() error {
channel := ppc.connection.Channel("im.cwtch.peer", channels.Outbound)
if channel != nil {
peerchannel, ok := channel.Handler.(*peer.CwtchPeerChannel)
if ok {
peerchannel.SendMessage(invite)
}
}
return nil
})
ppc.connection.Do(func() error {
channel := ppc.connection.Channel("im.cwtch.peer", channels.Outbound)
if channel != nil {
peerchannel, ok := channel.Handler.(*peer.CwtchPeerChannel)
if ok {
peerchannel.SendMessage(invite)
}
}
return nil
})
}
// Run manages the setup and teardown of a peer->peer connection
@ -71,17 +70,17 @@ func (ppc *PeerPeerConnection) Run() error {
return nil
})
time.Sleep(time.Second * 1)
ppc.connection.Do(func() error {
channel := ppc.connection.Channel("im.cwtch.peer", channels.Outbound)
if channel != nil {
peerchannel, ok := channel.Handler.(*peer.CwtchPeerChannel)
if ok {
peerchannel.SendMessage(ppc.profile.GetCwtchIdentityPacket())
}
}
return nil
})
time.Sleep(time.Second * 1)
ppc.connection.Do(func() error {
channel := ppc.connection.Channel("im.cwtch.peer", channels.Outbound)
if channel != nil {
peerchannel, ok := channel.Handler.(*peer.CwtchPeerChannel)
if ok {
peerchannel.SendMessage(ppc.profile.GetCwtchIdentityPacket())
}
}
return nil
})
}()

8
peer/connections/peerpeerconnection_test.go

@ -15,7 +15,7 @@ import (
"time"
)
func PeerAuthValid(hostname string, publicKey rsa.PublicKey) (allowed, known bool) {
func PeerAuthValid(string, rsa.PublicKey) (allowed, known bool) {
return true, true
}
@ -37,9 +37,9 @@ func runtestpeer(t *testing.T, tp *TestPeer) {
t.Errorf("ServerAuth Error: %v", err)
}
tp.RegisterChannelHandler("im.cwtch.peer", func() channels.Handler {
peer := new(peer.CwtchPeerChannel)
peer.Handler = tp
return peer
cpc := new(peer.CwtchPeerChannel)
cpc.Handler = tp
return cpc
})
go func() {

2
peer/connections/peerserverconnection_test.go

@ -15,7 +15,7 @@ import (
"time"
)
func ServerAuthValid(hostname string, publicKey rsa.PublicKey) (allowed, known bool) {
func ServerAuthValid(string, rsa.PublicKey) (allowed, known bool) {
return true, true
}

22
peer/cwtch_peer.go

@ -45,14 +45,14 @@ type CwtchPeer struct {
}
func NewCwtchPeer(name string) *CwtchPeer {
peer := new(CwtchPeer)
peer.Profile = model.GenerateNewProfile(name)
peer.PendingInvites = make(map[string][]string)
peer.Log = make(chan string)
peer.peerconnections = make(map[string]*connections.PeerPeerConnection)
peer.serverconnections = make(map[string]*connections.PeerServerConnection)
peer.Init()
return peer
cp := new(CwtchPeer)
cp.Profile = model.GenerateNewProfile(name)
cp.PendingInvites = make(map[string][]string)
cp.Log = make(chan string)
cp.peerconnections = make(map[string]*connections.PeerPeerConnection)
cp.serverconnections = make(map[string]*connections.PeerServerConnection)
cp.Init()
return cp
}
func (cp *CwtchPeer) Save(profilefile string) error {
@ -65,9 +65,9 @@ func (cp *CwtchPeer) Save(profilefile string) error {
func LoadCwtchPeer(profilefile string) (*CwtchPeer, error) {
bytes, _ := ioutil.ReadFile(profilefile)
peer := new(CwtchPeer)
err := json.Unmarshal(bytes, &peer)
return peer, err
cp := new(CwtchPeer)
err := json.Unmarshal(bytes, &cp)
return cp, err
}
// AddContactRequest is the entry point for CwtchPeer relationships

14
peer/listen/peer_listen_channel.go

@ -21,37 +21,37 @@ type CwtchPeerSendChannelHandler interface {
}
// Type returns the type string for this channel, e.g. "im.ricochet.server.listen".
func (cc *CwtchPeerListenChannel) Type() string {
func (cplc *CwtchPeerListenChannel) Type() string {
return "im.cwtch.server.listen"
}
// Closed is called when the channel is closed for any reason.
func (cc *CwtchPeerListenChannel) Closed(err error) {
func (cplc *CwtchPeerListenChannel) Closed(err error) {
}
// OnlyClientCanOpen - for Cwtch server channels can only be opened by peers
func (cc *CwtchPeerListenChannel) OnlyClientCanOpen() bool {
func (cplc *CwtchPeerListenChannel) OnlyClientCanOpen() bool {
return true
}
// Singleton - for Cwtch channels there can only be one instance per direction
func (cc *CwtchPeerListenChannel) Singleton() bool {
func (cplc *CwtchPeerListenChannel) Singleton() bool {
return true
}
// Bidirectional - for Cwtch channels are not bidrectional
func (cc *CwtchPeerListenChannel) Bidirectional() bool {
func (cplc *CwtchPeerListenChannel) Bidirectional() bool {
return false
}
// RequiresAuthentication - Cwtch channels require no auth channels
func (cc *CwtchPeerListenChannel) RequiresAuthentication() string {
func (cplc *CwtchPeerListenChannel) RequiresAuthentication() string {
return "none"
}
// OpenInbound - peers should never respond to open inbound requests from servers
func (cc *CwtchPeerListenChannel) OpenInbound(channel *channels.Channel, raw *Protocol_Data_Control.OpenChannel) ([]byte, error) {
func (cplc *CwtchPeerListenChannel) OpenInbound(channel *channels.Channel, raw *Protocol_Data_Control.OpenChannel) ([]byte, error) {
return nil, errors.New("client does not receive inbound listen channels")
}

10
peer/peer/peer_channel_test.go

@ -32,7 +32,7 @@ func TestPeerChannelAttributes(t *testing.T) {
}
type TestHandler struct {
Received bool
Received bool
ReceviedGroupInvite bool
}
@ -44,11 +44,10 @@ func (th *TestHandler) ClientIdentity(ci *protocol.CwtchIdentity) {
func (th *TestHandler) HandleGroupInvite(ci *protocol.GroupChatInvite) {
///if ci.GetName() == "hello" {
th.ReceviedGroupInvite = true
th.ReceviedGroupInvite = true
//}
}
func TestPeerChannel(t *testing.T) {
th := new(TestHandler)
cpc := new(CwtchPeerChannel)
@ -101,11 +100,10 @@ func TestPeerChannel(t *testing.T) {
t.Errorf("Should have sent packet to channel")
}
gci := &protocol.GroupChatInvite{
GroupName: "hello",
GroupName: "hello",
GroupSharedKey: []byte{},
ServerHost: "abc.onion",
ServerHost: "abc.onion",
}
cpp = &protocol.CwtchPeerPacket{

20
peer/send/peer_send_channel.go

@ -13,42 +13,42 @@ import (
// CwtchPeerSendChannel is the peer implementation of im.cwtch.server.send
type CwtchPeerSendChannel struct {
channel *channels.Channel
spamGuard spam.SpamGuard
spamGuard spam.Guard
challenge []byte
}
// Type returns the type string for this channel, e.g. "im.ricochet.server.send".
func (cc *CwtchPeerSendChannel) Type() string {
func (cplc *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) {
func (cplc *CwtchPeerSendChannel) Closed(err error) {
}
// OnlyClientCanOpen - for Cwtch server channels only peers may open.
func (cc *CwtchPeerSendChannel) OnlyClientCanOpen() bool {
func (cplc *CwtchPeerSendChannel) OnlyClientCanOpen() bool {
return true
}
// Singleton - for Cwtch channels there can only be one instance per direction
func (cc *CwtchPeerSendChannel) Singleton() bool {
func (cplc *CwtchPeerSendChannel) Singleton() bool {
return true
}
// Bidirectional - for Cwtch channels are not bidrectional
func (cc *CwtchPeerSendChannel) Bidirectional() bool {
func (cplc *CwtchPeerSendChannel) Bidirectional() bool {
return false
}
// RequiresAuthentication - Cwtch channels require no auth
func (cc *CwtchPeerSendChannel) RequiresAuthentication() string {
func (cplc *CwtchPeerSendChannel) RequiresAuthentication() string {
return "none"
}
// OpenInbound should never be called on peers.
func (cc *CwtchPeerSendChannel) OpenInbound(channel *channels.Channel, raw *Protocol_Data_Control.OpenChannel) ([]byte, error) {
func (cplc *CwtchPeerSendChannel) OpenInbound(channel *channels.Channel, raw *Protocol_Data_Control.OpenChannel) ([]byte, error) {
return nil, errors.New("client does not receive inbound listen channels")
}
@ -84,7 +84,7 @@ func (cplc *CwtchPeerSendChannel) SendGroupMessage(gm *protocol.GroupMessage) {
}
// Packet should never be
func (cc *CwtchPeerSendChannel) Packet(data []byte) {
func (cplc *CwtchPeerSendChannel) Packet(data []byte) {
// If we receive a packet on this channel, close the connection
cc.channel.CloseChannel()
cplc.channel.CloseChannel()
}

2
peer/send/peer_send_channel_test.go

@ -64,7 +64,7 @@ func TestPeerSendChannel(t *testing.T) {
channel.ID = 3
success := false
var sg spam.SpamGuard
var sg spam.Guard
sg.Difficulty = 2
channel.SendMessage = func(message []byte) {

1
peer/test_profile

@ -0,0 +1 @@
{"Profile":{"Name":"alice","Ed25519PublicKey":"jd/KsmjnaiNRQfwkUU2KOv78epqHQtc/NuQ7vHhL1pU=","Contacts":{},"Ed25519PrivateKey":"hJqPzncQMthT/C6MJe5wwqijF8LZlItwuVqRRPWF2uSN38qyaOdqI1FB/CRRTYo6/vx6modC1z825Du8eEvWlQ==","OnionPrivateKey":{"N":116625881909264071736606689586851027031866677768702967565587135989634906670425929131611970003568513685918847077357562012284752737018357715084617885889134319857398716428989490466264550714347160579478956149389453246821375180647178126226570098474391873393638846273276470804476875874646605047892383342437534890107,"E":65537,"D":74117948357734540608048409620402906386884464181553604820280211391554295479244395506837947276326786319461067500372956617050825510731565357481641438382630323642658160945533297126458940294613864770177502808097743782578588323228321725977812335445321391168916952291786105711946242663437273644485564881061465737473,"Primes":[10118822147491588191804307827889610500703007978301844285618895021194403722583146885313445367368523265387812462145972329196076399224393167672743566765418817,11525638084090164427262755292570520360826215923789098544956165178885657272566959447401401652883909093499220795453042380071024329286949114855482181086877371],"Precomputed":{"Dp":7235119182011013971770905973952227719653675845144337141219485492060511748176545509342631641895250014740877549722450880359615790586310997408254322575453953,"Dq":2255287590069308461101630740984853641564847231436767013145518749012461187777876435501710099586237548484581343071697140272377679765259860062357195023545713,"Qinv":6483591420973981267201623607283357903939001496206473754008877212497299522594502989717337115365261073580285165813624061295197296922762434701906760350701367,"CRTValues":[]}},"Groups":{}},"PendingContacts":null,"PendingInvites":{}}

8
protocol/spam/spamguard.go

@ -10,13 +10,13 @@ import (
"io"
)
type SpamGuard struct {
type Guard struct {
Difficulty int
nonce [24]byte
}
//GenerateChallenge returns a channel result packet with a spamguard challenge nonce
func (sg *SpamGuard) GenerateChallenge(channelID int32) []byte {
func (sg *Guard) GenerateChallenge(channelID int32) []byte {
cr := &Protocol_Data_Control.ChannelResult{
ChannelIdentifier: proto.Int32(channelID),
@ -42,7 +42,7 @@ func (sg *SpamGuard) GenerateChallenge(channelID int32) []byte {
// 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 *Guard) SolveChallenge(challenge []byte, message []byte) []byte {
solved := false
var spamguard [24]byte
sum := sha256.Sum256([]byte{})
@ -72,7 +72,7 @@ func (sg *SpamGuard) SolveChallenge(challenge []byte, message []byte) []byte {
}
// ValidateChallenge returns true if the message and spamguard pass the challenge
func (sg *SpamGuard) ValidateChallenge(message []byte, spamguard []byte) bool {
func (sg *Guard) ValidateChallenge(message []byte, spamguard []byte) bool {
if len(spamguard) != 24 {
return false
}

4
protocol/spam/spamguard_test.go

@ -8,7 +8,7 @@ import (
)
func TestSpamGuard(t *testing.T) {
var spamGuard SpamGuard
var spamGuard Guard
spamGuard.Difficulty = 2
challenge := spamGuard.GenerateChallenge(3)
@ -23,7 +23,7 @@ func TestSpamGuard(t *testing.T) {
t.Logf("Solved: %v %v", challenge, sgsolve)
result := spamGuard.ValidateChallenge([]byte("Hello"), sgsolve)
if result != true {
t.Errorf("Validating SpamGuard Failed")
t.Errorf("Validating Guard Failed")
}
return
}

2
server/send/server_send_channel.go

@ -19,7 +19,7 @@ type CwtchServerSendChannel struct {
// Methods of Handler are called for Cwtch events on this channel
Handler CwtchServerSendChannelHandler
channel *channels.Channel
spamguard spam.SpamGuard
spamguard spam.Guard
}
// CwtchChannelHandler is implemented by an application type to receive

4
server/send/server_send_channel_test.go

@ -75,7 +75,7 @@ func TestServerSendChannel(t *testing.T) {
if control.GetChannelResult() != nil {
var spamguard spam.SpamGuard
var spamguard spam.Guard
spamguard.Difficulty = 2
ce, _ := proto.GetExtension(control.GetChannelResult(), protocol.E_ServerNonce)
@ -136,7 +136,7 @@ func TestServerSendChannelNoSpamGuard(t *testing.T) {
if control.GetChannelResult() != nil {
var spamguard spam.SpamGuard
var spamguard spam.Guard
spamguard.Difficulty = 2
ce, _ := proto.GetExtension(control.GetChannelResult(), protocol.E_ServerNonce)

6
server/server.go

@ -35,7 +35,7 @@ func (s *Server) Run(privateKeyFile string) {
ms := new(storage.MessageStore)
ms.Init("cwtch.messages")
af.AddHandler("im.cwtch.server.listen", func(rai *application.ApplicationInstance) func() channels.Handler {
si := new(ServerInstance)
si := new(Instance)
si.Init(rai, cwtchserver, ms)
return func() channels.Handler {
cslc := new(listen.CwtchServerListenChannel)
@ -44,7 +44,7 @@ func (s *Server) Run(privateKeyFile string) {
})
af.AddHandler("im.cwtch.server.fetch", func(rai *application.ApplicationInstance) func() channels.Handler {
si := new(ServerInstance)
si := new(Instance)
si.Init(rai, cwtchserver, ms)
return func() channels.Handler {
cssc := new(fetch.CwtchServerFetchChannel)
@ -54,7 +54,7 @@ func (s *Server) Run(privateKeyFile string) {
})
af.AddHandler("im.cwtch.server.send", func(rai *application.ApplicationInstance) func() channels.Handler {
si := new(ServerInstance)
si := new(Instance)
si.Init(rai, cwtchserver, ms)
return func() channels.Handler {
cssc := new(send.CwtchServerSendChannel)

8
server/server_instance.go

@ -8,24 +8,24 @@ import (
"github.com/s-rah/go-ricochet/channels"
)
type ServerInstance struct {
type Instance struct {
rai *application.ApplicationInstance
ra *application.RicochetApplication
msi storage.MessageStoreInterface
}
func (si *ServerInstance) Init(rai *application.ApplicationInstance, ra *application.RicochetApplication, msi storage.MessageStoreInterface) {
func (si *Instance) Init(rai *application.ApplicationInstance, ra *application.RicochetApplication, msi storage.MessageStoreInterface) {
si.rai = rai
si.ra = ra
si.msi = msi
}
func (si *ServerInstance) HandleFetchRequest() []*protocol.GroupMessage {
func (si *Instance) HandleFetchRequest() []*protocol.GroupMessage {
return si.msi.FetchMessages()
}
// HandleGroupMessage
func (si *ServerInstance) HandleGroupMessage(gm *protocol.GroupMessage) {
func (si *Instance) HandleGroupMessage(gm *protocol.GroupMessage) {
si.msi.AddMessage(*gm)
go si.ra.Broadcast(func(rai *application.ApplicationInstance) {
rai.Connection.Do(func() error {

2
server/server_instance_test.go

@ -9,7 +9,7 @@ import (
)
func TestServerInstance(t *testing.T) {
si := new(ServerInstance)
si := new(Instance)
ai := new(application.ApplicationInstance)
ra := new(application.RicochetApplication)
msi := new(storage.MessageStore)

Loading…
Cancel
Save