-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.cs
322 lines (270 loc) · 12.1 KB
/
Session.cs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* Copyright (c) 2009, Joeri Bekker
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using Microsoft.Win32;
namespace JoeriBekker.PuttyTunnelManager
{
class Session
{
public static string PUTTY_REGISTRY_KEY_SESSION_HOSTNAME = "HostName";
public static string PUTTY_REGISTRY_KEY_SESSION_PORTFORWARDINGS = "PortForwardings";
public static string PUTTY_REGISTRY_KEY_SESSION_USERNAME = "Username";
public static string PUTTY_REGISTRY_KEY_SESSION_PORTNUMBER = "PortNumber";
public static string PUTTY_REGISTRY_KEY_SESSION_COMPRESSION = "Compression";
public static string PUTTY_REGISTRY_KEY_SESSION_LOCALPORTACCEPTALL = "LocalPortAcceptAll";
public static string PTM_REGISTRY_KEY_SESSION_PORTFORWARDINGS = "PortForwardings";
public static string PTM_REGISTRY_KEY_SESSION_USEPTMFORTUNNELS = "UsePtmForTunnels";
private string name;
private string hostname;
private string username;
private int port;
private bool compression;
private bool localPortsAcceptAll;
private List<Tunnel> tunnels;
private bool usePtmForTunnels;
private PuttyLink puttyLink;
private bool resume;
public static List<Session> OpenSessions = new List<Session>();
public Session(string name, string hostname, int port)
{
this.name = name;
this.hostname = hostname;
this.username = "";
this.port = port;
this.compression = false;
this.localPortsAcceptAll = false;
this.tunnels = new List<Tunnel>();
this.usePtmForTunnels = false;
this.puttyLink = null;
this.resume = false;
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
}
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Suspend && this.IsOpen)
{
this.resume = true;
this.Close();
}
else if (e.Mode == PowerModes.Resume && this.resume)
{
this.resume = false;
this.Open();
}
}
public string PuttyKeyPath
{
get { return PuttySettings.PUTTY_REGISTRY_KEYPATH_SESSIONS + @"\" + Uri.EscapeUriString(this.name); }
}
public string PuttyTunnelManagerKeyPath
{
get { return PuttyTunnelManagerSettings.PTM_REGISTRY_KEYPATH_SESSIONS + @"\" + Uri.EscapeUriString(name); }
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public string Hostname
{
get { return this.hostname; }
set { this.hostname = value; }
}
public string Username
{
get { return this.username; }
set { this.username = value; }
}
public int Port
{
get { return this.port; }
set { this.port = value; }
}
public bool Compression
{
get { return this.compression; }
set { this.compression = value; }
}
public bool LocalPortsAcceptAll
{
get { return this.localPortsAcceptAll; }
set { this.localPortsAcceptAll = value; }
}
public bool UsePtmForTunnels
{
get { return this.usePtmForTunnels; }
set { this.usePtmForTunnels = value; }
}
public List<Tunnel> Tunnels
{
get { return tunnels; }
}
public bool IsOpen
{
get { return (this.puttyLink != null && this.puttyLink.IsActive); }
}
public virtual void Close()
{
if (this.IsOpen)
this.puttyLink.Stop();
//if (OpenSessions.Contains(this))
// OpenSessions.Remove(this);
this.puttyLink = null;
}
public virtual void Open()
{
if (this.IsOpen)
throw new SessionAlreadyOpenException();
// Rather then just checking our own tunnels, just go over TCP listeners.
foreach (Tunnel tunnel in this.Tunnels)
{
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] ipEndPoints = ipGlobalProperties.GetActiveTcpListeners();
foreach (IPEndPoint ipEndPoint in ipEndPoints)
{
if (ipEndPoint.Port == tunnel.SourcePort)
{
throw new PortAlreadyInUseException(tunnel);
}
}
}
this.puttyLink = new PuttyLink(this);
Thread thread = new Thread(new ThreadStart(this.puttyLink.Start));
thread.IsBackground = true;
thread.Start();
//OpenSessions.Add(this);
}
public virtual void Serialize()
{
RegistryKey puttySessionKey = Registry.CurrentUser.CreateSubKey(this.PuttyKeyPath);
puttySessionKey.SetValue(PUTTY_REGISTRY_KEY_SESSION_HOSTNAME, this.hostname, RegistryValueKind.String);
puttySessionKey.SetValue(PUTTY_REGISTRY_KEY_SESSION_PORTNUMBER, this.port, RegistryValueKind.DWord);
puttySessionKey.SetValue(PUTTY_REGISTRY_KEY_SESSION_USERNAME, this.username, RegistryValueKind.String);
puttySessionKey.SetValue(PUTTY_REGISTRY_KEY_SESSION_COMPRESSION, this.compression, RegistryValueKind.DWord);
puttySessionKey.SetValue(PUTTY_REGISTRY_KEY_SESSION_LOCALPORTACCEPTALL, this.localPortsAcceptAll, RegistryValueKind.DWord);
StringBuilder buffer = new StringBuilder();
foreach (Tunnel tunnel in this.Tunnels)
{
buffer.Append(tunnel.Serialize());
}
if (this.usePtmForTunnels)
{
// Save Tunnel Manager specific settings
RegistryKey ptmSessionKey = Registry.CurrentUser.CreateSubKey(this.PuttyTunnelManagerKeyPath);
ptmSessionKey.SetValue(PTM_REGISTRY_KEY_SESSION_PORTFORWARDINGS, buffer.ToString(), RegistryValueKind.String);
ptmSessionKey.SetValue(PTM_REGISTRY_KEY_SESSION_USEPTMFORTUNNELS, this.usePtmForTunnels, RegistryValueKind.DWord);
ptmSessionKey.Close();
}
else
{
puttySessionKey.SetValue(PUTTY_REGISTRY_KEY_SESSION_PORTFORWARDINGS, buffer.ToString(), RegistryValueKind.String);
}
puttySessionKey.Close();
}
public static Session Load(string keyName)
{
string puttyKeyPath = PuttySettings.PUTTY_REGISTRY_KEYPATH_SESSIONS + @"\" + keyName;
RegistryKey puttySessionKey = Registry.CurrentUser.OpenSubKey(puttyKeyPath);
Session session = new Session(
Uri.UnescapeDataString(keyName),
puttySessionKey.GetValue(PUTTY_REGISTRY_KEY_SESSION_HOSTNAME, "").ToString(),
Int32.Parse(puttySessionKey.GetValue(PUTTY_REGISTRY_KEY_SESSION_PORTNUMBER, "22").ToString())
);
session.Username = puttySessionKey.GetValue(PUTTY_REGISTRY_KEY_SESSION_USERNAME, "").ToString();
session.Compression = puttySessionKey.GetValue(PUTTY_REGISTRY_KEY_SESSION_COMPRESSION, 0).Equals(1);
session.LocalPortsAcceptAll = puttySessionKey.GetValue(PUTTY_REGISTRY_KEY_SESSION_LOCALPORTACCEPTALL, 0).Equals(1);
string ptmKeyPath = PuttyTunnelManagerSettings.PTM_REGISTRY_KEYPATH_SESSIONS + @"\" + keyName;
RegistryKey ptmSessionKey = Registry.CurrentUser.OpenSubKey(ptmKeyPath);
// Load Tunnel Manager specific settings.
if (ptmSessionKey != null)
{
session.UsePtmForTunnels = ptmSessionKey.GetValue(PTM_REGISTRY_KEY_SESSION_USEPTMFORTUNNELS, 0).Equals(1);
}
string[] portForwardingList;
if (session.UsePtmForTunnels && ptmSessionKey != null)
{
portForwardingList = ptmSessionKey.GetValue(PTM_REGISTRY_KEY_SESSION_PORTFORWARDINGS, "").ToString().Split(',');
ptmSessionKey.Close();
}
else
portForwardingList = puttySessionKey.GetValue(PUTTY_REGISTRY_KEY_SESSION_PORTFORWARDINGS, "").ToString().Split(',');
puttySessionKey.Close();
foreach (string portForwarding in portForwardingList)
{
if (portForwarding.Length > 0)
{
Tunnel t = Tunnel.Load(session, portForwarding);
session.Tunnels.Add(t);
}
}
return session;
}
public virtual void Delete()
{
try
{
if (this.IsOpen)
this.Close();
Registry.CurrentUser.DeleteSubKey(this.PuttyKeyPath);
Registry.CurrentUser.DeleteSubKey(this.PuttyTunnelManagerKeyPath);
}
catch (Exception) { }
}
public virtual void RemovePuttyTunnels()
{
RegistryKey puttySessionKey = Registry.CurrentUser.OpenSubKey(this.PuttyKeyPath, true);
puttySessionKey.SetValue(PUTTY_REGISTRY_KEY_SESSION_PORTFORWARDINGS, "", RegistryValueKind.String);
puttySessionKey.Close();
}
public virtual void MergeBackPuttyTunnels()
{
try
{
Registry.CurrentUser.DeleteSubKey(this.PuttyTunnelManagerKeyPath);
}
catch (Exception) { }
RegistryKey puttySessionKey = Registry.CurrentUser.OpenSubKey(this.PuttyKeyPath);
string[] portForwardingList = puttySessionKey.GetValue(PUTTY_REGISTRY_KEY_SESSION_PORTFORWARDINGS, "").ToString().Split(',');
puttySessionKey.Close();
List<Tunnel> additionalTunnels = new List<Tunnel>();
foreach (string portForwarding in portForwardingList)
{
if (portForwarding.Length > 0)
{
Tunnel t1 = Tunnel.Load(this, portForwarding);
foreach (Tunnel t2 in this.tunnels)
{
if (!t1.Equals(t2))
additionalTunnels.Add(t1);
}
}
}
foreach (Tunnel tunnel in additionalTunnels)
this.tunnels.Add(tunnel);
}
}
}