2023-10-07 05:29:05 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net"
|
2023-11-22 07:30:47 -07:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2023-11-22 08:53:42 -07:00
|
|
|
"path"
|
2023-11-22 07:30:47 -07:00
|
|
|
"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")
|
2023-11-09 20:02:23 -07:00
|
|
|
cmdList := flag.String("c", "/etc/traygent.json", "List of commands to execute")
|
2023-10-07 05:29:05 -06:00
|
|
|
flag.Parse()
|
|
|
|
|
2023-12-05 07:18:22 -07:00
|
|
|
os.Remove(*sock)
|
|
|
|
|
2023-11-09 15:43:14 -07:00
|
|
|
l, err := net.Listen("unix", *sock)
|
2023-10-07 05:29:05 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2023-11-22 07:30:47 -07:00
|
|
|
defer l.Close()
|
2023-11-10 19:17:56 -07:00
|
|
|
|
2023-11-22 07:30:47 -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,
|
2023-11-09 15:43:14 -07:00
|
|
|
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)
|
|
|
|
}
|
2023-11-22 07:30:47 -07:00
|
|
|
setIcon := func() {
|
2023-11-12 19:11:55 -07:00
|
|
|
iconImg := buildImage(len(tagent.keys), tagent.locked)
|
|
|
|
desk.SetSystemTrayIcon(iconImg)
|
|
|
|
}
|
|
|
|
|
2023-11-22 07:30:47 -07:00
|
|
|
setIcon()
|
2023-11-12 19:11:55 -07:00
|
|
|
|
2023-10-07 05:29:05 -06:00
|
|
|
go func() {
|
2023-11-09 15:43:14 -07:00
|
|
|
for {
|
|
|
|
tagent.RemoveLocked()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}()
|
2023-10-07 05:29:05 -06:00
|
|
|
|
2023-11-09 15:43:14 -07:00
|
|
|
go func() {
|
2023-10-07 05:29:05 -06:00
|
|
|
for {
|
|
|
|
c, err := tagent.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-15 12:04:11 -07:00
|
|
|
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 {
|
2023-11-22 07:30:47 -07:00
|
|
|
setIcon()
|
2023-11-12 19:11:55 -07:00
|
|
|
c.Run(fp)
|
|
|
|
}
|
|
|
|
case rm := <-tagent.rmChan:
|
|
|
|
c := cmds.Get("removed")
|
|
|
|
if c != nil {
|
2023-11-22 07:30:47 -07:00
|
|
|
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-11-09 20:02:23 -07:00
|
|
|
}
|
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
|
|
|
}
|