mirror of
https://github.com/fiatjaf/nak.git
synced 2025-12-10 17:38:51 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5f7926471 | ||
|
|
e008e08105 |
68
bunker.go
68
bunker.go
@@ -1,10 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
@@ -65,13 +68,18 @@ var bunker = &cli.Command{
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
npub, _ := nip19.EncodePublicKey(pubkey)
|
npub, _ := nip19.EncodePublicKey(pubkey)
|
||||||
|
bunkerURI := fmt.Sprintf("bunker://%s?%s", pubkey, qs.Encode())
|
||||||
bold := color.New(color.Bold).Sprint
|
bold := color.New(color.Bold).Sprint
|
||||||
log("listening at %v:\n pubkey: %s \n npub: %s\n bunker: %s\n\n",
|
|
||||||
bold(relayURLs),
|
printBunkerInfo := func() {
|
||||||
bold(pubkey),
|
log("listening at %v:\n pubkey: %s \n npub: %s\n bunker: %s\n\n",
|
||||||
bold(npub),
|
bold(relayURLs),
|
||||||
bold(fmt.Sprintf("bunker://%s?%s", pubkey, qs.Encode())),
|
bold(pubkey),
|
||||||
)
|
bold(npub),
|
||||||
|
bold(bunkerURI),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
printBunkerInfo()
|
||||||
|
|
||||||
alwaysYes := c.Bool("yes")
|
alwaysYes := c.Bool("yes")
|
||||||
|
|
||||||
@@ -85,10 +93,21 @@ var bunker = &cli.Command{
|
|||||||
})
|
})
|
||||||
|
|
||||||
signer := nip46.NewStaticKeySigner(sec)
|
signer := nip46.NewStaticKeySigner(sec)
|
||||||
|
handlerWg := sync.WaitGroup{}
|
||||||
|
printLock := sync.Mutex{}
|
||||||
|
|
||||||
|
// just a gimmick
|
||||||
|
var cancelPreviousBunkerInfoPrint context.CancelFunc
|
||||||
|
_, cancel := context.WithCancel(c.Context)
|
||||||
|
cancelPreviousBunkerInfoPrint = cancel
|
||||||
|
|
||||||
for ie := range events {
|
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, harmless, err := signer.HandleRequest(ie.Event)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log("< failed to handle request from %s: %s", ie.Event.PubKey, err.Error())
|
log("< failed to handle request from %s: %s\n", ie.Event.PubKey, err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,16 +117,39 @@ var bunker = &cli.Command{
|
|||||||
log("~ responding with %s\n", string(jresp))
|
log("~ responding with %s\n", string(jresp))
|
||||||
|
|
||||||
if alwaysYes || harmless || askProceed(ie.Event.PubKey) {
|
if alwaysYes || harmless || askProceed(ie.Event.PubKey) {
|
||||||
|
handlerWg.Add(len(relayURLs))
|
||||||
for _, relayURL := range relayURLs {
|
for _, relayURL := range relayURLs {
|
||||||
if relay, _ := pool.EnsureRelay(relayURL); relay != nil {
|
go func(relayURL string) {
|
||||||
if err := relay.Publish(c.Context, eventResponse); err == nil {
|
if relay, _ := pool.EnsureRelay(relayURL); relay != nil {
|
||||||
log("* sent response through %s\n", relay.URL)
|
err := relay.Publish(c.Context, eventResponse)
|
||||||
} else {
|
printLock.Lock()
|
||||||
log("* failed to send response: %s\n", err)
|
if err == nil {
|
||||||
|
log("* sent response through %s\n", relay.URL)
|
||||||
|
} else {
|
||||||
|
log("* failed to send response: %s\n", err)
|
||||||
|
}
|
||||||
|
printLock.Unlock()
|
||||||
|
handlerWg.Done()
|
||||||
}
|
}
|
||||||
}
|
}(relayURL)
|
||||||
}
|
}
|
||||||
|
handlerWg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// just after handling one request we trigger this
|
||||||
|
go func() {
|
||||||
|
ctx, cancel := context.WithCancel(c.Context)
|
||||||
|
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):
|
||||||
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
|
printBunkerInfo()
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user