tordam

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

commit 475f06264c63ca48384bbc8c0f1492fd7d4512c8
parent bf06d4818e02fde075fbb2748afc06b37c6ba376
Author: parazyd <parazyd@dyne.org>
Date:   Sat, 27 Oct 2018 12:49:19 +0200

Implement polling routine for deleting expired nodes.

This adds a -ttl flag that will make a polling goroutine active and make
it poll all the nodes it finds in Redis. If their lastseen is more than
the TTL interval, they will be deleted.

This does not disallow them to reappear once they annouce again.

Diffstat:
Mcmd/dam-dir/main.go | 33+++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+), 0 deletions(-)

diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go @@ -27,6 +27,7 @@ import ( "net/http" "os" "os/exec" + "strconv" "sync" "time" @@ -209,6 +210,30 @@ 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() + lib.CheckError(err) + now := time.Time.Unix(time.Now()) + + for _, i := range nodes { + res, err := lib.RedisCli.HGet(i, "lastseen").Result() + lib.CheckError(err) + lastseen, err := strconv.Atoi(res) + lib.CheckError(err) + + diff := int64((now - int64(lastseen)) / 60) + if diff > interval { + log.Printf("Deleting %s from redis because of expiration\n", i) + // TODO: Redis pubsub + lib.RedisCli.Del(i) + } + } + time.Sleep(time.Duration(interval) * time.Minute) + } +} + // handleElse is a noop for anything that isn't /announce. We don't care about // other requests (yet). func handleElse(rw http.ResponseWriter, request *http.Request) {} @@ -216,8 +241,10 @@ func handleElse(rw http.ResponseWriter, request *http.Request) {} func main() { var wg sync.WaitGroup var t bool + var ttl int64 flag.BoolVar(&t, "t", false, "Mark all new nodes valid initially") + flag.Int64Var(&ttl, "ttl", 0, "Set expiry time in minutes (TTL) for nodes") flag.Parse() if t { @@ -249,6 +276,12 @@ func main() { wg.Add(1) go srv.ListenAndServe() log.Println("Listening on", ListenAddress) + + if ttl > 0 { + log.Println("Enabling TTL polling.") + go pollNodeTTL(ttl) + } + wg.Wait() os.Exit(1) }