main.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "net"
  7. "os"
  8. "os/signal"
  9. "path"
  10. "syscall"
  11. "github.com/ncruces/zenity"
  12. "golang.org/x/crypto/ssh/agent"
  13. )
  14. func confirm(comment string) bool {
  15. return zenity.Question(
  16. fmt.Sprintf("Are you sure you want to allow using the SSH key '%s'?", comment),
  17. zenity.Title("Allow SSH Key"),
  18. zenity.QuestionIcon,
  19. ) == nil
  20. }
  21. func getTmpDir() string {
  22. dir, ok := os.LookupEnv("XDG_RUNTIME_DIR")
  23. if ok {
  24. return dir
  25. }
  26. dir, ok = os.LookupEnv("TMPDIR")
  27. if ok {
  28. return dir
  29. }
  30. return "/tmp"
  31. }
  32. func main() {
  33. sock := flag.String(
  34. "sock",
  35. path.Join(getTmpDir(), "agent.sock"),
  36. "Path to socket",
  37. )
  38. secretiveSock := flag.String(
  39. "secretive-sock",
  40. path.Join(os.Getenv("HOME"), "Library/Containers/com.maxgoedjen.Secretive.SecretAgent/Data/socket.ssh"),
  41. "Path to secretive agent socket",
  42. )
  43. keyring := NewKeyring(confirm)
  44. conn, err := net.Dial("unix", *secretiveSock)
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. secretive := agent.NewClient(conn)
  49. proxy := NewProxy(secretive, keyring)
  50. socket, err := net.Listen("unix", *sock)
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. c := make(chan os.Signal, 1)
  55. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  56. go func() {
  57. <-c
  58. os.Remove(*sock)
  59. os.Exit(1)
  60. }()
  61. for {
  62. conn, err := socket.Accept()
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. agent.ServeAgent(proxy, conn)
  67. }
  68. }