mirror of
https://github.com/khairul169/vaulterm.git
synced 2026-09-15 17:03:30 +07:00
feat: update
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
type Hosts struct{ db *gorm.DB }
|
||||
|
||||
func NewHostsRepository() *Hosts {
|
||||
func NewRepository() *Hosts {
|
||||
return &Hosts{db: db.Get()}
|
||||
}
|
||||
|
||||
@@ -19,13 +19,7 @@ func (r *Hosts) GetAll() ([]*models.Host, error) {
|
||||
return rows, ret.Error
|
||||
}
|
||||
|
||||
type GetHostResult struct {
|
||||
Host *models.Host
|
||||
Key map[string]interface{}
|
||||
AltKey map[string]interface{}
|
||||
}
|
||||
|
||||
func (r *Hosts) Get(id string) (*GetHostResult, error) {
|
||||
func (r *Hosts) Get(id string) (*models.HostDecrypted, error) {
|
||||
var host models.Host
|
||||
ret := r.db.Joins("Key").Joins("AltKey").Where("hosts.id = ?", id).First(&host)
|
||||
|
||||
@@ -33,17 +27,9 @@ func (r *Hosts) Get(id string) (*GetHostResult, error) {
|
||||
return nil, ret.Error
|
||||
}
|
||||
|
||||
res := &GetHostResult{Host: &host}
|
||||
|
||||
if host.Key.Data != "" {
|
||||
if err := host.Key.DecryptData(&res.Key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if host.AltKey.Data != "" {
|
||||
if err := host.AltKey.DecryptData(&res.AltKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, err := host.DecryptKeys()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, ret.Error
|
||||
|
||||
@@ -19,7 +19,7 @@ func Router(app *fiber.App) {
|
||||
}
|
||||
|
||||
func getAll(c *fiber.Ctx) error {
|
||||
repo := NewHostsRepository()
|
||||
repo := NewRepository()
|
||||
rows, err := repo.GetAll()
|
||||
if err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
@@ -36,7 +36,7 @@ func create(c *fiber.Ctx) error {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
|
||||
repo := NewHostsRepository()
|
||||
repo := NewRepository()
|
||||
item := &models.Host{
|
||||
Type: body.Type,
|
||||
Label: body.Label,
|
||||
@@ -47,6 +47,13 @@ func create(c *fiber.Ctx) error {
|
||||
KeyID: body.KeyID,
|
||||
AltKeyID: body.AltKeyID,
|
||||
}
|
||||
|
||||
osName, err := tryConnect(item)
|
||||
if err != nil {
|
||||
return utils.ResponseError(c, fmt.Errorf("cannot connect to the host: %s", err), 500)
|
||||
}
|
||||
item.OS = osName
|
||||
|
||||
if err := repo.Create(item); err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
@@ -60,7 +67,7 @@ func update(c *fiber.Ctx) error {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
|
||||
repo := NewHostsRepository()
|
||||
repo := NewRepository()
|
||||
|
||||
id := c.Params("id")
|
||||
exist, _ := repo.Exists(id)
|
||||
@@ -79,6 +86,13 @@ func update(c *fiber.Ctx) error {
|
||||
KeyID: body.KeyID,
|
||||
AltKeyID: body.AltKeyID,
|
||||
}
|
||||
|
||||
osName, err := tryConnect(item)
|
||||
if err != nil {
|
||||
return utils.ResponseError(c, fmt.Errorf("cannot connect to the host: %s", err), 500)
|
||||
}
|
||||
item.OS = osName
|
||||
|
||||
if err := repo.Update(item); err != nil {
|
||||
return utils.ResponseError(c, err, 500)
|
||||
}
|
||||
@@ -87,7 +101,7 @@ func update(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func delete(c *fiber.Ctx) error {
|
||||
repo := NewHostsRepository()
|
||||
repo := NewRepository()
|
||||
|
||||
id := c.Params("id")
|
||||
exist, _ := repo.Exists(id)
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"rul.sh/vaulterm/app/keychains"
|
||||
"rul.sh/vaulterm/lib"
|
||||
"rul.sh/vaulterm/models"
|
||||
)
|
||||
|
||||
func tryConnect(host *models.Host) (string, error) {
|
||||
keyRepo := keychains.NewRepository()
|
||||
|
||||
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,
|
||||
})
|
||||
|
||||
con, err := c.Connect()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
os, err := c.GetOS(c, con)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return os, nil
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
Reference in New Issue
Block a user