2024-11-09 14:37:09 +00:00
|
|
|
package hosts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-11-10 18:49:18 +07:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-11-16 02:34:07 +07:00
|
|
|
"rul.sh/vaulterm/server/app/keychains"
|
|
|
|
"rul.sh/vaulterm/server/lib"
|
|
|
|
"rul.sh/vaulterm/server/models"
|
|
|
|
"rul.sh/vaulterm/server/utils"
|
2024-11-09 14:37:09 +00:00
|
|
|
)
|
|
|
|
|
2024-11-10 18:49:18 +07:00
|
|
|
func tryConnect(c *fiber.Ctx, host *models.Host) (string, error) {
|
|
|
|
user := utils.GetUser(c)
|
|
|
|
keyRepo := keychains.NewRepository(&keychains.Keychains{User: user})
|
2024-11-09 14:37:09 +00:00
|
|
|
|
|
|
|
var key map[string]interface{}
|
|
|
|
var altKey map[string]interface{}
|
|
|
|
|
|
|
|
if host.KeyID != nil {
|
|
|
|
keychain, _ := keyRepo.Get(*host.KeyID)
|
|
|
|
if keychain == nil {
|
|
|
|
return "", fmt.Errorf("key %s not found", *host.KeyID)
|
|
|
|
}
|
|
|
|
keychain.DecryptData(&key)
|
|
|
|
}
|
|
|
|
if host.AltKeyID != nil {
|
|
|
|
keychain, _ := keyRepo.Get(*host.AltKeyID)
|
|
|
|
if keychain == nil {
|
|
|
|
return "", fmt.Errorf("key %s not found", *host.KeyID)
|
|
|
|
}
|
|
|
|
keychain.DecryptData(&altKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
if host.Type == "ssh" {
|
|
|
|
c := lib.NewSSHClient(&lib.SSHClientConfig{
|
|
|
|
HostName: host.Host,
|
|
|
|
Port: host.Port,
|
|
|
|
Key: key,
|
|
|
|
AltKey: altKey,
|
|
|
|
})
|
|
|
|
|
2024-11-13 22:45:03 +00:00
|
|
|
if err := c.Connect(); err != nil {
|
2024-11-09 14:37:09 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2024-11-13 22:45:03 +00:00
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
os, err := c.GetOS(c)
|
2024-11-09 14:37:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|