Skip to content

Commit

Permalink
feat: 初始化ssh模型
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 21, 2024
1 parent 0786451 commit f2d2bad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 15 additions & 1 deletion internal/biz/ssh.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
18 changes: 6 additions & 12 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ssh

import (
"os"
"time"

"golang.org/x/crypto/ssh"
Expand All @@ -19,7 +18,7 @@ type ClientConfig struct {
HostAddr string
User string
Password string
KeyPath string
Key string
Timeout time.Duration
}

Expand All @@ -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,
}
}

Expand All @@ -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
}
Expand All @@ -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))
}

0 comments on commit f2d2bad

Please sign in to comment.