tordam

A library for peer discovery inside the Tor network
git clone https://git.parazyd.org/tordam
Log | Files | Refs | README | LICENSE

commit 48b7018b6eedd9592657a0384f4f3a8d270300db
parent 2b894da7e0625dc4fccb811b001073c76c49019e
Author: parazyd <parazyd@dyne.org>
Date:   Sat,  9 Dec 2017 20:02:59 +0100

Chdir to a working directory where we can keep our files on startup

Diffstat:
Mcmd/dam-client/main.go | 18+++++++++++++-----
Mcmd/dam-dir/main.go | 15+++++++++++++--
2 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/cmd/dam-client/main.go b/cmd/dam-client/main.go @@ -16,14 +16,14 @@ import ( "github.com/parazyd/tor-dam/pkg/lib" ) +// Cwd holds the path to the directory where we will Chdir on startup. +var Cwd = os.Getenv("HOME") + "/.dam" + // Bits hold the size of our RSA private key. Tor standard is 1024. const Bits = 1024 -// Privpath holds the path of where our private key is. -const Privpath = "/tmp/dam-private.key" - -// Pubpath holds the path of where our public key is. -//const Pubpath = "/tmp/dam-public.pub" +// Privpath holds the name of where our private key is. +const Privpath = "dam-private.key" // Postmsg holds the message we are signing with our private key. const Postmsg = "I am a DAM node!" @@ -107,6 +107,14 @@ func announce(dir string, vals map[string]string, privkey *rsa.PrivateKey) (bool } func main() { + if _, err := os.Stat(Cwd); os.IsNotExist(err) { + err := os.Mkdir(Cwd, 0700) + lib.CheckError(err) + } + log.Println("Chdir to", Cwd) + err := os.Chdir(Cwd) + lib.CheckError(err) + if _, err := os.Stat(Privpath); os.IsNotExist(err) { key, err := lib.GenRsa(Bits) lib.CheckError(err) diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go @@ -7,6 +7,7 @@ import ( "encoding/json" "log" "net/http" + "os" "os/exec" "sync" "time" @@ -15,6 +16,9 @@ import ( "github.com/parazyd/tor-dam/pkg/lib" ) +// Cwd holds the path to the directory where we will Chdir on startup. +var Cwd = os.Getenv("HOME") + "/.dam" + // ListenAddress controls where our HTTP API daemon is listening. const ListenAddress = "127.0.0.1:49371" @@ -181,8 +185,15 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { func main() { var wg sync.WaitGroup - _, err := RedisCli.Ping().Result() - if err != nil { + if _, err := os.Stat(Cwd); os.IsNotExist(err) { + err := os.Mkdir(Cwd, 0700) + lib.CheckError(err) + } + log.Println("Chdir to", Cwd) + err := os.Chdir(Cwd) + lib.CheckError(err) + + if _, err := RedisCli.Ping().Result(); err != nil { // We assume redis is not running. Start it up. startRedis() }