tMake flags global variables in dam-client and dam-dir. - tordam - A library for peer discovery inside the Tor network HTML git clone https://git.parazyd.org/tordam DIR Log DIR Files DIR Refs DIR README DIR LICENSE --- DIR commit 7a8f7cbade62be63c5ff6eaaa36a258f4c12c25f DIR parent f0a5b182e0630a0751bea2cbaf452af75a467761 HTML Author: parazyd <parazyd@dyne.org> Date: Fri, 4 Oct 2019 15:31:18 +0200 Make flags global variables in dam-client and dam-dir. Diffstat: M cmd/dam-client/main.go | 32 +++++++++++++++---------------- M cmd/dam-dir/main.go | 27 +++++++++++++++------------ 2 files changed, 30 insertions(+), 29 deletions(-) --- DIR diff --git a/cmd/dam-client/main.go b/cmd/dam-client/main.go t@@ -46,6 +46,13 @@ type msgStruct struct { Secret string } +var ( + noremote = flag.Bool("noremote", false, "Don't fetch remote entrypoints.") + gen = flag.Bool("gen", false, "Only (re)generate keypairs and exit cleanly.") + annint = flag.Int("ai", 5, "Announce interval (in minutes)") + remoteentry = flag.String("remoteentry", "https://dam.decodeproject.eu/dirs.txt", "Remote list of entrypoints. (comma-separated)") +) + func clientInit(gen bool) error { pub, priv, err := lib.GenEd25519() if err != nil { t@@ -74,13 +81,13 @@ func clientInit(gen bool) error { return nil } -func fetchNodeList(epLists []string, noremote bool) ([]string, error) { +func fetchNodeList(epLists []string, remote bool) ([]string, error) { var nodeslice, nodelist []string log.Println("Fetching a list of nodes.") // Remote network entrypoints - if !(noremote) { + if !(remote) { for _, i := range epLists { log.Println("Fetching", i) n, err := lib.HTTPDownload(i) t@@ -223,21 +230,12 @@ func announce(node string, vals map[string]string, privkey ed25519.PrivateKey) ( } func main() { - var noremote, gen bool - var ai int - var dh string - - flag.BoolVar(&noremote, "d", false, "Don't fetch remote entrypoints.") - flag.BoolVar(&gen, "gen", false, "Only (re)generate keypairs and exit cleanly.") - flag.IntVar(&ai, "ai", 5, "Announce interval in minutes.") - flag.StringVar(&dh, "dh", "https://dam.decodeproject.eu/dirs.txt", - "Remote lists of entrypoints. (comma-separated)") flag.Parse() // Network entrypoints. These files hold the lists of nodes we can announce // to initially. Format is "DIR:unlikelynamefora.onion", other lines are // ignored and can be used as comments or similar. - epLists := strings.Split(dh, ",") + epLists := strings.Split(*remoteentry, ",") if _, err := os.Stat(lib.Workdir); os.IsNotExist(err) { err := os.Mkdir(lib.Workdir, 0700) t@@ -246,8 +244,8 @@ func main() { err := os.Chdir(lib.Workdir) lib.CheckError(err) - if _, err = os.Stat(lib.PrivKeyPath); os.IsNotExist(err) || gen { - err = clientInit(gen) + if _, err = os.Stat(lib.PrivKeyPath); os.IsNotExist(err) || *gen { + err = clientInit(*gen) lib.CheckError(err) } t@@ -290,7 +288,7 @@ func main() { log.Println("Announcing to nodes...") var ann = 0 // Track of successful authentications. var wg sync.WaitGroup - nodes, err := fetchNodeList(epLists, noremote) + nodes, err := fetchNodeList(epLists, *noremote) if err != nil { // No route to host, or failed download. Try later. log.Println("Failed to fetch any nodes. Retrying in a minute.") t@@ -333,7 +331,7 @@ func main() { wg.Wait() log.Printf("%d successful authentications.\n", ann) - log.Printf("Waiting %d min before next announce.\n", ai) - time.Sleep(time.Duration(ai) * time.Minute) + log.Printf("Waiting %d min before next announce.\n", *annint) + time.Sleep(time.Duration(*annint) * time.Minute) } } DIR diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go t@@ -47,6 +47,12 @@ type nodeStruct struct { Valid int64 } +var ( + testnet = flag.Bool("t", false, "Mark all new nodes valid initially.") + ttl = flag.Int64("ttl", 0, "Set expiry time in minutes (TTL) for nodes.") + redconf = flag.String("redconf", "/usr/local/share/tor-dam/redis.conf", "Path to redis' redis.conf.") +) + func postback(rw http.ResponseWriter, data map[string]string, retCode int) error { jsonVal, err := json.Marshal(data) if err != nil { t@@ -219,15 +225,12 @@ func pollNodeTTL(interval int64) { func handleElse(rw http.ResponseWriter, request *http.Request) {} func main() { - var wg sync.WaitGroup - var ttl int64 - var redconf string - - flag.BoolVar(&lib.Testnet, "t", false, "Mark all new nodes valid initially") - flag.Int64Var(&ttl, "ttl", 0, "Set expiry time in minutes (TTL) for nodes") - flag.StringVar(&redconf, "redconf", "/usr/local/share/tor-dam/redis.conf", - "Path to redis' redis.conf.") flag.Parse() + var wg sync.WaitGroup + if *testnet { + log.Println("Enabling testnet") + lib.Testnet = true + } // Chdir to our working directory. if _, err := os.Stat(lib.Workdir); os.IsNotExist(err) { t@@ -239,7 +242,7 @@ func main() { if _, err := lib.RedisCli.Ping().Result(); err != nil { // We assume redis is not running. Start it up. - cmd, err := lib.StartRedis(redconf) + cmd, err := lib.StartRedis(*redconf) defer cmd.Process.Kill() lib.CheckError(err) } t@@ -261,9 +264,9 @@ func main() { go srv.ListenAndServe() log.Println("Listening on", ListenAddress) - if ttl > 0 { - log.Printf("Enabling TTL polling (%d minute expire time).\n", ttl) - go pollNodeTTL(ttl) + if *ttl > 0 { + log.Printf("Enabling TTL polling (%d minute expire time).\n", *ttl) + go pollNodeTTL(*ttl) } wg.Wait()