-
Notifications
You must be signed in to change notification settings - Fork 1
/
QRPage.go
53 lines (45 loc) · 1.19 KB
/
QRPage.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
package main
import (
_ "embed"
b64 "encoding/base64"
"strconv"
"strings"
qrcode "github.com/skip2/go-qrcode"
)
func qrCodeGenPNG(content string) []byte {
var png []byte
png, err := qrcode.Encode(content, qrcode.Medium, 256)
if err != nil {
return nil
}
return png
}
func qrCodeBase64PNG(content string) string {
qrCodeData := qrCodeGenPNG(content)
base64Str := b64.StdEncoding.EncodeToString(qrCodeData)
return base64Str
}
func qrServerInfoString(ip string) string {
hostname := getHostname()
if len(hostname) > 100 {
hostname = hostname[:100]
}
return qrCodeBase64PNG(GetString("serverSecret") + "," + ip + "," + strconv.Itoa(GetInt("port")) + "," + hostname)
}
//go:embed html/dist/index.html
var qrPageContents string
func qrPageURL() string {
host := "localhost"
ipOverride := GetString("ipOverride")
if len(ipOverride) > 0 {
host = ipOverride
}
return "http://" + host + ":" + strconv.Itoa(GetInt("port")) + "/info"
}
func assembleQRPage(host string, port int, secret string) string {
pageStr := qrPageContents
portStr := strconv.Itoa(port)
pageStr = strings.Replace(pageStr, "~~IP~~", host, 1)
pageStr = strings.Replace(pageStr, "~~PORT~~", portStr, 1)
return pageStr
}