Skip to content

Commit

Permalink
优化ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
1113655791@qq.com committed May 4, 2023
1 parent 1dccdad commit 71d02d7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
42 changes: 23 additions & 19 deletions dial/dial_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,15 @@ func (this *_websocket) Close() error {
//================================SSH================================

type SSHConfig struct {
Addr string
User string
Password string //类型为password
Type string //password 或者 key
Timeout time.Duration
key string //类型为key
keyPassword string //类型为key
High int //高
Wide int //宽
Addr string
User string
Password string //类型为password
Timeout time.Duration
High int //高
Wide int //宽
//Type string //password 或者 key
//key string //类型为key
//keyPassword string //类型为key
}

func (this *SSHConfig) new() *SSHConfig {
Expand All @@ -319,6 +319,10 @@ type Client struct {
err io.Reader
}

func (this *Client) Write(p []byte) (int, error) {
return this.Writer.Write(append(p, '\n'))
}

func SSH(cfg *SSHConfig) (io.ReadWriteCloser, error) {
cfg.new()
config := &ssh.ClientConfig{
Expand All @@ -327,14 +331,14 @@ func SSH(cfg *SSHConfig) (io.ReadWriteCloser, error) {
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{ssh.Password(cfg.Password)},
}
switch cfg.Type {
case "key":
signer, err := ssh.ParsePrivateKeyWithPassphrase([]byte(cfg.key), []byte(cfg.keyPassword))
if err != nil {
return nil, err
}
config.Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
}
//switch cfg.Type {
//case "key":
// signer, err := ssh.ParsePrivateKeyWithPassphrase([]byte(cfg.key), []byte(cfg.keyPassword))
// if err != nil {
// return nil, err
// }
// config.Auth = []ssh.AuthMethod{ssh.PublicKeys(signer)}
//}
sshClient, err := ssh.Dial("tcp", cfg.Addr, config)
if err != nil {
return nil, err
Expand All @@ -344,7 +348,7 @@ func SSH(cfg *SSHConfig) (io.ReadWriteCloser, error) {
return nil, err
}
modes := ssh.TerminalModes{
ssh.ECHO: 1, // 禁用回显(0禁用,1启动)
ssh.ECHO: 0, // 禁用回显(0禁用,1启动)
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, //output speed = 14.4kbaud
}
Expand All @@ -360,7 +364,7 @@ func SSH(cfg *SSHConfig) (io.ReadWriteCloser, error) {
if err != nil {
return nil, err
}
if err := session.RequestPty("xterm", cfg.High, cfg.Wide, modes); err != nil {
if err := session.RequestPty("xterm-256color", cfg.High, cfg.Wide, modes); err != nil {
return nil, err
}
if err := session.Shell(); err != nil {
Expand Down
31 changes: 29 additions & 2 deletions dial/dial_dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ package dial

import (
"context"
"fmt"
"github.com/injoyai/io"
"testing"
"time"
)

func TestNewWebsocket(t *testing.T) {
//"ws://192.168.10.3:1880/node-red/comms"
url := "ws://192.168.10.24:10001/api/user/notice/ws"
//"ws://192.168.10.3:1880/node-red/comms"
url = "ws://192.168.10.24:10001/api/ai/info/runtime/ws?id=83"
//url := "ws://192.168.10.24:10001/api/user/notice/ws"
//url += "?token=jbYKl72cbOGvbVRwIqM4r6eoirw8f1JRD44+4D5E/URRY4L6TTZYYb/9yhedvd2Ii2GtLo9MieBy5FBeUhugK5jHvppFjExz3B5DVFPqsomF5wezKDFc8a2hZSQ9IDHTS/C+j/3ESSRdbkVHPFxbzQ=="
//url = strings.ReplaceAll(url, "+", "%2B")
t.Log(url)
RedialWebsocket(url, map[string][]string{}, io.WithClientDebug())
RedialWebsocket(url, nil, io.WithClientDebug(), func(ctx context.Context, c *io.Client) {
c.GoTimerWriter(time.Second*10, func(w *io.IWriter) error {
_, err := w.WriteString("83")
return err
})
})
select {}
}

Expand Down Expand Up @@ -40,3 +49,21 @@ func TestRedialTCP(t *testing.T) {
func TestRedialMQTT(t *testing.T) {
//RedialMQTT("xxx")
}

func TestRedialSSH(t *testing.T) {
RedialSSH(&SSHConfig{
Addr: "192.168.10.40:22",
User: "qinalang",
Password: "ql1123",
}, func(ctx context.Context, c *io.Client) {
c.Debug()
go func() {
for {
msg := ""
fmt.Scan(&msg)
c.WriteString(msg)
}
}()
})
select {}
}

0 comments on commit 71d02d7

Please sign in to comment.