commit a22b27bfe6cbfa02cc14b7eeaae4b90fe389eeb9
parent 96c148ef56eaa2f1d8bc5314568ea9d4ce265922
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:
6 files changed, 1 insertion(+), 74 deletions(-)
diff --git a/announce_test.go b/announce_test.go
@@ -23,7 +23,6 @@ import (
"crypto/rand"
"encoding/base64"
"os"
- "path/filepath"
"testing"
)
@@ -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",
diff --git a/cmd/tor-dam/tor-dam.go b/cmd/tor-dam/tor-dam.go
@@ -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))
}
diff --git a/config.go b/config.go
@@ -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.
@@ -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"
diff --git a/database.go b/database.go
@@ -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)
- }()
-}
diff --git a/peer_announce.go b/peer_announce.go
@@ -22,7 +22,6 @@ import (
"crypto/ed25519"
"encoding/base64"
"log"
- "path/filepath"
"strings"
"github.com/creachadair/jrpc2"
@@ -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 {
@@ -112,6 +110,5 @@ func AppendPeers(p []string) error {
Peers[i] = Peer{}
}
- writePeersDBWithSem(filepath.Join(Cfg.Datadir, dbFile))
return nil
}
diff --git a/rpc_announce.go b/rpc_announce.go
@@ -22,7 +22,6 @@ import (
"crypto/ed25519"
"encoding/base64"
"errors"
- "path/filepath"
"strings"
"time"
)
@@ -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
}
@@ -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
}