From 34c189af2868c9a5d7f95a26a4dd88c9660f112f Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 2 Mar 2024 08:18:40 -0300 Subject: [PATCH] bunker improvements. --- bunker.go | 39 +++++++++++++++++++++------------------ go.mod | 3 ++- go.sum | 6 ++++-- helpers.go | 4 +++- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/bunker.go b/bunker.go index 2991f25..2191701 100644 --- a/bunker.go +++ b/bunker.go @@ -101,11 +101,16 @@ var bunker = &cli.Command{ _, cancel := context.WithCancel(c.Context) cancelPreviousBunkerInfoPrint = cancel + // asking user for authorization + signer.AuthorizeRequest = func(harmless bool, from string) bool { + return alwaysYes || harmless || askProceed(from) + } + for ie := range events { cancelPreviousBunkerInfoPrint() // this prevents us from printing a million bunker info blocks // handle the NIP-46 request event - req, resp, eventResponse, harmless, err := signer.HandleRequest(ie.Event) + req, resp, eventResponse, err := signer.HandleRequest(ie.Event) if err != nil { log("< failed to handle request from %s: %s\n", ie.Event.PubKey, err.Error()) continue @@ -116,25 +121,23 @@ var bunker = &cli.Command{ jresp, _ := json.MarshalIndent(resp, " ", " ") log("~ responding with %s\n", string(jresp)) - if alwaysYes || harmless || askProceed(ie.Event.PubKey) { - handlerWg.Add(len(relayURLs)) - for _, relayURL := range relayURLs { - go func(relayURL string) { - if relay, _ := pool.EnsureRelay(relayURL); relay != nil { - err := relay.Publish(c.Context, eventResponse) - printLock.Lock() - if err == nil { - log("* sent response through %s\n", relay.URL) - } else { - log("* failed to send response: %s\n", err) - } - printLock.Unlock() - handlerWg.Done() + handlerWg.Add(len(relayURLs)) + for _, relayURL := range relayURLs { + go func(relayURL string) { + if relay, _ := pool.EnsureRelay(relayURL); relay != nil { + err := relay.Publish(c.Context, eventResponse) + printLock.Lock() + if err == nil { + log("* sent response through %s\n", relay.URL) + } else { + log("* failed to send response: %s\n", err) } - }(relayURL) - } - handlerWg.Wait() + printLock.Unlock() + handlerWg.Done() + } + }(relayURL) } + handlerWg.Wait() // just after handling one request we trigger this go func() { diff --git a/go.mod b/go.mod index f1b2328..286d2d0 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e github.com/fatih/color v1.16.0 github.com/mailru/easyjson v0.7.7 - github.com/nbd-wtf/go-nostr v0.28.6 + github.com/nbd-wtf/go-nostr v0.29.0 github.com/nbd-wtf/nostr-sdk v0.0.5 github.com/urfave/cli/v2 v2.25.7 golang.org/x/exp v0.0.0-20231006140011-7918f672742d @@ -38,4 +38,5 @@ require ( github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect golang.org/x/crypto v0.7.0 // indirect golang.org/x/sys v0.14.0 // indirect + golang.org/x/text v0.8.0 // indirect ) diff --git a/go.sum b/go.sum index a23cf72..ac963f2 100644 --- a/go.sum +++ b/go.sum @@ -79,8 +79,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/nbd-wtf/go-nostr v0.28.6 h1:iOyzk+6ReG0lvyRAar7w7omFmUk5mnXDyFYkJ+zEjiw= -github.com/nbd-wtf/go-nostr v0.28.6/go.mod h1:aFcp8NO3erHg+glzBfh4wpaMrV1/ahcUPAgITdptxwA= +github.com/nbd-wtf/go-nostr v0.29.0 h1:kpAZ9oQPFeB9aJPloCsGS+UCNDPyN0jkt7sxlxxZock= +github.com/nbd-wtf/go-nostr v0.29.0/go.mod h1:tiKJY6fWYSujbTQb201Y+IQ3l4szqYVt+fsTnsm7FCk= github.com/nbd-wtf/nostr-sdk v0.0.5 h1:rec+FcDizDVO0W25PX0lgYMXvP7zNNOgI3Fu9UCm4BY= github.com/nbd-wtf/nostr-sdk v0.0.5/go.mod h1:iJJsikesCGLNFZ9dLqhLPDzdt924EagUmdQxT3w2Lmk= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= @@ -142,6 +142,8 @@ golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/helpers.go b/helpers.go index 858f6a4..30b2bfd 100644 --- a/helpers.go +++ b/helpers.go @@ -142,7 +142,9 @@ func gatherSecretKeyOrBunkerFromArguments(c *cli.Context) (string, *nip46.Bunker } else { clientKey = nostr.GeneratePrivateKey() } - bunker, err := nip46.ConnectBunker(c.Context, clientKey, bunkerURL, nil) + bunker, err := nip46.ConnectBunker(c.Context, clientKey, bunkerURL, nil, func(s string) { + fmt.Fprintf(os.Stderr, color.CyanString("[nip46]: open the following URL: %s"), s) + }) return "", bunker, err } sec := c.String("sec")