2018-03-11 18:49:10 +00:00
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/peer/peer"
|
|
|
|
"cwtch.im/cwtch/protocol"
|
2018-03-11 18:49:10 +00:00
|
|
|
"github.com/s-rah/go-ricochet"
|
2018-03-11 19:05:35 +00:00
|
|
|
"github.com/s-rah/go-ricochet/channels"
|
2018-03-11 18:49:10 +00:00
|
|
|
"github.com/s-rah/go-ricochet/connection"
|
2018-03-11 19:05:35 +00:00
|
|
|
"github.com/s-rah/go-ricochet/identity"
|
2018-03-11 18:49:10 +00:00
|
|
|
//"github.com/s-rah/go-ricochet/utils"
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/model"
|
2018-05-01 21:36:03 +00:00
|
|
|
"log"
|
2018-03-14 22:23:35 +00:00
|
|
|
"time"
|
2018-03-11 18:49:10 +00:00
|
|
|
)
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// PeerPeerConnection encapsulates a single outgoing Peer->Peer connection
|
2018-03-11 18:49:10 +00:00
|
|
|
type PeerPeerConnection struct {
|
|
|
|
connection.AutoConnectionHandler
|
|
|
|
PeerHostname string
|
|
|
|
state ConnectionState
|
2018-05-20 18:29:46 +00:00
|
|
|
connection *connection.Connection
|
2018-03-14 22:23:35 +00:00
|
|
|
profile *model.Profile
|
2018-03-11 18:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// NewPeerPeerConnection creates a new peer connection for the given hostname and profile.
|
2018-03-11 19:05:35 +00:00
|
|
|
func NewPeerPeerConnection(peerhostname string, profile *model.Profile) *PeerPeerConnection {
|
2018-03-11 18:49:10 +00:00
|
|
|
ppc := new(PeerPeerConnection)
|
|
|
|
ppc.PeerHostname = peerhostname
|
2018-03-11 19:05:35 +00:00
|
|
|
ppc.profile = profile
|
2018-03-11 18:49:10 +00:00
|
|
|
ppc.Init()
|
|
|
|
return ppc
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetState returns the current connection state
|
|
|
|
func (ppc *PeerPeerConnection) GetState() ConnectionState {
|
|
|
|
return ppc.state
|
|
|
|
}
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// ClientIdentity passes the given CwtchIdentity packet to the profile.
|
2018-03-11 19:05:35 +00:00
|
|
|
func (ppc *PeerPeerConnection) ClientIdentity(ci *protocol.CwtchIdentity) {
|
2018-03-14 22:23:35 +00:00
|
|
|
ppc.profile.AddCwtchIdentity(ppc.PeerHostname, ci)
|
2018-03-11 19:05:35 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// HandleGroupInvite passes the given group invite tothe profile
|
2018-03-12 18:43:51 +00:00
|
|
|
func (ppc *PeerPeerConnection) HandleGroupInvite(gci *protocol.GroupChatInvite) {
|
2018-03-15 20:53:22 +00:00
|
|
|
ppc.profile.ProcessInvite(gci, ppc.PeerHostname)
|
2018-03-12 18:43:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// GetClientIdentityPacket returns nil to avoid peers constantly sending identity packets to eachother.
|
2018-05-01 20:44:45 +00:00
|
|
|
func (ppc *PeerPeerConnection) GetClientIdentityPacket() []byte {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-16 21:31:06 +00:00
|
|
|
// SendGroupInvite sends the given serialized invite packet to the Peer
|
2018-03-12 18:43:51 +00:00
|
|
|
func (ppc *PeerPeerConnection) SendGroupInvite(invite []byte) {
|
2018-03-14 22:23:35 +00:00
|
|
|
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 {
|
2018-05-01 21:36:03 +00:00
|
|
|
log.Printf("Sending group invite packet\n")
|
2018-03-14 22:23:35 +00:00
|
|
|
peerchannel.SendMessage(invite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2018-03-11 19:05:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:49:10 +00:00
|
|
|
// Run manages the setup and teardown of a peer->peer connection
|
|
|
|
func (ppc *PeerPeerConnection) Run() error {
|
2018-05-01 20:44:45 +00:00
|
|
|
ppc.state = CONNECTING
|
2018-03-11 18:49:10 +00:00
|
|
|
rc, err := goricochet.Open(ppc.PeerHostname)
|
|
|
|
if err == nil {
|
2018-05-01 20:44:45 +00:00
|
|
|
rc.TraceLog(false)
|
2018-05-20 18:29:46 +00:00
|
|
|
ppc.connection = rc
|
2018-03-11 18:49:10 +00:00
|
|
|
ppc.state = CONNECTED
|
2018-05-20 18:29:46 +00:00
|
|
|
_, err := connection.HandleOutboundConnection(ppc.connection).ProcessAuthAsClient(identity.Initialize(ppc.profile.Name, ppc.profile.OnionPrivateKey))
|
2018-03-11 19:05:35 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
|
2018-03-14 22:23:35 +00:00
|
|
|
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
|
|
|
|
})
|
2018-03-11 19:05:35 +00:00
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
ppc.connection.Process(ppc)
|
|
|
|
}
|
2018-03-11 18:49:10 +00:00
|
|
|
}
|
2018-03-15 20:53:22 +00:00
|
|
|
ppc.state = FAILED
|
2018-03-11 18:49:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-05-30 17:41:02 +00:00
|
|
|
|
2018-05-30 18:42:17 +00:00
|
|
|
// Close closes the connection
|
|
|
|
func (ppc *PeerPeerConnection) Close() {
|
2018-05-30 17:41:02 +00:00
|
|
|
ppc.state = KILLED
|
2018-06-05 22:06:38 +00:00
|
|
|
ppc.connection.Conn.Close()
|
2018-05-30 17:41:02 +00:00
|
|
|
}
|