tUse filepath.Join instead of strings.Join to create paths. - 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 96c148ef56eaa2f1d8bc5314568ea9d4ce265922 DIR parent 79e3828b0a407d302f13439ad960a5d146f58db9 HTML Author: parazyd <parazyd@dyne.org> Date: Fri, 12 Mar 2021 00:27:14 +0100 Use filepath.Join instead of strings.Join to create paths. Diffstat: M announce_test.go | 9 +++------ M cmd/tor-dam/tor-dam.go | 23 ++++++++++++++--------- M peer_announce.go | 3 ++- M rpc_announce.go | 5 +++-- 4 files changed, 22 insertions(+), 18 deletions(-) --- DIR diff --git a/announce_test.go b/announce_test.go t@@ -23,7 +23,7 @@ import ( "crypto/rand" "encoding/base64" "os" - "strings" + "path/filepath" "testing" ) t@@ -33,11 +33,8 @@ func TestAnnounce(t *testing.T) { t.Fatal(err) } - Cfg.Datadir = os.Getenv("TMPDIR") - if Cfg.Datadir == "" { - Cfg.Datadir = "/tmp" - } - defer os.Remove(strings.Join([]string{Cfg.Datadir, dbFile}, "/")) + Cfg.Datadir = os.TempDir() + defer os.Remove(filepath.Join(Cfg.Datadir, dbFile)) vals := []string{ "p7qaewjgnvnaeihhyybmoofd5avh665kr3awoxlh5rt6ox743kjdr6qd.onion:666", DIR diff --git a/cmd/tor-dam/tor-dam.go b/cmd/tor-dam/tor-dam.go t@@ -28,6 +28,7 @@ import ( "log" "net" "os" + "path/filepath" "strings" "sync" "time" t@@ -50,6 +51,8 @@ var ( noannounce = flag.Bool("n", false, "Do not announce to peers") ) +// generateED25519Keypair is a helper function to generate it, and save the +// seed to a file for later reuse. func generateED25519Keypair(dir string) error { _, sk, err := ed25519.GenerateKey(rand.Reader) if err != nil { t@@ -59,12 +62,14 @@ func generateED25519Keypair(dir string) error { return err } - seedpath := strings.Join([]string{dir, "ed25519.seed"}, "/") + seedpath := filepath.Join(dir, "ed25519.seed") log.Println("Writing ed25519 key seed to", seedpath) return ioutil.WriteFile(seedpath, []byte(base64.StdEncoding.EncodeToString(sk.Seed())), 0600) } +// loadED25519Seed is a helper function to read an existing key seed and +// return an ed25519.PrivateKey. func loadED25519Seed(file string) (ed25519.PrivateKey, error) { log.Println("Reading ed25519 seed from", file) t@@ -86,9 +91,12 @@ func main() { var wg sync.WaitGroup var err error + // Assign the global tordam data directory + tordam.Cfg.Datadir = *datadir + // Generate the ed25519 keypair used for signing and validating if *generate { - if err := generateED25519Keypair(*datadir); err != nil { + if err := generateED25519Keypair(tordam.Cfg.Datadir); err != nil { log.Fatal(err) } os.Exit(0) t@@ -106,12 +114,9 @@ func main() { log.Fatalf("invalid listen address: %s (%v)", *listen, err) } - // Assign the global tordam data directory - tordam.Cfg.Datadir = *datadir - // Load the ed25519 signing key into the tordam global - tordam.SignKey, err = loadED25519Seed(strings.Join( - []string{*datadir, "ed25519.seed"}, "/")) + tordam.SignKey, err = loadED25519Seed( + filepath.Join(tordam.Cfg.Datadir, "ed25519.seed")) if err != nil { log.Fatal(err) } t@@ -132,8 +137,8 @@ func main() { // Read the onion hostname from the datadir and map it into the // global tordam.Onion variable - onionaddr, err := ioutil.ReadFile(strings.Join([]string{ - tordam.Cfg.Datadir, "hs", "hostname"}, "/")) + onionaddr, err := ioutil.ReadFile( + filepath.Join(tordam.Cfg.Datadir, "hs", "hostname")) if err != nil { log.Fatal(err) } DIR diff --git a/peer_announce.go b/peer_announce.go t@@ -22,6 +22,7 @@ import ( "crypto/ed25519" "encoding/base64" "log" + "path/filepath" "strings" "github.com/creachadair/jrpc2" t@@ -111,6 +112,6 @@ func AppendPeers(p []string) error { Peers[i] = Peer{} } - writePeersDBWithSem(strings.Join([]string{Cfg.Datadir, dbFile}, "/")) + writePeersDBWithSem(filepath.Join(Cfg.Datadir, dbFile)) return nil } DIR diff --git a/rpc_announce.go b/rpc_announce.go t@@ -22,6 +22,7 @@ import ( "crypto/ed25519" "encoding/base64" "errors" + "path/filepath" "strings" "time" ) t@@ -122,7 +123,7 @@ func (Ann) Init(ctx context.Context, vals []string) ([]string, error) { peer.Trusted = 0 Peers[onion] = peer - writePeersDBWithSem(strings.Join([]string{Cfg.Datadir, dbFile}, "/")) + writePeersDBWithSem(filepath.Join(Cfg.Datadir, dbFile)) return []string{nonce, newrevoke}, nil } t@@ -195,7 +196,7 @@ func (Ann) Validate(ctx context.Context, vals []string) ([]string, error) { peer.LastSeen = time.Now().Unix() Peers[onion] = peer - writePeersDBWithSem(strings.Join([]string{Cfg.Datadir, dbFile}, "/")) + writePeersDBWithSem(filepath.Join(Cfg.Datadir, dbFile)) rpcInfo("ann.Validate", "sending back list of peers to", onion) return ret, nil