-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
406 lines (380 loc) · 13.7 KB
/
MainWindow.xaml.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
namespace SshTool
{
public partial class MainWindow : Window
{
SshClient client;
ForwardedPortLocal forwardedPortLocal;
Config[] configs;
int last_selected = -1;
public MainWindow()
{
InitializeComponent();
ReadConfig();
}
public void ReadConfig()
{
if (File.Exists("config"))
{
byte[] bytes = File.ReadAllBytes("config");
string[] info = System.Text.Encoding.Default.GetString(bytes).Split('\n');
// Console.WriteLine(info.Length);
if (info.Length > 1)
{
// MsgAppend("已读取配置");
Console.WriteLine(info[0]);
last_selected = int.Parse(info[0]);
configs = new Config[info.Length - 1];
for(int i = 1; i < info.Length; i++)
{
// Console.WriteLine("ID: " + i + " MSG:" + info[i]);
string[] items = info[i].Split('\t');
// Console.WriteLine(info[i]);
if (items.Length == 7)
{
configs[i-1] = new Config(i-1, items[0], items[1], items[2], items[3], items[4], items[5], items[6]);
}
}
if (configs.Length < 1)
{
MsgAppend("配置信息为空");
}
else
{
if (last_selected < 0)
{
last_selected = 0;
}
config_info.ItemsSource = GetConfigLabels();
config_info.SelectionChanged += ConfigOnSelect;
config_info.SelectedIndex = last_selected;
MsgAppend("发现配置,数量:" + configs.Length);
}
}
else
{
configs = null;
config_info.SelectedItem = null;
MsgAppend("配置内容为空");
}
}
else
{
MsgAppend("未发现配置文件");
}
}
byte[] ConfigToBytes(Config config)
{
return System.Text.Encoding.Default.GetBytes("\n" + config.label + "\t" + config.ssh_host + "\t" +
config.ssh_user + "\t" + config.ssh_password + "\t" + config.ssh_port + "\t" + config.app_port + "\t" + config.local_port);
}
public void SaveConfig()
{
if (File.Exists("config"))
{
File.Delete("config");
}
FileStream f = File.Create("config");
byte[] bytes = System.Text.Encoding.Default.GetBytes(last_selected.ToString());
f.Write(bytes, 0, bytes.Length);
for (int i = 0; i < configs.Length; i++)
{
if(-999 == configs[i].index)
{
continue;
}
bytes = ConfigToBytes(configs[i]);
f.Write(bytes, 0, bytes.Length);
}
f.Flush();
f.Close();
//MsgAppend("已保存配置");
}
List<string> GetConfigLabels()
{
List<string> list = new List<string>();
foreach(Config cfg in configs)
{
list.Add(cfg.index + "\t名称:" + GetLimitedString(cfg.label) + "\t地址:" +
GetLimitedString(cfg.ssh_host));
}
return list;
}
string GetLimitedString(string str)
{
int limited_len = 15;
if(str.Length < limited_len)
{
return str;
}
return str.Substring(0, limited_len) + "...";
}
void ConfigOnSelect(object sender, SelectionChangedEventArgs e)
{
if(null == configs)
{
config_info.ItemsSource = null;
config_label.Text = "";
ssh_host.Text = "";
ssh_user.Text = "";
ssh_password.Text = "";
ssh_port.Text = "";
app_port.Text = "";
local_port.Text = "";
return;
}
if(null == config_info.SelectedItem)
{
last_selected = 0;
}
else
{
last_selected = int.Parse(config_info.SelectedItem.ToString().Split('\t')[0]);
}
Config selectedOption = configs[last_selected];
config_label.Text = selectedOption.label;
ssh_host.Text = selectedOption.ssh_host;
ssh_user.Text = selectedOption.ssh_user;
ssh_password.Text = selectedOption.ssh_password;
ssh_port.Text = selectedOption.ssh_port;
app_port.Text = selectedOption.app_port;
local_port.Text = selectedOption.local_port;
//MsgAppend("载入配置: " + selectedOption.label);
SaveConfig();
}
public void MsgAppend(string msg)
{
info.Text = info.Text + "\n" + msg;
info.ScrollToEnd();
}
bool IsLabelExist(string label)
{
foreach(Config cfg in configs)
{
if (cfg.label.Equals(label))
{
return true;
}
}
return false;
}
public void Ssh_Start(string host_s, string user_s, string password_s,
string server_port_s, string app_port_s, string local_port_s)
{
if (host_s.Contains("@"))
{
user_s = host_s.Substring(0, host_s.IndexOf("@"));
ssh_user.Text = user_s;
host_s = host_s.Substring(host_s.IndexOf("@") + 1);
ssh_host.Text = host_s;
}
//string config_temp = host_s + "\n" + user_s + "\n" + password_s + "\n" + server_port_s + "\n" + app_port_s + "\n" + local_port_s;
//SaveConfig();
MsgAppend("服务器地址:" + host_s);
MsgAppend("ssh端口:" + server_port_s);
MsgAppend("ssh用户名:" + user_s);
MsgAppend("ssh密码:" + password_s);
MsgAppend("应用运行端口:" + app_port_s);
MsgAppend("本地浏览端口:" + local_port_s);
client = new SshClient(host_s, int.Parse(server_port_s), user_s, password_s);
client.KeepAliveInterval = new TimeSpan(0, 0, 5);
client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20);
try
{
client.Connect();
}
catch (Exception ex)
{
string err = "未知错误";
Console.WriteLine(ex);
if(ex.ToString().Contains("Permission denied"))
{
err = "用户名或密码错误";
}
if (ex.ToString().Contains("actively refused"))
{
err = "服务器异常或ssh端口错误";
}
if (ex.ToString().Contains("No such host"))
{
err = "服务器地址错误";
}
MsgAppend("连接异常: " + err);
}
MsgAppend("连接状态:" + client.IsConnected);
if (client.IsConnected)
{
try
{
forwardedPortLocal = new ForwardedPortLocal("127.0.0.1", uint.Parse(local_port_s), "localhost", uint.Parse(app_port_s));
client.AddForwardedPort(forwardedPortLocal);
forwardedPortLocal.Start();
MsgAppend("转发状态:" + forwardedPortLocal.IsStarted);
MsgAppend("本地浏览地址:http://127.0.0.1:" + local_port_s);
Process.Start("http://127.0.0.1:" + local_port_s);
connect.IsEnabled = false;
disconnect.IsEnabled = true;
}
catch (Exception e)
{
Console.WriteLine(e);
MsgAppend("转发异常");
}
}
}
public void Ssh_Stop()
{
if (!(null == forwardedPortLocal) && forwardedPortLocal.IsStarted)
{
forwardedPortLocal.Stop();
MsgAppend("转发状态: " + forwardedPortLocal.IsStarted);
}
if (!(null == client) && client.IsConnected)
{
client.Disconnect();
MsgAppend("连接状态: " + client.IsConnected);
}
MsgAppend("连接已断开");
}
public void Exit()
{
forwardedPortLocal.Dispose();
client.Dispose();
Environment.Exit(0);
}
private void Create_Click(object sender, RoutedEventArgs e)
{
NewConfigFromInput();
}
void NewConfigFromInput()
{
var host = ssh_host.Text.Trim();
var label = config_label.Text.Trim();
var ssh_u = ssh_user.Text.Trim();
var ssh_p = ssh_port.Text.Trim();
var pwd = ssh_password.Text.Trim();
var app_p = app_port.Text.Trim();
var local_p = local_port.Text.Trim();
if (IsEmpty(host) || IsEmpty(label) || IsEmpty(label) || IsEmpty(ssh_u) || IsEmpty(pwd) || IsEmpty(app_p) || IsEmpty(local_p))
{
MessageBox.Show("所有参数均为必填项");
return;
}
if (null == configs)
{
configs = new Config[1];
configs[0] = new Config(configs.Length, label, host, ssh_u, pwd, ssh_p, app_p, local_p);
}
else if (IsLabelExist(label))
{
MessageBox.Show("配置名称已存在,请重新填写配置信息后再次点击新增");
return;
}
else
{
Config cfg = new Config(configs.Length, label, host, ssh_u, pwd, ssh_p, app_p, local_p);
Config[] Cfgs = new Config[configs.Length + 1];
configs.CopyTo(Cfgs, 0);
Cfgs[Cfgs.Length - 1] = cfg;
configs = Cfgs;
last_selected = Cfgs.Length - 1;
}
SaveConfig();
ReadConfig();
MsgAppend("添加成功");
}
private void Del_Click(object sender, RoutedEventArgs e)
{
if(null == configs || configs.Length < 1)
{
MsgAppend("当前配置信息为空");
}
else
{
configs[last_selected].index = -999;
last_selected = 0;
MsgAppend("删除成功");
SaveConfig();
ReadConfig();
}
}
private void analyse_config_Click(object sender, RoutedEventArgs e)
{
// ssh -p 19749 [email protected]
var cfg = config_analyse.Text.Trim().Split(' ');
local_port.Text = "12345";
app_port.Text = "6006";
if(null != configs)
{
config_label.Text = configs.Length.ToString();
}
else
{
config_label.Text = "0";
}
if (cfg.Length == 4)
{
ssh_port.Text = cfg[2];
var res = cfg[3].Split('@');
if(res.Length == 2)
{
ssh_user.Text = res[0];
ssh_host.Text = res[1];
config_label.Text = "新配置" + config_label.Text;
ssh_password.Text = "";
}
MsgAppend("解析完成");
}
else
{
MsgAppend("格式不符,解析失败");
}
}
private void Connect_Click(object sender, RoutedEventArgs e)
{
var host = ssh_host.Text.Trim();
var ssh_u = ssh_user.Text.Trim();
var ssh_p = ssh_port.Text.Trim();
var pwd = ssh_password.Text.Trim();
var app_p = app_port.Text.Trim();
var local_p = local_port.Text.Trim();
if (IsEmpty(host) || IsEmpty(ssh_p) || IsEmpty(pwd) || IsEmpty(app_p) || IsEmpty(local_p))
{
MessageBox.Show("所有参数均为必填项");
}
else
{
Ssh_Start(host, ssh_u, pwd, ssh_p, app_p, local_p);
}
}
private void Disconnect_Click(object sender, RoutedEventArgs e)
{
Ssh_Stop();
connect.IsEnabled = true;
disconnect.IsEnabled = false;
}
bool IsEmpty(string s)
{
return string.IsNullOrEmpty(s);
}
private void Exit_Click(object sender, RoutedEventArgs e)
{
if ((null == forwardedPortLocal) || (null == client))
{
Environment.Exit(0);
}
if (forwardedPortLocal.IsStarted || client.IsConnected)
{
Console.WriteLine("自动关闭连接");
Ssh_Stop();
}
Exit();
}
}
}