traygent/main.go

130 lines
2.3 KiB
Go
Raw Normal View History

2023-10-07 05:29:05 -06:00
package main
import (
"flag"
"log"
"net"
"os"
"os/signal"
2023-11-22 08:53:42 -07:00
"path"
"syscall"
2023-10-07 05:29:05 -06:00
"time"
2023-11-12 19:11:55 -07:00
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/driver/desktop"
2023-10-07 05:29:05 -06:00
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
2023-11-22 08:53:42 -07:00
func init() {
syscall.Umask(0077)
}
2023-10-07 05:29:05 -06:00
func main() {
2023-11-22 08:53:42 -07:00
sock := flag.String("s", path.Join(os.Getenv("HOME"), ".traygent"), "Socket path to create")
cmdList := flag.String("c", "/etc/traygent.json", "List of commands to execute")
2023-10-07 05:29:05 -06:00
flag.Parse()
os.Remove(*sock)
l, err := net.Listen("unix", *sock)
2023-10-07 05:29:05 -06:00
if err != nil {
log.Fatalln(err)
}
defer l.Close()
2023-11-10 19:17:56 -07:00
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
go func(c chan os.Signal) {
s := <-c
log.Printf("caught %q, shutting down...\n", s)
os.Remove(*sock)
os.Exit(0)
}(sig)
2023-11-12 19:11:55 -07:00
2023-11-10 19:17:56 -07:00
cmds := LoadCommands(*cmdList)
2023-10-07 05:29:05 -06:00
tagent := Traygent{
listener: l,
addChan: make(chan ssh.PublicKey),
rmChan: make(chan string),
sigReq: make(chan ssh.PublicKey),
sigResp: make(chan bool),
2023-10-07 05:29:05 -06:00
}
2024-01-16 19:14:57 -07:00
trayApp := app.NewWithID("com.bolddaemon.traygent")
app.SetMetadata(fyne.AppMetadata{
Name: "traygent",
})
2023-11-12 19:11:55 -07:00
var desk desktop.App
var ok bool
2024-01-16 19:14:57 -07:00
if desk, ok = trayApp.(desktop.App); ok {
2023-11-12 19:11:55 -07:00
m := fyne.NewMenu("traygent",
fyne.NewMenuItem("Remove Keys", func() {
tagent.RemoveAll()
}),
)
desk.SetSystemTrayMenu(m)
}
setIcon := func() {
2023-11-12 19:11:55 -07:00
iconImg := buildImage(len(tagent.keys), tagent.locked)
desk.SetSystemTrayIcon(iconImg)
}
setIcon()
2023-11-12 19:11:55 -07:00
2023-10-07 05:29:05 -06:00
go func() {
for {
tagent.RemoveLocked()
time.Sleep(1 * time.Second)
}
}()
2023-10-07 05:29:05 -06:00
go func() {
2023-10-07 05:29:05 -06:00
for {
c, err := tagent.listener.Accept()
if err != nil {
log.Println(err)
continue
}
go agent.ServeAgent(&tagent, c)
2023-10-07 05:29:05 -06:00
}
}()
2023-11-12 19:11:55 -07:00
go func() {
for {
select {
case added := <-tagent.addChan:
fp := ssh.FingerprintSHA256(added)
c := cmds.Get("added")
if c != nil {
setIcon()
2023-11-12 19:11:55 -07:00
c.Run(fp)
}
case rm := <-tagent.rmChan:
c := cmds.Get("removed")
if c != nil {
setIcon()
2023-11-12 19:11:55 -07:00
c.Run(rm)
}
case pub := <-tagent.sigReq:
fp := ssh.FingerprintSHA256(pub)
c := cmds.Get("sign")
if c != nil {
if c.Run(fp) {
go func() { tagent.sigResp <- true }()
} else {
go func() { tagent.sigResp <- false }()
}
}
2023-10-07 05:29:05 -06:00
}
}
2023-11-12 19:11:55 -07:00
}()
2024-01-16 19:14:57 -07:00
trayApp.Run()
2023-10-07 05:29:05 -06:00
}