tdam-dir: Don't request a descriptor if we have already seen the node. - 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 c0a9715772f824767c5ae8803bfdb0a1709a93f6
   DIR parent 177de482a528382347db09ffc1b817a859651cfb
  HTML Author: parazyd <parazyd@dyne.org>
       Date:   Sat,  9 Dec 2017 15:41:24 +0100
       
       dam-dir: Don't request a descriptor if we have already seen the node.
       
       Diffstat:
         M cmd/dam-dir/main.go                 |      12 +++++++++++-
         M pkg/lib/helpers.go                  |      38 ++++++++++++++++---------------
       
       2 files changed, 31 insertions(+), 19 deletions(-)
       ---
   DIR diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go
       t@@ -57,7 +57,17 @@ func handlePost(rw http.ResponseWriter, request *http.Request) {
                        "secret":    n.Secret,
                }
        
       -        pkey, valid := lib.ValidateReq(req)
       +        // Check if we have seen this node already.
       +        ex, err := RedisCli.Exists(n.Address).Result()
       +        lib.CheckError(err)
       +        var pub = ""
       +        if ex == 1 {
       +                res, err := RedisCli.HGet(n.Address, "pubkey").Result()
       +                pub = string(res)
       +                lib.CheckError(err)
       +        }
       +
       +        pkey, valid := lib.ValidateReq(req, pub)
                if !(valid) && pkey == nil {
                        log.Fatalln("Request is not valid.")
                } else if !(valid) && pkey != nil {
   DIR diff --git a/pkg/lib/helpers.go b/pkg/lib/helpers.go
       t@@ -50,7 +50,7 @@ func FetchHSPubkey(addr string) string {
        }
        
        // ValidateReq validates our given request against some checks.
       -func ValidateReq(req map[string]string) ([]byte, bool) {
       +func ValidateReq(req map[string]string, pubkey string) ([]byte, bool) {
                // Validate nodetype.
                if req["nodetype"] != "node" {
                        return nil, false
       t@@ -61,24 +61,26 @@ func ValidateReq(req map[string]string) ([]byte, bool) {
                if len(re.FindString(req["address"])) != 22 {
                        return nil, false
                }
       -        // Address is valid, we try to fetch its pubkey from a HSDir
       -        var pubkey string
       -        var cnt = 0
       -        log.Println(req["address"], "seems valid")
       -        for { // We try until we have it.
       -                cnt++
       -                if cnt > 10 {
       -                        // We probably can't get a good HSDir. The client shall retry
       -                        // later on.
       -                        return []byte("Couldn't get a descriptor. Try later."), false
       -                }
       -                pubkey = FetchHSPubkey(req["address"])
       -                if strings.HasPrefix(pubkey, "-----BEGIN RSA PUBLIC KEY-----") &&
       -                        strings.HasSuffix(pubkey, "-----END RSA PUBLIC KEY-----") {
       -                        log.Println("Got descriptor!")
       -                        break
       +
       +        if len(pubkey) == 0 {
       +                // Address is valid, we try to fetch its pubkey from a HSDir
       +                cnt := 0
       +                log.Println(req["address"], "seems valid")
       +                for { // We try until we have it.
       +                        cnt++
       +                        if cnt > 10 {
       +                                // We probably can't get a good HSDir. The client shall retry
       +                                // later on.
       +                                return []byte("Couldn't get a descriptor. Try later."), false
       +                        }
       +                        pubkey = FetchHSPubkey(req["address"])
       +                        if strings.HasPrefix(pubkey, "-----BEGIN RSA PUBLIC KEY-----") &&
       +                                strings.HasSuffix(pubkey, "-----END RSA PUBLIC KEY-----") {
       +                                log.Println("Got descriptor!")
       +                                break
       +                        }
       +                        time.Sleep(2000 * time.Millisecond)
                        }
       -                time.Sleep(2000 * time.Millisecond)
                }
                // Validate signature.
                msg := []byte(req["message"])