mirror of
https://github.com/fiatjaf/nak.git
synced 2024-10-30 09:09:08 -04:00
print event more consistently and auth when required and allowed.
This commit is contained in:
parent
26b1aa359a
commit
4d75605c20
67
event.go
67
event.go
|
@ -47,6 +47,10 @@ example:
|
||||||
Name: "envelope",
|
Name: "envelope",
|
||||||
Usage: "print the event enveloped in a [\"EVENT\", ...] message ready to be sent to a relay",
|
Usage: "print the event enveloped in a [\"EVENT\", ...] message ready to be sent to a relay",
|
||||||
},
|
},
|
||||||
|
&cli.BoolFlag{
|
||||||
|
Name: "auth",
|
||||||
|
Usage: "always perform NIP-42 \"AUTH\" when facing an \"auth-required: \" rejection and try again",
|
||||||
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "nson",
|
Name: "nson",
|
||||||
Usage: "encode the event using NSON",
|
Usage: "encode the event using NSON",
|
||||||
|
@ -110,6 +114,8 @@ example:
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doAuth := c.Bool("auth")
|
||||||
|
|
||||||
// then process input and generate events
|
// then process input and generate events
|
||||||
for stdinEvent := range getStdinLinesOrBlank() {
|
for stdinEvent := range getStdinLinesOrBlank() {
|
||||||
evt := nostr.Event{
|
evt := nostr.Event{
|
||||||
|
@ -118,6 +124,7 @@ example:
|
||||||
|
|
||||||
kindWasSupplied := false
|
kindWasSupplied := false
|
||||||
mustRehashAndResign := false
|
mustRehashAndResign := false
|
||||||
|
|
||||||
if stdinEvent != "" {
|
if stdinEvent != "" {
|
||||||
if err := easyjson.Unmarshal([]byte(stdinEvent), &evt); err != nil {
|
if err := easyjson.Unmarshal([]byte(stdinEvent), &evt); err != nil {
|
||||||
lineProcessingError(c, "invalid event received from stdin: %s", err)
|
lineProcessingError(c, "invalid event received from stdin: %s", err)
|
||||||
|
@ -192,24 +199,7 @@ example:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(relays) > 0 {
|
// print event as json
|
||||||
fmt.Println(evt.String())
|
|
||||||
os.Stdout.Sync()
|
|
||||||
for _, relay := range relays {
|
|
||||||
log("publishing to %s... ", relay.URL)
|
|
||||||
if relay, err := nostr.RelayConnect(c.Context, relay.URL); err != nil {
|
|
||||||
log("failed to connect: %s\n", err)
|
|
||||||
} else {
|
|
||||||
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
if status, err := relay.Publish(ctx, evt); err != nil {
|
|
||||||
log("failed: %s\n", err)
|
|
||||||
} else {
|
|
||||||
log("%s.\n", status)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
var result string
|
var result string
|
||||||
if c.Bool("envelope") {
|
if c.Bool("envelope") {
|
||||||
j, _ := json.Marshal(nostr.EventEnvelope{Event: evt})
|
j, _ := json.Marshal(nostr.EventEnvelope{Event: evt})
|
||||||
|
@ -221,9 +211,50 @@ example:
|
||||||
result = string(j)
|
result = string(j)
|
||||||
}
|
}
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// publish to relays
|
||||||
|
if len(relays) > 0 {
|
||||||
|
os.Stdout.Sync()
|
||||||
|
for _, relay := range relays {
|
||||||
|
log("publishing to %s... ", relay.URL)
|
||||||
|
if relay, err := nostr.RelayConnect(c.Context, relay.URL); err != nil {
|
||||||
|
log("failed to connect: %s\n", err)
|
||||||
|
} else {
|
||||||
|
publish:
|
||||||
|
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
status, err := relay.Publish(ctx, evt)
|
||||||
|
if err == nil {
|
||||||
|
// published fine probably
|
||||||
|
log("%s.\n", status)
|
||||||
|
goto end
|
||||||
|
}
|
||||||
|
|
||||||
|
// error publishing
|
||||||
|
if isAuthRequired(err.Error()) && sec != "" && doAuth {
|
||||||
|
// if the relay is requesting auth and we can auth, let's do it
|
||||||
|
log("performing auth... ")
|
||||||
|
st, err := relay.Auth(c.Context, func(evt *nostr.Event) error { return evt.Sign(sec) })
|
||||||
|
if st == nostr.PublishStatusSucceeded {
|
||||||
|
// try to publish again, but this time don't try to auth again
|
||||||
|
doAuth = false
|
||||||
|
goto publish
|
||||||
|
} else {
|
||||||
|
// auth error
|
||||||
|
if err == nil {
|
||||||
|
err = fmt.Errorf("no response from relay")
|
||||||
|
}
|
||||||
|
log("auth error: %s. ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log("failed: %s\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
end:
|
||||||
exitIfLineProcessingError(c)
|
exitIfLineProcessingError(c)
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
|
|
@ -164,3 +164,7 @@ func gatherSecretKeyFromArguments(c *cli.Context) (string, error) {
|
||||||
|
|
||||||
return sec, nil
|
return sec, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isAuthRequired(msg string) bool {
|
||||||
|
return strings.HasPrefix(msg, "msg: auth-required:")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user