From 8bbcb0165ab2aadc041755ea5f3c6d6ce98033e0 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Mon, 17 Jun 2024 14:49:52 +0200 Subject: [PATCH] multi: add configurable index page that lists users --- config.json | 3 +- go.mod | 2 ++ go.sum | 3 ++ main.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) diff --git a/config.json b/config.json index 4ef4879..8dc8659 100644 --- a/config.json +++ b/config.json @@ -3,7 +3,8 @@ "InvoiceMacaroonPath": "/home/alice/.lnd/data/chain/bitcoin/mainnet/invoices.macaroon", "TLSCertPath": "/home/alice/.lnd/tls.cert", "WorkingDir": "/home/alice/.go-host-lnaddr", - "Private": true, + "ExternalURL": "https://allmysats.com", + "ListAllURLs": true, "LightningAddresses": [ "tips@allmysats.com" ], diff --git a/go.mod b/go.mod index 79fde91..d4090e7 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,10 @@ go 1.19 require ( github.com/MadAppGang/httplog v1.3.0 github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f + github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible github.com/lightningnetwork/lnd v0.17.2-beta + github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e google.golang.org/grpc v1.58.3 gopkg.in/macaroon.v2 v2.1.0 ) diff --git a/go.sum b/go.sum index 235506d..78587ca 100644 --- a/go.sum +++ b/go.sum @@ -86,6 +86,7 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 h1:KdUfX2zKommPRa+PD0sWZUyXe9 github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9VhRV3jjAVU7DJVjMaK+IsvSeZvFo= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= +github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d h1:yJzD/yFppdVCf6ApMkVy8cUxV0XrxdP9rVf6D87/Mng= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/btcwallet v0.16.10-0.20231017144732-e3ff37491e9c h1:+7tbYEUj0TYYIvuvE9YP+x5dU3FT/8J6Qh8d5YvQwrE= github.com/btcsuite/btcwallet v0.16.10-0.20231017144732-e3ff37491e9c/go.mod h1:WSKhOJWUmUOHKCKEzdt+jWAHFAE/t4RqVbCwL2pEdiU= @@ -516,6 +517,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y= github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js= github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= diff --git a/main.go b/main.go index 1ad1f12..a449152 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,12 @@ package main import ( + "bytes" "encoding/base64" "encoding/json" "flag" "fmt" + "html/template" baselog "log" "net/http" "os" @@ -12,8 +14,10 @@ import ( "github.com/MadAppGang/httplog" "github.com/btcsuite/btclog" + "github.com/btcsuite/btcutil/bech32" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/macaroons" + "github.com/skip2/go-qrcode" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "gopkg.in/macaroon.v2" @@ -24,6 +28,8 @@ type ServerConfig struct { InvoiceMacaroonPath string TLSCertPath string WorkingDir string + ExternalURL string + ListAllURLs bool LightningAddresses []string MinSendableMsat int MaxSendableMsat int @@ -119,6 +125,7 @@ func main() { setupHandlerPerAddress(config) setupNostrHandlers(config.Nostr) setupNotificators(config) + setupIndexHandler(config) http.HandleFunc("/invoice/", useLogger( invoiceManager.handleInvoiceCreation(config), @@ -193,6 +200,90 @@ func setupNostrHandlers(nostr *NostrConfig) { ) } +func setupIndexHandler(config ServerConfig) { + if !config.ListAllURLs || len(config.LightningAddresses) == 0 || + config.ExternalURL == "" { + return + } + + type user struct { + User string + Encoded string + QRCode string + } + + var users []user + for _, addr := range config.LightningAddresses { + userName := strings.Split(addr, "@")[0] + url := fmt.Sprintf("%s/.well-known/lnurlp/%s", + config.ExternalURL, userName) + + converted, err := bech32.ConvertBits([]byte(url), 8, 5, true) + if err != nil { + log.Errorf("Unable to convert url: %v", err) + } + + lnurl, err := bech32.Encode("lnurl", converted) + if err != nil { + log.Errorf("Unable to encode url: %v", err) + continue + } + + png, err := qrcode.Encode(lnurl, qrcode.Highest, 256) + if err != nil { + log.Errorf("Unable to encode QR code: %v", err) + continue + } + + users = append(users, user{ + User: userName, + Encoded: lnurl, + QRCode: base64.StdEncoding.EncodeToString(png), + }) + + } + htmlTemplate := ` + + + LNURLs + + +

LNURLs

+ + + +` + + bodyTemlate, err := template.New("html").Parse(htmlTemplate) + if err != nil { + log.Errorf("Error building URL template: %w", err) + return + } + + var buf bytes.Buffer + err = bodyTemlate.Execute(&buf, users) + if err != nil { + log.Errorf("Error executing URL template: %w", err) + return + } + + http.HandleFunc( + "/", useLogger(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + w.WriteHeader(http.StatusOK) + _, _ = w.Write(buf.Bytes()) + }), + ) +} + func metadataToString(config ServerConfig) (string, error) { thumbnailMetadata, err := thumbnailToMetadata(config.Thumbnail)