diff --git a/zapconnect.go b/zapconnect.go index 2a6f135..b391ca8 100644 --- a/zapconnect.go +++ b/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() }