mirror of
https://github.com/labring/sealos.git
synced 2026-10-24 01:38:59 +08:00
* refactor(master): add config client * refactor(master): refactor types * refactor(master): refactor types * refactor(master): refactor types * refactor(master): update go mod * refactor(master): update go mod * refactor(master): refactor types * refactor(master): refactor types * refactor(master): add go mod * refactor(master): add compress module
169 lines
4.4 KiB
Go
169 lines
4.4 KiB
Go
// Copyright © 2021 sealos.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package ssh
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/fanux/sealos/pkg/utils/iputils"
|
|
|
|
"github.com/fanux/sealos/pkg/utils/logger"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
/**
|
|
SSH connection operation
|
|
*/
|
|
func (s *SSH) connect(host string) (*ssh.Client, error) {
|
|
auth := s.sshAuthMethod(s.Password, s.PkFile, s.PkPassword)
|
|
config := ssh.Config{
|
|
Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
|
|
}
|
|
DefaultTimeout := time.Duration(15) * time.Second
|
|
if s.Timeout == nil {
|
|
s.Timeout = &DefaultTimeout
|
|
}
|
|
clientConfig := &ssh.ClientConfig{
|
|
User: s.User,
|
|
Auth: auth,
|
|
Timeout: *s.Timeout,
|
|
Config: config,
|
|
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
|
return nil
|
|
},
|
|
}
|
|
ip, port := iputils.GetSSHHostIPAndPort(host)
|
|
addr := s.addrReformat(ip, port)
|
|
return ssh.Dial("tcp", addr, clientConfig)
|
|
}
|
|
|
|
func (s *SSH) Connect(host string) (*ssh.Client, *ssh.Session, error) {
|
|
client, err := s.connect(host)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
session, err := client.NewSession()
|
|
if err != nil {
|
|
_ = client.Close()
|
|
return nil, nil, err
|
|
}
|
|
|
|
modes := ssh.TerminalModes{
|
|
ssh.ECHO: 0, //disable echoing
|
|
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
|
|
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
|
|
}
|
|
|
|
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
|
|
_ = session.Close()
|
|
_ = client.Close()
|
|
return nil, nil, err
|
|
}
|
|
|
|
return client, session, nil
|
|
}
|
|
|
|
func (s *SSH) sshAuthMethod(password, pkFile, pkPasswd string) (auth []ssh.AuthMethod) {
|
|
if fileExist(pkFile) {
|
|
am, err := s.sshPrivateKeyMethod(pkFile, pkPasswd)
|
|
if err == nil {
|
|
auth = append(auth, am)
|
|
}
|
|
}
|
|
if password != "" {
|
|
auth = append(auth, s.sshPasswordMethod(password))
|
|
}
|
|
return auth
|
|
}
|
|
|
|
//Authentication with a private key,private key has password and no password to verify in this
|
|
func (s *SSH) sshPrivateKeyMethod(pkFile, pkPassword string) (am ssh.AuthMethod, err error) {
|
|
pkData, err := ioutil.ReadFile(filepath.Clean(pkFile))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var pk ssh.Signer
|
|
if pkPassword == "" {
|
|
pk, err = ssh.ParsePrivateKey(pkData)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else {
|
|
bufPwd := []byte(pkPassword)
|
|
pk, err = ssh.ParsePrivateKeyWithPassphrase(pkData, bufPwd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return ssh.PublicKeys(pk), nil
|
|
}
|
|
|
|
func fileExist(path string) bool {
|
|
_, err := os.Stat(path)
|
|
return err == nil || os.IsExist(err)
|
|
}
|
|
func (s *SSH) sshPasswordMethod(password string) ssh.AuthMethod {
|
|
return ssh.Password(password)
|
|
}
|
|
|
|
func (s *SSH) addrReformat(host, port string) string {
|
|
if !strings.Contains(host, ":") {
|
|
host = fmt.Sprintf("%s:%s", host, port)
|
|
}
|
|
return host
|
|
}
|
|
|
|
//RemoteFileExist is
|
|
func (s *SSH) IsFileExist(host, remoteFilePath string) bool {
|
|
// if remote file is
|
|
// ls -l | grep aa | wc -l
|
|
remoteFileName := path.Base(remoteFilePath) // aa
|
|
remoteFileDirName := path.Dir(remoteFilePath)
|
|
//it's bug: if file is aa.bak, `ls -l | grep aa | wc -l` is 1 ,should use `ll aa 2>/dev/null |wc -l`
|
|
//remoteFileCommand := fmt.Sprintf("ls -l %s| grep %s | grep -v grep |wc -l", remoteFileDirName, remoteFileName)
|
|
remoteFileCommand := fmt.Sprintf("ls -l %s/%s 2>/dev/null |wc -l", remoteFileDirName, remoteFileName)
|
|
|
|
data, err := s.CmdToString(host, remoteFileCommand, " ")
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
logger.Error("[ssh][%s]remoteFileCommand err:%s", host, err)
|
|
}
|
|
}()
|
|
if err != nil {
|
|
panic(1)
|
|
}
|
|
count, err := strconv.Atoi(strings.TrimSpace(data))
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
logger.Error("[ssh][%s]RemoteFileExist:%s", host, err)
|
|
}
|
|
}()
|
|
if err != nil {
|
|
panic(1)
|
|
}
|
|
return count != 0
|
|
}
|