tcmd/tor-dam: Write some more documentation and clean up. - tordam - A library for peer discovery inside the Tor network (HTM) git clone https://git.parazyd.org/tordam (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit 7af2d65892ef925279b8d5b41b44015995433915 (DIR) parent 0d439d8aa0449fed236a0a5ff5ceedb9d69ff860 (HTM) Author: parazyd <parazyd@dyne.org> Date: Mon, 8 Mar 2021 01:11:43 +0100 cmd/tor-dam: Write some more documentation and clean up. Diffstat: M cmd/tor-dam/tor-dam.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) --- (DIR) diff --git a/cmd/tor-dam/tor-dam.go b/cmd/tor-dam/tor-dam.go t@@ -78,11 +78,14 @@ func loadED25519Seed(file string) (ed25519.PrivateKey, error) { return ed25519.NewKeyFromSeed(dec), nil } +// main here is the reference workflow of tor-dam's peer discovery. It's steps +// are commented and implement a generic way of using the tordam library. func main() { flag.Parse() var wg sync.WaitGroup var err error + // Generate the ed25519 keypair used for signing and validating if *generate { if err := generateED25519Keypair(*datadir); err != nil { log.Fatal(err) t@@ -90,13 +93,6 @@ func main() { os.Exit(0) } - // Validate given seeds - for _, i := range strings.Split(*seeds, ",") { - if err := tordam.ValidateOnionInternal(i); err != nil { - log.Fatalf("invalid seed %s (%v)", i, err) - } - } - // Assign portmap to tordam Cfg global and validate it tordam.Cfg.Portmap = strings.Split(*portmap, ",") if err := tordam.ValidatePortmap(tordam.Cfg.Portmap); err != nil { t@@ -142,16 +138,16 @@ func main() { log.Println("Our onion address is:", tordam.Onion) // Start the JSON-RPC server with announce endpoints. - // This is done in the library user rather than internally in the library + // This is done in the program rather than internally in the library // because it is more useful and easier to add additional JSON-RPC - // endpoints to the same server. + // endpoints to the same server if necessary. l, err := net.Listen(jrpc2.Network(tordam.Cfg.Listen.String()), tordam.Cfg.Listen.String()) if err != nil { log.Fatal(err) } defer l.Close() - // Endpoints are assigned here + // JSON-RPC endpoints are assigned here assigner := handler.ServiceMap{ // "ann" is the JSON-RPC endpoint for peer discovery/announcement "ann": handler.NewService(tordam.Ann{}), t@@ -166,6 +162,13 @@ func main() { wg.Wait() } + // Validate given seeds + for _, i := range strings.Split(*seeds, ",") { + if err := tordam.ValidateOnionInternal(i); err != nil { + log.Fatalf("invalid seed %s (%v)", i, err) + } + } + // Announce to initial seeds var succ int = 0 // Track of successful announces for _, i := range strings.Split(*seeds, ",") { t@@ -187,9 +190,8 @@ func main() { log.Printf("Successfully announced to %d peers.", succ) } - j, err := json.Marshal(tordam.Peers) - if err != nil { - log.Fatal(err) - } + // Marshal the global Peers map to JSON and print it out. + // Internally, the db is also saved (written to a file) in the datadir. + j, _ := json.Marshal(tordam.Peers) log.Println(string(j)) }