2019-05-15 19:45:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"cwtch.im/tapir"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/identity"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
|
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
|
|
|
|
"golang.org/x/crypto/ed25519"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-05-21 18:28:10 +00:00
|
|
|
// SimpleApp is a trivial implementation of a basic p2p application
|
2019-05-15 19:45:45 +00:00
|
|
|
type SimpleApp struct {
|
|
|
|
tapir.AuthApp
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInstance should always return a new instantiation of the application.
|
|
|
|
func (ea SimpleApp) NewInstance() tapir.Application {
|
|
|
|
return new(SimpleApp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init is run when the connection is first started.
|
|
|
|
func (ea SimpleApp) Init(connection *tapir.Connection) {
|
|
|
|
// First run the Authentication App
|
|
|
|
ea.AuthApp.Init(connection)
|
|
|
|
|
2019-05-21 18:28:10 +00:00
|
|
|
if connection.HasCapability(tapir.AuthCapability) {
|
2019-05-15 19:45:45 +00:00
|
|
|
// The code for out simple application (We just send and receive "Hello"
|
|
|
|
connection.Send([]byte("Hello"))
|
|
|
|
message := connection.Expect()
|
|
|
|
log.Infof("Received: %q", message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
log.SetLevel(log.LevelDebug)
|
|
|
|
|
|
|
|
// Connect to Tor
|
|
|
|
var acn connectivity.ACN
|
|
|
|
acn, _ = connectivity.StartTor("./", "")
|
|
|
|
acn.WaitTillBootstrapped()
|
|
|
|
|
|
|
|
// Generate Server Keys
|
|
|
|
pubkey, privateKey, _ := ed25519.GenerateKey(rand.Reader)
|
|
|
|
sk := ed25519.PrivateKey(privateKey)
|
|
|
|
pk := ed25519.PublicKey(pubkey)
|
|
|
|
id := identity.InitializeV3("server", &sk, &pk)
|
|
|
|
|
2019-06-05 18:44:26 +00:00
|
|
|
// Init a Client to Connect to the Server
|
2019-05-15 19:45:45 +00:00
|
|
|
go client(acn, pubkey)
|
|
|
|
|
2019-06-05 18:44:26 +00:00
|
|
|
// Init the Server running the Simple App.
|
2019-05-15 19:45:45 +00:00
|
|
|
var service tapir.Service
|
|
|
|
service = new(tapir.BaseOnionService)
|
2019-06-05 18:44:26 +00:00
|
|
|
service.Init(acn, sk, id)
|
2019-05-15 19:45:45 +00:00
|
|
|
service.Listen(SimpleApp{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client will Connect and launch it's own Echo App goroutine.
|
|
|
|
func client(acn connectivity.ACN, key ed25519.PublicKey) {
|
|
|
|
pubkey, privateKey, _ := ed25519.GenerateKey(rand.Reader)
|
|
|
|
sk := ed25519.PrivateKey(privateKey)
|
|
|
|
pk := ed25519.PublicKey(pubkey)
|
|
|
|
id := identity.InitializeV3("client", &sk, &pk)
|
|
|
|
var client tapir.Service
|
|
|
|
client = new(tapir.BaseOnionService)
|
2019-06-05 18:44:26 +00:00
|
|
|
client.Init(acn, sk, id)
|
2019-05-21 18:28:49 +00:00
|
|
|
cid, _ := client.Connect(utils.GetTorV3Hostname(key), SimpleApp{})
|
2019-05-15 19:45:45 +00:00
|
|
|
|
|
|
|
// Once connected, it shouldn't take long to authenticate and run the application. So for the purposes of this demo
|
|
|
|
// we will wait a little while then exit.
|
|
|
|
time.Sleep(time.Second * 5)
|
2019-05-21 18:28:10 +00:00
|
|
|
|
2019-05-21 18:28:49 +00:00
|
|
|
conn, _ := client.GetConnection(cid)
|
2019-05-21 18:28:10 +00:00
|
|
|
log.Debugf("Client has Auth: %v", conn.HasCapability(tapir.AuthCapability))
|
|
|
|
|
2019-05-15 19:45:45 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|