From 7c3eb03ac52df8968e53eab68e9df275d9142f29 Mon Sep 17 00:00:00 2001 From: Otto Suess Date: Thu, 14 Feb 2019 11:03:35 +0100 Subject: [PATCH 1/2] fix #6: make certificate optional --- config.go | 1 + lnd_connect_uri.md | 4 ++- lndconnect.go | 64 ++++++++++++++++++++++++++++------------------ lndconnect_test.go | 16 ++++++++++++ 4 files changed, 59 insertions(+), 26 deletions(-) diff --git a/config.go b/config.go index f42ea10..76468cf 100644 --- a/config.go +++ b/config.go @@ -48,6 +48,7 @@ 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"` + NoCert bool `long:"nocert" description:"don't include the certificate"` 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"` diff --git a/lnd_connect_uri.md b/lnd_connect_uri.md index befafe8..36b1ee5 100644 --- a/lnd_connect_uri.md +++ b/lnd_connect_uri.md @@ -4,8 +4,10 @@ Zap-iOS can open URLs with the scheme `lndconnect:` to directly connect to remot ### Syntax +[foo] means optional, are placeholders + ``` -lndconnect://:?cert=&macaroon= +lndconnect://:?[cert=&]macaroon= ``` ### Javascript reference implementation diff --git a/lndconnect.go b/lndconnect.go index d1bee20..51316f8 100644 --- a/lndconnect.go +++ b/lndconnect.go @@ -52,19 +52,45 @@ func main() { } func displayLink(loadedConfig *config) { - certBytes, err := ioutil.ReadFile(loadedConfig.TLSCertPath) - if err != nil { - fmt.Println(err) - return - } + var err error - block, _ := pem.Decode(certBytes) - if block == nil || block.Type != "CERTIFICATE" { - fmt.Println("failed to decode PEM block containing certificate") + // host + ipString := "" + if loadedConfig.LndConnect.Host != "" { + ipString = loadedConfig.LndConnect.Host + } else if loadedConfig.LndConnect.LocalIp { + ipString = getLocalIP() + } else if loadedConfig.LndConnect.Localhost { + ipString = "127.0.0.1" + } else { + ipString = getPublicIP() } - certificate := b64.RawURLEncoding.EncodeToString([]byte(block.Bytes)) + ipString = net.JoinHostPort(ipString, fmt.Sprint(loadedConfig.LndConnect.Port)) + + u := url.URL{Scheme: "lndconnect", Host: ipString} + q := u.Query() + + // cert + if !loadedConfig.LndConnect.NoCert { + certBytes, err := ioutil.ReadFile(loadedConfig.TLSCertPath) + if err != nil { + fmt.Println(err) + return + } + + block, _ := pem.Decode(certBytes) + if block == nil || block.Type != "CERTIFICATE" { + fmt.Println("failed to decode PEM block containing certificate") + } + + certificate := b64.RawURLEncoding.EncodeToString([]byte(block.Bytes)) + + q.Add("cert", certificate) + } + + // macaroon var macBytes []byte if loadedConfig.LndConnect.Invoice { macBytes, err = ioutil.ReadFile(loadedConfig.InvoiceMacPath) @@ -81,24 +107,10 @@ func displayLink(loadedConfig *config) { macaroonB64 := b64.RawURLEncoding.EncodeToString([]byte(macBytes)) - ipString := "" - if loadedConfig.LndConnect.Host != "" { - ipString = loadedConfig.LndConnect.Host - } else if loadedConfig.LndConnect.LocalIp { - ipString = getLocalIP() - } else if loadedConfig.LndConnect.Localhost { - ipString = "127.0.0.1" - } else { - ipString = getPublicIP() - } - - ipString = net.JoinHostPort(ipString, fmt.Sprint(loadedConfig.LndConnect.Port)) - - u := url.URL{Scheme: "lndconnect", Host: ipString} - q := u.Query() - q.Add("cert", certificate) q.Add("macaroon", macaroonB64) + + // custom query for _, s := range loadedConfig.LndConnect.Query { queryParts := strings.Split(s, "=") @@ -112,6 +124,8 @@ func displayLink(loadedConfig *config) { u.RawQuery = q.Encode() + + // generate link / QR Code if loadedConfig.LndConnect.Url { fmt.Println(u.String()) } else if loadedConfig.LndConnect.Image { diff --git a/lndconnect_test.go b/lndconnect_test.go index 3d6cb1d..2a606ee 100644 --- a/lndconnect_test.go +++ b/lndconnect_test.go @@ -62,3 +62,19 @@ func ExampleHost() { // Output: lndconnect://1.2.3.4:0?cert=MIICiDCCAi-gAwIBAgIQdo5v0QB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HJlYWQSBXdyaXRlGhQKBXBlZXJzEgRyZWFkEgV3cml0ZQAABiAiUTBv3Eh6iDbdjmXCfNxp4HBEcOYNzXhrm-ncLHf5jA } + +func ExampleNoCert() { + c := &config{ + TLSCertPath: "testdata/tls.cert", + AdminMacPath: "testdata/admin.macaroon", + LndConnect: &lndConnectConfig{ + Localhost: true, + Url: true, + Host: "1.2.3.4", + NoCert: true, + }, + } + displayLink(c) + + // Output: lndconnect://1.2.3.4:0?macaroon=AgEDbG5kArsBAwoQ3_I9f6kgSE6aUPd85lWpOBIBMBoWCgdhZGRyZXNzEgRyZWFkEgV3cml0ZRoTCgRpbmZvEgRyZWFkEgV3cml0ZRoXCghpbnZvaWNlcxIEcmVhZBIFd3JpdGUaFgoHbWVzc2FnZRIEcmVhZBIFd3JpdGUaFwoIb2ZmY2hhaW4SBHJlYWQSBXdyaXRlGhYKB29uY2hhaW4SBHJlYWQSBXdyaXRlGhQKBXBlZXJzEgRyZWFkEgV3cml0ZQAABiAiUTBv3Eh6iDbdjmXCfNxp4HBEcOYNzXhrm-ncLHf5jA +} From a317fc028b3538b11c9640e011afd90e3990efc7 Mon Sep 17 00:00:00 2001 From: Otto Suess Date: Thu, 14 Feb 2019 11:13:26 +0100 Subject: [PATCH 2/2] doc: update readme --- README.md | 1 + config.go | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c003e2c..f7115f7 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ lndconnect -i, --localip Include local ip in QRCode -l, --localhost Use 127.0.0.1 for ip --host= Use specific host name + --nocert Don't include the certificate -p, --port= Use this port (default: 10009) -j, --url Display url instead of a QRCode -o, --image Output QRCode to file diff --git a/config.go b/config.go index 76468cf..3cd1694 100644 --- a/config.go +++ b/config.go @@ -48,12 +48,12 @@ 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"` - NoCert bool `long:"nocert" description:"don't include the certificate"` + NoCert bool `long:"nocert" description:"Don't include the certificate"` 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"` + 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"` }