-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathssh.go
168 lines (150 loc) · 3.36 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"encoding/json"
"fyne.io/fyne/v2/widget"
"golang.org/x/crypto/ssh"
"log"
"net"
"strconv"
)
type SSHConfigData struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
User string `json:"user,omitempty"`
Pswd string `json:"pswd,omitempty"`
}
type SSHConfigForm struct {
nameEntry *widget.Entry
hostEntry *widget.Entry
portEntry *widget.Entry
userEntry *widget.Entry
pswdEntry *widget.Entry
}
type SSHConfig struct {
data *SSHConfigData
form *SSHConfigForm
onOk func()
}
func (c *SSHConfig) Name() string {
return c.data.Name
}
func (c *SSHConfig) Type() string {
return "ssh"
}
func (c *SSHConfig) Load(s string) error {
data := &SSHConfigData{}
err := json.Unmarshal([]byte(s), data)
if err != nil {
return err
}
c.data = data
return nil
}
func (c *SSHConfig) Data() interface{} {
return c.data
}
func (c *SSHConfig) Form() *widget.Form {
c.form = &SSHConfigForm{}
nameEntry := widget.NewEntry()
hostEntry := widget.NewEntry()
portEntry := widget.NewEntry()
userEntry := widget.NewEntry()
pswdEntry := widget.NewEntry()
portEntry.Text = "22"
portEntry.Validator = func(s string) error {
_, err := strconv.Atoi(s)
return err
}
pswdEntry.Password = true
data := c.data
if data != nil {
nameEntry.Text = data.Name
nameEntry.Disable()
hostEntry.Text = data.Host
portEntry.Text = strconv.Itoa(data.Port)
userEntry.Text = data.User
pswdEntry.Text = data.Pswd
}
c.onOk = func() {
if c.data == nil {
c.data = &SSHConfigData{Type: c.Type()}
}
c.data.Name = nameEntry.Text
c.data.Host = hostEntry.Text
c.data.Port, _ = strconv.Atoi(portEntry.Text)
c.data.User = userEntry.Text
c.data.Pswd = pswdEntry.Text
}
return widget.NewForm([]*widget.FormItem{
widget.NewFormItem("Name", nameEntry),
widget.NewFormItem("Host", hostEntry),
widget.NewFormItem("Port", portEntry),
widget.NewFormItem("Username", userEntry),
widget.NewFormItem("Password", pswdEntry),
}...)
}
func (c *SSHConfig) OnOk() {
c.onOk()
}
func (c *SSHConfig) Term(win *Window) {
conf := c.data
cli := ssh.ClientConfig{User: conf.User, Auth: []ssh.AuthMethod{
ssh.Password(conf.Pswd),
}, HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
}}
addr := conf.Host + ":" + strconv.Itoa(conf.Port)
conn, err := ssh.Dial("tcp", addr, &cli)
if err != nil {
log.Println(err)
win.showError(err)
return
}
session, err := conn.NewSession()
if err != nil {
log.Println(err)
win.showError(err)
return
}
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
}
err = session.RequestPty("xterm-color", 24, 80, modes)
if err != nil {
log.Println(err)
win.showError(err)
return
}
in, err := session.StdinPipe()
if err != nil {
log.Println(err)
win.showError(err)
return
}
out, err := session.StdoutPipe()
if err != nil {
log.Println(err)
win.showError(err)
return
}
term := NewTerm(conf.Name, c)
go func() {
defer session.Close()
err = term.RunWithReaderAndWriter(in, out)
if err != nil {
log.Println(err)
}
session.Close()
}()
go func() {
err := session.Shell()
if err != nil {
log.Println(err)
}
}()
win.AddTermTab(term)
}