forked from cwtch.im/cwtch
peer->peer connection
This commit is contained in:
parent
335a8d76c3
commit
ac3c055e0c
|
@ -1,14 +1,15 @@
|
||||||
package connections
|
package connections
|
||||||
|
|
||||||
import (
|
import (
|
||||||
//"git.mascherari.press/cwtch/peer/peer"
|
"git.mascherari.press/cwtch/peer/peer"
|
||||||
//"git.mascherari.press/cwtch/protocol"
|
"git.mascherari.press/cwtch/protocol"
|
||||||
"github.com/s-rah/go-ricochet"
|
"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/connection"
|
||||||
//"github.com/s-rah/go-ricochet/identity"
|
"github.com/s-rah/go-ricochet/identity"
|
||||||
//"github.com/s-rah/go-ricochet/utils"
|
//"github.com/s-rah/go-ricochet/utils"
|
||||||
//"time"
|
"time"
|
||||||
|
"git.mascherari.press/cwtch/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PeerPeerConnection struct {
|
type PeerPeerConnection struct {
|
||||||
|
@ -16,11 +17,13 @@ type PeerPeerConnection struct {
|
||||||
PeerHostname string
|
PeerHostname string
|
||||||
state ConnectionState
|
state ConnectionState
|
||||||
connection connection.Connection
|
connection connection.Connection
|
||||||
|
profile *model.Profile
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPeerPeerConnection(peerhostname string) *PeerPeerConnection {
|
func NewPeerPeerConnection(peerhostname string, profile *model.Profile) *PeerPeerConnection {
|
||||||
ppc := new(PeerPeerConnection)
|
ppc := new(PeerPeerConnection)
|
||||||
ppc.PeerHostname = peerhostname
|
ppc.PeerHostname = peerhostname
|
||||||
|
ppc.profile = profile
|
||||||
ppc.Init()
|
ppc.Init()
|
||||||
return ppc
|
return ppc
|
||||||
}
|
}
|
||||||
|
@ -30,6 +33,12 @@ func (ppc *PeerPeerConnection) GetState() ConnectionState {
|
||||||
return ppc.state
|
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
|
// Run manages the setup and teardown of a peer->peer connection
|
||||||
func (ppc *PeerPeerConnection) Run() error {
|
func (ppc *PeerPeerConnection) Run() error {
|
||||||
rc, err := goricochet.Open(ppc.PeerHostname)
|
rc, err := goricochet.Open(ppc.PeerHostname)
|
||||||
|
@ -37,6 +46,31 @@ func (ppc *PeerPeerConnection) Run() error {
|
||||||
rc.TraceLog(true)
|
rc.TraceLog(true)
|
||||||
ppc.connection = *rc
|
ppc.connection = *rc
|
||||||
ppc.state = CONNECTED
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,16 +6,20 @@ import (
|
||||||
"github.com/s-rah/go-ricochet/connection"
|
"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"
|
"github.com/s-rah/go-ricochet/utils"
|
||||||
|
"github.com/s-rah/go-ricochet/channels"
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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) {
|
func PeerAuthValid(hostname string, publicKey rsa.PublicKey) (allowed, known bool) {
|
||||||
return true, true
|
return true, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func runtestserver(t *testing.T) {
|
func runtestserver(t *testing.T,tp *TestPeer) {
|
||||||
ln, _ := net.Listen("tcp", "127.0.0.1:5452")
|
ln, _ := net.Listen("tcp", "127.0.0.1:5452")
|
||||||
conn, _ := ln.Accept()
|
conn, _ := ln.Accept()
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
@ -32,20 +36,46 @@ func runtestserver(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("ServerAuth Error: %v", err)
|
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) {
|
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
|
//numcalls := 0
|
||||||
go runtestserver(t)
|
tp := new(TestPeer)
|
||||||
|
tp.Init()
|
||||||
|
go runtestserver(t, tp)
|
||||||
state := ppc.GetState()
|
state := ppc.GetState()
|
||||||
if state != DISCONNECTED {
|
if state != DISCONNECTED {
|
||||||
t.Errorf("new connections should start in disconnected state")
|
t.Errorf("new connections should start in disconnected state")
|
||||||
}
|
}
|
||||||
go ppc.Run()
|
go ppc.Run()
|
||||||
time.Sleep(time.Second * 1)
|
time.Sleep(time.Second * 5)
|
||||||
state = ppc.GetState()
|
state = ppc.GetState()
|
||||||
if state != CONNECTED {
|
if state != AUTHENTICATED {
|
||||||
t.Errorf("connection state should be connected, was instead %v", state)
|
t.Errorf("connection state should be authenticated(3), was instead %v", state)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tp.ReceivedIdentityPacket == false {
|
||||||
|
t.Errorf("should have received an identity packet")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue