mirror of
https://github.com/fiatjaf/nak.git
synced 2024-11-22 08:19:06 -05:00
nak req: --force-pre-auth flag.
This commit is contained in:
parent
441ee9a5ed
commit
ac00c5065f
|
@ -50,7 +50,7 @@ var bunker = &cli.Command{
|
||||||
qs := url.Values{}
|
qs := url.Values{}
|
||||||
relayURLs := make([]string, 0, c.Args().Len())
|
relayURLs := make([]string, 0, c.Args().Len())
|
||||||
if relayUrls := c.Args().Slice(); len(relayUrls) > 0 {
|
if relayUrls := c.Args().Slice(); len(relayUrls) > 0 {
|
||||||
_, relays := connectToAllRelays(ctx, relayUrls)
|
_, relays := connectToAllRelays(ctx, relayUrls, false)
|
||||||
if len(relays) == 0 {
|
if len(relays) == 0 {
|
||||||
log("failed to connect to any of the given relays.\n")
|
log("failed to connect to any of the given relays.\n")
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
|
|
2
event.go
2
event.go
|
@ -145,7 +145,7 @@ example:
|
||||||
// try to connect to the relays here
|
// try to connect to the relays here
|
||||||
var relays []*nostr.Relay
|
var relays []*nostr.Relay
|
||||||
if relayUrls := c.Args().Slice(); len(relayUrls) > 0 {
|
if relayUrls := c.Args().Slice(); len(relayUrls) > 0 {
|
||||||
_, relays = connectToAllRelays(ctx, relayUrls)
|
_, relays = connectToAllRelays(ctx, relayUrls, false)
|
||||||
if len(relays) == 0 {
|
if len(relays) == 0 {
|
||||||
log("failed to connect to any of the given relays.\n")
|
log("failed to connect to any of the given relays.\n")
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
|
|
36
helpers.go
36
helpers.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/chzyer/readline"
|
"github.com/chzyer/readline"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
@ -117,13 +118,48 @@ func normalizeAndValidateRelayURLs(wsurls []string) error {
|
||||||
func connectToAllRelays(
|
func connectToAllRelays(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
relayUrls []string,
|
relayUrls []string,
|
||||||
|
forcePreAuth bool,
|
||||||
opts ...nostr.PoolOption,
|
opts ...nostr.PoolOption,
|
||||||
) (*nostr.SimplePool, []*nostr.Relay) {
|
) (*nostr.SimplePool, []*nostr.Relay) {
|
||||||
relays := make([]*nostr.Relay, 0, len(relayUrls))
|
relays := make([]*nostr.Relay, 0, len(relayUrls))
|
||||||
pool := nostr.NewSimplePool(ctx, opts...)
|
pool := nostr.NewSimplePool(ctx, opts...)
|
||||||
|
relayLoop:
|
||||||
for _, url := range relayUrls {
|
for _, url := range relayUrls {
|
||||||
log("connecting to %s... ", url)
|
log("connecting to %s... ", url)
|
||||||
if relay, err := pool.EnsureRelay(url); err == nil {
|
if relay, err := pool.EnsureRelay(url); err == nil {
|
||||||
|
if forcePreAuth {
|
||||||
|
log("waiting for auth challenge... ")
|
||||||
|
signer := opts[0].(nostr.WithAuthHandler)
|
||||||
|
time.Sleep(time.Millisecond * 200)
|
||||||
|
challengeWaitLoop:
|
||||||
|
for {
|
||||||
|
// beginhack
|
||||||
|
// here starts the biggest and ugliest hack of this codebase
|
||||||
|
if err := relay.Auth(ctx, func(authEvent *nostr.Event) error {
|
||||||
|
challengeTag := authEvent.Tags.GetFirst([]string{"challenge", ""})
|
||||||
|
if (*challengeTag)[1] == "" {
|
||||||
|
return fmt.Errorf("auth not received yet *****")
|
||||||
|
}
|
||||||
|
return signer(authEvent)
|
||||||
|
}); err == nil {
|
||||||
|
// auth succeeded
|
||||||
|
break challengeWaitLoop
|
||||||
|
} else {
|
||||||
|
// auth failed
|
||||||
|
if strings.HasSuffix(err.Error(), "auth not received yet *****") {
|
||||||
|
// it failed because we didn't receive the challenge yet, so keep waiting
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
continue challengeWaitLoop
|
||||||
|
} else {
|
||||||
|
// it failed for some other reason, so skip this relay
|
||||||
|
log(err.Error() + "\n")
|
||||||
|
continue relayLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// endhack
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
relays = append(relays, relay)
|
relays = append(relays, relay)
|
||||||
log("ok.\n")
|
log("ok.\n")
|
||||||
} else {
|
} else {
|
||||||
|
|
12
req.go
12
req.go
|
@ -104,6 +104,11 @@ example:
|
||||||
Name: "auth",
|
Name: "auth",
|
||||||
Usage: "always perform NIP-42 \"AUTH\" when facing an \"auth-required: \" rejection and try again",
|
Usage: "always perform NIP-42 \"AUTH\" when facing an \"auth-required: \" rejection and try again",
|
||||||
},
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "force-pre-auth",
|
||||||
|
Aliases: []string{"fpa"},
|
||||||
|
Usage: "after connecting, for a NIP-42 \"AUTH\" message to be received, act on it and only then send the \"REQ\"",
|
||||||
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "sec",
|
Name: "sec",
|
||||||
Usage: "secret key to sign the AUTH challenge, as hex or nsec",
|
Usage: "secret key to sign the AUTH challenge, as hex or nsec",
|
||||||
|
@ -127,11 +132,12 @@ example:
|
||||||
ArgsUsage: "[relay...]",
|
ArgsUsage: "[relay...]",
|
||||||
Action: func(ctx context.Context, c *cli.Command) error {
|
Action: func(ctx context.Context, c *cli.Command) error {
|
||||||
var pool *nostr.SimplePool
|
var pool *nostr.SimplePool
|
||||||
|
|
||||||
relayUrls := c.Args().Slice()
|
relayUrls := c.Args().Slice()
|
||||||
if len(relayUrls) > 0 {
|
if len(relayUrls) > 0 {
|
||||||
var relays []*nostr.Relay
|
var relays []*nostr.Relay
|
||||||
pool, relays = connectToAllRelays(ctx, relayUrls, nostr.WithAuthHandler(func(evt *nostr.Event) error {
|
pool, relays = connectToAllRelays(ctx, relayUrls, c.Bool("force-pre-auth"), nostr.WithAuthHandler(func(evt *nostr.Event) error {
|
||||||
if !c.Bool("auth") {
|
if !c.Bool("auth") && !c.Bool("force-pre-auth") {
|
||||||
return fmt.Errorf("auth not authorized")
|
return fmt.Errorf("auth not authorized")
|
||||||
}
|
}
|
||||||
sec, bunker, err := gatherSecretKeyOrBunkerFromArguments(ctx, c)
|
sec, bunker, err := gatherSecretKeyOrBunkerFromArguments(ctx, c)
|
||||||
|
@ -148,7 +154,7 @@ example:
|
||||||
} else {
|
} else {
|
||||||
pk, _ = nostr.GetPublicKey(sec)
|
pk, _ = nostr.GetPublicKey(sec)
|
||||||
}
|
}
|
||||||
log("performing auth as %s...\n", pk)
|
log("performing auth as %s... ", pk)
|
||||||
|
|
||||||
if bunker != nil {
|
if bunker != nil {
|
||||||
return bunker.SignEvent(ctx, evt)
|
return bunker.SignEvent(ctx, evt)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user