mirror of
https://github.com/tnb-labs/panel.git
synced 2026-08-31 01:12:17 +08:00
feat: 初始化ssh模型
This commit is contained in:
+1
-1
@@ -18,7 +18,7 @@
|
||||
|
||||
The Rat Panel is an open source lightweight Linux server operation and maintenance management panel developed using Golang and Vue.
|
||||
|
||||
QQ group: [12370907](https://jq.qq.com/?_wv=1027&k=I1oJKSTH) | WeChat group:[Copy this link](https://work.weixin.qq.com/gm/d8ebf618553398d454e3378695c858b6) | Forum: [tom.moe](https://tom.moe) | Sponsor: [Open Collective](https://opencollective.com/tnb)
|
||||
QQ group: [12370907](https://jq.qq.com/?_wv=1027&k=I1oJKSTH) | WeChat group: [Copy this link](https://work.weixin.qq.com/gm/d8ebf618553398d454e3378695c858b6) | Forum: [tom.moe](https://tom.moe) | Sponsor: [Open Collective](https://opencollective.com/tnb)
|
||||
|
||||
## Advantages
|
||||
|
||||
|
||||
+15
-1
@@ -1,6 +1,20 @@
|
||||
package biz
|
||||
|
||||
import "github.com/TheTNB/panel/internal/http/request"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/TheTNB/panel/internal/http/request"
|
||||
"github.com/TheTNB/panel/pkg/ssh"
|
||||
)
|
||||
|
||||
type SSH struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Host string `json:"host"`
|
||||
Port uint `json:"port"`
|
||||
Config ssh.ClientConfig `json:"config"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type SSHRepo interface {
|
||||
GetInfo() (map[string]any, error)
|
||||
|
||||
+6
-12
@@ -1,7 +1,6 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
@@ -19,7 +18,7 @@ type ClientConfig struct {
|
||||
HostAddr string
|
||||
User string
|
||||
Password string
|
||||
KeyPath string
|
||||
Key string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
@@ -33,13 +32,13 @@ func ClientConfigPassword(hostAddr, user, Password string) *ClientConfig {
|
||||
}
|
||||
}
|
||||
|
||||
func ClientConfigPublicKey(hostAddr, user, keyPath string) *ClientConfig {
|
||||
func ClientConfigPublicKey(hostAddr, user, key string) *ClientConfig {
|
||||
return &ClientConfig{
|
||||
Timeout: time.Second * 5,
|
||||
AuthMethod: PUBLICKEY,
|
||||
HostAddr: hostAddr,
|
||||
User: user,
|
||||
KeyPath: keyPath,
|
||||
Key: key,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +53,7 @@ func NewSSHClient(conf *ClientConfig) (*ssh.Client, error) {
|
||||
case PASSWORD:
|
||||
config.Auth = []ssh.AuthMethod{ssh.Password(conf.Password)}
|
||||
case PUBLICKEY:
|
||||
signer, err := getKey(conf.KeyPath)
|
||||
signer, err := parseKey(conf.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -68,11 +67,6 @@ func NewSSHClient(conf *ClientConfig) (*ssh.Client, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func getKey(keyPath string) (ssh.Signer, error) {
|
||||
key, err := os.ReadFile(keyPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ssh.ParsePrivateKey(key)
|
||||
func parseKey(key string) (ssh.Signer, error) {
|
||||
return ssh.ParsePrivateKey([]byte(key))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user