tordam

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

commit b28b13a0285e5459b23fc0b2ee42262f75efbf1a
parent a22b27bfe6cbfa02cc14b7eeaae4b90fe389eeb9
Author: parazyd <parazyd@dyne.org>
Date:   Sun, 21 Mar 2021 11:38:09 +0100

Improve logging.

Diffstat:
Mannounce_test.go | 1+
Mcmd/tor-dam/tor-dam.go | 3+++
Mlogging.go | 51++++++++++++++++++++++++++++++++++++++++++---------
Mpeer_announce.go | 6+++---
Mrpc_announce.go | 35++++++++++++++++++-----------------
5 files changed, 67 insertions(+), 29 deletions(-)

diff --git a/announce_test.go b/announce_test.go @@ -33,6 +33,7 @@ func TestAnnounce(t *testing.T) { } Cfg.Datadir = os.TempDir() + LogInit(os.Stdout) vals := []string{ "p7qaewjgnvnaeihhyybmoofd5avh665kr3awoxlh5rt6ox743kjdr6qd.onion:666", diff --git a/cmd/tor-dam/tor-dam.go b/cmd/tor-dam/tor-dam.go @@ -91,6 +91,9 @@ func main() { var wg sync.WaitGroup var err error + // Initialize tordam logger + tordam.LogInit(os.Stdout) + // Assign the global tordam data directory tordam.Cfg.Datadir = *datadir diff --git a/logging.go b/logging.go @@ -19,18 +19,51 @@ package tordam import ( "log" + "os" + "path/filepath" + "runtime" "strings" ) -func rpcWarn(msg ...string) { - text := strings.Join(msg[1:], " ") - log.Printf("RPC warning: (%s) %s", msg[0], text) +var ( + inte *log.Logger + warn *log.Logger + info *log.Logger +) + +// LogInit is the initializer for the internal tordam logging functions. +// It should be called from programs using the library, with something like: +// tordam.LogInit(os.Stdout) +func LogInit(f *os.File) { + inte = log.New(f, "(tordam) INTERNAL ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) + warn = log.New(f, "(tordam) WARNING: ", log.Ldate|log.Ltime) + info = log.New(f, "(tordam) INFO: ", log.Ldate|log.Ltime) } -func rpcInfo(msg ...string) { - text := strings.Join(msg[1:], " ") - log.Printf("RPC info: (%s) %s", msg[0], text) + +func fname() string { + pc, _, _, _ := runtime.Caller(2) + fn := runtime.FuncForPC(pc) + + var fnName string + + if fn == nil { + fnName = "?()" + } else { + dotName := filepath.Ext(fn.Name()) + fnName = strings.TrimLeft(dotName, ".") + "()" + } + + return fnName } -func rpcInternalErr(msg ...string) { - text := strings.Join(msg[1:], " ") - log.Printf("RPC internal error: (%s) %s", msg[0], text) + +func rpcWarn(msg string) { + warn.Printf("%s: %s", fname(), msg) +} + +func rpcInfo(msg string) { + info.Printf("%s: %s", fname(), msg) +} + +func rpcInternalErr(msg string) { + inte.Printf("%s: %s", fname(), msg) } diff --git a/peer_announce.go b/peer_announce.go @@ -21,7 +21,7 @@ import ( "context" "crypto/ed25519" "encoding/base64" - "log" + "fmt" "strings" "github.com/creachadair/jrpc2" @@ -33,7 +33,7 @@ import ( // success, it appends the peers received from the endpoint to the global // Peers map, which in turn also writes it to the peers db file. func Announce(onionaddr string) error { - log.Println("Announcing to", onionaddr) + rpcInfo(fmt.Sprintf("Announcing to %s", onionaddr)) if err := ValidateOnionInternal(onionaddr); err != nil { return err @@ -104,7 +104,7 @@ func AppendPeers(p []string) error { continue } if err := ValidateOnionInternal(i); err != nil { - log.Printf("warning: received garbage peer (%v)", err) + rpcWarn(fmt.Sprintf("received garbage peer (%v)", err)) continue } Peers[i] = Peer{} diff --git a/rpc_announce.go b/rpc_announce.go @@ -22,6 +22,7 @@ import ( "crypto/ed25519" "encoding/base64" "errors" + "fmt" "strings" "time" ) @@ -59,11 +60,11 @@ func (Ann) Init(ctx context.Context, vals []string) ([]string, error) { portmap := strings.Split(vals[2], ",") if err := ValidateOnionInternal(onion); err != nil { - rpcWarn("ann.Init", err.Error()) + rpcWarn(err.Error()) return nil, err } - rpcInfo("ann.Init", "got request for", onion) + rpcInfo(fmt.Sprintf("got request for %s", onion)) var peer Peer reallySeen := false @@ -78,39 +79,39 @@ func (Ann) Init(ctx context.Context, vals []string) ([]string, error) { if reallySeen { // Peer announced to us before if len(vals) != 4 { - rpcWarn("ann.Init", "no revocation key provided") + rpcWarn("no revocation key provided") return nil, errors.New("no revocation key provided") } revoke := vals[3] if strings.Compare(revoke, peer.PeerRevoke) != 0 { - rpcWarn("ann.Init", "revocation key doesn't match") + rpcWarn("revocation key doesn't match") return nil, errors.New("revocation key doesn't match") } } pk, err := base64.StdEncoding.DecodeString(pubkey) if err != nil { - rpcWarn("ann.Init", "got invalid base64 public key") + rpcWarn("got invalid base64 public key") return nil, errors.New("invalid base64 public key") } else if len(pk) != 32 { - rpcWarn("ann.Init", "got invalid pubkey (len != 32)") + rpcWarn("got invalid pubkey (len != 32)") return nil, errors.New("invalid public key") } if err := ValidatePortmap(portmap); err != nil { - rpcWarn("ann.Init", err.Error()) + rpcWarn(err.Error()) return nil, err } nonce, err := RandomGarbage(32) if err != nil { - rpcInternalErr("ann.Init", err.Error()) + rpcInternalErr(err.Error()) return nil, errors.New("internal error") } newrevoke, err := RandomGarbage(128) if err != nil { - rpcInternalErr("ann.Init", err.Error()) + rpcInternalErr(err.Error()) return nil, errors.New("internal error") } @@ -151,36 +152,36 @@ func (Ann) Validate(ctx context.Context, vals []string) ([]string, error) { signature := vals[1] if err := ValidateOnionInternal(onion); err != nil { - rpcWarn("ann.Validate", err.Error()) + rpcWarn(err.Error()) return nil, err } - rpcInfo("ann.Validate", "got request for", onion) + rpcInfo(fmt.Sprintf("got request for %s", onion)) peer, ok := Peers[onion] if !ok { - rpcWarn("ann.Validate", onion, "not in peer map") + rpcWarn(fmt.Sprintf("%s not in peer map", onion)) return nil, errors.New("this onion was not seen before") } if peer.Pubkey == nil || peer.Nonce == "" { - rpcWarn("ann.Validate", onion, "tried to validate before init") + rpcWarn(fmt.Sprintf("%s tried to validate before init", onion)) return nil, errors.New("tried to validate before init") } sig, err := base64.StdEncoding.DecodeString(signature) if err != nil { - rpcWarn("ann.Validate", "invalid base64 signature string") + rpcWarn("invalid base64 signature string") return nil, errors.New("invalid base64 signature string") } if !ed25519.Verify(peer.Pubkey, []byte(peer.Nonce), sig) { - rpcWarn("ann.Validate", "signature verification failed") + rpcWarn("signature verification failed") // delete(Peers, onion) return nil, errors.New("signature verification failed") } - rpcInfo("ann.Validate", "validation success for", onion) + rpcInfo(fmt.Sprintf("validation success for %s", onion)) var ret []string for addr, data := range Peers { @@ -194,6 +195,6 @@ func (Ann) Validate(ctx context.Context, vals []string) ([]string, error) { peer.LastSeen = time.Now().Unix() Peers[onion] = peer - rpcInfo("ann.Validate", "sending back list of peers to", onion) + rpcInfo(fmt.Sprintf("sending back list of peers to %s", onion)) return ret, nil }