Browse Source

fix #6: make certificate optional

make_cert_optional
Otto Suess 6 years ago
parent
commit
7c3eb03ac5
No known key found for this signature in database GPG Key ID: F7EFC44C2C240A11
  1. 1
      config.go
  2. 4
      lnd_connect_uri.md
  3. 64
      lndconnect.go
  4. 16
      lndconnect_test.go

1
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"`

4
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, <bar> are placeholders
```
lndconnect://<host>:<port>?cert=<base64url DER certifcate>&macaroon=<base64url macaroon>
lndconnect://<host>:<port>?[cert=<base64url DER certifcate>&]macaroon=<base64url macaroon>
```
### Javascript reference implementation

64
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 {

16
lndconnect_test.go

@ -62,3 +62,19 @@ func ExampleHost() {
// Output: lndconnect://1.2.3.4:0?cert=MIICiDCCAi-gAwIBAgIQdo5v0QBXHnji4hRaeeMjNDAKBggqhkjOPQQDAjBHMR8wHQYDVQQKExZsbmQgYXV0b2dlbmVyYXRlZCBjZXJ0MSQwIgYDVQQDExtKdXN0dXNzLU1hY0Jvb2stUHJvLTMubG9jYWwwHhcNMTgwODIzMDU1ODEwWhcNMTkxMDE4MDU1ODEwWjBHMR8wHQYDVQQKExZsbmQgYXV0b2dlbmVyYXRlZCBjZXJ0MSQwIgYDVQQDExtKdXN0dXNzLU1hY0Jvb2stUHJvLTMubG9jYWwwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAASFhRm-w_T10PoKtg4lm9hBNJjJD473fkzHwPUFwy91vTrQSf7543j2JrgFo8mbTV0VtpgqkfK1IMVKMLrF21xio4H8MIH5MA4GA1UdDwEB_wQEAwICpDAPBgNVHRMBAf8EBTADAQH_MIHVBgNVHREEgc0wgcqCG0p1c3R1c3MtTWFjQm9vay1Qcm8tMy5sb2NhbIIJbG9jYWxob3N0ggR1bml4ggp1bml4cGFja2V0hwR_AAABhxAAAAAAAAAAAAAAAAAAAAABhxD-gAAAAAAAAAAAAAAAAAABhxD-gAAAAAAAAAwlc9Zck7bDhwTAqAEEhxD-gAAAAAAAABiNp__-GxXGhxD-gAAAAAAAAKWJ5tliDORjhwQKDwAChxD-gAAAAAAAAG6Wz__-3atFhxD92tDQyv4TAQAAAAAAABAAMAoGCCqGSM49BAMCA0cAMEQCIA9O9xtazmdxCKj0MfbFHVBq5I7JMnOFPpwRPJXQfrYaAiBd5NyJQCwlSx5ECnPOH5sRpv26T8aUcXbmynx9CoDufA&macaroon=AgEDbG5kArsBAwoQ3_I9f6kgSE6aUPd85lWpOBIBMBoWCgdhZGRyZXNzEgRyZWFkEgV3cml0ZRoTCgRpbmZvEgRyZWFkEgV3cml0ZRoXCghpbnZvaWNlcxIEcmVhZBIFd3JpdGUaFgoHbWVzc2FnZRIEcmVhZBIFd3JpdGUaFwoIb2ZmY2hhaW4SBHJlYWQSBXdyaXRlGhYKB29uY2hhaW4SBHJlYWQSBXdyaXRlGhQKBXBlZXJzEgRyZWFkEgV3cml0ZQAABiAiUTBv3Eh6iDbdjmXCfNxp4HBEcOYNzXhrm-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
}

Loading…
Cancel
Save