From 7d5798e813763af4e5e39646d9acb899f0289bed Mon Sep 17 00:00:00 2001 From: Otto Suess Date: Mon, 7 Jan 2019 17:24:06 +0100 Subject: [PATCH] feature: add --query argument to enable custom query parameters --- README.md | 2 ++ config.go | 19 +++++++++++-------- lndconnect.go | 30 +++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 670ee98..ca55d4a 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ lndconnect -o, --image Output QRCode to file --invoice Use invoice macaroon --readonly Use readonly macaroon +-q, --query= Add additional url query parameters + --lnddir= The base directory that contains lnd's data, logs, configuration file, etc. --configfile= Path to configuration file diff --git a/config.go b/config.go index 045c2bd..f42ea10 100644 --- a/config.go +++ b/config.go @@ -42,15 +42,18 @@ type chainConfig struct { RegTest bool `long:"regtest" description:"Use the regression test network"` } +type arrayFlags []string + type lndConnectConfig struct { - LocalIp bool `short:"i" long:"localip" description:"Include local ip in QRCode"` - Localhost bool `short:"l" long:"localhost" description:"Use 127.0.0.1 for ip"` - Host string `long:"host" description:"Use specific host name"` - Port uint16 `short:"p" long:"port" description:"Use this port"` - Url bool `short:"j" long:"url" description:"Display url instead of a QRCode"` - Image bool `short:"o" long:"image" description:"Output QRCode to file"` - Invoice bool `long:"invoice" description:"use invoice macaroon"` - Readonly bool `long:"readonly" description:"use readonly macaroon"` + LocalIp bool `short:"i" long:"localip" description:"Include local ip in QRCode"` + Localhost bool `short:"l" long:"localhost" description:"Use 127.0.0.1 for ip"` + Host string `long:"host" description:"Use specific host name"` + Port uint16 `short:"p" long:"port" description:"Use this port"` + Url bool `short:"j" long:"url" description:"Display url instead of a QRCode"` + Image bool `short:"o" long:"image" description:"Output QRCode to file"` + Invoice bool `long:"invoice" description:"use invoice macaroon"` + Readonly bool `long:"readonly" description:"use readonly macaroon"` + Query arrayFlags `short:"q" long:"query" description:"Add additional url query parameters"` } // config defines the configuration options for lndconnect. diff --git a/lndconnect.go b/lndconnect.go index a28904b..d315eb6 100644 --- a/lndconnect.go +++ b/lndconnect.go @@ -6,7 +6,9 @@ import ( "fmt" "io/ioutil" "net" + "net/url" "os" + "strings" "github.com/Baozisoftware/qrcode-terminal-go" "github.com/glendc/go-external-ip" @@ -86,20 +88,34 @@ func main() { ipString = getPublicIP() } - ipString = net.JoinHostPort( - ipString, fmt.Sprint(loadedConfig.LndConnect.Port), - ) + ipString = net.JoinHostPort(ipString, fmt.Sprint(loadedConfig.LndConnect.Port)) - urlString := fmt.Sprintf("lndconnect://%s?cert=%s&macaroon=%s", ipString, certificate, macaroonB64) + u := url.URL{Scheme: "lndconnect", Host: ipString} + q := u.Query() + q.Add("cert", certificate) + q.Add("macaroon", macaroonB64) + + for _, s := range loadedConfig.LndConnect.Query { + queryParts := strings.Split(s, "=") + + if len(queryParts) != 2 { + fmt.Println("Invalid Query Argument:", s) + return + } + + q.Add(queryParts[0], queryParts[1]) + } + + u.RawQuery = q.Encode() if loadedConfig.LndConnect.Url { - fmt.Println(urlString) + fmt.Println(u.String()) } else if loadedConfig.LndConnect.Image { - qrcode.WriteFile(urlString, qrcode.Medium, 512, "lndconnect-qr.png") + qrcode.WriteFile(u.String(), qrcode.Medium, 512, "lndconnect-qr.png") fmt.Println("Wrote QR Code to file \"lndconnect-qr.png\"") } else { obj := qrcodeTerminal.New() - obj.Get(urlString).Print() + obj.Get(u.String()).Print() fmt.Println("\n⚠️ Press \"cmd + -\" a few times to see the full QR Code!\nIf that doesn't work run \"lndconnect -j\" to get a code you can copy paste into the app.") } }