This repository has been archived by the owner on Dec 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
zapconnect.go
117 lines (100 loc) · 2.61 KB
/
zapconnect.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
package main
import (
b64 "encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"net"
"os"
"github.com/Baozisoftware/qrcode-terminal-go"
"github.com/glendc/go-external-ip"
"github.com/skip2/go-qrcode"
)
type certificates struct {
Cert string `json:"c"`
Macaroon string `json:"m"`
Ip string `json:"ip,omitempty"`
}
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()
return
}
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.StdEncoding.EncodeToString([]byte(block.Bytes))
var macBytes []byte
if loadedConfig.ZapConnect.Invoice {
macBytes, err = ioutil.ReadFile(loadedConfig.InvoiceMacPath)
} else if loadedConfig.ZapConnect.Readonly {
macBytes, err = ioutil.ReadFile(loadedConfig.ReadMacPath)
} else {
macBytes, err = ioutil.ReadFile(loadedConfig.AdminMacPath)
}
if err != nil {
fmt.Println(err)
return
}
macaroonB64 := b64.StdEncoding.EncodeToString([]byte(macBytes))
ipString := ""
if loadedConfig.ZapConnect.Host != "" {
ipString = loadedConfig.ZapConnect.Host
} else if loadedConfig.ZapConnect.LocalIp {
ipString = getLocalIP()
} else if loadedConfig.ZapConnect.Localhost {
ipString = "127.0.0.1"
} else {
ipString = getPublicIP()
}
ipString = net.JoinHostPort(
ipString, fmt.Sprint(loadedConfig.ZapConnect.Port),
)
cert := &certificates{
Cert: certificate,
Macaroon: macaroonB64,
Ip: ipString,
}
certB, _ := json.Marshal(cert)
if loadedConfig.ZapConnect.Json {
fmt.Println(string(certB))
} else if loadedConfig.ZapConnect.Image {
qrcode.WriteFile(string(certB), qrcode.Medium, 512, "zapconnect-qr.png")
fmt.Println("Wrote QR Code to file \"zapconnect-qr.png\"")
} else {
obj := qrcodeTerminal.New()
obj.Get(string(certB)).Print()
fmt.Println("\n⚠️ Press \"cmd + -\" a few times to see the full QR Code!\nIf that doesn't work run \"zapconnect -j\" to get a code you can copy paste into the app.")
}
}