2018-04-30 21:47:21 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2018-06-20 18:41:36 +00:00
|
|
|
"cwtch.im/cwtch/connectivity/tor"
|
2018-05-28 18:05:06 +00:00
|
|
|
"cwtch.im/cwtch/peer"
|
2018-07-01 18:43:05 +00:00
|
|
|
"fmt"
|
2018-05-01 20:44:45 +00:00
|
|
|
"log"
|
2018-07-01 18:43:05 +00:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
2018-04-30 21:47:21 +00:00
|
|
|
)
|
|
|
|
|
2018-06-19 22:28:44 +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
|
2018-04-30 21:47:21 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 22:28:44 +00:00
|
|
|
// NewProfile creates a new cwtchPeer with a given name.
|
2018-06-23 23:39:15 +00:00
|
|
|
func (app *Application) NewProfile(name string, filename string, password string) error {
|
|
|
|
profile := peer.NewCwtchPeer(name, password)
|
2018-04-30 21:47:21 +00:00
|
|
|
app.Peer = profile
|
2018-05-01 20:44:45 +00:00
|
|
|
err := profile.Save(filename)
|
|
|
|
if err == nil {
|
2018-06-20 18:41:36 +00:00
|
|
|
|
2018-07-01 18:43:05 +00:00
|
|
|
err := app.startTor()
|
2018-06-20 18:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-01 20:44:45 +00:00
|
|
|
go func() {
|
|
|
|
err := app.Peer.Listen()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return err
|
2018-04-30 21:47:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 18:43:05 +00:00
|
|
|
func (app *Application) startTor() error {
|
|
|
|
|
|
|
|
// Creating a local cwtch tor server config for the user
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// creating /home/<usr>/.cwtch/torrc file
|
|
|
|
// SOCKSPort socksPort
|
|
|
|
// ControlPort controlPort
|
|
|
|
torrc := path.Join(usr.HomeDir, ".cwtch", "torrc")
|
|
|
|
if _, err := os.Stat(torrc); os.IsNotExist(err) {
|
|
|
|
|
|
|
|
os.MkdirAll(path.Join(usr.HomeDir, ".cwtch"), 0700)
|
|
|
|
|
|
|
|
file, err := os.Create(torrc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(file, "SOCKSPort %d\nControlPort %d\n", 9050, 9051)
|
|
|
|
file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
tm, err := tor.NewTorManager(9050, 9051, torrc)
|
|
|
|
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.
|
2018-06-24 18:37:11 +00:00
|
|
|
func (app *Application) SetProfile(filename string, password string) error {
|
2018-06-23 23:39:15 +00:00
|
|
|
profile, err := peer.LoadCwtchPeer(filename, password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-30 21:47:21 +00:00
|
|
|
app.Peer = profile
|
|
|
|
if err == nil {
|
2018-06-20 18:41:36 +00:00
|
|
|
|
2018-07-01 18:43:05 +00:00
|
|
|
err := app.startTor()
|
2018-06-20 18:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-01 20:44:45 +00:00
|
|
|
go func() {
|
|
|
|
err := app.Peer.Listen()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2018-04-30 21:47:21 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|