tRemove functionality for writing peers.json. - tordam - A library for peer discovery inside the Tor network (HTM) git clone https://git.parazyd.org/tordam (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit a22b27bfe6cbfa02cc14b7eeaae4b90fe389eeb9 (DIR) parent 96c148ef56eaa2f1d8bc5314568ea9d4ce265922 (HTM) Author: parazyd <parazyd@dyne.org> Date: Sun, 21 Mar 2021 11:06:16 +0100 Remove functionality for writing peers.json. This should be done externally, and not in the library. Diffstat: M announce_test.go | 2 -- M cmd/tor-dam/tor-dam.go | 1 - M config.go | 12 ------------ D database.go | 51 ------------------------------- M peer_announce.go | 5 +---- M rpc_announce.go | 4 ---- 6 files changed, 1 insertion(+), 74 deletions(-) --- (DIR) diff --git a/announce_test.go b/announce_test.go t@@ -23,7 +23,6 @@ import ( "crypto/rand" "encoding/base64" "os" - "path/filepath" "testing" ) t@@ -34,7 +33,6 @@ func TestAnnounce(t *testing.T) { } Cfg.Datadir = os.TempDir() - defer os.Remove(filepath.Join(Cfg.Datadir, dbFile)) vals := []string{ "p7qaewjgnvnaeihhyybmoofd5avh665kr3awoxlh5rt6ox743kjdr6qd.onion:666", (DIR) diff --git a/cmd/tor-dam/tor-dam.go b/cmd/tor-dam/tor-dam.go t@@ -205,7 +205,6 @@ func main() { } // Marshal the global Peers map to JSON and print it out. - // Internally, the db is also saved (written to a file) in the datadir. j, _ := json.Marshal(tordam.Peers) log.Println(string(j)) } (DIR) diff --git a/config.go b/config.go t@@ -18,11 +18,8 @@ package tordam import ( - "context" "crypto/ed25519" "net" - - "golang.org/x/sync/semaphore" ) // Config is the configuration structure, to be filled by library user. t@@ -45,12 +42,3 @@ var Cfg = Config{} // Peers is the global map of peers var Peers = map[string]Peer{} - -// dbSem is the internal semaphore for writing the peer db -var dbSem = semaphore.NewWeighted(1) - -// dbSemCtx is the context for dbSem -var dbSemCtx = context.Background() - -// dbFile is the filename for the Peers JSON database -var dbFile = "peers.json" (DIR) diff --git a/database.go b/database.go t@@ -1,51 +0,0 @@ -// Copyright (c) 2017-2021 Ivan Jelincic <parazyd@dyne.org> -// -// This file is part of tordam -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see <https://www.gnu.org/licenses/>. - -package tordam - -import ( - "encoding/json" - "io/ioutil" - "log" -) - -// WritePeersDB marshals the Peers global to JSON and writes to given file. -// Please note that this should be probably used in conjunction with some sort -// of semaphore. -func WritePeersDB(file string) error { - j, err := json.Marshal(Peers) - if err != nil { - return err - } - return ioutil.WriteFile(file, j, 0600) -} - -// writePeersDBWithSem is an internal function to call WritePeersDB safely -// using an internal semaphore. Programs using this library should probably -// implement something similar if they want to write Peers to a file. -func writePeersDBWithSem(file string) { - if err := dbSem.Acquire(dbSemCtx, 1); err != nil { - log.Println("warning: failed to acquire sem for writing:", err) - return - } - go func() { - if err := WritePeersDB(file); err != nil { - log.Println("warning: failed to write peers db:", err) - } - dbSem.Release(1) - }() -} (DIR) diff --git a/peer_announce.go b/peer_announce.go t@@ -22,7 +22,6 @@ import ( "crypto/ed25519" "encoding/base64" "log" - "path/filepath" "strings" "github.com/creachadair/jrpc2" t@@ -96,8 +95,7 @@ func Announce(onionaddr string) error { // AppendPeers appends given []string peers to the global Peers map. Usually // received by validating ourself to a peer and them replying with a list of // their valid peers. If a peer is not in format of "unlikelyname.onion:port", -// they will not be appended. When done, the function also writes the Peers -// struct as a JSON file in the Datadir. +// they will not be appended. // As a placeholder, this function can return an error, but it has no reason // to do so right now. func AppendPeers(p []string) error { t@@ -112,6 +110,5 @@ func AppendPeers(p []string) error { Peers[i] = Peer{} } - writePeersDBWithSem(filepath.Join(Cfg.Datadir, dbFile)) return nil } (DIR) diff --git a/rpc_announce.go b/rpc_announce.go t@@ -22,7 +22,6 @@ import ( "crypto/ed25519" "encoding/base64" "errors" - "path/filepath" "strings" "time" ) t@@ -123,7 +122,6 @@ func (Ann) Init(ctx context.Context, vals []string) ([]string, error) { peer.Trusted = 0 Peers[onion] = peer - writePeersDBWithSem(filepath.Join(Cfg.Datadir, dbFile)) return []string{nonce, newrevoke}, nil } t@@ -196,8 +194,6 @@ func (Ann) Validate(ctx context.Context, vals []string) ([]string, error) { peer.LastSeen = time.Now().Unix() Peers[onion] = peer - writePeersDBWithSem(filepath.Join(Cfg.Datadir, dbFile)) - rpcInfo("ann.Validate", "sending back list of peers to", onion) return ret, nil }