- proto: FileEntry, FileListResp types; MsgFileListReq/Resp msg types;
CmdGetFileList + EvtFileList IPC types; Files field on IpcMessage
- mesh: ShareDir field + ScanShareDir(); on DataChannel open, auto-send
MsgFileListReq to new peer; handle MsgFileListReq (scan + reply) and
MsgFileListResp (emit EvtFileList to IPC subscribers)
- ipc: get_file_list command — own list returned immediately; remote peer
list requested via DataChannel (response arrives as EvtFileList event)
- daemon: -share-dir flag wired to mesh.ShareDir
- test scripts: pass -share-dir /home/frejoh/Downloads/{alice,bob,charlie};
test-network.sh verifies each peer's own file list via get_file_list
- FUTURE.md: document per-network share directories and multi-network design
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
// waste-daemon: the local peer process.
|
|
// Run one of these on each friend's machine.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/waste-go/internal/anchor"
|
|
"github.com/waste-go/internal/crypto"
|
|
"github.com/waste-go/internal/invite"
|
|
"github.com/waste-go/internal/ipc"
|
|
"github.com/waste-go/internal/mesh"
|
|
"github.com/waste-go/internal/proto"
|
|
"github.com/waste-go/internal/store"
|
|
)
|
|
|
|
func main() {
|
|
dataDir := flag.String("data-dir", "~/.waste", "path to identity/config directory")
|
|
alias := flag.String("alias", "anon", "display name shown to peers (advisory only)")
|
|
ipcPort := flag.Int("ipc-port", 17337, "port for local IPC (UI connects here)")
|
|
anchorURL := flag.String("anchor", "", "anchor WebSocket URL, e.g. ws://your-vps:17339/ws")
|
|
shareDir := flag.String("share-dir", "", "directory to share with peers on the network")
|
|
joinInvite := flag.String("join", "", "waste: invite string — sets anchor URL and auto-joins the network on startup")
|
|
flag.Parse()
|
|
|
|
// --join overrides/sets the anchor URL and triggers an auto-join.
|
|
var autoJoinNetwork string
|
|
if *joinInvite != "" {
|
|
inv, err := invite.Decode(*joinInvite)
|
|
if err != nil {
|
|
log.Fatalf("invalid invite: %v", err)
|
|
}
|
|
*anchorURL = inv.Anchor
|
|
autoJoinNetwork = inv.Network
|
|
log.Printf("daemon: invite decoded — anchor=%s network=%s", inv.Anchor, inv.Network)
|
|
}
|
|
|
|
dir := expandHome(*dataDir)
|
|
id, err := crypto.LoadOrCreate(dir, *alias)
|
|
if err != nil {
|
|
log.Fatalf("identity: %v", err)
|
|
}
|
|
log.Printf("daemon: local peer id: %s alias: %s", id.PeerID().Short(), id.Alias)
|
|
|
|
st, err := store.Open(filepath.Join(dir, "messages.db"))
|
|
if err != nil {
|
|
log.Fatalf("store: %v", err)
|
|
}
|
|
defer st.Close()
|
|
|
|
m := mesh.New(id, st)
|
|
if *shareDir != "" {
|
|
m.ShareDir = expandHome(*shareDir)
|
|
log.Printf("daemon: sharing %s", m.ShareDir)
|
|
}
|
|
|
|
// joinFn is passed to the IPC layer; it's called when the UI sends join_network.
|
|
joinFn := func(ctx context.Context, networkName string) {
|
|
if *anchorURL == "" {
|
|
log.Printf("daemon: join_network: no -anchor flag set")
|
|
m.Emit(proto.IpcMessage{Type: proto.EvtError, ErrorMessage: "no anchor configured — start daemon with -anchor <url>"})
|
|
return
|
|
}
|
|
log.Printf("daemon: joining network %q via %s", networkName, *anchorURL)
|
|
anchor.Run(ctx, *anchorURL, networkName, id, m)
|
|
log.Printf("daemon: left network %q", networkName)
|
|
}
|
|
|
|
// Auto-join from --join flag before starting IPC (non-blocking).
|
|
if autoJoinNetwork != "" {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
_ = cancel // lifecycle managed by the joinFn / ipc.Run leave
|
|
go joinFn(ctx, autoJoinNetwork)
|
|
}
|
|
|
|
if err := ipc.Run(m, *ipcPort, *anchorURL, joinFn); err != nil {
|
|
log.Fatalf("ipc: %v", err)
|
|
}
|
|
}
|
|
|
|
func expandHome(path string) string {
|
|
if len(path) >= 2 && path[:2] == "~/" {
|
|
if home, err := os.UserHomeDir(); err == nil {
|
|
return home + path[1:]
|
|
}
|
|
}
|
|
return path
|
|
}
|
|
|