feat: persistent multi-share configuration (shares.json + localStorage)
Daemon side: - internal/shares: new package reads/writes shares.json next to identity - proto: add ShareEntry type, add_share/remove_share/list_shares IPC commands, shares_list event, path field on FileEntry - netmgr: load shares.json on startup; ScanAllShares combines legacy ShareDir with shares.json entries (recursive walk with relative paths) - mesh: ScanFiles callback lets manager inject multi-share scanning without mesh knowing about shares.json - ipc: handle add_share, remove_share, list_shares commands Browser side: - ShareManager component replaces FolderPicker: shows list of named shares with remove/re-pick buttons; persists share records to waste_shares in localStorage (name, global flag, networkId) - FolderPicker retained for internal use; Sidebar now uses ShareManager Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
||||
"github.com/waste-go/internal/invite"
|
||||
"github.com/waste-go/internal/netmgr"
|
||||
"github.com/waste-go/internal/proto"
|
||||
"github.com/waste-go/internal/shares"
|
||||
)
|
||||
|
||||
// RunWS starts a WebSocket IPC server on 127.0.0.1:wsPort.
|
||||
@@ -246,7 +247,7 @@ func handleClient(conn net.Conn, mgr *netmgr.Manager) {
|
||||
Type: proto.EvtFileList,
|
||||
NetworkID: n.ID,
|
||||
PeerID: ptr(n.Identity.PeerID()),
|
||||
Files: n.Mesh.ScanShareDir(),
|
||||
Files: mgr.ScanAllShares(n.ID),
|
||||
})
|
||||
} else {
|
||||
req, err := json.Marshal(proto.PeerMessage{Type: proto.MsgFileListReq})
|
||||
@@ -258,6 +259,35 @@ func handleClient(conn net.Conn, mgr *netmgr.Manager) {
|
||||
}
|
||||
}
|
||||
|
||||
case proto.CmdAddShare:
|
||||
if cmd.Path == "" {
|
||||
send(errMsg("add_share: path is required"))
|
||||
continue
|
||||
}
|
||||
networks := cmd.ShareNetworks
|
||||
if len(networks) == 0 {
|
||||
networks = []string{"*"}
|
||||
}
|
||||
if err := mgr.Shares.Add(shares.Share{Path: cmd.Path, Networks: networks}); err != nil {
|
||||
send(errMsg(fmt.Sprintf("add_share: %v", err)))
|
||||
continue
|
||||
}
|
||||
send(sharesListMsg(mgr))
|
||||
|
||||
case proto.CmdRemoveShare:
|
||||
if cmd.Path == "" {
|
||||
send(errMsg("remove_share: path is required"))
|
||||
continue
|
||||
}
|
||||
if err := mgr.Shares.Remove(cmd.Path); err != nil {
|
||||
send(errMsg(fmt.Sprintf("remove_share: %v", err)))
|
||||
continue
|
||||
}
|
||||
send(sharesListMsg(mgr))
|
||||
|
||||
case proto.CmdListShares:
|
||||
send(sharesListMsg(mgr))
|
||||
|
||||
case proto.CmdGenerateInvite:
|
||||
n := mgr.Resolve(cmd.NetworkID)
|
||||
if n == nil {
|
||||
@@ -389,6 +419,18 @@ func errMsg(s string) proto.IpcMessage {
|
||||
return proto.IpcMessage{Type: proto.EvtError, ErrorMessage: s}
|
||||
}
|
||||
|
||||
func sharesListMsg(mgr *netmgr.Manager) proto.IpcMessage {
|
||||
all := mgr.Shares.All()
|
||||
entries := make([]proto.ShareEntry, len(all))
|
||||
for i, sh := range all {
|
||||
entries[i] = proto.ShareEntry{Path: sh.Path, Networks: sh.Networks}
|
||||
}
|
||||
return proto.IpcMessage{Type: proto.EvtSharesList, Shares: entries}
|
||||
}
|
||||
|
||||
// ensure shares import is used
|
||||
var _ = shares.Share{}
|
||||
|
||||
func ptr[T any](v T) *T { return &v }
|
||||
|
||||
func randomHex(n int) string {
|
||||
|
||||
@@ -29,6 +29,9 @@ type Mesh struct {
|
||||
Store *store.Store // may be nil if persistence is disabled
|
||||
ShareDir string // directory whose contents are shared with peers; "" = no sharing
|
||||
DownloadDir string // directory where received files are saved
|
||||
// ScanFiles overrides ScanShareDir when set — allows the manager to inject
|
||||
// multi-share scanning without the mesh needing to know about shares.json.
|
||||
ScanFiles func() []proto.FileEntry
|
||||
|
||||
mu sync.RWMutex
|
||||
peers map[proto.PeerID]*PeerConn
|
||||
|
||||
@@ -201,7 +201,12 @@ func dispatchPeerMessage(msg proto.PeerMessage, from proto.PeerID, m *Mesh) {
|
||||
m.Emit(proto.IpcMessage{Type: proto.EvtMessageReceived, Message: chat})
|
||||
|
||||
case proto.MsgFileListReq:
|
||||
files := m.ScanShareDir()
|
||||
var files []proto.FileEntry
|
||||
if m.ScanFiles != nil {
|
||||
files = m.ScanFiles()
|
||||
} else {
|
||||
files = m.ScanShareDir()
|
||||
}
|
||||
resp, err := json.Marshal(proto.PeerMessage{
|
||||
Type: proto.MsgFileListResp,
|
||||
FileListResp: &proto.FileListResp{Files: files},
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
"github.com/waste-go/internal/crypto"
|
||||
"github.com/waste-go/internal/mesh"
|
||||
"github.com/waste-go/internal/proto"
|
||||
"github.com/waste-go/internal/shares"
|
||||
"github.com/waste-go/internal/store"
|
||||
)
|
||||
|
||||
@@ -54,13 +56,22 @@ type Manager struct {
|
||||
|
||||
subsMu sync.Mutex
|
||||
subs []chan proto.IpcMessage
|
||||
|
||||
Shares *shares.Store // persistent multi-share configuration
|
||||
}
|
||||
|
||||
// New creates a Manager from the given config.
|
||||
// Loads shares.json from StoreDir if it exists.
|
||||
func New(cfg Config) *Manager {
|
||||
sh, err := shares.Load(cfg.StoreDir)
|
||||
if err != nil {
|
||||
log.Printf("netmgr: loading shares.json: %v (starting empty)", err)
|
||||
sh, _ = shares.Load("") // fallback to empty
|
||||
}
|
||||
return &Manager{
|
||||
networks: make(map[string]*Network),
|
||||
cfg: cfg,
|
||||
Shares: sh,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,6 +111,8 @@ func (mgr *Manager) Join(name, shareDir string) (string, error) {
|
||||
m.ShareDir = mgr.cfg.ShareDir
|
||||
}
|
||||
m.DownloadDir = filepath.Join(mgr.cfg.StoreDir, "downloads-"+netID_full)
|
||||
capturedNetID := netID
|
||||
m.ScanFiles = func() []proto.FileEntry { return mgr.ScanAllShares(capturedNetID) }
|
||||
|
||||
// Forward all mesh events to the Manager's fan-out, tagging with network_id.
|
||||
meshEvents := m.Subscribe()
|
||||
@@ -183,6 +196,8 @@ func (mgr *Manager) JoinByHash(netHash64, shareDir string) (string, error) {
|
||||
m.ShareDir = mgr.cfg.ShareDir
|
||||
}
|
||||
m.DownloadDir = filepath.Join(mgr.cfg.StoreDir, "downloads-"+netID)
|
||||
capturedNetID2 := netID
|
||||
m.ScanFiles = func() []proto.FileEntry { return mgr.ScanAllShares(capturedNetID2) }
|
||||
|
||||
meshEvents := m.Subscribe()
|
||||
go func() {
|
||||
@@ -313,6 +328,50 @@ func (mgr *Manager) All() []*Network {
|
||||
return out
|
||||
}
|
||||
|
||||
// ScanAllShares returns file entries from all share roots visible to networkID,
|
||||
// combining the network's legacy ShareDir with entries from shares.json.
|
||||
func (mgr *Manager) ScanAllShares(netID string) []proto.FileEntry {
|
||||
var files []proto.FileEntry
|
||||
|
||||
// Legacy single-dir share from the network mesh.
|
||||
if n := mgr.Resolve(netID); n != nil {
|
||||
files = append(files, n.Mesh.ScanShareDir()...)
|
||||
}
|
||||
|
||||
// Additional shares from shares.json.
|
||||
if mgr.Shares != nil {
|
||||
for _, sh := range mgr.Shares.ForNetwork(netID) {
|
||||
files = append(files, scanDir(sh.Path)...)
|
||||
}
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
// scanDir recursively walks a directory and returns FileEntry for each file.
|
||||
func scanDir(root string) []proto.FileEntry {
|
||||
var files []proto.FileEntry
|
||||
_ = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
info, err := d.Info()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
rel, err := filepath.Rel(root, path)
|
||||
if err != nil {
|
||||
rel = d.Name()
|
||||
}
|
||||
files = append(files, proto.FileEntry{
|
||||
Name: d.Name(),
|
||||
SizeBytes: info.Size(),
|
||||
Path: rel,
|
||||
})
|
||||
return nil
|
||||
})
|
||||
return files
|
||||
}
|
||||
|
||||
// AnchorURL returns the configured anchor URL.
|
||||
func (mgr *Manager) AnchorURL() string { return mgr.cfg.AnchorURL }
|
||||
|
||||
|
||||
@@ -111,6 +111,13 @@ type GossipEntry struct {
|
||||
type FileEntry struct {
|
||||
Name string `json:"name"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Path string `json:"path,omitempty"` // relative path including filename
|
||||
}
|
||||
|
||||
// ShareEntry describes one persistent share root.
|
||||
type ShareEntry struct {
|
||||
Path string `json:"path"`
|
||||
Networks []string `json:"networks"` // ["*"] = global
|
||||
}
|
||||
|
||||
// FileListResp is the payload for MsgFileListResp.
|
||||
@@ -222,6 +229,9 @@ const (
|
||||
CmdGetFileList IpcMsgType = "get_file_list"
|
||||
CmdExportIdentity IpcMsgType = "export_identity" // returns encrypted backup blob
|
||||
CmdImportIdentity IpcMsgType = "import_identity" // replaces identity from backup blob
|
||||
CmdAddShare IpcMsgType = "add_share" // add a share root; fields: path, networks
|
||||
CmdRemoveShare IpcMsgType = "remove_share" // remove a share root; field: path
|
||||
CmdListShares IpcMsgType = "list_shares" // returns shares_list event
|
||||
|
||||
// Events (daemon → UI)
|
||||
EvtMessageReceived IpcMsgType = "message_received"
|
||||
@@ -240,6 +250,7 @@ const (
|
||||
EvtNetworkLeft IpcMsgType = "network_left"
|
||||
EvtIdentityExported IpcMsgType = "identity_exported"
|
||||
EvtIdentityImported IpcMsgType = "identity_imported"
|
||||
EvtSharesList IpcMsgType = "shares_list"
|
||||
)
|
||||
|
||||
// NetworkInfo summarises one joined network for state_snapshot and network_joined events.
|
||||
@@ -292,6 +303,8 @@ type IpcMessage struct {
|
||||
ErrorMessage string `json:"error_message,omitempty"`
|
||||
InviteString string `json:"invite,omitempty"`
|
||||
Files []FileEntry `json:"files,omitempty"`
|
||||
Shares []ShareEntry `json:"shares,omitempty"`
|
||||
ShareNetworks []string `json:"networks,omitempty"` // for add_share command
|
||||
// export_identity / import_identity
|
||||
Passphrase string `json:"passphrase,omitempty"` // import only; never echoed back
|
||||
Backup string `json:"backup,omitempty"` // JSON backup blob
|
||||
|
||||
99
internal/shares/shares.go
Normal file
99
internal/shares/shares.go
Normal file
@@ -0,0 +1,99 @@
|
||||
// Package shares manages the persistent multi-share configuration stored in shares.json.
|
||||
// Shares are additive to the existing single-dir ShareDir mechanism.
|
||||
package shares
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Share describes one shared directory entry.
|
||||
type Share struct {
|
||||
Path string `json:"path"`
|
||||
Networks []string `json:"networks"` // ["*"] = global (all networks); otherwise list of network IDs
|
||||
}
|
||||
|
||||
// Store manages the shares.json file.
|
||||
type Store struct {
|
||||
mu sync.RWMutex
|
||||
path string
|
||||
shares []Share
|
||||
}
|
||||
|
||||
// Load reads shares.json from dataDir. Returns an empty store if the file doesn't exist.
|
||||
func Load(dataDir string) (*Store, error) {
|
||||
s := &Store{path: filepath.Join(dataDir, "shares.json")}
|
||||
data, err := os.ReadFile(s.path)
|
||||
if os.IsNotExist(err) {
|
||||
return s, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal(data, &s.shares); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Store) save() error {
|
||||
data, err := json.MarshalIndent(s.shares, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(s.path, data, 0600)
|
||||
}
|
||||
|
||||
// All returns a copy of all shares.
|
||||
func (s *Store) All() []Share {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
out := make([]Share, len(s.shares))
|
||||
copy(out, s.shares)
|
||||
return out
|
||||
}
|
||||
|
||||
// ForNetwork returns all shares visible on networkID (global + network-specific).
|
||||
func (s *Store) ForNetwork(networkID string) []Share {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
var out []Share
|
||||
for _, sh := range s.shares {
|
||||
for _, n := range sh.Networks {
|
||||
if n == "*" || n == networkID {
|
||||
out = append(out, sh)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Add adds a share (no-op if path already present). Persists immediately.
|
||||
func (s *Store) Add(sh Share) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for i, existing := range s.shares {
|
||||
if existing.Path == sh.Path {
|
||||
s.shares[i].Networks = sh.Networks
|
||||
return s.save()
|
||||
}
|
||||
}
|
||||
s.shares = append(s.shares, sh)
|
||||
return s.save()
|
||||
}
|
||||
|
||||
// Remove removes the share with the given path. Persists immediately.
|
||||
func (s *Store) Remove(path string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for i, sh := range s.shares {
|
||||
if sh.Path == path {
|
||||
s.shares = append(s.shares[:i], s.shares[i+1:]...)
|
||||
return s.save()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user