URI: 
       tImplement a Redis publish/subscribe channel. - 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 5413e1a25d4831ded3096d8f379a9b1cfd5676ab
   DIR parent bbad7869cabc96604d8ababb595b21461d6e3f69
  HTML Author: parazyd <parazyd@dyne.org>
       Date:   Fri, 26 Oct 2018 18:24:29 +0200
       
       Implement a Redis publish/subscribe channel.
       
       This will be used for Gource visualizations, and will show and notify
       when nodes are added or announced again.
       
       Diffstat:
         M cmd/dam-dir/main.go                 |       9 +++++++++
         M pkg/damlib/config.go                |       3 +++
         M pkg/damlib/redis.go                 |      28 +++++++++++++++++++++++++++-
       
       3 files changed, 39 insertions(+), 1 deletion(-)
       ---
   DIR diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go
       t@@ -58,6 +58,11 @@ func startRedis() {
        
                _, err = lib.RedisCli.Ping().Result()
                lib.CheckError(err)
       +
       +        PubSub := lib.RedisCli.Subscribe(lib.PubSubChan)
       +        _, err = PubSub.Receive()
       +        lib.CheckError(err)
       +        log.Printf("Created \"%s\" channel in redis\n", lib.PubSubChan)
        }
        
        func postback(rw http.ResponseWriter, data map[string]string, retCode int) error {
       t@@ -185,12 +190,16 @@ func handlePost(rw http.ResponseWriter, request *http.Request) {
                                if err := postback(rw, ret, 200); err != nil {
                                        lib.CheckError(err)
                                }
       +
       +                        lib.PublishToRedis(n.Address)
       +
                                return
                        }
        
                        // If we have't returned so far, the handshake is invalid.
                        log.Printf("%s: 2/2 handshake invalid.\n", n.Address)
                        // Delete it all from redis.
       +                // TODO: Also pubsub here.
                        _, err := lib.RedisCli.Del(n.Address).Result()
                        lib.CheckError(err)
                        if err := postback(rw, ret, 400); err != nil {
   DIR diff --git a/pkg/damlib/config.go b/pkg/damlib/config.go
       t@@ -31,6 +31,9 @@ const RsaBits = 1024
        // PrivKeyPath holds the name of where our private key is.
        const PrivKeyPath = "dam-private.key"
        
       +// PubSubChan is the name of the pub/sub channel we're publishing to in Redis.
       +const PubSubChan = "tordam"
       +
        // PostMsg holds the message we are signing with our private key.
        const PostMsg = "I am a DAM node!"
        
   DIR diff --git a/pkg/damlib/redis.go b/pkg/damlib/redis.go
       t@@ -20,7 +20,11 @@ package damlib
         * along with this source code. If not, see <http://www.gnu.org/licenses/>.
         */
        
       -import "github.com/go-redis/redis"
       +import (
       +        "fmt"
       +
       +        "github.com/go-redis/redis"
       +)
        
        // RedisAddress points us to our Redis instance.
        const RedisAddress = "127.0.0.1:6379"
       t@@ -31,3 +35,25 @@ var RedisCli = redis.NewClient(&redis.Options{
                Password: "",
                DB:       0,
        })
       +
       +// PublishToRedis is a function that publishes a node's status to Redis.
       +// This is used for Gource visualization.
       +func PublishToRedis(address string) {
       +        var timestamp, username, modtype, onion, pubstr string
       +
       +        nodedata, err := RedisCli.HGetAll(address).Result()
       +        CheckError(err)
       +
       +        timestamp = nodedata["lastseen"]
       +        if timestamp == nodedata["firstseen"] {
       +                modtype = "A"
       +        } else {
       +                modtype = "M"
       +        }
       +        username = address
       +        onion = address
       +
       +        pubstr = fmt.Sprintf("%s|%s|%s|%s\n", timestamp, username, modtype, onion)
       +
       +        RedisCli.Publish(PubSubChan, pubstr)
       +}