Browse Source

add option to include local ip in qrcode

make_cert_optional
Otto Suess 7 years ago
parent
commit
ea8ff2e060
No known key found for this signature in database GPG Key ID: 354749C62B4BAC41
  1. 37
      zapconnect.go

37
zapconnect.go

@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"net"
"io/ioutil"
"github.com/Baozisoftware/qrcode-terminal-go"
"encoding/json"
@ -12,9 +13,29 @@ b64 "encoding/base64"
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 main() {
ipPtr := flag.Bool("i", false, "Include local ip in QRCode.")
jsonPtr := flag.Bool("j", false, "Generate json instead of a QRCode.")
flag.Parse()
loadedConfig, _ := loadConfig()
certBytes, err := ioutil.ReadFile(loadedConfig.TLSCertPath)
@ -29,17 +50,21 @@ func main() {
sEnc := b64.StdEncoding.EncodeToString([]byte(macBytes))
ipString := ""
if *ipPtr {
ipString = getLocalIP() + ":10009"
}
cert := &certificates{
Cert: string(certBytes),
Macaroon: sEnc}
Macaroon: sEnc,
Ip: ipString}
certB, _ := json.Marshal(cert)
jsonPtr := flag.Bool("j", false, "Generate json instead of a QRCode.")
flag.Parse()
if *jsonPtr {
fmt.Print(string(certB))
} else {
if *jsonPtr {
fmt.Println(string(certB))
} else {
obj := qrcodeTerminal.New()
obj.Get(string(certB)).Print()
}

Loading…
Cancel
Save