mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-11 22:00:27 +08:00
在各个地方支持IPv6
This commit is contained in:
@@ -2,6 +2,7 @@ package installers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
@@ -17,7 +18,7 @@ type BaseInstaller struct {
|
|||||||
client *SSHClient
|
client *SSHClient
|
||||||
}
|
}
|
||||||
|
|
||||||
// 登录SSH服务
|
// Login 登录SSH服务
|
||||||
func (this *BaseInstaller) Login(credentials *Credentials) error {
|
func (this *BaseInstaller) Login(credentials *Credentials) error {
|
||||||
var hostKeyCallback ssh.HostKeyCallback = nil
|
var hostKeyCallback ssh.HostKeyCallback = nil
|
||||||
|
|
||||||
@@ -78,7 +79,7 @@ func (this *BaseInstaller) Login(credentials *Credentials) error {
|
|||||||
Timeout: 5 * time.Second, // TODO 后期可以设置这个超时时间
|
Timeout: 5 * time.Second, // TODO 后期可以设置这个超时时间
|
||||||
}
|
}
|
||||||
|
|
||||||
sshClient, err := ssh.Dial("tcp", credentials.Host+":"+strconv.Itoa(credentials.Port), config)
|
sshClient, err := ssh.Dial("tcp", configutils.QuoteIP(credentials.Host)+":"+strconv.Itoa(credentials.Port), config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -90,7 +91,7 @@ func (this *BaseInstaller) Login(credentials *Credentials) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关闭SSH服务
|
// Close 关闭SSH服务
|
||||||
func (this *BaseInstaller) Close() error {
|
func (this *BaseInstaller) Close() error {
|
||||||
if this.client != nil {
|
if this.client != nil {
|
||||||
return this.client.Close()
|
return this.client.Close()
|
||||||
@@ -99,7 +100,7 @@ func (this *BaseInstaller) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找最新的版本的文件
|
// LookupLatestInstaller 查找最新的版本的文件
|
||||||
func (this *BaseInstaller) LookupLatestInstaller(filePrefix string) (string, error) {
|
func (this *BaseInstaller) LookupLatestInstaller(filePrefix string) (string, error) {
|
||||||
matches, err := filepath.Glob(Tea.Root + Tea.DS + "deploy" + Tea.DS + "*.zip")
|
matches, err := filepath.Glob(Tea.Root + Tea.DS + "deploy" + Tea.DS + "*.zip")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -131,7 +132,7 @@ func (this *BaseInstaller) LookupLatestInstaller(filePrefix string) (string, err
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 上传安装助手
|
// InstallHelper 上传安装助手
|
||||||
func (this *BaseInstaller) InstallHelper(targetDir string) (env *Env, err error) {
|
func (this *BaseInstaller) InstallHelper(targetDir string) (env *Env, err error) {
|
||||||
uname, _, err := this.client.Exec("uname -a")
|
uname, _, err := this.client.Exec("uname -a")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func NewSSHClient(raw *ssh.Client) (*SSHClient, error) {
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行shell命令
|
// Exec 执行shell命令
|
||||||
func (this *SSHClient) Exec(cmd string) (stdout string, stderr string, err error) {
|
func (this *SSHClient) Exec(cmd string) (stdout string, stderr string, err error) {
|
||||||
session, err := this.raw.NewSession()
|
session, err := this.raw.NewSession()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -86,7 +86,7 @@ func (this *SSHClient) Chmod(path string, mode os.FileMode) error {
|
|||||||
return this.sftp.Chmod(path, mode)
|
return this.sftp.Chmod(path, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 拷贝文件
|
// Copy 拷贝文件
|
||||||
func (this *SSHClient) Copy(localPath string, remotePath string, mode os.FileMode) error {
|
func (this *SSHClient) Copy(localPath string, remotePath string, mode os.FileMode) error {
|
||||||
localFp, err := os.Open(localPath)
|
localFp, err := os.Open(localPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -110,12 +110,12 @@ func (this *SSHClient) Copy(localPath string, remotePath string, mode os.FileMod
|
|||||||
return this.Chmod(remotePath, mode)
|
return this.Chmod(remotePath, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取新Session
|
// NewSession 获取新Session
|
||||||
func (this *SSHClient) NewSession() (*ssh.Session, error) {
|
func (this *SSHClient) NewSession() (*ssh.Session, error) {
|
||||||
return this.raw.NewSession()
|
return this.raw.NewSession()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取文件内容
|
// ReadFile 读取文件内容
|
||||||
func (this *SSHClient) ReadFile(path string) ([]byte, error) {
|
func (this *SSHClient) ReadFile(path string) ([]byte, error) {
|
||||||
fp, err := this.sftp.OpenFile(path, 0444)
|
fp, err := this.sftp.OpenFile(path, 0444)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -134,7 +134,7 @@ func (this *SSHClient) ReadFile(path string) ([]byte, error) {
|
|||||||
return buffer.Bytes(), nil
|
return buffer.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入文件内容
|
// WriteFile 写入文件内容
|
||||||
func (this *SSHClient) WriteFile(path string, data []byte) (n int, err error) {
|
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)
|
fp, err := this.sftp.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -148,7 +148,7 @@ func (this *SSHClient) WriteFile(path string, data []byte) (n int, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除文件
|
// Remove 删除文件
|
||||||
func (this *SSHClient) Remove(path string) error {
|
func (this *SSHClient) Remove(path string) error {
|
||||||
return this.sftp.Remove(path)
|
return this.sftp.Remove(path)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
"net"
|
"net"
|
||||||
@@ -252,7 +253,7 @@ func (this *NodeGrantService) TestNodeGrant(ctx context.Context, req *pb.TestNod
|
|||||||
Timeout: 5 * time.Second, // TODO 后期可以设置这个超时时间
|
Timeout: 5 * time.Second, // TODO 后期可以设置这个超时时间
|
||||||
}
|
}
|
||||||
|
|
||||||
sshClient, err := ssh.Dial("tcp", req.Host+":"+fmt.Sprintf("%d", req.Port), config)
|
sshClient, err := ssh.Dial("tcp", configutils.QuoteIP(req.Host)+":"+fmt.Sprintf("%d", req.Port), config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp.Error = "connect failed: " + err.Error()
|
resp.Error = "connect failed: " + err.Error()
|
||||||
return resp, nil
|
return resp, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user