forked from LN-Zap/lndconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lndconnect.go
136 lines (112 loc) · 2.94 KB
/
lndconnect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
b64 "encoding/base64"
"encoding/pem"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"strings"
"github.com/Baozisoftware/qrcode-terminal-go"
"github.com/glendc/go-external-ip"
"github.com/skip2/go-qrcode"
)
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func getPublicIP() string {
consensus := externalip.DefaultConsensus(nil, nil)
ip, err := consensus.ExternalIP()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return ip.String()
}
func main() {
loadedConfig, err := loadConfig()
if err != nil {
fmt.Println(err)
return
}
displayLink(loadedConfig)
}
func displayLink(loadedConfig *config) {
var err error
// 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()
}
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)
} else if loadedConfig.LndConnect.Readonly {
macBytes, err = ioutil.ReadFile(loadedConfig.ReadMacPath)
} else {
macBytes, err = ioutil.ReadFile(loadedConfig.AdminMacPath)
}
if err != nil {
fmt.Println(err)
return
}
macaroonB64 := b64.RawURLEncoding.EncodeToString([]byte(macBytes))
q.Add("macaroon", macaroonB64)
// custom query
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()
// generate link / QR Code
if loadedConfig.LndConnect.Url {
fmt.Println(u.String())
} else if loadedConfig.LndConnect.Image {
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(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.")
}
}