399 lines
7.9 KiB
Go
399 lines
7.9 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
|
|
logging "github.com/ipfs/go-log/v2"
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/k4lipso/pentapass/rpc"
|
|
"github.com/k4lipso/pentapass/crypto"
|
|
)
|
|
|
|
var (
|
|
dbPath = flag.String("db", "./db", "db file path")
|
|
logger = logging.Logger("globaldb")
|
|
// topicName = "globaldb-example"
|
|
// netTopic = "globaldb-example-net"
|
|
// config = "globaldb-example"
|
|
)
|
|
|
|
// Create the root command
|
|
var rootCmd = &cobra.Command{
|
|
Use: "ppass",
|
|
Short: "Interact with the Password Store",
|
|
}
|
|
|
|
// Create the 'list' subcommand
|
|
var listCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List all passwords",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
//all, _ := cmd.Flags().GetBool("all")
|
|
dbPath, _ := cmd.Flags().GetString("db")
|
|
|
|
client, err := rpc.Receive(dbPath)
|
|
|
|
if err != nil {
|
|
fmt.Printf("dialing: %s\n", err)
|
|
return
|
|
}
|
|
|
|
var names []string
|
|
namespace := "root"
|
|
err = client.Call("Query.GetAllNames", &namespace, &names)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
for _, name := range names {
|
|
fmt.Println(name)
|
|
}
|
|
|
|
},
|
|
}
|
|
|
|
var generateCmd = &cobra.Command{
|
|
Use: "generate",
|
|
Short: "Generate a Password",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
dbPath, _ := cmd.Flags().GetString("db")
|
|
client, err := rpc.Receive(dbPath)
|
|
|
|
if err != nil {
|
|
fmt.Printf("dialing: %s\n", err)
|
|
return
|
|
}
|
|
|
|
serviceName := args[0]
|
|
|
|
var password *crypto.Password
|
|
np := rpc.NamespaceService{ Namespace: "root", Service: serviceName }
|
|
err = client.Call("Query.Generate", &np, &password)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
fmt.Println(*password)
|
|
},
|
|
}
|
|
|
|
var showCmd = &cobra.Command{
|
|
Use: "show",
|
|
Short: "show a Password",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
dbPath, _ := cmd.Flags().GetString("db")
|
|
client, err := rpc.Receive(dbPath)
|
|
|
|
if err != nil {
|
|
fmt.Printf("dialing: %s\n", err)
|
|
return
|
|
}
|
|
|
|
serviceName := args[0]
|
|
|
|
var password *crypto.Password
|
|
np := rpc.NamespaceService{ Namespace: "root", Service: serviceName }
|
|
err = client.Call("Query.Get", &np, &password)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
fmt.Println(password.Password)
|
|
},
|
|
}
|
|
|
|
var deleteCmd = &cobra.Command{
|
|
Use: "delete",
|
|
Short: "delete a Password",
|
|
Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
dbPath, _ := cmd.Flags().GetString("db")
|
|
client, err := rpc.Receive(dbPath)
|
|
|
|
if err != nil {
|
|
fmt.Printf("dialing: %s\n", err)
|
|
return
|
|
}
|
|
|
|
serviceName := args[0]
|
|
|
|
var success *bool
|
|
np := rpc.NamespaceService{ Namespace: "root", Service: serviceName }
|
|
err = client.Call("Query.Delete", &np, &success)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
fmt.Println(*success)
|
|
},
|
|
}
|
|
|
|
|
|
func init() {
|
|
// Add flags to the 'list' command
|
|
//listCmd.Flags().BoolP("all", "a", false, "List all items")
|
|
rootCmd.PersistentFlags().String("db", "", "db path")
|
|
rootCmd.MarkPersistentFlagRequired("db")
|
|
|
|
// Add subcommands to the root command
|
|
rootCmd.AddCommand(listCmd)
|
|
rootCmd.AddCommand(generateCmd)
|
|
rootCmd.AddCommand(showCmd)
|
|
rootCmd.AddCommand(deleteCmd)
|
|
}
|
|
|
|
func main() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
//flag.Parse()
|
|
|
|
//client, err := rpc.Receive(*dbPath)
|
|
|
|
//if err != nil {
|
|
// fmt.Printf("dialing: %s\n", err)
|
|
// return
|
|
//}
|
|
|
|
//var names []string
|
|
//namespace := "root"
|
|
//err = client.Call("Query.GetAllNames", &namespace, &names)
|
|
|
|
//if err != nil {
|
|
// fmt.Println(err)
|
|
//}
|
|
|
|
//fmt.Println(names)
|
|
|
|
//var password *crypto.Password
|
|
//np := rpc.NamespaceService{ Namespace: "root", Service: "Test" }
|
|
//err = client.Call("Query.Generate", &np, &password)
|
|
|
|
//if err != nil {
|
|
// fmt.Println(err)
|
|
//}
|
|
|
|
//fmt.Println(*password)
|
|
|
|
//var success bool
|
|
//err = client.Call("Query.Delete", &np, &success)
|
|
|
|
//if success == true {
|
|
// fmt.Println("Deleted Test")
|
|
//}
|
|
|
|
//var password2 *crypto.Password
|
|
//err = client.Call("Query.Get", &np, &password2)
|
|
|
|
//if err != nil {
|
|
// fmt.Println(err)
|
|
// return
|
|
//}
|
|
|
|
//fmt.Println(*password2)
|
|
|
|
// 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))
|
|
// err = val.Put(password.Service, 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("> ")
|
|
}
|