Generate QR code for loading config on mobile

This commit is contained in:
Daniel Lundin
2019-10-02 20:43:45 +02:00
parent 2eab0cea18
commit 8682fe4f5b
4 changed files with 43 additions and 11 deletions
+1
View File
@@ -9,6 +9,7 @@ require (
github.com/google/nftables v0.0.0-20190906062827-5d14089d2edc
github.com/julienschmidt/httprouter v1.3.0
github.com/sirupsen/logrus v1.4.2
github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9
github.com/vishvananda/netlink v1.0.0
github.com/vishvananda/netns v0.0.0-20190625233234-7109fa855b0f
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20190925195211-ef61b881e46f
+8
View File
@@ -3,10 +3,12 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/nftables v0.0.0-20190906062827-5d14089d2edc h1:2ZY2fDYgf8rheZ6YQWHXVQ0pAOws6N1yuaBxRf3EP5k=
github.com/google/nftables v0.0.0-20190906062827-5d14089d2edc/go.mod h1:DfTD7lq9Gq5pLrgCmJDbGtrcWF/h7i5XWgEC/6bQu0s=
@@ -24,13 +26,18 @@ github.com/mdlayher/netlink v0.0.0-20190513144208-ba284d510044/go.mod h1:gOrA34z
github.com/mdlayher/netlink v0.0.0-20190516121005-0087c778e469/go.mod h1:gOrA34zDL0K3RsACQe54bDYLF/CeFspQ9m5DOycycQ8=
github.com/mdlayher/netlink v0.0.0-20190614145538-d8264f87dbe3 h1:3IPcWjiboJFnnvHeXxT4pYw33BiPJn/DC5BKhcGEbGk=
github.com/mdlayher/netlink v0.0.0-20190614145538-d8264f87dbe3/go.mod h1:ISujvOTprADlNr00kvJIu0d23q57wk2NSV/PT/TEk4E=
github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721 h1:RlZweED6sbSArvlE924+mUcZuXKLBHA35U7LN621Bws=
github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721/go.mod h1:Ickgr2WtCLZ2MDGd4Gr0geeCH5HybhRJbonOgQpvSxc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9 h1:lpEzuenPuO1XNTeikEmvqYFcU37GVLl8SRNblzyvGBE=
github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/vishvananda/netlink v1.0.0 h1:bqNY2lgheFIu1meHUFSH3d7vG93AFyqg3oGbJCOJgSM=
github.com/vishvananda/netlink v1.0.0/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk=
@@ -65,4 +72,5 @@ golang.zx2c4.com/wireguard/wgctrl v0.0.0-20190925195211-ef61b881e46f/go.mod h1:8
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+28 -9
View File
@@ -20,6 +20,7 @@ import (
"github.com/google/nftables/expr"
"github.com/julienschmidt/httprouter"
log "github.com/sirupsen/logrus"
"github.com/skip2/go-qrcode"
"github.com/vishvananda/netlink"
"github.com/vishvananda/netns"
"golang.zx2c4.com/wireguard/wgctrl"
@@ -417,15 +418,8 @@ func (s *Server) GetClient(w http.ResponseWriter, r *http.Request, ps httprouter
return
}
format := r.URL.Query().Get("format")
if format == "config" {
filename := fmt.Sprintf("%s.conf", client.Name)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
w.WriteHeader(http.StatusOK)
allowedIPs := strings.Join(*wgAllowedIPs, ",")
fmt.Fprintf(w, `
allowedIPs := strings.Join(*wgAllowedIPs, ",")
configData := fmt.Sprintf(`
[Interface]
Address = %s
PrivateKey = %s
@@ -435,6 +429,31 @@ PublicKey = %s
AllowedIPs = %s
Endpoint = %s
`, client.IP.String(), client.PrivateKey, "8.8.8.8", s.Config.PublicKey, allowedIPs, *wgEndpoint)
format := r.URL.Query().Get("format")
if format == "qrcode" {
prefix := r.URL.Query().Get("prefix")
r.URL.RawQuery = "format=config"
url := fmt.Sprintf("%s%s", prefix, r.URL)
log.Debug("Rendering QR code for URL: ", url)
png, err := qrcode.Encode(fmt.Sprintf(url), qrcode.Medium, 180)
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(http.StatusOK)
w.Write(png)
return
}
if format == "config" {
filename := fmt.Sprintf("%s.conf", client.Name)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, configData)
return
}
+6 -2
View File
@@ -12,6 +12,9 @@
hash = dev.PrivateKey.charCodeAt(i) + ((hash << 5) - hash);
}
const color = "hsl(" + (hash % 360) + ",50%,95%)";
var qrCodeURL = "/api/v1/users/" + user + "/clients/" + clientId + "?format=qrcode&prefix=" + [location.protocol, '//', location.host].join('')
</script>
<style>
@@ -22,8 +25,10 @@
<div class="card">
<div class="card-body" style="background-color: {color}">
<a href="/client/{clientId}" use:link replace role="button" class="btn btn-secondary material-icons float-right">edit</a>
<i class="material-icons" aria-hidden="true">devices</i>
<img src="{qrCodeURL}" class="qrcode float-right"/>
<h4 class="card-title">{dev.Name}</h4>
<dl class="row">
<dt class="col-sm-2">IP</dt>
@@ -32,8 +37,7 @@
<dd class="col-sm-10">{dev.PublicKey}</dd>
</dl>
<a href="/api/v1/users/{user}/clients/{clientId}?format=config" role="button" class="btn btn-raised btn-primary">Download Config</a>
<a href="/api/v1/users/{user}/clients/{clientId}?format=config" role="button" class="btn btn-raised btn-primary">Download Config</a>
</div>
</div>