tFix compilation with latest Redis library. - 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 573769406a8be94de602fc1c7e38a8dc24991503 DIR parent 31e003e1deb00e8611d652d77c12b763f8ba96a3 HTML Author: parazyd <parazyd@dyne.org> Date: Mon, 5 Oct 2020 22:03:22 +0200 Fix compilation with latest Redis library. Diffstat: M cmd/dam-client/main.go | 9 +++------ M cmd/dam-dir/main.go | 20 ++++++++++---------- M cmd/dam-gource/main.go | 4 ++-- M pkg/damlib/redis.go | 14 +++++++++----- M pkg/damlib/validate.go | 22 +++++++--------------- 5 files changed, 31 insertions(+), 38 deletions(-) --- DIR diff --git a/cmd/dam-client/main.go b/cmd/dam-client/main.go t@@ -109,9 +109,9 @@ func fetchNodeList(epLists []string, remote bool) ([]string, error) { } // Local nodes known to Redis - nodes, _ := lib.RedisCli.Keys("*.onion").Result() + nodes, _ := lib.RedisCli.Keys(lib.Rctx, "*.onion").Result() for _, i := range nodes { - valid, err := lib.RedisCli.HGet(i, "valid").Result() + valid, err := lib.RedisCli.HGet(lib.Rctx, i, "valid").Result() if err != nil { // Possible RedisCli bug, possible Redis bug. To be investigated. // Sometimes it returns err, but it's nil and does not say what's t@@ -215,11 +215,8 @@ func announce(node string, vals map[string]string, privkey ed25519.PrivateKey) ( } for k, v := range nodes { log.Printf("Adding %s to Redis.\n", k) - redRet, err := lib.RedisCli.HMSet(k, v).Result() + _, err = lib.RedisCli.HMSet(lib.Rctx, k, v).Result() lib.CheckError(err) - if redRet != "OK" { - log.Println("Redis returned:", redRet) - } } return true, nil } DIR diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go t@@ -121,7 +121,7 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { } log.Printf("%s: 1/2 handshake invalid: %s\n", n.Address, msg) // Delete it all from redis. - _, err := lib.RedisCli.Del(n.Address).Result() + _, err := lib.RedisCli.Del(lib.Rctx, n.Address).Result() lib.CheckError(err) if err := postback(rw, ret, 400); err != nil { lib.CheckError(err) t@@ -136,7 +136,7 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { if valid { log.Printf("%s: 2/2 handshake valid.\n", n.Address) - hasConsensus, err := lib.RedisCli.HGet(n.Address, "valid").Result() + hasConsensus, err := lib.RedisCli.HGet(lib.Rctx, n.Address, "valid").Result() lib.CheckError(err) us := request.Host // Assume our name is what was requested as the URL. t@@ -146,13 +146,13 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { // The node does have consensus, we'll teach it about the valid // nodes we know. log.Printf("%s has consensus. Propagating our nodes to it...\n", n.Address) - nodes, err := lib.RedisCli.Keys("*.onion").Result() + nodes, err := lib.RedisCli.Keys(lib.Rctx, "*.onion").Result() lib.CheckError(err) for _, i := range nodes { if i == n.Address { continue } - nodedata, err := lib.RedisCli.HGetAll(i).Result() + nodedata, err := lib.RedisCli.HGetAll(lib.Rctx, i).Result() lib.CheckError(err) if nodedata["valid"] == "1" { nodemap[i] = nodedata t@@ -163,7 +163,7 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { log.Printf("%s does not have consensus. Propagating ourself to it...\n", n.Address) // The node doesn't have consensus in the network. We will only // teach it about ourself. - nodedata, err := lib.RedisCli.HGetAll(us).Result() + nodedata, err := lib.RedisCli.HGetAll(lib.Rctx, us).Result() lib.CheckError(err) nodemap[us] = nodedata delete(nodemap[us], "secret") t@@ -187,7 +187,7 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { log.Printf("%s: 2/2 handshake invalid.\n", n.Address) // Delete it all from redis. lib.PublishToRedis("d", n.Address) - _, err := lib.RedisCli.Del(n.Address).Result() + _, err := lib.RedisCli.Del(lib.Rctx, n.Address).Result() lib.CheckError(err) if err := postback(rw, ret, 400); err != nil { lib.CheckError(err) t@@ -199,12 +199,12 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { func pollNodeTTL(interval int64) { for { log.Println("Polling redis for expired nodes") - nodes, err := lib.RedisCli.Keys("*.onion").Result() + nodes, err := lib.RedisCli.Keys(lib.Rctx, "*.onion").Result() lib.CheckError(err) now := time.Now().Unix() for _, i := range nodes { - res, err := lib.RedisCli.HGet(i, "lastseen").Result() + res, err := lib.RedisCli.HGet(lib.Rctx, i, "lastseen").Result() lib.CheckError(err) lastseen, err := strconv.Atoi(res) lib.CheckError(err) t@@ -213,7 +213,7 @@ func pollNodeTTL(interval int64) { if diff > interval { log.Printf("Deleting %s from redis because of expiration\n", i) lib.PublishToRedis("d", i) - lib.RedisCli.Del(i) + lib.RedisCli.Del(lib.Rctx, i) } } time.Sleep(time.Duration(interval) * time.Minute) t@@ -240,7 +240,7 @@ func main() { err := os.Chdir(lib.Workdir) lib.CheckError(err) - if _, err := lib.RedisCli.Ping().Result(); err != nil { + if _, err := lib.RedisCli.Ping(lib.Rctx).Result(); err != nil { // We assume redis is not running. Start it up. cmd, err := lib.StartRedis(*redconf) defer cmd.Process.Kill() DIR diff --git a/cmd/dam-gource/main.go b/cmd/dam-gource/main.go t@@ -28,8 +28,8 @@ import ( ) func main() { - pubsub := lib.RedisCli.Subscribe(lib.PubSubChan) - _, err := pubsub.Receive() + pubsub := lib.RedisCli.Subscribe(lib.Rctx, lib.PubSubChan) + _, err := pubsub.Receive(lib.Rctx) lib.CheckError(err) fmt.Fprintf(os.Stderr, "Subscribed to %s channel in Redis\n", lib.PubSubChan) DIR diff --git a/pkg/damlib/redis.go b/pkg/damlib/redis.go t@@ -21,6 +21,7 @@ package damlib */ import ( + "context" "fmt" "log" "os/exec" t@@ -32,6 +33,9 @@ import ( // RedisAddress points us to our Redis instance. const RedisAddress = "127.0.0.1:6379" +// Rctx is the context for Redis +var Rctx = context.Background() + // RedisCli is our global Redis client var RedisCli = redis.NewClient(&redis.Options{ Addr: RedisAddress, t@@ -50,12 +54,12 @@ func StartRedis(conf string) (*exec.Cmd, error) { } time.Sleep(500 * time.Millisecond) - if _, err := RedisCli.Ping().Result(); err != nil { + if _, err := RedisCli.Ping(Rctx).Result(); err != nil { return cmd, err } - PubSub := RedisCli.Subscribe(PubSubChan) - if _, err := PubSub.Receive(); err != nil { + PubSub := RedisCli.Subscribe(Rctx, PubSubChan) + if _, err := PubSub.Receive(Rctx); err != nil { return cmd, err } t@@ -68,7 +72,7 @@ func StartRedis(conf string) (*exec.Cmd, error) { func PublishToRedis(mt, address string) { var timestamp, username, modtype, onion, pubstr string - nodedata, err := RedisCli.HGetAll(address).Result() + nodedata, err := RedisCli.HGetAll(Rctx, address).Result() CheckError(err) timestamp = nodedata["lastseen"] t@@ -84,5 +88,5 @@ func PublishToRedis(mt, address string) { pubstr = fmt.Sprintf("%s|%s|%s|%s", timestamp, username, modtype, onion) - RedisCli.Publish(PubSubChan, pubstr) + RedisCli.Publish(Rctx, PubSubChan, pubstr) } DIR diff --git a/pkg/damlib/validate.go b/pkg/damlib/validate.go t@@ -79,12 +79,12 @@ func ValidateFirstHandshake(req map[string]string) (bool, string) { var pubstr string var pubkey ed25519.PublicKey // Check if we have seen this node already. - ex, err := RedisCli.Exists(req["address"]).Result() + ex, err := RedisCli.Exists(Rctx, req["address"]).Result() CheckError(err) if ex == 1 { // We saw it so we should have the public key in redis. // If we do not, that is an internal error. - pubstr, err = RedisCli.HGet(req["address"], "pubkey").Result() + pubstr, err = RedisCli.HGet(Rctx, req["address"], "pubkey").Result() CheckError(err) } else { // We take it from the announce. t@@ -128,13 +128,9 @@ func ValidateFirstHandshake(req map[string]string) (bool, string) { } log.Printf("%s: writing to redis\n", req["address"]) - redRet, err := RedisCli.HMSet(req["address"], info).Result() + _, err = RedisCli.HMSet(Rctx, req["address"], info).Result() CheckError(err) - if redRet != "OK" { - return false, "Internal server error" - } - return true, encodedSecret } t@@ -160,19 +156,19 @@ func ValidateSecondHandshake(req map[string]string) (bool, string) { var pubstr string var pubkey ed25519.PublicKey // Check if we have seen this node already. - ex, err := RedisCli.Exists(req["address"]).Result() + ex, err := RedisCli.Exists(Rctx, req["address"]).Result() CheckError(err) if ex == 1 { // We saw it so we should have the public key in redis. // If we do not, that is an internal error. - pubstr, err = RedisCli.HGet(req["address"], "pubkey").Result() + pubstr, err = RedisCli.HGet(Rctx, req["address"], "pubkey").Result() CheckError(err) } else { log.Printf("%s tried to jump in 2/2 handshake before doing the first.\n", req["address"]) return false, "We have not seen you before. Please authenticate properly." } - localSec, err := RedisCli.HGet(req["address"], "secret").Result() + localSec, err := RedisCli.HGet(Rctx, req["address"], "secret").Result() CheckError(err) if !(localSec == req["secret"] && localSec == req["message"]) { t@@ -208,12 +204,8 @@ func ValidateSecondHandshake(req map[string]string) (bool, string) { } // Can not cast, need this for HMSet log.Printf("%s: writing to redis\n", req["address"]) - redRet, err := RedisCli.HMSet(req["address"], info).Result() + _, err = RedisCli.HMSet(Rctx, req["address"], info).Result() CheckError(err) - if redRet != "OK" { - return false, "Internal server error" - } - return true, WelcomeMsg }