forked from maximbaz/yubikey-touch-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (91 loc) · 2.84 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path"
"strings"
"sync"
"syscall"
"github.com/maximbaz/yubikey-touch-detector/detector"
"github.com/maximbaz/yubikey-touch-detector/notifier"
log "github.com/sirupsen/logrus"
)
const appVersion = "1.10.1"
func main() {
truthyValues := map[string]bool{"true": true, "yes": true, "1": true}
envVerbose := truthyValues[strings.ToLower(os.Getenv("YUBIKEY_TOUCH_DETECTOR_VERBOSE"))]
envLibnotify := truthyValues[strings.ToLower(os.Getenv("YUBIKEY_TOUCH_DETECTOR_LIBNOTIFY"))]
envStdout := truthyValues[strings.ToLower(os.Getenv("YUBIKEY_TOUCH_DETECTOR_STDOUT"))]
envNosocket := truthyValues[strings.ToLower(os.Getenv("YUBIKEY_TOUCH_DETECTOR_NOSOCKET"))]
var version bool
var verbose bool
var libnotify bool
var stdout bool
var nosocket bool
var gpgPubringPath string
flag.BoolVar(&version, "version", false, "print version and exit")
flag.BoolVar(&verbose, "v", envVerbose, "enable debug logging")
flag.BoolVar(&libnotify, "libnotify", envLibnotify, "show desktop notifications using libnotify")
flag.BoolVar(&stdout, "stdout", envStdout, "print notifications to stdout")
flag.BoolVar(&nosocket, "no-socket", envNosocket, "disable unix socket notifier")
flag.Parse()
if version {
fmt.Println("YubiKey touch detector version:", appVersion)
os.Exit(0)
}
if verbose {
log.SetLevel(log.DebugLevel)
}
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
log.Debug("Starting YubiKey touch detector")
gpgHome := os.Getenv("GNUPGHOME")
if gpgHome != "" {
gpgPubringPath = path.Join(gpgHome, "pubring.kbx")
} else {
gpgPubringPath = "$HOME/.gnupg/pubring.kbx"
}
gpgPubringPath = os.ExpandEnv(gpgPubringPath)
exits := &sync.Map{}
go setupExitSignalWatch(exits)
notifiers := &sync.Map{}
if verbose {
go notifier.SetupDebugNotifier(notifiers)
}
if !nosocket {
go notifier.SetupUnixSocketNotifier(notifiers, exits)
}
if libnotify {
go notifier.SetupLibnotifyNotifier(notifiers)
}
if stdout {
go notifier.SetupStdoutNotifier(notifiers)
}
go detector.WatchU2F(notifiers)
go detector.WatchHMAC(notifiers)
if _, err := os.Stat(gpgPubringPath); err == nil {
requestGPGCheck := make(chan bool)
go detector.CheckGPGOnRequest(requestGPGCheck, notifiers)
go detector.WatchGPG(gpgPubringPath, requestGPGCheck)
go detector.WatchSSH(requestGPGCheck, exits)
} else {
log.Debugf("'%v' could not be found. Disabling GPG and SSH watchers.", gpgPubringPath)
}
wait := make(chan bool)
<-wait
}
func setupExitSignalWatch(exits *sync.Map) {
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, os.Interrupt, syscall.SIGTERM)
<-exitSignal
println()
exits.Range(func(k, v interface{}) bool {
exit := v.(chan bool)
exit <- true // Notify exit watcher
<-exit // Wait for confirmation
return true
})
log.Debug("Stopping YubiKey touch detector")
os.Exit(0)
}