tMore robustness in dam-dir - 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 2e02e951df8b4bb89560d1fdc0116d9231290d63 (DIR) parent 4f05f88997bce535ef0cc782633172330ac16c11 (HTM) Author: parazyd <parazyd@dyne.org> Date: Sun, 10 Dec 2017 13:50:27 +0100 More robustness in dam-dir Diffstat: M cmd/dam-client/main.go | 6 +++--- M cmd/dam-dir/main.go | 32 ++++++++++++++++++++++++++----- M pkg/lib/helpers.go | 2 +- 3 files changed, 31 insertions(+), 9 deletions(-) --- (DIR) diff --git a/cmd/dam-client/main.go b/cmd/dam-client/main.go t@@ -38,9 +38,9 @@ func announce(dir string, vals map[string]string, privkey *rsa.PrivateKey) (bool return false, err } - if dir == "localhost" { + if dir == "localhost" || dir == "127.0.0.1" { // Modify the string if we are authenticating to ourself. - dir = "localhost:49371" + dir += ":49371" } log.Println("Announcing keypair to:", dir) t@@ -180,7 +180,7 @@ func main() { var ann = 0 // Track of how many successful authentications - dirs := []string{"qvhgzxjkdchj2jl5.onion", "localhost"} + dirs := []string{"3mb6b3exknytbqdg.onion", "localhost"} var wg sync.WaitGroup for _, i := range dirs { (DIR) diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go t@@ -45,7 +45,7 @@ type nodeStruct struct { } func startRedis() { - log.Println("Staring up redis-server...") + log.Println("Starting up redis-server...") cmd := exec.Command("redis-server", "/usr/local/share/tor-dam/redis.conf") err := cmd.Start() lib.CheckError(err) t@@ -57,14 +57,24 @@ func startRedis() { } func handlePost(rw http.ResponseWriter, request *http.Request) { + if request.Method != "POST" || request.Header["Content-Type"][0] != "application/json" { + return + } + decoder := json.NewDecoder(request.Body) var n nodeStruct err := decoder.Decode(&n) - lib.CheckError(err) + if err != nil { + log.Println("Failed decoding request:", err) + return + } decSig, err := base64.StdEncoding.DecodeString(n.Signature) - lib.CheckError(err) + if err != nil { + log.Println("Failed decoding signature:", err) + return + } req := map[string]string{ "nodetype": n.Nodetype, t@@ -86,7 +96,14 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { pkey, valid := lib.ValidateReq(req, pub) if !(valid) && pkey == nil { - log.Fatalln("Request is not valid.") + ret := map[string]string{ + "secret": "Request is not valid.", + } + jsonVal, err := json.Marshal(ret) + lib.CheckError(err) + rw.Header().Set("Content-Type", "application/json") + rw.Write(jsonVal) + return } else if !(valid) && pkey != nil { // We couldn't get a descriptor. ret := map[string]string{ t@@ -213,6 +230,11 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { } } +func handleElse(rw http.ResponseWriter, request *http.Request) { + // noop for anything that isn't /announce. + return +} + func main() { var wg sync.WaitGroup t@@ -220,7 +242,6 @@ func main() { err := os.Mkdir(Cwd, 0700) lib.CheckError(err) } - log.Println("Chdir to", Cwd) err := os.Chdir(Cwd) lib.CheckError(err) t@@ -230,6 +251,7 @@ func main() { } http.HandleFunc("/announce", handlePost) + http.HandleFunc("/", handleElse) wg.Add(1) go http.ListenAndServe(ListenAddress, nil) (DIR) diff --git a/pkg/lib/helpers.go b/pkg/lib/helpers.go t@@ -36,7 +36,7 @@ func FetchHSPubkey(addr string) string { log.Println("Fetching pubkey for:", addr) - cmd := exec.Command("dirauth.py", addr) + cmd := exec.Command("damauth.py", addr) cmd.Stdout = &outb cmd.Stderr = &errb err := cmd.Start()