2023-11-20 13:00:50 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-11 14:34:15 -04:00
|
|
|
"bytes"
|
2024-06-25 21:18:26 -04:00
|
|
|
"context"
|
2024-07-11 14:34:15 -04:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
2023-11-20 13:00:50 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-07-11 14:34:15 -04:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-11-20 13:00:50 -05:00
|
|
|
|
2024-07-30 10:43:14 -04:00
|
|
|
"github.com/fiatjaf/cli/v3"
|
2024-07-11 14:34:15 -04:00
|
|
|
"github.com/nbd-wtf/go-nostr"
|
2023-11-20 13:00:50 -05:00
|
|
|
"github.com/nbd-wtf/go-nostr/nip11"
|
2024-07-11 14:34:15 -04:00
|
|
|
"github.com/nbd-wtf/go-nostr/nip86"
|
2023-11-20 13:00:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var relay = &cli.Command{
|
|
|
|
Name: "relay",
|
|
|
|
Usage: "gets the relay information document for the given relay, as JSON",
|
|
|
|
Description: `example:
|
2023-11-20 13:01:51 -05:00
|
|
|
nak relay nostr.wine`,
|
2024-08-07 10:46:07 -04:00
|
|
|
ArgsUsage: "<relay-url>",
|
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2024-03-19 10:34:59 -04:00
|
|
|
for url := range getStdinLinesOrArguments(c.Args()) {
|
|
|
|
if url == "" {
|
|
|
|
return fmt.Errorf("specify the <relay-url>")
|
|
|
|
}
|
2023-11-20 13:00:50 -05:00
|
|
|
|
2024-06-25 21:18:26 -04:00
|
|
|
info, err := nip11.Fetch(ctx, url)
|
2024-03-19 10:34:59 -04:00
|
|
|
if err != nil {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "failed to fetch '%s' information document: %w", url, err)
|
2024-03-19 10:34:59 -04:00
|
|
|
continue
|
|
|
|
}
|
2023-11-20 13:00:50 -05:00
|
|
|
|
2024-03-19 10:34:59 -04:00
|
|
|
pretty, _ := json.MarshalIndent(info, "", " ")
|
|
|
|
stdout(string(pretty))
|
|
|
|
}
|
2023-11-20 13:00:50 -05:00
|
|
|
return nil
|
|
|
|
},
|
2024-07-11 14:34:15 -04:00
|
|
|
Commands: (func() []*cli.Command {
|
|
|
|
commands := make([]*cli.Command, 0, 12)
|
|
|
|
|
|
|
|
for _, def := range []struct {
|
|
|
|
method string
|
|
|
|
args []string
|
2024-07-12 12:57:58 -04:00
|
|
|
}{
|
|
|
|
{"allowpubkey", []string{"pubkey", "reason"}},
|
|
|
|
{"banpubkey", []string{"pubkey", "reason"}},
|
|
|
|
{"listallowedpubkeys", nil},
|
|
|
|
{"allowpubkey", []string{"pubkey", "reason"}},
|
|
|
|
{"listallowedpubkeys", nil},
|
|
|
|
{"listeventsneedingmoderation", nil},
|
|
|
|
{"allowevent", []string{"id", "reason"}},
|
|
|
|
{"banevent", []string{"id", "reason"}},
|
|
|
|
{"listbannedevents", nil},
|
|
|
|
{"changerelayname", []string{"name"}},
|
|
|
|
{"changerelaydescription", []string{"description"}},
|
|
|
|
{"changerelayicon", []string{"icon"}},
|
|
|
|
{"allowkind", []string{"kind"}},
|
|
|
|
{"disallowkind", []string{"kind"}},
|
|
|
|
{"listallowedkinds", nil},
|
|
|
|
{"blockip", []string{"ip", "reason"}},
|
|
|
|
{"unblockip", []string{"ip", "reason"}},
|
|
|
|
{"listblockedips", nil},
|
|
|
|
} {
|
|
|
|
def := def
|
|
|
|
|
2024-07-11 14:34:15 -04:00
|
|
|
flags := make([]cli.Flag, len(def.args), len(def.args)+4)
|
|
|
|
for i, argName := range def.args {
|
|
|
|
flags[i] = declareFlag(argName)
|
|
|
|
}
|
|
|
|
|
2024-09-17 10:27:59 -04:00
|
|
|
flags = append(flags, defaultKeyFlags...)
|
2024-07-11 14:34:15 -04:00
|
|
|
|
|
|
|
cmd := &cli.Command{
|
|
|
|
Name: def.method,
|
|
|
|
Usage: fmt.Sprintf(`the "%s" relay management RPC call`, def.method),
|
|
|
|
Description: fmt.Sprintf(
|
|
|
|
`the "%s" management RPC call, see https://nips.nostr.com/86 for more information`, def.method),
|
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
|
|
|
params := make([]any, len(def.args))
|
|
|
|
for i, argName := range def.args {
|
|
|
|
params[i] = getArgument(c, argName)
|
|
|
|
}
|
|
|
|
req := nip86.Request{Method: def.method, Params: params}
|
|
|
|
reqj, _ := json.Marshal(req)
|
|
|
|
|
|
|
|
relayUrls := c.Args().Slice()
|
|
|
|
if len(relayUrls) == 0 {
|
|
|
|
stdout(string(reqj))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-29 20:11:15 -04:00
|
|
|
kr, _, err := gatherKeyerFromArguments(ctx, c)
|
2024-07-11 14:34:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, relayUrl := range relayUrls {
|
|
|
|
httpUrl := "http" + nostr.NormalizeURL(relayUrl)[2:]
|
2024-07-12 12:57:58 -04:00
|
|
|
log("calling '%s' on %s... ", def.method, httpUrl)
|
2024-07-11 14:34:15 -04:00
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
body.Write(reqj)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", httpUrl, body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authorization
|
|
|
|
payloadHash := sha256.Sum256(reqj)
|
2024-09-17 10:27:59 -04:00
|
|
|
tokenEvent := nostr.Event{
|
2024-07-11 14:34:15 -04:00
|
|
|
Kind: 27235,
|
|
|
|
CreatedAt: nostr.Now(),
|
|
|
|
Tags: nostr.Tags{
|
2024-07-16 12:26:54 -04:00
|
|
|
{"u", httpUrl},
|
|
|
|
{"method", "POST"},
|
2024-07-11 14:34:15 -04:00
|
|
|
{"payload", hex.EncodeToString(payloadHash[:])},
|
|
|
|
},
|
|
|
|
}
|
2024-09-17 10:27:59 -04:00
|
|
|
if err := kr.SignEvent(ctx, &tokenEvent); err != nil {
|
|
|
|
return fmt.Errorf("failed to sign token event: %w", err)
|
2024-07-11 14:34:15 -04:00
|
|
|
}
|
2024-09-17 10:27:59 -04:00
|
|
|
evtj, _ := json.Marshal(tokenEvent)
|
2024-07-11 14:34:15 -04:00
|
|
|
req.Header.Set("Authorization", "Nostr "+base64.StdEncoding.EncodeToString(evtj))
|
|
|
|
|
|
|
|
// Content-Type
|
|
|
|
req.Header.Set("Content-Type", "application/nostr+json+rpc")
|
|
|
|
|
|
|
|
// make request to relay
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log("failed: %s\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log("failed to read response: %s\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 300 {
|
|
|
|
log("failed with status %d\n", resp.StatusCode)
|
|
|
|
bodyPrintable := string(b)
|
|
|
|
if len(bodyPrintable) > 300 {
|
|
|
|
bodyPrintable = bodyPrintable[0:297] + "..."
|
|
|
|
}
|
|
|
|
log(bodyPrintable)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var response nip86.Response
|
|
|
|
if err := json.Unmarshal(b, &response); err != nil {
|
|
|
|
log("bad json response: %s\n", err)
|
|
|
|
bodyPrintable := string(b)
|
|
|
|
if len(bodyPrintable) > 300 {
|
|
|
|
bodyPrintable = bodyPrintable[0:297] + "..."
|
|
|
|
}
|
|
|
|
log(bodyPrintable)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
// print the result
|
|
|
|
log("\n")
|
|
|
|
pretty, _ := json.MarshalIndent(response, "", " ")
|
|
|
|
stdout(string(pretty))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Flags: flags,
|
|
|
|
}
|
|
|
|
|
|
|
|
commands = append(commands, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return commands
|
|
|
|
})(),
|
|
|
|
}
|
|
|
|
|
|
|
|
func declareFlag(argName string) cli.Flag {
|
|
|
|
usage := "parameter for this management RPC call, see https://nips.nostr.com/86 for more information."
|
|
|
|
switch argName {
|
|
|
|
case "kind":
|
|
|
|
return &cli.IntFlag{Name: argName, Required: true, Usage: usage}
|
|
|
|
case "reason":
|
|
|
|
return &cli.StringFlag{Name: argName, Usage: usage}
|
|
|
|
default:
|
|
|
|
return &cli.StringFlag{Name: argName, Required: true, Usage: usage}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getArgument(c *cli.Command, argName string) any {
|
|
|
|
switch argName {
|
|
|
|
case "kind":
|
|
|
|
return c.Int(argName)
|
|
|
|
default:
|
|
|
|
return c.String(argName)
|
|
|
|
}
|
2023-11-20 13:00:50 -05:00
|
|
|
}
|