WIP add/remove peers from cli
This commit is contained in:
@@ -111,6 +111,81 @@ var showCmd = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
var printPeerStrCmd = &cobra.Command{
|
||||
Use: "peerstring",
|
||||
Short: "prints your own peerstring",
|
||||
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
|
||||
}
|
||||
|
||||
var result *string
|
||||
err = client.Call("Query.GetPeerString", 0, &result)
|
||||
fmt.Println(*result)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
var addPeerCmd = &cobra.Command{
|
||||
Use: "addpeer",
|
||||
Short: "add a peer",
|
||||
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
|
||||
}
|
||||
|
||||
peerString := args[0]
|
||||
|
||||
var success *bool
|
||||
np := rpc.NamespacePeer{ Namespace: "root", Peer: peerString }
|
||||
err = client.Call("Query.AddPeer", &np, &success)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(*success)
|
||||
},
|
||||
}
|
||||
|
||||
var removePeerCmd = &cobra.Command{
|
||||
Use: "removepeer",
|
||||
Short: "remove a peer",
|
||||
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
|
||||
}
|
||||
|
||||
peerString := args[0]
|
||||
|
||||
var success *bool
|
||||
np := rpc.NamespacePeer{ Namespace: "root", Peer: peerString }
|
||||
err = client.Call("Query.DeletePeer", &np, &success)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(*success)
|
||||
},
|
||||
}
|
||||
|
||||
var deleteCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Short: "delete a Password",
|
||||
@@ -150,6 +225,9 @@ func init() {
|
||||
rootCmd.AddCommand(generateCmd)
|
||||
rootCmd.AddCommand(showCmd)
|
||||
rootCmd.AddCommand(deleteCmd)
|
||||
rootCmd.AddCommand(addPeerCmd)
|
||||
rootCmd.AddCommand(removePeerCmd)
|
||||
rootCmd.AddCommand(printPeerStrCmd)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -76,12 +76,6 @@ func main() {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
Cfg, err := storage.NewConfig(*dbPath + "/config.json")
|
||||
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
storageHandler := storage.StorageHandler{
|
||||
Ctx: ctx ,
|
||||
Store: store,
|
||||
@@ -89,9 +83,16 @@ func main() {
|
||||
Ipfs: ipfs,
|
||||
PubSub: ps,
|
||||
Key: key,
|
||||
Config: Cfg,
|
||||
}
|
||||
|
||||
Cfg, err := storageHandler.NewConfig(*dbPath + "/config.json")
|
||||
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
|
||||
storageHandler.Config = Cfg
|
||||
storageHandler.ConfigPath = *dbPath + "/config.json"
|
||||
storageHandler.InitNamespaces()
|
||||
|
||||
for _, val := range storageHandler.Namespaces {
|
||||
|
||||
54
rpc/rpc.go
54
rpc/rpc.go
@@ -24,6 +24,11 @@ type NamespaceService struct {
|
||||
Service string
|
||||
}
|
||||
|
||||
type NamespacePeer struct {
|
||||
Namespace string
|
||||
Peer string
|
||||
}
|
||||
|
||||
func (t *Query) Generate(np *NamespaceService, reply *crypto.Password) error {
|
||||
val, ok := StorageHandler.Namespaces[np.Namespace]
|
||||
|
||||
@@ -74,6 +79,55 @@ func (t *Query) Get(np *NamespaceService, reply *crypto.Password) error {
|
||||
// return nil
|
||||
//}
|
||||
|
||||
func (t *Query) GetPeerString(_ *int, result *string) error {
|
||||
*result = StorageHandler.Host.ID().String() + "/" + StorageHandler.Key.Recipient().String()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Query) AddPeer(np *NamespacePeer, success *bool) error {
|
||||
namespace := np.Namespace
|
||||
val, ok := StorageHandler.Namespaces[namespace]
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("Namespace does not exist")
|
||||
}
|
||||
|
||||
peer, err := storage.PeerFromString(np.Peer)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing peer string: %s\n", err)
|
||||
*success = false
|
||||
return err
|
||||
}
|
||||
|
||||
val.AddPeer(peer)
|
||||
*success = true
|
||||
StorageHandler.UpdateConfig()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Query) DeletePeer(np *NamespacePeer, success *bool) error {
|
||||
namespace := np.Namespace
|
||||
val, ok := StorageHandler.Namespaces[namespace]
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("Namespace does not exist")
|
||||
}
|
||||
|
||||
peer, err := storage.PeerFromString(np.Peer)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing peer string: %s\n", err)
|
||||
*success = false
|
||||
return err
|
||||
}
|
||||
|
||||
val.RemovePeer(peer)
|
||||
*success = true
|
||||
StorageHandler.UpdateConfig()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Query) Delete(np *NamespaceService, success *bool) error {
|
||||
namespace := np.Namespace
|
||||
val, ok := StorageHandler.Namespaces[namespace]
|
||||
|
||||
@@ -2,12 +2,14 @@ package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"sync"
|
||||
"strings"
|
||||
|
||||
"github.com/ipfs/go-datastore/query"
|
||||
"github.com/libp2p/go-libp2p"
|
||||
@@ -22,6 +24,7 @@ import (
|
||||
ds "github.com/ipfs/go-datastore"
|
||||
ipfslite "github.com/hsanjuan/ipfs-lite"
|
||||
"github.com/libp2p/go-libp2p/core/network"
|
||||
"github.com/google/uuid"
|
||||
|
||||
multiaddr "github.com/multiformats/go-multiaddr"
|
||||
|
||||
@@ -38,7 +41,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
topicNameFlag = "afbjlask-23klaj2idalj2-ajl2kjd3i-2ldakjd4"
|
||||
topicNameFlag = "afbjlask-23klaj2idalj2-ajl2kjd3i-2ldakjd3"
|
||||
logger = logging.Logger("globaldb")
|
||||
Listen = libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0")
|
||||
)
|
||||
@@ -114,25 +117,6 @@ type NamespaceConfig struct {
|
||||
|
||||
type Config []NamespaceConfig
|
||||
|
||||
func NewConfig(filename string) ([]NamespaceConfig, error) {
|
||||
//fmt.Println("NewConfig Path not implemented yet")
|
||||
content, err := os.ReadFile(filename)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not read config file: %s", err)
|
||||
}
|
||||
|
||||
var result []NamespaceConfig
|
||||
err = json.Unmarshal(content, &result)
|
||||
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not parse config file: %s", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
|
||||
type WhitelistConnectionGater struct {
|
||||
whitelistedPeers map[peer.ID]struct{}
|
||||
@@ -194,6 +178,41 @@ type Namespace struct {
|
||||
TrustedPeers []Peer
|
||||
}
|
||||
|
||||
func PeerFromString(str string) (Peer, error) {
|
||||
parts := strings.Split(str, "/")
|
||||
|
||||
fmt.Println(str)
|
||||
fmt.Println(parts)
|
||||
if len(parts) != 2 {
|
||||
return Peer{}, fmt.Errorf("Invalid Peer String")
|
||||
}
|
||||
//TODO: validate each part
|
||||
|
||||
return Peer{ Id: parts[0], Key: parts[1] }, nil
|
||||
}
|
||||
|
||||
func (n *Namespace) AddPeer(peer Peer) {
|
||||
for _, CurrentPeer := range n.TrustedPeers {
|
||||
if CurrentPeer.Id == peer.Id && CurrentPeer.Key == peer.Key {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
n.TrustedPeers = append(n.TrustedPeers, peer)
|
||||
}
|
||||
|
||||
func (n *Namespace) RemovePeer(peer Peer) {
|
||||
var Peers []Peer
|
||||
for _, CurrentPeer := range n.TrustedPeers {
|
||||
if CurrentPeer.Id == peer.Id && CurrentPeer.Key == peer.Key {
|
||||
continue
|
||||
}
|
||||
|
||||
Peers = append(Peers, CurrentPeer)
|
||||
}
|
||||
|
||||
n.TrustedPeers = Peers
|
||||
}
|
||||
|
||||
func (n *Namespace) GetRecipients() []string {
|
||||
var result []string
|
||||
@@ -358,6 +377,74 @@ type StorageHandler struct {
|
||||
Key *agelib.X25519Identity
|
||||
Config []NamespaceConfig
|
||||
Namespaces map[string]*Namespace
|
||||
ConfigPath string
|
||||
}
|
||||
|
||||
func (s *StorageHandler) UpdateConfig() {
|
||||
s.recreateConfig()
|
||||
s.writeConfig(s.ConfigPath, s.Config)
|
||||
}
|
||||
|
||||
func (s *StorageHandler) recreateConfig() {
|
||||
for idx, namespaceConfig := range s.Config {
|
||||
s.Config[idx].Peers = s.Namespaces[namespaceConfig.Name].TrustedPeers
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StorageHandler) writeConfig(filename string, config []NamespaceConfig) error {
|
||||
jsonData, err := json.Marshal(config)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error during config initialization")
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
err = os.WriteFile(filename, jsonData, 0644)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error during config initialization")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StorageHandler) NewConfig(filename string) ([]NamespaceConfig, error) {
|
||||
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
|
||||
err := s.writeConfig(filename, []NamespaceConfig{
|
||||
{
|
||||
Name: "root",
|
||||
Id: uuid.New().String(),
|
||||
Peers: []Peer{
|
||||
{
|
||||
Id: s.Host.ID().String(),
|
||||
Key: s.Key.Recipient().String(),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not create config file: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not read config file: %s", err)
|
||||
}
|
||||
|
||||
var result []NamespaceConfig
|
||||
err = json.Unmarshal(content, &result)
|
||||
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Could not parse config file: %s", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *StorageHandler) GetDefaultNamespace(Name string) *Namespace {
|
||||
|
||||
Reference in New Issue
Block a user