Files
EdgeAPI/internal/installers/ssh_client.go

277 lines
5.4 KiB
Go
Raw Normal View History

2020-09-06 16:19:54 +08:00
package installers
import (
"bytes"
"io"
"net"
"os"
"strings"
2024-07-27 14:15:25 +08:00
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
2020-09-06 16:19:54 +08:00
)
type SSHClient struct {
raw *ssh.Client
sftp *sftp.Client
2021-12-06 19:27:11 +08:00
sudo bool
sudoPassword string
2020-09-06 16:19:54 +08:00
}
func NewSSHClient(raw *ssh.Client) (*SSHClient, error) {
c := &SSHClient{
raw: raw,
}
sftpClient, err := sftp.NewClient(raw)
if err != nil {
_ = c.Close()
return nil, err
}
c.sftp = sftpClient
return c, nil
}
2021-12-06 19:27:11 +08:00
// Sudo 设置使用Sudo
func (this *SSHClient) Sudo(password string) {
this.sudo = true
this.sudoPassword = password
}
2021-07-20 10:55:34 +08:00
// Exec 执行shell命令
2020-09-06 16:19:54 +08:00
func (this *SSHClient) Exec(cmd string) (stdout string, stderr string, err error) {
2021-12-06 19:27:11 +08:00
if this.raw.User() != "root" && this.sudo {
return this.execSudo(cmd, this.sudoPassword)
}
2020-09-06 16:19:54 +08:00
session, err := this.raw.NewSession()
if err != nil {
return "", "", err
}
defer func() {
_ = session.Close()
}()
2021-12-06 19:27:11 +08:00
var stdoutBuf = &bytes.Buffer{}
var stderrBuf = &bytes.Buffer{}
2020-09-06 16:19:54 +08:00
session.Stdout = stdoutBuf
session.Stderr = stderrBuf
err = session.Run(cmd)
if err != nil {
return stdoutBuf.String(), stderrBuf.String(), err
}
return strings.TrimRight(stdoutBuf.String(), "\n"), stderrBuf.String(), nil
}
2021-12-06 19:27:11 +08:00
// execSudo 使用sudo执行shell命令
func (this *SSHClient) execSudo(cmd string, password string) (stdout string, stderr string, err error) {
session, err := this.raw.NewSession()
if err != nil {
return "", "", err
}
defer func() {
_ = session.Close()
}()
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echo
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
err = session.RequestPty("xterm", 80, 40, modes)
if err != nil {
return "", "", err
}
var stderrBuf = &bytes.Buffer{}
session.Stderr = stderrBuf
pipeIn, err := session.StdinPipe()
if err != nil {
return "", "", err
}
pipeOut, err := session.StdoutPipe()
if err != nil {
return "", "", err
}
var resultErr error
var stdoutBuf = bytes.NewBuffer([]byte{})
go func() {
var buf = make([]byte, 512)
for {
n, err := pipeOut.Read(buf)
if n > 0 {
if strings.Contains(string(buf[:n]), "[sudo] password for") {
_, err = pipeIn.Write([]byte(password + "\n"))
if err != nil {
resultErr = err
return
}
continue
}
stdoutBuf.Write(buf[:n])
}
if err != nil {
return
}
}
}()
err = session.Run("sudo " + cmd)
stdout = strings.TrimSpace(stdoutBuf.String())
stderr = strings.TrimSpace(stderrBuf.String())
if err != nil {
return stdout, stderr, err
}
if resultErr != nil {
return stdout, stderr, resultErr
}
return stdout, stderr, nil
}
2020-09-06 16:19:54 +08:00
func (this *SSHClient) Listen(network string, addr string) (net.Listener, error) {
return this.raw.Listen(network, addr)
}
func (this *SSHClient) Dial(network string, addr string) (net.Conn, error) {
return this.raw.Dial(network, addr)
}
func (this *SSHClient) Close() error {
if this.sftp != nil {
_ = this.sftp.Close()
}
return this.raw.Close()
}
func (this *SSHClient) OpenFile(path string, flags int) (*sftp.File, error) {
return this.sftp.OpenFile(path, flags)
}
func (this *SSHClient) Stat(path string) (os.FileInfo, error) {
return this.sftp.Stat(path)
}
func (this *SSHClient) Mkdir(path string) error {
return this.sftp.Mkdir(path)
}
func (this *SSHClient) MkdirAll(path string) error {
2022-11-18 15:44:53 +08:00
err := this.sftp.MkdirAll(path)
if err != nil && this.sudo {
_, _, err2 := this.execSudo("mkdir -p "+path, this.sudoPassword)
if err2 == nil {
return nil
}
}
return err
2020-09-06 16:19:54 +08:00
}
func (this *SSHClient) Chmod(path string, mode os.FileMode) error {
return this.sftp.Chmod(path, mode)
}
2021-07-20 10:55:34 +08:00
// Copy 拷贝文件
2020-09-06 16:19:54 +08:00
func (this *SSHClient) Copy(localPath string, remotePath string, mode os.FileMode) error {
localFp, err := os.Open(localPath)
if err != nil {
return err
}
defer func() {
_ = localFp.Close()
}()
remoteFp, err := this.sftp.OpenFile(remotePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY)
if err != nil {
return err
}
defer func() {
_ = remoteFp.Close()
}()
_, err = io.Copy(remoteFp, localFp)
if err != nil {
return err
}
return this.Chmod(remotePath, mode)
}
2021-07-20 10:55:34 +08:00
// NewSession 获取新Session
2020-09-06 16:19:54 +08:00
func (this *SSHClient) NewSession() (*ssh.Session, error) {
return this.raw.NewSession()
}
2021-07-20 10:55:34 +08:00
// ReadFile 读取文件内容
2020-09-06 16:19:54 +08:00
func (this *SSHClient) ReadFile(path string) ([]byte, error) {
fp, err := this.sftp.OpenFile(path, 0444)
if err != nil {
return nil, err
}
defer func() {
_ = fp.Close()
}()
buffer := bytes.NewBuffer([]byte{})
_, err = io.Copy(buffer, fp)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
2021-07-20 10:55:34 +08:00
// WriteFile 写入文件内容
2020-09-06 16:19:54 +08:00
func (this *SSHClient) WriteFile(path string, data []byte) (n int, err error) {
fp, err := this.sftp.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY)
if err != nil {
return 0, err
}
defer func() {
_ = fp.Close()
}()
n, err = fp.Write(data)
return
}
2020-10-28 12:35:36 +08:00
2021-07-20 10:55:34 +08:00
// Remove 删除文件
2020-10-28 12:35:36 +08:00
func (this *SSHClient) Remove(path string) error {
return this.sftp.Remove(path)
}
2021-12-06 19:27:11 +08:00
// User 用户名
func (this *SSHClient) User() string {
return this.raw.User()
}
// UserHome 用户地址
func (this *SSHClient) UserHome() string {
homeStdout, _, err := this.Exec("echo $HOME")
if err != nil {
return this.defaultUserHome()
}
var home = strings.TrimSpace(homeStdout)
if len(home) > 0 {
return home
}
return this.defaultUserHome()
}
func (this *SSHClient) defaultUserHome() string {
var user = this.raw.User()
if user == "root" {
return "/root"
}
return "/home/" + user
}