2018-04-30 21:47:21 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-01-04 21:44:21 +00:00
|
|
|
"cwtch.im/cwtch/event"
|
2019-06-05 20:40:55 +00:00
|
|
|
"cwtch.im/cwtch/model"
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/peer"
|
2019-05-15 20:12:11 +00:00
|
|
|
"cwtch.im/cwtch/protocol/connections"
|
2018-10-06 03:50:55 +00:00
|
|
|
"cwtch.im/cwtch/storage"
|
2018-07-01 18:43:05 +00:00
|
|
|
"fmt"
|
2019-05-15 20:12:11 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/identity"
|
2019-07-10 20:30:24 +00:00
|
|
|
"strconv"
|
2018-10-06 03:50:55 +00:00
|
|
|
|
2018-11-10 22:14:12 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
2018-12-04 02:52:11 +00:00
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
2018-09-21 18:53:10 +00:00
|
|
|
"io/ioutil"
|
2018-07-01 18:43:05 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2018-09-21 18:53:10 +00:00
|
|
|
"sync"
|
2018-04-30 21:47:21 +00:00
|
|
|
)
|
|
|
|
|
2019-06-05 20:40:55 +00:00
|
|
|
type applicationCore struct {
|
|
|
|
eventBuses map[string]event.Manager
|
|
|
|
|
|
|
|
directory string
|
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
2019-07-10 20:30:24 +00:00
|
|
|
type appletPeers struct {
|
2019-06-05 20:40:55 +00:00
|
|
|
peers map[string]peer.CwtchPeer
|
|
|
|
}
|
|
|
|
|
2019-07-10 20:30:24 +00:00
|
|
|
type appletACN struct {
|
|
|
|
acn connectivity.ACN
|
|
|
|
}
|
|
|
|
|
2018-09-21 18:53:10 +00:00
|
|
|
type application struct {
|
2019-06-05 20:40:55 +00:00
|
|
|
applicationCore
|
2019-07-10 20:30:24 +00:00
|
|
|
appletPeers
|
|
|
|
appletACN
|
2019-06-05 20:40:55 +00:00
|
|
|
storage map[string]storage.ProfileStore
|
|
|
|
engines map[string]connections.Engine
|
|
|
|
appBus event.Manager
|
2018-09-21 18:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Application is a full cwtch peer application. It allows management, usage and storage of multiple peers
|
|
|
|
type Application interface {
|
2019-06-05 20:40:55 +00:00
|
|
|
LoadProfiles(password string)
|
|
|
|
CreatePeer(name string, password string)
|
2018-11-22 00:08:47 +00:00
|
|
|
LaunchPeers()
|
2018-09-21 18:53:10 +00:00
|
|
|
|
2019-06-05 20:40:55 +00:00
|
|
|
GetPrimaryBus() event.Manager
|
|
|
|
GetEventBus(onion string) event.Manager
|
|
|
|
|
2019-05-15 20:12:11 +00:00
|
|
|
ShutdownPeer(string)
|
2018-09-21 18:53:10 +00:00
|
|
|
Shutdown()
|
2019-06-05 20:40:55 +00:00
|
|
|
|
|
|
|
GetPeer(onion string) peer.CwtchPeer
|
|
|
|
ListPeers() map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadProfileFn is the function signature for a function in an app that loads a profile
|
|
|
|
type LoadProfileFn func(profile *model.Profile, store storage.ProfileStore)
|
|
|
|
|
2019-06-21 21:50:43 +00:00
|
|
|
func newAppCore(appDirectory string) *applicationCore {
|
|
|
|
appCore := &applicationCore{eventBuses: make(map[string]event.Manager), directory: appDirectory}
|
2019-06-05 20:40:55 +00:00
|
|
|
os.MkdirAll(path.Join(appCore.directory, "profiles"), 0700)
|
|
|
|
return appCore
|
2018-07-10 20:42:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 20:30:24 +00:00
|
|
|
func (ap *appletPeers) init() {
|
|
|
|
ap.peers = make(map[string]peer.CwtchPeer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appletACN) init(acn connectivity.ACN, publish func(int, string)) {
|
|
|
|
a.acn = acn
|
|
|
|
acn.SetStatusCallback(publish)
|
|
|
|
prog, status := acn.GetBootstrapStatus()
|
|
|
|
publish(prog, status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *appletACN) Shutdown() {
|
|
|
|
a.acn.Close()
|
|
|
|
}
|
|
|
|
|
2018-07-10 20:42:47 +00:00
|
|
|
// NewApp creates a new app with some environment awareness and initializes a Tor Manager
|
2018-11-22 18:01:04 +00:00
|
|
|
func NewApp(acn connectivity.ACN, appDirectory string) Application {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Debugf("NewApp(%v)\n", appDirectory)
|
2019-07-10 20:30:24 +00:00
|
|
|
app := &application{storage: make(map[string]storage.ProfileStore), engines: make(map[string]connections.Engine), applicationCore: *newAppCore(appDirectory), appBus: event.NewEventManager()}
|
|
|
|
app.appletPeers.init()
|
|
|
|
|
|
|
|
fn := func(progress int, status string) {
|
|
|
|
progStr := strconv.Itoa(progress)
|
|
|
|
app.appBus.Publish(event.NewEventList(event.ACNStatus, event.Progreess, progStr, event.Status, status))
|
|
|
|
}
|
|
|
|
app.appletACN.init(acn, fn)
|
2018-11-10 22:14:12 +00:00
|
|
|
return app
|
2018-04-30 21:47:21 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 20:40:55 +00:00
|
|
|
// CreatePeer creates a new Peer with a given name and core required accessories (eventbus)
|
|
|
|
func (ac *applicationCore) CreatePeer(name string, password string) (*model.Profile, error) {
|
2018-12-04 02:52:11 +00:00
|
|
|
log.Debugf("CreatePeer(%v)\n", name)
|
2018-09-21 18:53:10 +00:00
|
|
|
|
2019-02-04 18:25:58 +00:00
|
|
|
profile := storage.NewProfile(name)
|
2019-06-05 20:40:55 +00:00
|
|
|
|
|
|
|
ac.mutex.Lock()
|
|
|
|
defer ac.mutex.Unlock()
|
|
|
|
|
|
|
|
_, exists := ac.eventBuses[profile.Onion]
|
2018-09-21 18:53:10 +00:00
|
|
|
if exists {
|
2019-06-05 20:40:55 +00:00
|
|
|
return nil, fmt.Errorf("Error: profile for onion %v already exists", profile.Onion)
|
|
|
|
}
|
|
|
|
|
|
|
|
eventBus := event.NewEventManager()
|
|
|
|
ac.eventBuses[profile.Onion] = eventBus
|
|
|
|
|
|
|
|
return profile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePeer creates a new Peer with the given name and required accessories (eventbus, storage, protocol engine)
|
|
|
|
func (app *application) CreatePeer(name string, password string) {
|
|
|
|
profile, err := app.applicationCore.CreatePeer(name, password)
|
|
|
|
if err != nil {
|
|
|
|
app.appBus.Publish(event.NewEventList(event.PeerError, event.Error, err.Error()))
|
|
|
|
return
|
2018-05-01 20:44:45 +00:00
|
|
|
}
|
2019-06-05 20:40:55 +00:00
|
|
|
|
|
|
|
profileStore := storage.NewProfileWriterStore(app.eventBuses[profile.Onion], path.Join(app.directory, "profiles", profile.LocalID), password, profile)
|
|
|
|
app.storage[profile.Onion] = profileStore
|
|
|
|
|
|
|
|
pc := app.storage[profile.Onion].GetProfileCopy()
|
|
|
|
peer := peer.FromProfile(pc)
|
2019-06-21 21:50:43 +00:00
|
|
|
peer.Init(app.eventBuses[profile.Onion])
|
2019-05-15 20:12:11 +00:00
|
|
|
|
|
|
|
blockedPeers := profile.BlockedPeers()
|
|
|
|
// TODO: Would be nice if ProtocolEngine did not need to explicitly be given the Private Key.
|
|
|
|
identity := identity.InitializeV3(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey)
|
2019-06-05 20:40:55 +00:00
|
|
|
engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, app.acn, app.eventBuses[profile.Onion], blockedPeers)
|
2019-05-15 20:12:11 +00:00
|
|
|
|
2019-06-05 20:40:55 +00:00
|
|
|
app.peers[profile.Onion] = peer
|
|
|
|
app.engines[profile.Onion] = engine
|
2018-09-21 18:53:10 +00:00
|
|
|
|
2019-06-05 20:40:55 +00:00
|
|
|
app.appBus.Publish(event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.Onion}))
|
2018-09-21 18:53:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 20:40:55 +00:00
|
|
|
// LoadProfiles takes a password and attempts to load any profiles it can from storage with it and create Peers for them
|
|
|
|
func (ac *applicationCore) LoadProfiles(password string, loadProfileFn LoadProfileFn) error {
|
|
|
|
files, err := ioutil.ReadDir(path.Join(ac.directory, "profiles"))
|
2018-09-21 18:53:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error: cannot read profiles directory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
2019-06-05 20:40:55 +00:00
|
|
|
eventBus := event.NewEventManager()
|
|
|
|
profileStore := storage.NewProfileWriterStore(eventBus, path.Join(ac.directory, "profiles", file.Name()), password, nil)
|
2019-01-21 20:11:40 +00:00
|
|
|
err = profileStore.Load()
|
2018-09-21 18:53:10 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-06 03:50:55 +00:00
|
|
|
|
2019-01-21 20:11:40 +00:00
|
|
|
profile := profileStore.GetProfileCopy()
|
2019-02-03 01:18:33 +00:00
|
|
|
|
2019-06-05 20:40:55 +00:00
|
|
|
_, exists := ac.eventBuses[profile.Onion]
|
2018-09-21 18:53:10 +00:00
|
|
|
if exists {
|
2019-01-21 20:11:40 +00:00
|
|
|
profileStore.Shutdown()
|
2019-05-15 20:12:11 +00:00
|
|
|
eventBus.Shutdown()
|
2019-01-21 20:11:40 +00:00
|
|
|
log.Errorf("profile for onion %v already exists", profile.Onion)
|
2018-09-21 18:53:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-11-22 00:08:47 +00:00
|
|
|
|
2019-06-05 20:40:55 +00:00
|
|
|
ac.mutex.Lock()
|
|
|
|
ac.eventBuses[profile.Onion] = eventBus
|
|
|
|
ac.mutex.Unlock()
|
|
|
|
|
|
|
|
loadProfileFn(profile, profileStore)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadProfiles takes a password and attempts to load any profiles it can from storage with it and create Peers for them
|
|
|
|
func (app *application) LoadProfiles(password string) {
|
2019-07-10 20:30:24 +00:00
|
|
|
count := 0
|
2019-06-05 20:40:55 +00:00
|
|
|
app.applicationCore.LoadProfiles(password, func(profile *model.Profile, profileStore storage.ProfileStore) {
|
2019-01-21 20:11:40 +00:00
|
|
|
peer := peer.FromProfile(profile)
|
2019-06-21 21:50:43 +00:00
|
|
|
peer.Init(app.eventBuses[profile.Onion])
|
2019-05-15 20:12:11 +00:00
|
|
|
|
|
|
|
blockedPeers := profile.BlockedPeers()
|
|
|
|
identity := identity.InitializeV3(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey)
|
2019-06-05 20:40:55 +00:00
|
|
|
engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, app.acn, app.eventBuses[profile.Onion], blockedPeers)
|
2018-09-21 18:53:10 +00:00
|
|
|
app.mutex.Lock()
|
2019-01-21 20:11:40 +00:00
|
|
|
app.peers[profile.Onion] = peer
|
|
|
|
app.storage[profile.Onion] = profileStore
|
2019-05-15 20:12:11 +00:00
|
|
|
app.engines[profile.Onion] = engine
|
2018-09-21 18:53:10 +00:00
|
|
|
app.mutex.Unlock()
|
2019-06-05 20:40:55 +00:00
|
|
|
app.appBus.Publish(event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.Onion}))
|
2019-07-10 20:30:24 +00:00
|
|
|
count++
|
2019-06-05 20:40:55 +00:00
|
|
|
})
|
2019-07-10 20:30:24 +00:00
|
|
|
if count == 0 {
|
|
|
|
message := event.NewEventList(event.AppError, event.Error, event.AppErrLoaded0)
|
|
|
|
app.appBus.Publish(message)
|
|
|
|
}
|
2019-06-05 20:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPrimaryBus returns the bus the Application uses for events that aren't peer specific
|
|
|
|
func (app *application) GetPrimaryBus() event.Manager {
|
|
|
|
return app.appBus
|
2018-04-30 21:47:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 20:12:11 +00:00
|
|
|
// LaunchPeers starts each peer Listening and connecting to peers and groups
|
2019-07-10 20:30:24 +00:00
|
|
|
func (ap *appletPeers) LaunchPeers() {
|
|
|
|
for _, p := range ap.peers {
|
2018-11-22 00:08:47 +00:00
|
|
|
if !p.IsStarted() {
|
2018-11-26 22:06:26 +00:00
|
|
|
p.Listen()
|
2019-05-15 20:12:11 +00:00
|
|
|
p.StartPeersConnections()
|
|
|
|
p.StartGroupConnections()
|
2018-11-22 00:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 18:53:10 +00:00
|
|
|
// ListPeers returns a map of onions to their profile's Name
|
2019-07-10 20:30:24 +00:00
|
|
|
func (ap *appletPeers) ListPeers() map[string]string {
|
2018-09-21 18:53:10 +00:00
|
|
|
keys := map[string]string{}
|
2019-07-10 20:30:24 +00:00
|
|
|
for k, p := range ap.peers {
|
2018-09-21 18:53:10 +00:00
|
|
|
keys[k] = p.GetProfile().Name
|
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
2018-07-10 20:42:47 +00:00
|
|
|
|
2018-10-06 03:50:55 +00:00
|
|
|
// GetPeer returns a cwtchPeer for a given onion address
|
2019-07-10 20:30:24 +00:00
|
|
|
func (ap *appletPeers) GetPeer(onion string) peer.CwtchPeer {
|
|
|
|
if peer, ok := ap.peers[onion]; ok {
|
2018-09-21 18:53:10 +00:00
|
|
|
return peer
|
|
|
|
}
|
2018-07-10 20:42:47 +00:00
|
|
|
return nil
|
2018-04-30 21:47:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 20:12:11 +00:00
|
|
|
// GetEventBus returns a cwtchPeer's event bus
|
2019-06-05 20:40:55 +00:00
|
|
|
func (ac *applicationCore) GetEventBus(onion string) event.Manager {
|
|
|
|
if manager, ok := ac.eventBuses[onion]; ok {
|
2019-05-15 20:12:11 +00:00
|
|
|
return manager
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-05 23:27:57 +00:00
|
|
|
|
2019-05-15 20:12:11 +00:00
|
|
|
// ShutdownPeer shuts down a peer and removes it from the app's management
|
|
|
|
func (app *application) ShutdownPeer(onion string) {
|
|
|
|
app.mutex.Lock()
|
|
|
|
defer app.mutex.Unlock()
|
|
|
|
app.eventBuses[onion].Shutdown()
|
|
|
|
delete(app.eventBuses, onion)
|
|
|
|
app.peers[onion].Shutdown()
|
|
|
|
delete(app.peers, onion)
|
|
|
|
app.engines[onion].Shutdown()
|
|
|
|
delete(app.engines, onion)
|
|
|
|
app.storage[onion].Shutdown()
|
|
|
|
delete(app.storage, onion)
|
2019-01-19 23:16:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 18:53:10 +00:00
|
|
|
// Shutdown shutsdown all peers of an app and then the tormanager
|
|
|
|
func (app *application) Shutdown() {
|
2019-05-15 20:12:11 +00:00
|
|
|
for id, peer := range app.peers {
|
2018-09-21 18:53:10 +00:00
|
|
|
peer.Shutdown()
|
2019-05-15 20:12:11 +00:00
|
|
|
app.engines[id].Shutdown()
|
|
|
|
app.storage[id].Shutdown()
|
|
|
|
app.eventBuses[id].Shutdown()
|
2018-09-21 18:53:10 +00:00
|
|
|
}
|
2019-06-05 20:40:55 +00:00
|
|
|
app.appBus.Shutdown()
|
2018-04-30 21:47:21 +00:00
|
|
|
}
|