From 1f1e69b34a3362ff0ffd3111dd369891be56f8d2 Mon Sep 17 00:00:00 2001 From: Otto Suess Date: Tue, 3 Jul 2018 09:23:33 +0200 Subject: [PATCH] include public ip by default --- zapconnect.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/zapconnect.go b/zapconnect.go index b391ca8..85e85d3 100644 --- a/zapconnect.go +++ b/zapconnect.go @@ -8,6 +8,9 @@ import ( "github.com/Baozisoftware/qrcode-terminal-go" "encoding/json" b64 "encoding/base64" +"net/http" +"os" +"strings" ) type certificates struct { @@ -31,6 +34,23 @@ func getLocalIP() string { return "" } +func getPublicIP() string { + resp, err := http.Get("http://ipv4.myexternalip.com/raw") + if err != nil { + os.Stderr.WriteString(err.Error()) + os.Stderr.WriteString("\n") + os.Exit(1) + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusOK { + bodyBytes, _ := ioutil.ReadAll(resp.Body) + return strings.TrimSpace(string(bodyBytes)) + } + + return "" +} + func main() { ipPtr := flag.Bool("i", false, "Include local ip in QRCode.") jsonPtr := flag.Bool("j", false, "Generate json instead of a QRCode.") @@ -53,6 +73,8 @@ func main() { ipString := "" if *ipPtr { ipString = getLocalIP() + ":10009" + } else { + ipString = getPublicIP() + ":10009" } cert := &certificates{