2018-03-11 18:49:10 +00:00
|
|
|
package peer
|
2018-03-09 20:44:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-03-30 21:16:51 +00:00
|
|
|
"errors"
|
2018-05-01 21:36:03 +00:00
|
|
|
"fmt"
|
2018-03-09 20:44:13 +00:00
|
|
|
"git.mascherari.press/cwtch/model"
|
2018-03-14 22:03:53 +00:00
|
|
|
"git.mascherari.press/cwtch/peer/connections"
|
|
|
|
"git.mascherari.press/cwtch/peer/peer"
|
2018-03-09 20:44:13 +00:00
|
|
|
"git.mascherari.press/cwtch/protocol"
|
|
|
|
"github.com/s-rah/go-ricochet/application"
|
|
|
|
"github.com/s-rah/go-ricochet/channels"
|
|
|
|
"github.com/s-rah/go-ricochet/connection"
|
|
|
|
"io/ioutil"
|
2018-05-01 20:44:45 +00:00
|
|
|
"log"
|
2018-03-09 20:44:13 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2018-03-10 21:26:19 +00:00
|
|
|
/**
|
|
|
|
|
|
|
|
PeerConnectionPool
|
|
|
|
Background thread running Process()
|
|
|
|
Pointer to connections
|
|
|
|
|
|
|
|
Move CwtchPeerChannel under peer/
|
|
|
|
Write tests for Peer Channel
|
|
|
|
*/
|
|
|
|
|
2018-03-09 20:44:13 +00:00
|
|
|
type CwtchPeer struct {
|
|
|
|
connection.AutoConnectionHandler
|
2018-03-30 21:16:51 +00:00
|
|
|
Profile *model.Profile
|
|
|
|
mutex sync.Mutex
|
|
|
|
Log chan string `json:"-"`
|
2018-03-15 20:53:22 +00:00
|
|
|
connectionsManager *connections.Manager
|
2018-05-01 20:44:45 +00:00
|
|
|
profilefile string
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 20:44:45 +00:00
|
|
|
func (cp *CwtchPeer) setup() {
|
2018-03-14 22:23:35 +00:00
|
|
|
cp.Log = make(chan string)
|
2018-03-15 20:53:22 +00:00
|
|
|
cp.connectionsManager = connections.NewConnectionsManager()
|
2018-03-14 22:23:35 +00:00
|
|
|
cp.Init()
|
2018-05-01 20:44:45 +00:00
|
|
|
|
|
|
|
go cp.connectionsManager.AttemptReconnections()
|
|
|
|
|
|
|
|
for onion := range cp.Profile.Contacts {
|
|
|
|
cp.PeerWithOnion(onion)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCwtchPeer(name string) *CwtchPeer {
|
|
|
|
cp := new(CwtchPeer)
|
|
|
|
cp.Profile = model.GenerateNewProfile(name)
|
|
|
|
cp.setup()
|
2018-03-14 22:23:35 +00:00
|
|
|
return cp
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *CwtchPeer) Save(profilefile string) error {
|
|
|
|
cp.mutex.Lock()
|
2018-03-14 22:03:53 +00:00
|
|
|
bytes, _ := json.Marshal(cp)
|
2018-03-09 20:44:13 +00:00
|
|
|
err := ioutil.WriteFile(profilefile, bytes, 0600)
|
2018-05-01 20:44:45 +00:00
|
|
|
cp.profilefile = profilefile
|
2018-03-09 20:44:13 +00:00
|
|
|
cp.mutex.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadCwtchPeer(profilefile string) (*CwtchPeer, error) {
|
|
|
|
bytes, _ := ioutil.ReadFile(profilefile)
|
2018-03-14 22:23:35 +00:00
|
|
|
cp := new(CwtchPeer)
|
|
|
|
err := json.Unmarshal(bytes, &cp)
|
2018-05-01 20:44:45 +00:00
|
|
|
cp.setup()
|
|
|
|
cp.profilefile = profilefile
|
2018-03-14 22:23:35 +00:00
|
|
|
return cp, err
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddContactRequest is the entry point for CwtchPeer relationships
|
2018-03-15 20:53:22 +00:00
|
|
|
func (cp *CwtchPeer) PeerWithOnion(onion string) {
|
|
|
|
cp.connectionsManager.ManagePeerConnection(onion, cp.Profile)
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InviteOnionToGroup kicks off the invite process
|
2018-03-15 20:53:22 +00:00
|
|
|
func (cp *CwtchPeer) InviteOnionToGroup(onion string, groupid string) error {
|
2018-03-30 21:16:51 +00:00
|
|
|
group := cp.Profile.GetGroupByGroupId(groupid)
|
2018-05-01 21:36:03 +00:00
|
|
|
if group != nil {
|
|
|
|
fmt.Printf("Constructing invite for group: %v\n", group)
|
2018-03-15 20:53:22 +00:00
|
|
|
invite := group.Invite()
|
|
|
|
ppc := cp.connectionsManager.GetPeerPeerConnectionForOnion(onion)
|
2018-05-01 21:36:03 +00:00
|
|
|
fmt.Printf("Got connection for group: %v - Sending Invite\n", ppc)
|
2018-03-15 20:53:22 +00:00
|
|
|
ppc.SendGroupInvite(invite)
|
|
|
|
}
|
|
|
|
return errors.New("group id could not be found")
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 21:16:51 +00:00
|
|
|
func (cp *CwtchPeer) ReceiveGroupMessage(server string, gm *protocol.GroupMessage) {
|
|
|
|
cp.Profile.AttemptDecryption(gm.Ciphertext)
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:44:13 +00:00
|
|
|
func (cp *CwtchPeer) JoinServer(onion string) {
|
2018-03-30 21:16:51 +00:00
|
|
|
cp.connectionsManager.ManageServerConnection(onion, cp.ReceiveGroupMessage)
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cp *CwtchPeer) SendMessageToGroup(groupid string, message string) {
|
2018-03-30 21:16:51 +00:00
|
|
|
group := cp.Profile.GetGroupByGroupId(groupid)
|
|
|
|
psc := cp.connectionsManager.GetPeerServerConnectionForOnion(group.GroupServer)
|
2018-03-31 19:33:32 +00:00
|
|
|
ct := cp.Profile.EncryptMessageToGroup(message, groupid)
|
2018-03-30 21:16:51 +00:00
|
|
|
gm := &protocol.GroupMessage{
|
|
|
|
Ciphertext: ct,
|
|
|
|
}
|
|
|
|
psc.SendGroupMessage(gm)
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 20:44:45 +00:00
|
|
|
func (cp *CwtchPeer) GetPeers() map[string]connections.ConnectionState {
|
|
|
|
return cp.connectionsManager.GetPeers()
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:44:13 +00:00
|
|
|
func (cp *CwtchPeer) Listen() error {
|
|
|
|
cwtchpeer := new(application.RicochetApplication)
|
|
|
|
l, err := application.SetupOnion("127.0.0.1:9051", "tcp4", "", cp.Profile.OnionPrivateKey, 9878)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
af := application.ApplicationInstanceFactory{}
|
|
|
|
af.Init()
|
|
|
|
af.AddHandler("im.cwtch.peer", func(rai *application.ApplicationInstance) func() channels.Handler {
|
|
|
|
cpi := new(CwtchPeerInstance)
|
|
|
|
cpi.Init(rai, cwtchpeer)
|
|
|
|
return func() channels.Handler {
|
2018-03-14 22:03:53 +00:00
|
|
|
cpc := new(peer.CwtchPeerChannel)
|
|
|
|
cpc.Handler = &CwtchPeerHandler{Onion: rai.RemoteHostname, Peer: cp}
|
|
|
|
return cpc
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
cwtchpeer.Init(cp.Profile.OnionPrivateKey, af, new(application.AcceptAllContactManager))
|
2018-05-01 20:44:45 +00:00
|
|
|
log.Printf("Running cwtch peer on %v", l.Addr().String())
|
2018-03-09 20:44:13 +00:00
|
|
|
cwtchpeer.Run(l)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:53:22 +00:00
|
|
|
type CwtchPeerInstance struct {
|
|
|
|
rai *application.ApplicationInstance
|
|
|
|
ra *application.RicochetApplication
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cpi *CwtchPeerInstance) Init(rai *application.ApplicationInstance, ra *application.RicochetApplication) {
|
|
|
|
cpi.rai = rai
|
|
|
|
cpi.ra = ra
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CwtchPeerHandler struct {
|
|
|
|
Onion string
|
|
|
|
Peer *CwtchPeer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cph *CwtchPeerHandler) ClientIdentity(ci *protocol.CwtchIdentity) {
|
2018-05-01 20:44:45 +00:00
|
|
|
log.Printf("Received Client Identity from %v %v\n", cph.Onion, ci.String())
|
2018-03-14 22:03:53 +00:00
|
|
|
cph.Peer.Profile.AddCwtchIdentity(cph.Onion, ci)
|
2018-05-01 20:44:45 +00:00
|
|
|
cph.Peer.Save(cph.Peer.profilefile)
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 22:03:53 +00:00
|
|
|
func (cph *CwtchPeerHandler) HandleGroupInvite(gci *protocol.GroupChatInvite) {
|
2018-05-01 21:36:03 +00:00
|
|
|
log.Printf("Received GroupID from %v %v\n", cph.Onion, gci.String())
|
2018-03-15 20:53:22 +00:00
|
|
|
cph.Peer.Profile.ProcessInvite(gci, cph.Onion)
|
2018-03-09 20:44:13 +00:00
|
|
|
}
|
2018-03-10 21:26:19 +00:00
|
|
|
|
|
|
|
func (cph *CwtchPeerHandler) HandleGroupMessage(gm *protocol.GroupMessage) {
|
|
|
|
}
|
2018-05-01 20:44:45 +00:00
|
|
|
|
|
|
|
func (cph *CwtchPeerHandler) GetClientIdentityPacket() []byte {
|
|
|
|
return cph.Peer.Profile.GetCwtchIdentityPacket()
|
|
|
|
}
|