300 lines
5.6 KiB
Go
300 lines
5.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
ipfslite "github.com/hsanjuan/ipfs-lite"
|
|
badger "github.com/ipfs/go-ds-badger2"
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
"github.com/k4lipso/pentapass/storage"
|
|
"github.com/k4lipso/pentapass/crypto/age"
|
|
"github.com/k4lipso/pentapass/crypto"
|
|
)
|
|
|
|
var (
|
|
topicNameFlag = flag.String("topicName", "akdjlask-23klaj2idalj2-ajl2kjd3i-2ldakjd2", "name of topic to join")
|
|
dbPath = flag.String("db", "./db", "db file path")
|
|
nameSpace = flag.String("namespace", "crdt", "namespace")
|
|
logger = logging.Logger("globaldb")
|
|
// topicName = "globaldb-example"
|
|
// netTopic = "globaldb-example-net"
|
|
// config = "globaldb-example"
|
|
)
|
|
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
ctx := context.Background()
|
|
data := *dbPath
|
|
|
|
key, err := age.LoadOrGenerateKeys(*dbPath + "/age.key")
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Printf("AgeKey: %s\n", key.String())
|
|
fmt.Printf("AgePublicKey: %s\n", key.Recipient().String())
|
|
|
|
//cipher, err := age.Encrypt([]byte("Test Message"), []string{key.Recipient().String()})
|
|
//fmt.Printf("Encrypted: %s\n", cipher)
|
|
//decrypted, err := age.Decrypt(cipher, key)
|
|
//fmt.Printf("Decrypted: %s\n", decrypted)
|
|
|
|
h, dht, err := storage.SetupLibp2pHost(ctx, *dbPath)
|
|
|
|
pid := h.ID().String()
|
|
fmt.Println(h.ID().String())
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
ps, err := pubsub.NewGossipSub(ctx, h)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
//topic, err := ps.Join(*topicNameFlag)
|
|
//if err != nil {
|
|
// panic(err)
|
|
//}
|
|
|
|
go storage.DiscoverPeers(ctx, h, dht)
|
|
|
|
store, err := badger.NewDatastore(data, &badger.DefaultOptions)
|
|
if err != nil {
|
|
logger.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
|
|
ipfs, err := ipfslite.New(ctx, store, nil, h, dht, nil)
|
|
if err != nil {
|
|
logger.Fatal(err)
|
|
}
|
|
|
|
Cfg, err := storage.NewConfig(*dbPath + "/config.json")
|
|
|
|
if err != nil {
|
|
logger.Fatal(err)
|
|
}
|
|
|
|
storageHandler := storage.StorageHandler{
|
|
Ctx: ctx ,
|
|
Store: store,
|
|
Host: h,
|
|
Ipfs: ipfs,
|
|
PubSub: ps,
|
|
Key: key,
|
|
Config: Cfg,
|
|
}
|
|
|
|
storageHandler.InitNamespaces()
|
|
|
|
for _, val := range storageHandler.Namespaces {
|
|
defer val.Close()
|
|
}
|
|
|
|
fmt.Printf(`
|
|
Peer ID: %s
|
|
Listen address: %s
|
|
Topic: %s
|
|
Data Folder: %s
|
|
|
|
Ready!
|
|
|
|
Commands:
|
|
|
|
> list -> list items in the store
|
|
> get <key> -> get value for a key
|
|
> put <key> <value> -> store value on a key
|
|
> exit -> quit
|
|
|
|
|
|
`,
|
|
pid, storage.Listen, *topicNameFlag, data,
|
|
)
|
|
|
|
if len(os.Args) > 1 && os.Args[1] == "daemon" {
|
|
fmt.Println("Running in daemon mode")
|
|
go func() {
|
|
for {
|
|
fmt.Printf("%s - %d connected peers\n", time.Now().Format(time.Stamp), len(storage.ConnectedPeers(h)))
|
|
time.Sleep(10 * time.Second)
|
|
}
|
|
}()
|
|
signalChan := make(chan os.Signal, 20)
|
|
signal.Notify(
|
|
signalChan,
|
|
syscall.SIGINT,
|
|
syscall.SIGTERM,
|
|
syscall.SIGHUP,
|
|
)
|
|
<-signalChan
|
|
return
|
|
}
|
|
|
|
fmt.Printf("> ")
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
for scanner.Scan() {
|
|
text := scanner.Text()
|
|
fields := strings.Fields(text)
|
|
if len(fields) == 0 {
|
|
fmt.Printf("> ")
|
|
continue
|
|
}
|
|
|
|
cmd := fields[0]
|
|
|
|
switch cmd {
|
|
case "exit", "quit":
|
|
return
|
|
case "debug":
|
|
if len(fields) < 2 {
|
|
fmt.Println("debug <on/off/peers>")
|
|
}
|
|
st := fields[1]
|
|
switch st {
|
|
case "on":
|
|
logging.SetLogLevel("globaldb", "debug")
|
|
case "off":
|
|
logging.SetLogLevel("globaldb", "error")
|
|
case "peers":
|
|
for _, p := range storage.ConnectedPeers(h) {
|
|
addrs, err := peer.AddrInfoToP2pAddrs(p)
|
|
if err != nil {
|
|
logger.Warn(err)
|
|
continue
|
|
}
|
|
for _, a := range addrs {
|
|
fmt.Println(a)
|
|
}
|
|
}
|
|
}
|
|
case "list":
|
|
if len(fields) < 2 {
|
|
fmt.Printf("Available Namespaces:\n")
|
|
for k := range storageHandler.Namespaces {
|
|
fmt.Printf("%s\n", k)
|
|
}
|
|
continue
|
|
}
|
|
|
|
namespace := fields[1]
|
|
|
|
fmt.Printf("Listing content of %s", namespace)
|
|
|
|
val, ok := storageHandler.Namespaces[namespace]
|
|
|
|
if !ok {
|
|
fmt.Println("Namespace does not exist")
|
|
continue
|
|
}
|
|
|
|
val.List()
|
|
case "get":
|
|
if len(fields) < 3 {
|
|
fmt.Println("get <namespace> <key>")
|
|
fmt.Println("> ")
|
|
continue
|
|
}
|
|
|
|
namespace := fields[1]
|
|
|
|
val, ok := storageHandler.Namespaces[namespace]
|
|
|
|
if !ok {
|
|
fmt.Println("Namespace does not exist")
|
|
continue
|
|
}
|
|
|
|
k := fields[2]
|
|
v, err := val.Get(k)
|
|
if err != nil {
|
|
printErr(err)
|
|
continue
|
|
}
|
|
|
|
fmt.Printf("[%s] -> %s\n", k, string(v))
|
|
case "generate":
|
|
if len(fields) < 3 {
|
|
fmt.Println("generate <namespace> <Service>")
|
|
fmt.Println("> ")
|
|
continue
|
|
}
|
|
|
|
namespace := fields[1]
|
|
|
|
val, ok := storageHandler.Namespaces[namespace]
|
|
|
|
if !ok {
|
|
fmt.Println("Namespace does not exist")
|
|
continue
|
|
}
|
|
|
|
service := fields[2]
|
|
password := crypto.NewPassword()
|
|
password.Service = service
|
|
|
|
data, err := password.ToJson()
|
|
if err != nil {
|
|
printErr(err)
|
|
continue
|
|
}
|
|
|
|
encryptedPassword, err := age.Encrypt(data, val.GetRecipients())
|
|
if err != nil {
|
|
printErr(err)
|
|
continue
|
|
}
|
|
|
|
err = val.Put(password.Id.String(), string(encryptedPassword))
|
|
if err != nil {
|
|
printErr(err)
|
|
continue
|
|
}
|
|
case "put":
|
|
if len(fields) < 4 {
|
|
fmt.Println("put <namespace> <key> <value>")
|
|
fmt.Println("> ")
|
|
continue
|
|
}
|
|
|
|
namespace := fields[1]
|
|
|
|
val, ok := storageHandler.Namespaces[namespace]
|
|
|
|
if !ok {
|
|
fmt.Println("Namespace does not exist")
|
|
continue
|
|
}
|
|
|
|
|
|
k := fields[2]
|
|
v := strings.Join(fields[3:], " ")
|
|
err := val.Put(k, v)
|
|
if err != nil {
|
|
printErr(err)
|
|
continue
|
|
}
|
|
}
|
|
fmt.Printf("> ")
|
|
}
|
|
}
|
|
|
|
func printErr(err error) {
|
|
fmt.Println("error:", err)
|
|
fmt.Println("> ")
|
|
}
|