SSH client with identities support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

identity.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "os"
  4. "path"
  5. "regexp"
  6. "strings"
  7. )
  8. type Identity struct {
  9. Name string
  10. Path string
  11. }
  12. func newIdentity(config Config, name string) Identity {
  13. return Identity{
  14. Name: name,
  15. Path: "",
  16. }
  17. }
  18. var hostRegexps = map[string]*regexp.Regexp{
  19. "ssh": regexp.MustCompile("(?P<user>.*@)?(?P<host>.*)"),
  20. "scp": regexp.MustCompile("(?P<user>.*@)?(?P<host>.*):(?P<file>.*)"),
  21. }
  22. func extractHost(prog string, args []string) string {
  23. args = removeOptions(prog, args)
  24. switch prog {
  25. case "ssh":
  26. if len(args) == 0 {
  27. return ""
  28. }
  29. re := hostRegexps["ssh"]
  30. arg := args[0]
  31. p := matchParams(re, arg)
  32. return p["host"]
  33. case "scp":
  34. re := hostRegexps["scp"]
  35. for _, arg := range args {
  36. if p := matchParams(re, arg); p != nil {
  37. return p["host"]
  38. }
  39. }
  40. }
  41. return ""
  42. }
  43. func findIdentityName(config Config, prog string, args []string) string {
  44. name := os.Getenv("SSH_IDENTITY")
  45. if name != "" {
  46. return name
  47. }
  48. identities := config.Identities
  49. host := extractHost(prog, args)
  50. for match, name := range identities {
  51. if strings.Contains(host, match) {
  52. return name
  53. }
  54. }
  55. return config.DefaultIdentity
  56. }
  57. func FindIdentity(config Config, prog string, args []string) Identity {
  58. name := findIdentityName(config, prog, args)
  59. path := path.Join(config.IdentitiesDir, name)
  60. return Identity{
  61. Name: name,
  62. Path: path,
  63. }
  64. }