From 440ffb64ef1fcf1ecceaaa65b9128035baa0c8e0 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 12 Mar 2018 11:43:51 -0700 Subject: [PATCH] Peer Group Invite --- model/profile.go | 4 ++-- model/profile_test | 2 +- model/profile_test.go | 2 +- peer/connections/peerpeerconnection.go | 18 ++++++++++++++++- peer/connections/peerpeerconnection_test.go | 6 +++--- peer/cwtch_peer.go | 4 ++-- peer/cwtch_peer_test.go | 6 +++--- peer/peer/peer_channel.go | 4 +++- peer/peer/peer_channel_test.go | 22 ++++++++++++++++++++- peer/test_profile | 1 - 10 files changed, 53 insertions(+), 16 deletions(-) delete mode 100644 peer/test_profile diff --git a/model/profile.go b/model/profile.go index e3d9a0b..4b3e9c9 100644 --- a/model/profile.go +++ b/model/profile.go @@ -65,7 +65,7 @@ func (p *Profile) GetCwtchIdentity() (message []byte) { // AddCwtchIdentity takes a wire message and if it is a CwtchIdentity message adds the identity as a contact // otherwise returns an error -func (p *Profile) AddCwtchIdentity(onion string, ci protocol.CwtchIdentity) { +func (p *Profile) AddCwtchIdentity(onion string, ci *protocol.CwtchIdentity) { p.AddContact(onion, PublicProfile{Name: ci.GetName(), Ed25519PublicKey: ci.GetEd25519PublicKey()}) } @@ -99,7 +99,7 @@ func (p *Profile) StartGroup(server string) (groupID string, invite []byte) { } // ProcessInvite -func (p *Profile) ProcessInvite(gci protocol.GroupChatInvite) { +func (p *Profile) ProcessInvite(gci *protocol.GroupChatInvite) { group := new(Group) group.GroupID = gci.GetGroupName() copy(group.GroupKey[:], gci.GetGroupSharedKey()[:]) diff --git a/model/profile_test b/model/profile_test index facd14b..5012e2c 100644 --- a/model/profile_test +++ b/model/profile_test @@ -1 +1 @@ -{"Name":"Sarah","Ed25519PublicKey":"M2dFnsHcdR7Lha8FpZ1FmgGqAAgs6w2NNBQgfVywj94=","Contacts":{},"Ed25519PrivateKey":"ZtbrDR982Ff9bLOI6kxbK0XiyNnRFyF1o3OlsKBZz6gzZ0Wewdx1HsuFrwWlnUWaAaoACCzrDY00FCB9XLCP3g==","OnionPrivateKey":{"N":142724584074611204803266575696785422729907718147588329252653937092083270046866173891429018691086667376925028577112559384958192132540674032393033261326073927443209907420866237116331281544745323643361522344826234584754455383914989807467870567115328170590992672168466646833750432351388078416618817943207651099679,"E":65537,"D":27413782509593143507690612247602650431421614600025052850029872592812368636799844316116216599530020735473583772963255833160714139561194511798884640639220872144174129684767880956395874567876885238587107317423104962863566342958842528741354706109784913872633964913685129018256002959052669282525505036968820501953,"Primes":[12356772725624677442234968801387889317557092443561606881006476461028875195308317938803572198485774285421241858540915953911278767498206501125289565176882519,11550312305950094688252881670532217665771581816925739370264710346800345350361389988692815692927550198499850330516112470522826081432865910985979998877995641],"Precomputed":{"Dp":7210207147117098586577161632877218600068675284713053828166816704246535794189318801559232257233234576632951003440242412584054981886551334506190369744253847,"Dq":11436989285172123299807475214421132081153867745358788633809269775016580117587806001590423448995841167301666041297474749562968639524305360750784839818555153,"Qinv":7742377143202795780729871745805336552195620685743089969461639809992143700499555945496100753389564870688108703251095656049359483242592495125530931590060376,"CRTValues":[]}},"Groups":{}} \ No newline at end of file +{"Name":"Sarah","Ed25519PublicKey":"a8pgrWp2v1IIQCyULC2iu1qL1drQ5t2U6IWP8StQbgQ=","Contacts":{},"Ed25519PrivateKey":"prCl0X2vmZf7Mk3llqvFtdorNumzHnGnD4jm3QMuSxZrymCtana/UghALJQsLaK7WovV2tDm3ZTohY/xK1BuBA==","OnionPrivateKey":{"N":131068731621357493720072929300222638098708595317493690917455701844894949969712456924415904360558466676729758490034054382690670564021915749623913489576811414808490682341447683428107033511871993975442547751364987326228375564356319863018740191894673856750381991533992750928853175751258987424504305668829389370781,"E":65537,"D":2627894370362753051683412867547988868298870779058954634260597711585699135758459624314242310904890751990828122372167591724606575234215745227975377653904361946969317142822427004567032046170712030718609055500182294452975841610151524604548863606200879307458625483418780389791138043842317728008422567429265129217,"Primes":[10788225097346971746043305034916905885872150156656727758248727746432901839298229013945049979389606656460780955780310528453022789715728380260987737082720253,12149239605094054781678325910916206629901461735333362155233083066169807219934646408991015480595627046734476920828757029582713785879808385848485040747462177],"Precomputed":{"Dp":5173284558864813480368386519558930089496071882650273635633043424129069168616589761833026926808934928248057785637240321920337324143555484786640548290258165,"Dq":6738186996138955142964184967422712034173952594044540452239377806264250008852778242428024332647052075845778218689956215744884274366529978620027377833723777,"Qinv":5251104905219058958861116066024964644816198404536226338388786686281405794213358346986237142459566456065203273657491361435682285121728843067653981338100228,"CRTValues":[]}},"Groups":{}} \ No newline at end of file diff --git a/model/profile_test.go b/model/profile_test.go index be358e7..eccf079 100644 --- a/model/profile_test.go +++ b/model/profile_test.go @@ -10,7 +10,7 @@ func TestProfile(t *testing.T) { profile := GenerateNewProfile("Sarah") err := profile.Save("./profile_test") if err != nil { - t.Errorf("Should have saved profile, but got error:", err) + t.Errorf("Should have saved profile, but got error: %v", err) } loadedProfile, err := LoadProfile("./profile_test") if err != nil || loadedProfile.Name != "Sarah" { diff --git a/peer/connections/peerpeerconnection.go b/peer/connections/peerpeerconnection.go index 1a109c3..ab137d3 100644 --- a/peer/connections/peerpeerconnection.go +++ b/peer/connections/peerpeerconnection.go @@ -34,9 +34,25 @@ func (ppc *PeerPeerConnection) GetState() ConnectionState { } func (ppc *PeerPeerConnection) ClientIdentity(ci *protocol.CwtchIdentity) { + ppc.profile.AddCwtchIdentity(ppc.PeerHostname, ci) } -func (ppc *PeerPeerConnection) HandleGroupInvite() { +func (ppc *PeerPeerConnection) HandleGroupInvite(gci *protocol.GroupChatInvite) { + 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 + }) } // Run manages the setup and teardown of a peer->peer connection diff --git a/peer/connections/peerpeerconnection_test.go b/peer/connections/peerpeerconnection_test.go index db2338f..6c6f19d 100644 --- a/peer/connections/peerpeerconnection_test.go +++ b/peer/connections/peerpeerconnection_test.go @@ -19,7 +19,7 @@ func PeerAuthValid(hostname string, publicKey rsa.PublicKey) (allowed, known boo return true, true } -func runtestserver(t *testing.T,tp *TestPeer) { +func runtestpeer(t *testing.T,tp *TestPeer) { ln, _ := net.Listen("tcp", "127.0.0.1:5452") conn, _ := ln.Accept() defer conn.Close() @@ -54,7 +54,7 @@ func (tp *TestPeer) ClientIdentity(ci *protocol.CwtchIdentity) { tp.ReceivedIdentityPacket = true } -func (tp *TestPeer) HandleGroupInvite() { +func (tp *TestPeer) HandleGroupInvite(gci *protocol.GroupChatInvite) { } func TestPeerPeerConnection(t *testing.T) { @@ -63,7 +63,7 @@ func TestPeerPeerConnection(t *testing.T) { //numcalls := 0 tp := new(TestPeer) tp.Init() - go runtestserver(t, tp) + go runtestpeer(t, tp) state := ppc.GetState() if state != DISCONNECTED { t.Errorf("new connections should start in disconnected state") diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index b172d67..eafbb75 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -39,7 +39,7 @@ type CwtchPeer struct { PendingContacts []string PendingInvites map[string][]string mutex sync.Mutex - Log chan string + Log chan string `json:"-"` } func NewCwtchPeer(name string) *CwtchPeer { @@ -53,7 +53,7 @@ func NewCwtchPeer(name string) *CwtchPeer { func (cp *CwtchPeer) Save(profilefile string) error { cp.mutex.Lock() - bytes, _ := json.Marshal(cp) + bytes,_ := json.Marshal(cp) err := ioutil.WriteFile(profilefile, bytes, 0600) cp.mutex.Unlock() return err diff --git a/peer/cwtch_peer_test.go b/peer/cwtch_peer_test.go index 918a6a8..c667cd2 100644 --- a/peer/cwtch_peer_test.go +++ b/peer/cwtch_peer_test.go @@ -6,10 +6,10 @@ import ( func TestCwtchPeerGenerate(t *testing.T) { sarah := NewCwtchPeer("sarah") - sarah.Save("test_profile") - sarahLoaded, err := LoadCwtchPeer("test_profile") + sarah.Save("./test_profile") + sarahLoaded, err := LoadCwtchPeer("./test_profile") if err != nil || sarahLoaded.Profile.Name != "sarah" { t.Errorf("something went wrong saving and loading profiles %v %v", err, sarahLoaded) } - t.Logf("%v", sarahLoaded) + //t.Logf("%v", sarahLoaded) } diff --git a/peer/peer/peer_channel.go b/peer/peer/peer_channel.go index a3dfc04..2544814 100644 --- a/peer/peer/peer_channel.go +++ b/peer/peer/peer_channel.go @@ -24,7 +24,7 @@ type CwtchPeerChannel struct { // events from a CwtchPeerChannel. type CwtchPeerChannelHandler interface { ClientIdentity(*protocol.CwtchIdentity) - HandleGroupInvite() + HandleGroupInvite(*protocol.GroupChatInvite) } // Type returns the type string for this channel, e.g. "im.ricochet.Cwtch". @@ -99,6 +99,8 @@ func (cpc *CwtchPeerChannel) Packet(data []byte) { if err == nil { if cpp.GetCwtchIdentify() != nil { cpc.Handler.ClientIdentity(cpp.GetCwtchIdentify()) + } else if cpp.GetGroupChatInvite() != nil { + cpc.Handler.HandleGroupInvite(cpp.GetGroupChatInvite()) } } } diff --git a/peer/peer/peer_channel_test.go b/peer/peer/peer_channel_test.go index 7f15009..2259aed 100644 --- a/peer/peer/peer_channel_test.go +++ b/peer/peer/peer_channel_test.go @@ -33,6 +33,7 @@ func TestPeerChannelAttributes(t *testing.T) { type TestHandler struct { Received bool + ReceviedGroupInvite bool } func (th *TestHandler) ClientIdentity(ci *protocol.CwtchIdentity) { @@ -41,9 +42,13 @@ func (th *TestHandler) ClientIdentity(ci *protocol.CwtchIdentity) { } } -func (th *TestHandler) HandleGroupInvite() { +func (th *TestHandler) HandleGroupInvite(ci *protocol.GroupChatInvite) { + ///if ci.GetName() == "hello" { + th.ReceviedGroupInvite = true + //} } + func TestPeerChannel(t *testing.T) { th := new(TestHandler) cpc := new(CwtchPeerChannel) @@ -96,4 +101,19 @@ func TestPeerChannel(t *testing.T) { t.Errorf("Should have sent packet to channel") } + + gci := &protocol.GroupChatInvite{ + GroupName: "hello", + GroupSharedKey: []byte{}, + ServerHost: "abc.onion", + } + + cpp = &protocol.CwtchPeerPacket{ + GroupChatInvite: gci, + } + packet, _ = proto.Marshal(cpp) + cpc.Packet(packet) + if th.ReceviedGroupInvite == false { + t.Errorf("Should have sent invite packet to handler") + } } diff --git a/peer/test_profile b/peer/test_profile deleted file mode 100644 index 203b81f..0000000 --- a/peer/test_profile +++ /dev/null @@ -1 +0,0 @@ -{"Profile":{"Name":"sarah","Ed25519PublicKey":"QUqkM0hmJ6UnfLFirkIqC0ZzxNVbMc55ePtpihk4QP4=","Contacts":{},"Ed25519PrivateKey":"B3gR0WdoGfGLONyV6Mq685ltyAE3coKo39Z6FtjOg8pBSqQzSGYnpSd8sWKuQioLRnPE1Vsxznl4+2mKGThA/g==","OnionPrivateKey":null,"Groups":{}},"PendingContacts":null,"PendingInvites":{}} \ No newline at end of file