commit 96c148ef56eaa2f1d8bc5314568ea9d4ce265922
parent 79e3828b0a407d302f13439ad960a5d146f58db9
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:
4 files changed, 22 insertions(+), 18 deletions(-)
diff --git a/announce_test.go b/announce_test.go
@@ -23,7 +23,7 @@ import (
"crypto/rand"
"encoding/base64"
"os"
- "strings"
+ "path/filepath"
"testing"
)
@@ -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",
diff --git a/cmd/tor-dam/tor-dam.go b/cmd/tor-dam/tor-dam.go
@@ -28,6 +28,7 @@ import (
"log"
"net"
"os"
+ "path/filepath"
"strings"
"sync"
"time"
@@ -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 {
@@ -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)
@@ -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)
@@ -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)
}
@@ -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)
}
diff --git a/peer_announce.go b/peer_announce.go
@@ -22,6 +22,7 @@ import (
"crypto/ed25519"
"encoding/base64"
"log"
+ "path/filepath"
"strings"
"github.com/creachadair/jrpc2"
@@ -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
}
diff --git a/rpc_announce.go b/rpc_announce.go
@@ -22,6 +22,7 @@ import (
"crypto/ed25519"
"encoding/base64"
"errors"
+ "path/filepath"
"strings"
"time"
)
@@ -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
}
@@ -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