cwtch/app/app.go

99 lines
2.6 KiB
Go
Raw Normal View History

2018-04-30 21:47:21 +00:00
package app
import (
"cwtch.im/cwtch/connectivity/tor"
2018-05-28 18:05:06 +00:00
"cwtch.im/cwtch/peer"
"errors"
2018-07-01 18:43:05 +00:00
"fmt"
"log"
2018-07-01 18:43:05 +00:00
"os"
"path"
2018-04-30 21:47:21 +00:00
)
// Application is a facade over a cwtchPeer that provides some wrapping logic.
2018-04-30 21:47:21 +00:00
type Application struct {
2018-06-29 19:04:52 +00:00
Peer peer.CwtchPeerInterface
TorManager *tor.Manager
directory string
}
// NewApp creates a new app with some environment awareness and initializes a Tor Manager
func NewApp(appDirectory string, torPath string) (*Application, error) {
log.Printf("NewApp(%v, %v)\n", appDirectory, torPath)
app := &Application{Peer: nil, directory: appDirectory}
os.MkdirAll(path.Join(appDirectory, "tor"), 0700)
err := app.startTor(torPath)
if err != nil {
return nil, err
}
return app, nil
2018-04-30 21:47:21 +00:00
}
// NewProfile creates a new cwtchPeer with a given name.
func (app *Application) NewProfile(name string, password string) error {
log.Printf("NewProfile(%v, %v)\n", name, password)
if app.Peer != nil {
return errors.New("Profile already created")
}
app.Peer = peer.NewCwtchPeer(name, password, path.Join(app.directory, name+".json"))
err := app.Peer.Save()
if err == nil {
err = app.startPeer()
}
return err
2018-04-30 21:47:21 +00:00
}
// startTor will create a local torrc if needed
func (app *Application) startTor(torPath string) error {
2018-07-01 18:43:05 +00:00
// Creating a local cwtch tor server config for the user
// creating $app.directory/torrc file
2018-07-01 18:43:05 +00:00
// SOCKSPort socksPort
// ControlPort controlPort
torrc := path.Join(app.directory, "tor", "torrc")
2018-07-01 18:43:05 +00:00
if _, err := os.Stat(torrc); os.IsNotExist(err) {
log.Printf("writing torrc to: %v\n", torrc)
2018-07-01 18:43:05 +00:00
file, err := os.Create(torrc)
if err != nil {
return err
}
fmt.Fprintf(file, "SOCKSPort %d\nControlPort %d\nCookieAuthentication 0\nSafeSocks 1\n", 9050, 9051)
2018-07-01 18:43:05 +00:00
file.Close()
}
tm, err := tor.NewTorManager(9050, 9051, torPath, torrc)
2018-07-01 18:43:05 +00:00
if err != nil {
return err
}
app.TorManager = tm
return nil
}
2018-05-16 21:11:04 +00:00
// SetProfile loads an existing profile from the given filename.
func (app *Application) SetProfile(filename string, password string) error {
2018-08-05 16:01:00 +00:00
if app.Peer == nil {
profile, err := peer.LoadCwtchPeer(path.Join(app.directory, filename), password)
if err != nil {
return err
}
app.Peer = profile
return app.startPeer()
}
2018-08-05 16:01:00 +00:00
return errors.New("profile is already loaded, to load a different profile you will need to restart the application")
}
func (app *Application) startPeer() error {
go func() {
e := app.Peer.Listen()
if e != nil {
log.Panic(e)
}
}()
return nil
2018-04-30 21:47:21 +00:00
}
2018-05-16 21:11:55 +00:00
// PeerRequest attempts to setup peer relationship with the given onion address.`
2018-04-30 21:47:21 +00:00
func (app *Application) PeerRequest(onion string) {
app.Peer.PeerWithOnion(onion)
}