/
Connection.Protocol.PuttyBase.vb
294 lines (248 loc) · 12.5 KB
/
Connection.Protocol.PuttyBase.vb
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports mRemoteNG.Messages
Imports mRemoteNG.App.Native
Imports System.Threading
Imports Microsoft.Win32
Imports System.Drawing
Imports mRemoteNG.App.Runtime
Namespace Connection
Namespace Protocol
Public Class PuttyBase
Inherits Connection.Protocol.Base
#Region "Constants"
Private Const IDM_RECONF As Int32 = &H50 ' PuTTY Settings Menu ID
#End Region
#Region "Private Properties"
Dim _isPuttyNg As Boolean
#End Region
#Region "Public Properties"
Private _PuttyProtocol As Putty_Protocol
Public Property PuttyProtocol() As Putty_Protocol
Get
Return Me._PuttyProtocol
End Get
Set(ByVal value As Putty_Protocol)
Me._PuttyProtocol = value
End Set
End Property
Private _PuttySSHVersion As Putty_SSHVersion
Public Property PuttySSHVersion() As Putty_SSHVersion
Get
Return Me._PuttySSHVersion
End Get
Set(ByVal value As Putty_SSHVersion)
Me._PuttySSHVersion = value
End Set
End Property
Private _PuttyHandle As IntPtr
Public Property PuttyHandle() As IntPtr
Get
Return Me._PuttyHandle
End Get
Set(ByVal value As IntPtr)
Me._PuttyHandle = value
End Set
End Property
Private _PuttyProcess As Process
Public Property PuttyProcess() As Process
Get
Return Me._PuttyProcess
End Get
Set(ByVal value As Process)
Me._PuttyProcess = value
End Set
End Property
Private Shared _PuttyPath As String
Public Shared Property PuttyPath() As String
Get
Return _PuttyPath
End Get
Set(ByVal value As String)
_PuttyPath = value
End Set
End Property
'Private borderWidth As Integer = frmMain.Size.Width - frmMain.ClientSize.Width
'Private borderHeight As Integer = frmMain.Size.Height - frmMain.ClientSize.Height
Private Shared _BorderSize As Size
Public Shared Property BorderSize() As Size
Get
Return _BorderSize
End Get
Set(ByVal value As Size)
_BorderSize = value
End Set
End Property
Public ReadOnly Property Focused() As Boolean
Get
If GetForegroundWindow() = PuttyHandle Then
Return True
Else
Return False
End If
End Get
End Property
#End Region
#Region "Private Events & Handlers"
Private Sub ProcessExited(ByVal sender As Object, ByVal e As System.EventArgs)
MyBase.Event_Closed(Me)
End Sub
#End Region
#Region "Public Methods"
Public Sub New()
End Sub
Public Overrides Function Connect() As Boolean
Try
_isPuttyNg = IsFilePuttyNg(PuttyPath)
PuttyProcess = New Process
With PuttyProcess.StartInfo
.FileName = _PuttyPath
Select Case Me._PuttyProtocol
Case Putty_Protocol.raw
.Arguments = "-load " & """" & Me.InterfaceControl.Info.PuttySession & """" & " -" & Me._PuttyProtocol.ToString & " -P " & Me.InterfaceControl.Info.Port & " """ & Me.InterfaceControl.Info.Hostname & """"
Case Putty_Protocol.rlogin
.Arguments = "-load " & """" & Me.InterfaceControl.Info.PuttySession & """" & " -" & Me._PuttyProtocol.ToString & " -P " & Me.InterfaceControl.Info.Port & " """ & Me.InterfaceControl.Info.Hostname & """"
Case Putty_Protocol.ssh
Dim UserArg As String = ""
Dim PassArg As String = ""
If My.Settings.EmptyCredentials = "windows" Then
UserArg = " -l """ & Environment.UserName & """"
ElseIf My.Settings.EmptyCredentials = "custom" Then
UserArg = " -l """ & My.Settings.DefaultUsername & """"
PassArg = " -pw """ & Security.Crypt.Decrypt(My.Settings.DefaultPassword, App.Info.General.EncryptionKey) & """"
End If
If Me.InterfaceControl.Info.Username <> "" Then
UserArg = " -l """ & Me.InterfaceControl.Info.Username & """"
End If
If Me.InterfaceControl.Info.Password <> "" Then
PassArg = " -pw """ & Me.InterfaceControl.Info.Password & """"
End If
.Arguments = "-load " & """" & Me.InterfaceControl.Info.PuttySession & """" & " -" & Me._PuttyProtocol.ToString & " -" & Me._PuttySSHVersion & UserArg & PassArg & " -P " & Me.InterfaceControl.Info.Port & " """ & Me.InterfaceControl.Info.Hostname & """"
Case Putty_Protocol.telnet
.Arguments = "-load " & """" & Me.InterfaceControl.Info.PuttySession & """" & " -" & Me._PuttyProtocol.ToString & " -P " & Me.InterfaceControl.Info.Port & " """ & Me.InterfaceControl.Info.Hostname & """"
Case Putty_Protocol.serial
.Arguments = "-load " & """" & Me.InterfaceControl.Info.PuttySession & """" & " -" & Me._PuttyProtocol.ToString & " -P " & Me.InterfaceControl.Info.Port & " """ & Me.InterfaceControl.Info.Hostname & """"
End Select
If _isPuttyNg Then
.Arguments = .Arguments & " -hwndparent " & Me.InterfaceControl.Handle.ToString()
End If
'REMOVE IN RELEASE!
'mC.AddMessage(Messages.MessageClass.InformationMsg, "PuTTY Arguments: " & .Arguments, True)
End With
PuttyProcess.EnableRaisingEvents = True
AddHandler PuttyProcess.Exited, AddressOf ProcessExited
PuttyProcess.Start()
PuttyProcess.WaitForInputIdle()
If _isPuttyNg Then
PuttyHandle = FindWindowEx(Me.InterfaceControl.Handle, 0, vbNullString, vbNullString)
Else
PuttyHandle = PuttyProcess.MainWindowHandle
SetParent(PuttyHandle, InterfaceControl.Handle)
End If
MessageCollector.AddMessage(Messages.MessageClass.InformationMsg, My.Language.strPuttyStuff, True)
MessageCollector.AddMessage(Messages.MessageClass.InformationMsg, String.Format(My.Language.strPuttyHandle, PuttyHandle.ToString), True)
MessageCollector.AddMessage(Messages.MessageClass.InformationMsg, String.Format(My.Language.strPuttyTitle, PuttyProcess.MainWindowTitle), True)
MessageCollector.AddMessage(Messages.MessageClass.InformationMsg, String.Format(My.Language.strPuttyParentHandle, Me.InterfaceControl.Parent.Handle.ToString), True)
Resize()
MyBase.Connect()
Return True
Catch ex As Exception
MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, My.Language.strPuttyConnectionFailed & vbNewLine & ex.Message)
Return False
End Try
End Function
Public Overrides Sub Focus()
Try
SetForegroundWindow(PuttyHandle)
Catch ex As Exception
MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, My.Language.strPuttyFocusFailed & vbNewLine & ex.Message, True)
End Try
End Sub
Public Overrides Sub Resize()
Try
If frmMain.WindowState = FormWindowState.Minimized Then Return
MoveWindow(PuttyHandle, -SystemInformation.FrameBorderSize.Width, -(SystemInformation.CaptionHeight + SystemInformation.FrameBorderSize.Height), InterfaceControl.Width + (SystemInformation.FrameBorderSize.Width * 2), InterfaceControl.Height + SystemInformation.CaptionHeight + (SystemInformation.FrameBorderSize.Height * 2), True)
Catch ex As Exception
MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, My.Language.strPuttyResizeFailed & vbNewLine & ex.Message, True)
End Try
End Sub
Public Overrides Sub Close()
Try
If PuttyProcess.HasExited = False Then
PuttyProcess.Kill()
End If
Catch ex As Exception
MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, My.Language.strPuttyKillFailed & vbNewLine & ex.Message, True)
End Try
Try
PuttyProcess.Dispose()
Catch ex As Exception
MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, My.Language.strPuttyDisposeFailed & vbNewLine & ex.Message, True)
End Try
MyBase.Close()
End Sub
Public Sub ShowSettingsDialog()
Try
PostMessage(Me.PuttyHandle, WM_SYSCOMMAND, IDM_RECONF, 0)
SetForegroundWindow(Me.PuttyHandle)
Catch ex As Exception
MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, My.Language.strPuttyShowSettingsDialogFailed & vbNewLine & ex.Message, True)
End Try
End Sub
#End Region
#Region "Public Shared Methods"
Public Shared Function GetSessions() As Array
Try
Dim regKey As RegistryKey
regKey = Registry.CurrentUser.OpenSubKey("Software\SimonTatham\PuTTY\Sessions")
Dim arrKeys() As String
arrKeys = regKey.GetSubKeyNames()
Array.Resize(arrKeys, arrKeys.Length + 1)
arrKeys(arrKeys.Length - 1) = "Default Settings"
For i As Integer = 0 To arrKeys.Length - 1
arrKeys(i) = System.Web.HttpUtility.UrlDecode(arrKeys(i))
Next
Return arrKeys
Catch ex As Exception
App.Runtime.MessageCollector.AddMessage(Messages.MessageClass.WarningMsg, My.Language.strPuttyGetSessionsFailed & vbNewLine & ex.Message, True)
Return Nothing
End Try
End Function
Public Shared Function IsFilePuttyNg(file As String) As Boolean
Dim isPuttyNg As Boolean
Try
isPuttyNg = FileVersionInfo.GetVersionInfo(file).InternalName.Contains("PuTTYNG")
Catch
isPuttyNg = False
End Try
Return isPuttyNg
End Function
Public Shared Sub StartPutty()
Try
Dim p As Process
Dim pSI As New ProcessStartInfo
pSI.FileName = PuttyPath
p = Process.Start(pSI)
p.WaitForExit()
mRemoteNG.Connection.PuttySession.PuttySessions = GetSessions()
Catch ex As Exception
MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, My.Language.strPuttyStartFailed & vbNewLine & ex.Message, True)
End Try
End Sub
#End Region
#Region "Enums"
Public Enum Putty_Protocol
ssh = 0
telnet = 1
rlogin = 2
raw = 3
serial = 4
End Enum
Public Enum Putty_SSHVersion
ssh1 = 1
ssh2 = 2
End Enum
#End Region
End Class
End Namespace
End Namespace