cwtch/peer/cwtch_peer.go

252 lines
7.3 KiB
Go
Raw Normal View History

package peer
2018-03-09 20:44:13 +00:00
import (
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/model"
"cwtch.im/cwtch/peer/connections"
"cwtch.im/cwtch/peer/peer"
"cwtch.im/cwtch/protocol"
2018-03-09 20:44:13 +00:00
"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
"github.com/s-rah/go-ricochet/application"
"github.com/s-rah/go-ricochet/channels"
"github.com/s-rah/go-ricochet/connection"
"io/ioutil"
"log"
2018-03-09 20:44:13 +00:00
"sync"
)
// CwtchPeer manages incoming and outgoing connections and all processing for a Cwtch Peer
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:"-"`
connectionsManager *connections.Manager
profilefile string
2018-03-09 20:44:13 +00:00
}
func (cp *CwtchPeer) setup() {
2018-03-14 22:23:35 +00:00
cp.Log = make(chan string)
cp.connectionsManager = connections.NewConnectionsManager()
2018-03-14 22:23:35 +00:00
cp.Init()
go cp.connectionsManager.AttemptReconnections()
for onion, profile := range cp.Profile.Contacts {
if profile.Trusted && !profile.Blocked {
cp.PeerWithOnion(onion)
}
}
2018-05-03 06:01:15 +00:00
for _, group := range cp.Profile.Groups {
2018-05-03 04:12:45 +00:00
if group.Accepted || group.Owner == "self" {
cp.JoinServer(group.GroupServer)
}
}
}
// NewCwtchPeer creates and returns a new CwtchPeer with the given name.
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
}
// Save saves the CwtchPeer profile state to a file.
2018-03-09 20:44:13 +00:00
func (cp *CwtchPeer) Save(profilefile string) error {
cp.mutex.Lock()
bytes, _ := json.Marshal(cp)
2018-03-09 20:44:13 +00:00
err := ioutil.WriteFile(profilefile, bytes, 0600)
cp.profilefile = profilefile
2018-03-09 20:44:13 +00:00
cp.mutex.Unlock()
return err
}
// LoadCwtchPeer loads an existing CwtchPeer from a file.
2018-03-09 20:44:13 +00:00
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)
cp.setup()
cp.profilefile = profilefile
2018-03-14 22:23:35 +00:00
return cp, err
2018-03-09 20:44:13 +00:00
}
// PeerWithOnion is the entry point for CwtchPeer relationships
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
func (cp *CwtchPeer) InviteOnionToGroup(onion string, groupid string) error {
2018-05-16 20:53:09 +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-05-16 20:18:47 +00:00
invite, err := group.Invite()
if err != nil {
return err
}
ppc := cp.connectionsManager.GetPeerPeerConnectionForOnion(onion)
if ppc == nil {
return errors.New("peer connection not setup for onion. peers must be trusted before sending")
}
if ppc.GetState() == connections.AUTHENTICATED {
fmt.Printf("Got connection for group: %v - Sending Invite\n", ppc)
ppc.SendGroupInvite(invite)
} else {
return errors.New("cannot send invite to onion: peer connection is not ready")
}
2018-05-03 04:12:45 +00:00
return nil
}
return errors.New("group id could not be found")
2018-03-09 20:44:13 +00:00
}
// ReceiveGroupMessage is a callback function that processes GroupMessages from a given server
2018-03-30 21:16:51 +00:00
func (cp *CwtchPeer) ReceiveGroupMessage(server string, gm *protocol.GroupMessage) {
cp.Profile.AttemptDecryption(gm.Ciphertext)
}
// JoinServer manages a new server connection with the given onion address
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
}
// SendMessageToGroup attemps to sent the given message to the given group id.
2018-05-03 04:12:45 +00:00
func (cp *CwtchPeer) SendMessageToGroup(groupid string, message string) error {
2018-05-16 20:53:09 +00:00
group := cp.Profile.GetGroupByGroupID(groupid)
2018-05-03 04:12:45 +00:00
if group == nil {
return errors.New("group does not exit")
}
2018-03-30 21:16:51 +00:00
psc := cp.connectionsManager.GetPeerServerConnectionForOnion(group.GroupServer)
if psc == nil {
return errors.New("could not find server connection to send message to")
}
ct, err := cp.Profile.EncryptMessageToGroup(message, groupid)
if err != nil {
return err
}
2018-03-30 21:16:51 +00:00
gm := &protocol.GroupMessage{
Ciphertext: ct,
}
err = psc.SendGroupMessage(gm)
return err
2018-03-09 20:44:13 +00:00
}
// GetPeers returns a list of peer connections.
func (cp *CwtchPeer) GetPeers() map[string]connections.ConnectionState {
return cp.connectionsManager.GetPeers()
}
// GetServers returns a list of server connections
2018-05-03 04:12:45 +00:00
func (cp *CwtchPeer) GetServers() map[string]connections.ConnectionState {
return cp.connectionsManager.GetServers()
}
// TrustPeer sets an existing peer relationship to trusted
func (cp *CwtchPeer) TrustPeer(peer string) error {
_, ok := cp.Profile.Contacts[peer]
if !ok {
return errors.New("peer does not exist")
}
cp.Profile.Contacts[peer].Trusted = true
cp.PeerWithOnion(peer)
return nil
}
// BlockPeer blocks an existing peer relationship.
func (cp *CwtchPeer) BlockPeer(peer string) error {
_, ok := cp.Profile.Contacts[peer]
if !ok {
return errors.New("peer does not exist")
}
cp.Profile.Contacts[peer].Blocked = true
return nil
}
// AcceptInvite accepts a given existing group invite
2018-05-03 04:12:45 +00:00
func (cp *CwtchPeer) AcceptInvite(groupID string) error {
2018-05-16 20:53:09 +00:00
g := cp.Profile.GetGroupByGroupID(groupID)
2018-05-03 04:12:45 +00:00
if g == nil {
return errors.New("group invite does not exit")
}
g.Accepted = true
return nil
}
// RejectInvite rejects a given group invite.
2018-05-03 04:12:45 +00:00
func (cp *CwtchPeer) RejectInvite(groupID string) error {
2018-05-16 20:53:09 +00:00
g := cp.Profile.GetGroupByGroupID(groupID)
2018-05-03 04:12:45 +00:00
if g == nil {
return errors.New("group invite does not exit")
}
g.Accepted = false
// TODO delete group from Profile
2018-05-03 04:12:45 +00:00
return nil
}
// Listen sets up an onion listener to process incoming cwtch messages
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 {
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.Name, cp.Profile.OnionPrivateKey, af, new(application.AcceptAllContactManager))
log.Printf("Running cwtch peer on %v", l.Addr().String())
2018-03-09 20:44:13 +00:00
cwtchpeer.Run(l)
return nil
}
// CwtchPeerInstance encapsulates incoming peer connections
type CwtchPeerInstance struct {
rai *application.ApplicationInstance
ra *application.RicochetApplication
}
// Init sets up a CwtchPeerInstance
func (cpi *CwtchPeerInstance) Init(rai *application.ApplicationInstance, ra *application.RicochetApplication) {
cpi.rai = rai
cpi.ra = ra
2018-03-09 20:44:13 +00:00
}
// CwtchPeerHandler encapsulates handling of incoming CwtchPackets
2018-03-09 20:44:13 +00:00
type CwtchPeerHandler struct {
Onion string
Peer *CwtchPeer
}
// ClientIdentity handles incoming ClientIdentity packets
2018-03-09 20:44:13 +00:00
func (cph *CwtchPeerHandler) ClientIdentity(ci *protocol.CwtchIdentity) {
log.Printf("Received Client Identity from %v %v\n", cph.Onion, ci.String())
cph.Peer.Profile.AddCwtchIdentity(cph.Onion, ci)
cph.Peer.Save(cph.Peer.profilefile)
2018-03-09 20:44:13 +00:00
}
// HandleGroupInvite handles incoming GroupInvites
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())
cph.Peer.Profile.ProcessInvite(gci, cph.Onion)
2018-03-09 20:44:13 +00:00
}
// GetClientIdentityPacket returns our ClientIdentity packet so it can be sent to the connected peer.
func (cph *CwtchPeerHandler) GetClientIdentityPacket() []byte {
return cph.Peer.Profile.GetCwtchIdentityPacket()
}