WIP add/remove peers from cli
This commit is contained in:
@@ -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