-turn-url and -turn-secret flags on the daemon; credentials generated using coturn use-auth-secret HMAC-SHA1 scheme (same as browser mode). ICEServers field on mesh.Mesh threads extra ICE servers through to every PeerConnection created by the anchor client. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
110 lines
3.4 KiB
Go
110 lines
3.4 KiB
Go
// waste-daemon: the local peer process.
|
|
// Run one of these on each friend's machine.
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/waste-go/internal/crypto"
|
|
"github.com/waste-go/internal/ipc"
|
|
"github.com/waste-go/internal/invite"
|
|
"github.com/waste-go/internal/netmgr"
|
|
)
|
|
|
|
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)")
|
|
wsPort := flag.Int("ws-port", 0, "port for WebSocket IPC (web UI); 0 = disabled")
|
|
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")
|
|
turnURL := flag.String("turn-url", "", "TURN server URL, e.g. turn:your-vps:3478")
|
|
turnSecret := flag.String("turn-secret", "", "shared secret for coturn use-auth-secret HMAC credential")
|
|
importBackup := flag.String("import-identity", "", "path to a yaw-key-backup-1 JSON file to import")
|
|
importPassword := flag.String("import-passphrase", "", "passphrase for --import-identity")
|
|
flag.Parse()
|
|
|
|
dir := expandHome(*dataDir)
|
|
|
|
// --import-identity: decrypt backup and write identity.json, then exit.
|
|
if *importBackup != "" {
|
|
if *importPassword == "" {
|
|
log.Fatal("--import-passphrase is required with --import-identity")
|
|
}
|
|
raw, err := os.ReadFile(*importBackup)
|
|
if err != nil {
|
|
log.Fatalf("import-identity: read file: %v", err)
|
|
}
|
|
imported, err := crypto.ImportIdentity(raw, *importPassword)
|
|
if err != nil {
|
|
log.Fatalf("import-identity: %v", err)
|
|
}
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
log.Fatalf("import-identity: mkdir: %v", err)
|
|
}
|
|
if err := crypto.SaveIdentity(dir, imported); err != nil {
|
|
log.Fatalf("import-identity: save: %v", err)
|
|
}
|
|
fmt.Printf("identity imported: %s\n", imported.PeerID())
|
|
return
|
|
}
|
|
|
|
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)
|
|
|
|
// --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)
|
|
}
|
|
|
|
mgr := netmgr.New(netmgr.Config{
|
|
MasterIdentity: id,
|
|
StoreDir: dir,
|
|
AnchorURL: *anchorURL,
|
|
ShareDir: expandHome(*shareDir),
|
|
TurnURL: *turnURL,
|
|
TurnSecret: *turnSecret,
|
|
})
|
|
|
|
if autoJoinNetwork != "" {
|
|
if _, err := mgr.Join(autoJoinNetwork, ""); err != nil {
|
|
log.Fatalf("auto-join: %v", err)
|
|
}
|
|
}
|
|
|
|
errCh := make(chan error, 2)
|
|
|
|
go func() { errCh <- ipc.Run(mgr, *ipcPort) }()
|
|
|
|
if *wsPort != 0 {
|
|
go func() { errCh <- ipc.RunWS(mgr, *wsPort) }()
|
|
}
|
|
|
|
if err := <-errCh; 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
|
|
}
|