tor-dam

tor distributed announce mechanism (not a dht)
git clone https://git.parazyd.org/tor-dam
Log | Files | Refs | README | LICENSE

redis.go (2510B)


      1 package damlib
      2 
      3 /*
      4  * Copyright (c) 2017-2018 Dyne.org Foundation
      5  * tor-dam is written and maintained by Ivan Jelincic <parazyd@dyne.org>
      6  *
      7  * This file is part of tor-dam
      8  *
      9  * This program is free software: you can redistribute it and/or modify
     10  * it under the terms of the GNU Affero General Public License as published by
     11  * the Free Software Foundation, either version 3 of the License, or
     12  * (at your option) any later version.
     13  *
     14  * This program is distributed in the hope that it will be useful,
     15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  * GNU Affero General Public License for more details.
     18  *
     19  * You should have received a copy of the GNU Affero General Public License
     20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     21  */
     22 
     23 import (
     24 	"context"
     25 	"fmt"
     26 	"log"
     27 	"os/exec"
     28 	"time"
     29 
     30 	"github.com/go-redis/redis"
     31 )
     32 
     33 // RedisAddress points us to our Redis instance.
     34 const RedisAddress = "127.0.0.1:6379"
     35 
     36 // Rctx is the context for Redis
     37 var Rctx = context.Background()
     38 
     39 // RedisCli is our global Redis client
     40 var RedisCli = redis.NewClient(&redis.Options{
     41 	Addr:     RedisAddress,
     42 	Password: "",
     43 	DB:       0,
     44 })
     45 
     46 // StartRedis is the function that will start up the Redis server. Takes the
     47 // path to a configuration file as an argument and returns error upon failure.
     48 func StartRedis(conf string) (*exec.Cmd, error) {
     49 	log.Println("Starting up redis-server...")
     50 	cmd := exec.Command("redis-server", conf)
     51 	err := cmd.Start()
     52 	if err != nil {
     53 		return cmd, err
     54 	}
     55 
     56 	time.Sleep(500 * time.Millisecond)
     57 	if _, err := RedisCli.Ping(Rctx).Result(); err != nil {
     58 		return cmd, err
     59 	}
     60 
     61 	PubSub := RedisCli.Subscribe(Rctx, PubSubChan)
     62 	if _, err := PubSub.Receive(Rctx); err != nil {
     63 		return cmd, err
     64 	}
     65 
     66 	log.Printf("Created \"%s\" channel in Redis.\n", PubSubChan)
     67 	return cmd, nil
     68 }
     69 
     70 // PublishToRedis is a function that publishes a node's status to Redis.
     71 // This is used for Gource visualization.
     72 func PublishToRedis(mt, address string) {
     73 	var timestamp, username, modtype, onion, pubstr string
     74 
     75 	nodedata, err := RedisCli.HGetAll(Rctx, address).Result()
     76 	CheckError(err)
     77 
     78 	timestamp = nodedata["lastseen"]
     79 	if timestamp == nodedata["firstseen"] {
     80 		modtype = "A"
     81 	} else if mt == "d" {
     82 		modtype = "D"
     83 	} else {
     84 		modtype = "M"
     85 	}
     86 	username = address
     87 	onion = address
     88 
     89 	pubstr = fmt.Sprintf("%s|%s|%s|%s", timestamp, username, modtype, onion)
     90 
     91 	RedisCli.Publish(Rctx, PubSubChan, pubstr)
     92 }