-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
screenshot_supported.go
52 lines (42 loc) · 1.39 KB
/
screenshot_supported.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
//go:build linux || windows || darwin || freebsd || netbsd || openbsd
// +build linux windows darwin freebsd netbsd openbsd
package otp
import (
"context"
"strings"
"github.com/AnomalRoil/screenshot"
"github.com/gopasspw/gopass/internal/out"
"github.com/makiuchi-d/gozxing"
"github.com/makiuchi-d/gozxing/qrcode"
)
// ParseScreen will attempt to parse all available screen and will look for otpauth QR codes. It returns the first one
// it has found.
func ParseScreen(ctx context.Context) (string, error) {
var qr string
for i := 0; i < screenshot.NumActiveDisplays(); i++ {
out.Noticef(ctx, "Scanning screen n°%d", i)
img, err := screenshot.CaptureDisplay(i)
if err != nil {
return "", err
}
out.OKf(ctx, "Area scanned on screen n°%d: %v", i, img.Bounds())
bmp, err := gozxing.NewBinaryBitmapFromImage(img)
if err != nil {
return "", err
}
qrReader := qrcode.NewQRCodeReader()
result, err := qrReader.Decode(bmp, nil)
if err != nil {
out.Warningf(ctx, "No QR code found while parsing screen n°%d.", i)
continue
}
out.Noticef(ctx, "Found a qrcode, checking.")
if strings.HasPrefix(result.GetText(), "otpauth://") {
qr = result.GetText()
out.OKf(ctx, "Found an otpauth:// QR code on screen n°%d (%v)", i, img.Bounds())
break
}
out.Warningf(ctx, "Not an otpauth:// QR code, please make sure to only have your OTP qrcode displayed.")
}
return qr, nil
}