tordam

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

commit 5413e1a25d4831ded3096d8f379a9b1cfd5676ab
parent bbad7869cabc96604d8ababb595b21461d6e3f69
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:
Mcmd/dam-dir/main.go | 9+++++++++
Mpkg/damlib/config.go | 3+++
Mpkg/damlib/redis.go | 28+++++++++++++++++++++++++++-
3 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go @@ -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 { @@ -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 { diff --git a/pkg/damlib/config.go b/pkg/damlib/config.go @@ -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!" diff --git a/pkg/damlib/redis.go b/pkg/damlib/redis.go @@ -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" @@ -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) +}