diff --git a/peer/connections/peerpeerconnection.go b/peer/connections/peerpeerconnection.go index 71110a6..1a109c3 100644 --- a/peer/connections/peerpeerconnection.go +++ b/peer/connections/peerpeerconnection.go @@ -1,14 +1,15 @@ package connections import ( - //"git.mascherari.press/cwtch/peer/peer" - //"git.mascherari.press/cwtch/protocol" + "git.mascherari.press/cwtch/peer/peer" + "git.mascherari.press/cwtch/protocol" "github.com/s-rah/go-ricochet" - //"github.com/s-rah/go-ricochet/channels" + "github.com/s-rah/go-ricochet/channels" "github.com/s-rah/go-ricochet/connection" - //"github.com/s-rah/go-ricochet/identity" + "github.com/s-rah/go-ricochet/identity" //"github.com/s-rah/go-ricochet/utils" - //"time" + "time" + "git.mascherari.press/cwtch/model" ) type PeerPeerConnection struct { @@ -16,11 +17,13 @@ type PeerPeerConnection struct { PeerHostname string state ConnectionState connection connection.Connection + profile *model.Profile } -func NewPeerPeerConnection(peerhostname string) *PeerPeerConnection { +func NewPeerPeerConnection(peerhostname string, profile *model.Profile) *PeerPeerConnection { ppc := new(PeerPeerConnection) ppc.PeerHostname = peerhostname + ppc.profile = profile ppc.Init() return ppc } @@ -30,6 +33,12 @@ func (ppc *PeerPeerConnection) GetState() ConnectionState { return ppc.state } +func (ppc *PeerPeerConnection) ClientIdentity(ci *protocol.CwtchIdentity) { +} + +func (ppc *PeerPeerConnection) HandleGroupInvite() { +} + // Run manages the setup and teardown of a peer->peer connection func (ppc *PeerPeerConnection) Run() error { rc, err := goricochet.Open(ppc.PeerHostname) @@ -37,6 +46,31 @@ func (ppc *PeerPeerConnection) Run() error { rc.TraceLog(true) ppc.connection = *rc ppc.state = CONNECTED + _, err := connection.HandleOutboundConnection(&ppc.connection).ProcessAuthAsClient(identity.Initialize(ppc.profile.Name, ppc.profile.OnionPrivateKey)) + if err == nil { + ppc.state = AUTHENTICATED + go func() { + ppc.connection.Do(func() error { + ppc.connection.RequestOpenChannel("im.cwtch.peer", &peer.CwtchPeerChannel{Handler: ppc}) + 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 + }) + + }() + + ppc.connection.Process(ppc) + } } return err } diff --git a/peer/connections/peerpeerconnection_test.go b/peer/connections/peerpeerconnection_test.go index ff08628..db2338f 100644 --- a/peer/connections/peerpeerconnection_test.go +++ b/peer/connections/peerpeerconnection_test.go @@ -6,16 +6,20 @@ import ( "github.com/s-rah/go-ricochet/connection" "github.com/s-rah/go-ricochet/identity" "github.com/s-rah/go-ricochet/utils" + "github.com/s-rah/go-ricochet/channels" "net" "testing" "time" + "git.mascherari.press/cwtch/model" + "git.mascherari.press/cwtch/protocol" + "git.mascherari.press/cwtch/peer/peer" ) func PeerAuthValid(hostname string, publicKey rsa.PublicKey) (allowed, known bool) { return true, true } -func runtestserver(t *testing.T) { +func runtestserver(t *testing.T,tp *TestPeer) { ln, _ := net.Listen("tcp", "127.0.0.1:5452") conn, _ := ln.Accept() defer conn.Close() @@ -32,20 +36,46 @@ func runtestserver(t *testing.T) { if err != nil { t.Errorf("ServerAuth Error: %v", err) } + tp.RegisterChannelHandler("im.cwtch.peer", func() channels.Handler { + peer := new(peer.CwtchPeerChannel) + peer.Handler = tp + return peer + }) + + rc.Process(tp) +} + +type TestPeer struct { + connection.AutoConnectionHandler + ReceivedIdentityPacket bool +} + +func (tp *TestPeer) ClientIdentity(ci *protocol.CwtchIdentity) { + tp.ReceivedIdentityPacket = true +} + +func (tp *TestPeer) HandleGroupInvite() { } func TestPeerPeerConnection(t *testing.T) { - ppc := NewPeerPeerConnection("127.0.0.1:5452|kwke2hntvyfqm7dr") + profile := model.GenerateNewProfile("sarah") + ppc := NewPeerPeerConnection("127.0.0.1:5452|kwke2hntvyfqm7dr", profile) //numcalls := 0 - go runtestserver(t) + tp := new(TestPeer) + tp.Init() + go runtestserver(t, tp) state := ppc.GetState() if state != DISCONNECTED { t.Errorf("new connections should start in disconnected state") } go ppc.Run() - time.Sleep(time.Second * 1) + time.Sleep(time.Second * 5) state = ppc.GetState() - if state != CONNECTED { - t.Errorf("connection state should be connected, was instead %v", state) + if state != AUTHENTICATED { + t.Errorf("connection state should be authenticated(3), was instead %v", state) + } + + if tp.ReceivedIdentityPacket == false { + t.Errorf("should have received an identity packet") } }