-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshwincs.go
215 lines (205 loc) · 7.43 KB
/
sshwincs.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"github.com/lxn/walk"
"github.com/lxn/walk/declarative"
)
// RemoteSSHShell struct type to store ssh data
type RemoteSSHShell struct {
AuthMthd int
HostAddr string
HostPort int
PreVTKEY string // private key path
PassPhrase string
PassWord string
UserName string
}
func startSSHsession(owner walk.Form, sendipaddr string) (int, error) {
const regexSingle = `^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z]{2,3})$`
var sshClientguiwindow struct {
dlgSSHAccwin *walk.Dialog
dbindersshx *walk.DataBinder
acceptPB, cancelPB *walk.PushButton
mapLineEd, pkLineEd, pKandPHm *walk.LineEdit
passGBOXm, pKeyGBOXm *walk.GroupBox
}
sshstatset := &RemoteSSHShell{
AuthMthd: 1,
HostPort: 22,
UserName: "root",
HostAddr: sendipaddr,
}
return declarative.Dialog{
AssignTo: &sshClientguiwindow.dlgSSHAccwin,
FixedSize: true,
Title: "SSH Client",
DefaultButton: &sshClientguiwindow.acceptPB,
CancelButton: &sshClientguiwindow.cancelPB,
DataBinder: declarative.DataBinder{
AssignTo: &sshClientguiwindow.dbindersshx,
Name: "sshClient",
DataSource: sshstatset,
ErrorPresenter: declarative.ToolTipErrorPresenter{},
},
MinSize: declarative.Size{Width: 320, Height: 300},
Layout: declarative.VBox{},
Children: []declarative.Widget{
declarative.GroupBox{
Title: "SSH Client Options",
Layout: declarative.HBox{},
Children: []declarative.Widget{
declarative.Composite{
Layout: declarative.Grid{Columns: 1},
Children: []declarative.Widget{
declarative.GroupBox{
Title: "Host Address To Connect",
Layout: declarative.Grid{Columns: 4},
Children: []declarative.Widget{
declarative.LineEdit{
ToolTipText: "username for ssh connection",
CueBanner: "User",
MaxSize: declarative.Size{Width: 55},
Text: declarative.Bind("UserName"),
},
declarative.LineEdit{
AssignTo: &sshClientguiwindow.mapLineEd,
ToolTipText: "format: IP|Domain",
CueBanner: "IP or Domain",
Text: declarative.Bind("HostAddr"),
},
declarative.NumberEdit{
ToolTipText: "port number",
MaxSize: declarative.Size{Width: 40},
MinSize: declarative.Size{Width: 40},
Value: declarative.Bind("HostPort"),
MinValue: 1,
MaxValue: 65535,
},
},
},
declarative.RadioButtonGroupBox{
Title: "Authentication",
Layout: declarative.HBox{},
DataMember: "AuthMthd",
Buttons: []declarative.RadioButton{
{MaxSize: declarative.Size{Width: 90}, Text: "Password", Value: 1, OnClicked: func() {
sshClientguiwindow.passGBOXm.SetVisible(true)
sshClientguiwindow.pKeyGBOXm.SetVisible(false)
sshClientguiwindow.pKandPHm.SetVisible(false)
}},
{MaxSize: declarative.Size{Width: 90}, Text: "PrivateK", Value: 2, OnClicked: func() {
sshClientguiwindow.passGBOXm.SetVisible(false)
sshClientguiwindow.pKeyGBOXm.SetVisible(true)
sshClientguiwindow.pKandPHm.SetVisible(false)
}},
{MaxSize: declarative.Size{Width: 90}, Text: "PK+PasPh", Value: 3, OnClicked: func() {
sshClientguiwindow.passGBOXm.SetVisible(false)
sshClientguiwindow.pKeyGBOXm.SetVisible(true)
sshClientguiwindow.pKandPHm.SetVisible(true)
}},
},
},
declarative.GroupBox{
AssignTo: &sshClientguiwindow.passGBOXm,
Title: "Login With Password",
Layout: declarative.Grid{Columns: 1},
Children: []declarative.Widget{
declarative.LineEdit{
CueBanner: "Password ...",
PasswordMode: true,
Text: declarative.Bind("PassWord"),
},
},
},
declarative.GroupBox{
AssignTo: &sshClientguiwindow.pKeyGBOXm,
Title: "Login With Private Key",
Visible: false,
Layout: declarative.VBox{},
Children: []declarative.Widget{
declarative.LineEdit{
AssignTo: &sshClientguiwindow.pkLineEd,
CueBanner: "Private Key File Path ...",
ReadOnly: true,
Text: declarative.Bind("PreVTKEY"),
},
declarative.PushButton{
Text: "Open Key File",
ToolTipText: "open ssh key file",
OnClicked: func() {
filePth, err := openPkFile(sshClientguiwindow.dlgSSHAccwin)
if err != nil {
walk.MsgBox(sshClientguiwindow.dlgSSHAccwin, "OS file error", fmt.Sprint(err), walk.MsgBoxIconError)
return
}
sshClientguiwindow.pkLineEd.SetText(filePth)
},
},
declarative.LineEdit{
AssignTo: &sshClientguiwindow.pKandPHm,
CueBanner: "Passphrase for Key ...",
PasswordMode: true,
Text: declarative.Bind("PassPhrase"),
},
},
},
},
},
},
},
declarative.Composite{
Layout: declarative.HBox{},
Children: []declarative.Widget{
declarative.HSpacer{},
declarative.PushButton{
AssignTo: &sshClientguiwindow.acceptPB,
Text: "Connect",
OnClicked: func() {
if err := sshClientguiwindow.dbindersshx.Submit(); err != nil {
return
}
if sshstatset.HostAddr == "" {
sshClientguiwindow.mapLineEd.SetFocus()
return
}
if match, _ := regexp.MatchString(regexSingle, sshstatset.HostAddr); !match {
walk.MsgBox(sshClientguiwindow.dlgSSHAccwin, "Host syntax error", "Invalid IPv4 or Domain address, format: IPv4|Domain\nExample: slc.snix.ir", walk.MsgBoxIconError)
return
}
if exeErr := exec.Command("cmd", "/c", "start",
os.Args[0],
"-auth", fmt.Sprint(sshstatset.AuthMthd),
"-addr", fmt.Sprintf("%v:%v", sshstatset.HostAddr, sshstatset.HostPort),
"-user", sshstatset.UserName,
"-pass", sshstatset.PassWord,
"-prvk", sshstatset.PreVTKEY,
"-prph", sshstatset.PassPhrase).Start(); exeErr != nil {
walk.MsgBox(sshClientguiwindow.dlgSSHAccwin, "Internal System Err", fmt.Sprintf("%v", exeErr), walk.MsgBoxIconError)
return
}
},
},
declarative.PushButton{
AssignTo: &sshClientguiwindow.cancelPB,
Text: "Cancel",
OnClicked: func() { sshClientguiwindow.dlgSSHAccwin.Cancel() },
},
},
},
},
}.Run(owner)
}
func openPkFile(dlgwinad walk.Form) (string, error) {
openSSHfile := new(walk.FileDialog)
openSSHfile.Filter = "PEM Files (*.pem)|*.pem|All Files (*.*)|*.*"
openSSHfile.Title = "Open SSH Key File"
if okfile, err := openSSHfile.ShowOpen(dlgwinad); err != nil {
return openSSHfile.FilePath, err
} else if !okfile {
return openSSHfile.FilePath, err
}
return openSSHfile.FilePath, nil
}