2023-12-02 10:18:55 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-02-17 15:56:57 -05:00
|
|
|
"context"
|
2023-12-02 10:18:55 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2024-05-15 16:31:01 -04:00
|
|
|
"strings"
|
2024-02-16 09:08:48 -05:00
|
|
|
"sync"
|
2024-02-17 15:56:57 -05:00
|
|
|
"time"
|
2023-12-02 10:18:55 -05:00
|
|
|
|
2024-02-12 13:39:13 -05:00
|
|
|
"github.com/fatih/color"
|
2024-07-30 10:43:14 -04:00
|
|
|
"github.com/fiatjaf/cli/v3"
|
2023-12-02 10:18:55 -05:00
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip19"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip46"
|
2023-12-09 15:42:01 -05:00
|
|
|
"golang.org/x/exp/slices"
|
2023-12-02 10:18:55 -05:00
|
|
|
)
|
|
|
|
|
2023-12-09 14:37:47 -05:00
|
|
|
var bunker = &cli.Command{
|
2024-08-07 10:46:07 -04:00
|
|
|
Name: "bunker",
|
|
|
|
Usage: "starts a NIP-46 signer daemon with the given --sec key",
|
|
|
|
ArgsUsage: "[relay...]",
|
|
|
|
Description: ``,
|
|
|
|
DisableSliceFlagSeparator: true,
|
2023-12-02 10:18:55 -05:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "sec",
|
|
|
|
Usage: "secret key to sign the event, as hex or nsec",
|
|
|
|
DefaultText: "the key '1'",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "prompt-sec",
|
|
|
|
Usage: "prompt the user to paste a hex or nsec with which to sign the event",
|
|
|
|
},
|
2024-05-15 16:31:01 -04:00
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "authorized-secrets",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Usage: "secrets for which we will always respond",
|
|
|
|
},
|
|
|
|
&cli.StringSliceFlag{
|
|
|
|
Name: "authorized-keys",
|
|
|
|
Aliases: []string{"k"},
|
|
|
|
Usage: "pubkeys for which we will always respond",
|
2023-12-02 10:18:55 -05:00
|
|
|
},
|
|
|
|
},
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2023-12-02 10:18:55 -05:00
|
|
|
// try to connect to the relays here
|
|
|
|
qs := url.Values{}
|
|
|
|
relayURLs := make([]string, 0, c.Args().Len())
|
|
|
|
if relayUrls := c.Args().Slice(); len(relayUrls) > 0 {
|
2024-09-17 07:09:20 -04:00
|
|
|
relays := connectToAllRelays(ctx, relayUrls, false)
|
2023-12-02 10:18:55 -05:00
|
|
|
if len(relays) == 0 {
|
|
|
|
log("failed to connect to any of the given relays.\n")
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
|
|
|
for _, relay := range relays {
|
|
|
|
relayURLs = append(relayURLs, relay.URL)
|
|
|
|
qs.Add("relay", relay.URL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(relayURLs) == 0 {
|
|
|
|
return fmt.Errorf("not connected to any relays: please specify at least one")
|
|
|
|
}
|
|
|
|
|
|
|
|
// gather the secret key
|
2024-06-25 21:18:26 -04:00
|
|
|
sec, _, err := gatherSecretKeyOrBunkerFromArguments(ctx, c)
|
2023-12-02 10:18:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-15 16:31:01 -04:00
|
|
|
|
|
|
|
// other arguments
|
|
|
|
authorizedKeys := c.StringSlice("authorized-keys")
|
|
|
|
authorizedSecrets := c.StringSlice("authorized-secrets")
|
|
|
|
|
|
|
|
// this will be used to auto-authorize the next person who connects who isn't pre-authorized
|
|
|
|
// it will be stored
|
|
|
|
newSecret := randString(12)
|
|
|
|
|
|
|
|
// static information
|
2023-12-02 10:18:55 -05:00
|
|
|
pubkey, err := nostr.GetPublicKey(sec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
npub, _ := nip19.EncodePublicKey(pubkey)
|
2024-08-23 15:17:17 -04:00
|
|
|
bold := color.New(color.Bold).Sprint
|
|
|
|
italic := color.New(color.Italic).Sprint
|
2024-02-17 15:56:57 -05:00
|
|
|
|
2024-05-15 16:31:01 -04:00
|
|
|
// this function will be called every now and then
|
2024-02-17 15:56:57 -05:00
|
|
|
printBunkerInfo := func() {
|
2024-05-15 16:31:01 -04:00
|
|
|
qs.Set("secret", newSecret)
|
|
|
|
bunkerURI := fmt.Sprintf("bunker://%s?%s", pubkey, qs.Encode())
|
|
|
|
|
|
|
|
authorizedKeysStr := ""
|
|
|
|
if len(authorizedKeys) != 0 {
|
|
|
|
authorizedKeysStr = "\n authorized keys:\n - " + italic(strings.Join(authorizedKeys, "\n - "))
|
|
|
|
}
|
|
|
|
|
|
|
|
authorizedSecretsStr := ""
|
|
|
|
if len(authorizedSecrets) != 0 {
|
|
|
|
authorizedSecretsStr = "\n authorized secrets:\n - " + italic(strings.Join(authorizedSecrets, "\n - "))
|
|
|
|
}
|
|
|
|
|
|
|
|
preauthorizedFlags := ""
|
|
|
|
for _, k := range authorizedKeys {
|
|
|
|
preauthorizedFlags += " -k " + k
|
|
|
|
}
|
|
|
|
for _, s := range authorizedSecrets {
|
|
|
|
preauthorizedFlags += " -s " + s
|
|
|
|
}
|
|
|
|
|
|
|
|
secretKeyFlag := ""
|
|
|
|
if sec := c.String("sec"); sec != "" {
|
|
|
|
secretKeyFlag = "--sec " + sec
|
|
|
|
}
|
|
|
|
|
2024-06-07 05:30:55 -04:00
|
|
|
relayURLsPossiblyWithoutSchema := make([]string, len(relayURLs))
|
|
|
|
for i, url := range relayURLs {
|
|
|
|
if strings.HasPrefix(url, "wss://") {
|
|
|
|
relayURLsPossiblyWithoutSchema[i] = url[6:]
|
|
|
|
} else {
|
|
|
|
relayURLsPossiblyWithoutSchema[i] = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-15 16:31:01 -04:00
|
|
|
restartCommand := fmt.Sprintf("nak bunker %s%s %s",
|
|
|
|
secretKeyFlag,
|
|
|
|
preauthorizedFlags,
|
2024-06-07 05:30:55 -04:00
|
|
|
strings.Join(relayURLsPossiblyWithoutSchema, " "),
|
2024-05-15 16:31:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
log("listening at %v:\n pubkey: %s \n npub: %s%s%s\n to restart: %s\n bunker: %s\n\n",
|
2024-08-23 15:17:17 -04:00
|
|
|
bold(relayURLs),
|
2024-02-17 15:56:57 -05:00
|
|
|
bold(pubkey),
|
|
|
|
bold(npub),
|
2024-05-15 16:31:01 -04:00
|
|
|
authorizedKeysStr,
|
|
|
|
authorizedSecretsStr,
|
|
|
|
color.CyanString(restartCommand),
|
2024-02-17 15:56:57 -05:00
|
|
|
bold(bunkerURI),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
printBunkerInfo()
|
2023-12-02 10:18:55 -05:00
|
|
|
|
|
|
|
// subscribe to relays
|
2024-05-15 16:31:01 -04:00
|
|
|
now := nostr.Now()
|
2024-09-17 07:09:20 -04:00
|
|
|
events := sys.Pool.SubMany(ctx, relayURLs, nostr.Filters{
|
2023-12-02 10:18:55 -05:00
|
|
|
{
|
2024-05-15 16:31:01 -04:00
|
|
|
Kinds: []int{nostr.KindNostrConnect},
|
|
|
|
Tags: nostr.TagMap{"p": []string{pubkey}},
|
|
|
|
Since: &now,
|
|
|
|
LimitZero: true,
|
2023-12-02 10:18:55 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-01-11 19:41:50 -05:00
|
|
|
signer := nip46.NewStaticKeySigner(sec)
|
2024-02-16 09:08:48 -05:00
|
|
|
handlerWg := sync.WaitGroup{}
|
|
|
|
printLock := sync.Mutex{}
|
2024-02-17 15:56:57 -05:00
|
|
|
|
|
|
|
// just a gimmick
|
|
|
|
var cancelPreviousBunkerInfoPrint context.CancelFunc
|
2024-06-25 21:18:26 -04:00
|
|
|
_, cancel := context.WithCancel(ctx)
|
2024-02-17 15:56:57 -05:00
|
|
|
cancelPreviousBunkerInfoPrint = cancel
|
|
|
|
|
2024-03-02 06:18:40 -05:00
|
|
|
// asking user for authorization
|
2024-05-15 16:31:01 -04:00
|
|
|
signer.AuthorizeRequest = func(harmless bool, from string, secret string) bool {
|
|
|
|
if secret == newSecret {
|
|
|
|
// store this key
|
|
|
|
authorizedKeys = append(authorizedKeys, from)
|
|
|
|
// discard this and generate a new secret
|
|
|
|
newSecret = randString(12)
|
|
|
|
// print bunker info again after this
|
|
|
|
go func() {
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
printBunkerInfo()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-08-06 09:56:06 -04:00
|
|
|
return slices.Contains(authorizedKeys, from) || slices.Contains(authorizedSecrets, secret)
|
2024-03-02 06:18:40 -05:00
|
|
|
}
|
|
|
|
|
2023-12-02 10:18:55 -05:00
|
|
|
for ie := range events {
|
2024-02-17 15:56:57 -05:00
|
|
|
cancelPreviousBunkerInfoPrint() // this prevents us from printing a million bunker info blocks
|
|
|
|
|
|
|
|
// handle the NIP-46 request event
|
2024-10-27 08:56:49 -04:00
|
|
|
req, resp, eventResponse, err := signer.HandleRequest(ctx, ie.Event)
|
2023-12-02 10:18:55 -05:00
|
|
|
if err != nil {
|
2024-02-16 09:08:48 -05:00
|
|
|
log("< failed to handle request from %s: %s\n", ie.Event.PubKey, err.Error())
|
2023-12-02 10:18:55 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
jreq, _ := json.MarshalIndent(req, " ", " ")
|
2024-02-12 13:39:13 -05:00
|
|
|
log("- got request from '%s': %s\n", color.New(color.Bold, color.FgBlue).Sprint(ie.Event.PubKey), string(jreq))
|
2023-12-02 10:18:55 -05:00
|
|
|
jresp, _ := json.MarshalIndent(resp, " ", " ")
|
|
|
|
log("~ responding with %s\n", string(jresp))
|
|
|
|
|
2024-03-02 06:18:40 -05:00
|
|
|
handlerWg.Add(len(relayURLs))
|
|
|
|
for _, relayURL := range relayURLs {
|
|
|
|
go func(relayURL string) {
|
2024-09-17 07:09:20 -04:00
|
|
|
if relay, _ := sys.Pool.EnsureRelay(relayURL); relay != nil {
|
2024-06-25 21:18:26 -04:00
|
|
|
err := relay.Publish(ctx, eventResponse)
|
2024-03-02 06:18:40 -05:00
|
|
|
printLock.Lock()
|
|
|
|
if err == nil {
|
|
|
|
log("* sent response through %s\n", relay.URL)
|
|
|
|
} else {
|
|
|
|
log("* failed to send response: %s\n", err)
|
2024-02-12 13:39:13 -05:00
|
|
|
}
|
2024-03-02 06:18:40 -05:00
|
|
|
printLock.Unlock()
|
|
|
|
handlerWg.Done()
|
|
|
|
}
|
|
|
|
}(relayURL)
|
2023-12-02 10:18:55 -05:00
|
|
|
}
|
2024-03-02 06:18:40 -05:00
|
|
|
handlerWg.Wait()
|
2024-02-17 15:56:57 -05:00
|
|
|
|
|
|
|
// just after handling one request we trigger this
|
|
|
|
go func() {
|
2024-06-25 21:18:26 -04:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2024-02-17 15:56:57 -05:00
|
|
|
defer cancel()
|
|
|
|
cancelPreviousBunkerInfoPrint = cancel
|
|
|
|
// the idea is that we will print the bunker URL again so it is easier to copy-paste by users
|
|
|
|
// but we will only do if the bunker is inactive for more than 5 minutes
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-time.After(time.Minute * 5):
|
2024-09-22 18:04:21 -04:00
|
|
|
log("\n")
|
2024-02-17 15:56:57 -05:00
|
|
|
printBunkerInfo()
|
|
|
|
}
|
|
|
|
}()
|
2023-12-02 10:18:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|