Add runnable node, HTTP API, all-devices revocation and reconnect sync
- cmd/node: init (recovery phrase onboarding), run, recover - api: public reads of canonical signed records, token-protected writes - identity: RevokeAll voids every device certified before a cutoff, so recovery works with only the phrase; store purges by certificate age - p2p: redial bootstrap peers, sync on every new connection (in-flight guard only), validate bootstrap addresses up front - docs: API, recovery semantics, peer-ID change on recovery Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
// Command node runs an Allmende node.
|
||||
//
|
||||
// node init first-time setup: creates your identity and shows your recovery phrase
|
||||
// node run run the node (replicates listings, serves the HTTP API)
|
||||
// node recover restore from the recovery phrase after a lost or compromised machine
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
certLifetime = 365 * 24 * time.Hour
|
||||
|
||||
dbFile = "allmende.db"
|
||||
tokenFile = "admin.token"
|
||||
)
|
||||
|
||||
const usage = `Allmende: a free, distributed marketplace for homes.
|
||||
|
||||
Usage:
|
||||
node init [--data DIR] create your identity (first run)
|
||||
node run [--data DIR] --region u1x run the node
|
||||
node recover [--data DIR] restore access using your recovery phrase
|
||||
|
||||
Run "node <command> -h" for the options of a command.
|
||||
`
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprint(os.Stderr, usage)
|
||||
os.Exit(2)
|
||||
}
|
||||
var err error
|
||||
switch os.Args[1] {
|
||||
case "init":
|
||||
err = cmdInit(os.Args[2:])
|
||||
case "run":
|
||||
err = cmdRun(os.Args[2:])
|
||||
case "recover":
|
||||
err = cmdRecover(os.Args[2:])
|
||||
case "help", "-h", "--help":
|
||||
fmt.Print(usage)
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unknown command %q\n\n%s", os.Args[1], usage)
|
||||
os.Exit(2)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// --- shared helpers -------------------------------------------------------
|
||||
|
||||
func defaultDataDir() string {
|
||||
if d := os.Getenv("ALLMENDE_DATA"); d != "" {
|
||||
return d
|
||||
}
|
||||
if x := os.Getenv("XDG_DATA_HOME"); x != "" {
|
||||
return filepath.Join(x, "allmende")
|
||||
}
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "allmende-data"
|
||||
}
|
||||
return filepath.Join(home, ".local", "share", "allmende")
|
||||
}
|
||||
|
||||
func dataFlag(fs *flag.FlagSet) *string {
|
||||
return fs.String("data", defaultDataDir(), "directory for keys and the database (env ALLMENDE_DATA)")
|
||||
}
|
||||
|
||||
// listFlag is a repeatable, comma-separated string flag.
|
||||
type listFlag []string
|
||||
|
||||
func (l *listFlag) String() string { return strings.Join(*l, ",") }
|
||||
func (l *listFlag) Set(v string) error {
|
||||
for _, p := range strings.Split(v, ",") {
|
||||
if p = strings.TrimSpace(p); p != "" {
|
||||
*l = append(*l, p)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newToken() (string, error) {
|
||||
var b [32]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(b[:]), nil
|
||||
}
|
||||
|
||||
// writeToken replaces the admin token file, readable only by the owner.
|
||||
func writeToken(dir, token string) error {
|
||||
path := filepath.Join(dir, tokenFile)
|
||||
tmp := path + ".tmp"
|
||||
if err := os.WriteFile(tmp, []byte(token+"\n"), 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, path)
|
||||
}
|
||||
|
||||
func readToken(dir string) string {
|
||||
b, err := os.ReadFile(filepath.Join(dir, tokenFile))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(string(b))
|
||||
}
|
||||
Reference in New Issue
Block a user