Skip to content

Commit 78d6bdf

Browse files
committed
fdsfasfas
1 parent 61af567 commit 78d6bdf

8 files changed

+468
-216
lines changed

Diff for: SSH/App.xaml.cs

+75-14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99

1010
namespace npcook.Ssh
1111
{
12+
[Serializable]
13+
public class ConnectException : Exception
14+
{
15+
public ConnectException() { }
16+
public ConnectException(string message) : base(message) { }
17+
public ConnectException(string message, Exception inner) : base(message, inner) { }
18+
protected ConnectException(
19+
System.Runtime.Serialization.SerializationInfo info,
20+
System.Runtime.Serialization.StreamingContext context) : base(info, context)
21+
{ }
22+
}
23+
1224
/// <summary>
1325
/// Interaction logic for App.xaml
1426
/// </summary>
@@ -32,27 +44,76 @@ private void Dispatcher_UnhandledException(object sender, System.Windows.Threadi
3244
Shutdown(1);
3345
}
3446

35-
private void this_Startup(object sender, StartupEventArgs e)
47+
private async void this_Startup(object sender, StartupEventArgs e)
3648
{
3749
ShutdownMode = ShutdownMode.OnExplicitShutdown;
38-
var dialog = new ConnectionDialog();
39-
if (dialog.ShowDialog().GetValueOrDefault(false))
50+
var connection = await AskForConnectionAsync();
51+
if (connection != null)
4052
{
41-
var mainWindow = new MainWindow();
42-
MainWindow = mainWindow;
43-
44-
var authList = new List<Authentication>();
45-
if (dialog.Password != "")
46-
authList.Add(new PasswordAuthentication(dialog.Password));
47-
if (dialog.KeyFilePath != "")
48-
authList.Add(new KeyAuthentication(File.Open(dialog.KeyFilePath, FileMode.Open), dialog.KeyFilePassphrase.ToString()));
49-
50-
mainWindow.Connect(dialog.Connection.Stream);
51-
mainWindow.Show();
53+
MainWindow = MakeWindowForConnection(connection);
5254
ShutdownMode = ShutdownMode.OnLastWindowClose;
5355
}
5456
else
5557
Shutdown();
5658
}
59+
60+
internal async Task<Connection> AskForConnectionAsync()
61+
{
62+
using (var closedEvent = new System.Threading.ManualResetEvent(false))
63+
{
64+
var dialog = new ConnectionDialog();
65+
dialog.Closed += (sender, e) =>
66+
{
67+
closedEvent.Set();
68+
};
69+
dialog.Show();
70+
71+
await Task.Run(new Action(() => closedEvent.WaitOne()));
72+
73+
if (dialog.Ok ?? false)
74+
return dialog.Connection;
75+
else
76+
return null;
77+
}
78+
}
79+
80+
internal Window MakeWindowForConnection(Connection connection)
81+
{
82+
var window = new MainWindow();
83+
window.Connect(connection.Stream, connection.Settings);
84+
window.Show();
85+
return window;
86+
}
87+
88+
internal async Task<Connection> MakeConnectionAsync(ConnectionSettings settings, int terminalCols, int terminalRows)
89+
{
90+
using (var doneEvent = new System.Threading.ManualResetEvent(false))
91+
{
92+
var connection = new Connection();
93+
string error = null;
94+
connection.Connected += (_sender, _e) =>
95+
{
96+
Dispatcher.Invoke(() =>
97+
{
98+
doneEvent.Set();
99+
});
100+
};
101+
connection.Failed += (_sender, _e) =>
102+
{
103+
Dispatcher.Invoke(() =>
104+
{
105+
error = _e.Message;
106+
doneEvent.Set();
107+
});
108+
};
109+
110+
connection.Connect(settings, App.DefaultTerminalCols, App.DefaultTerminalRows);
111+
112+
await Task.Run(new Action(() => doneEvent.WaitOne()));
113+
if (error != null)
114+
throw new ConnectException(error);
115+
return connection;
116+
}
117+
}
57118
}
58119
}

Diff for: SSH/Connection.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ public SshClient Client
4141
public ShellStream Stream
4242
{ get; private set; }
4343

44+
public ConnectionSettings Settings
45+
{ get; private set; }
46+
4447
public event EventHandler Connected;
4548
public event EventHandler<ConnectionFailedEventArgs> Failed;
4649

4750
public Connection()
4851
{ }
4952

50-
public void Connect(string serverAddress, int serverPort, string username, IEnumerable<Authentication> authentications, int terminalCols, int terminalRows)
53+
public void Connect(ConnectionSettings settings, int terminalCols, int terminalRows)
5154
{
55+
Settings = settings;
5256
if (dataThread != null)
5357
throw new InvalidOperationException("Already connecting to a server.");
5458
dataThread = new Thread(() =>
@@ -59,18 +63,14 @@ public void Connect(string serverAddress, int serverPort, string username, IEnum
5963
#else
6064
try
6165
{
62-
ConnectionInfo connectionInfo = new ConnectionInfo(serverAddress, serverPort, username, authentications.Select(auth =>
66+
var authentications = new List<AuthenticationMethod>();
67+
if (!string.IsNullOrEmpty(settings.KeyFilePath))
6368
{
64-
if (auth is PasswordAuthentication)
65-
return new PasswordAuthenticationMethod(username, (auth as PasswordAuthentication).Password) as AuthenticationMethod;
66-
else if (auth is KeyAuthentication)
67-
{
68-
var privateKeyFile = new PrivateKeyFile((auth as KeyAuthentication).Key, (auth as KeyAuthentication).Passphrase);
69-
return new PrivateKeyAuthenticationMethod(username, privateKeyFile) as AuthenticationMethod;
70-
}
71-
else
72-
throw new NotImplementedException("Unknown type of authentication given to Connect");
73-
}).ToArray());
69+
var privateKeyFile = new PrivateKeyFile(settings.KeyFilePath, settings.KeyFilePassphrase);
70+
authentications.Add(new PrivateKeyAuthenticationMethod(settings.Username, privateKeyFile));
71+
}
72+
authentications.Add(new PasswordAuthenticationMethod(settings.Username, settings.Password));
73+
ConnectionInfo connectionInfo = new ConnectionInfo(settings.ServerAddress, settings.ServerPort, settings.Username, authentications.ToArray());
7474

7575
Client = new SshClient(connectionInfo);
7676
Client.Connect();

Diff for: SSH/ConnectionDialog.xaml.cs

+18-28
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
namespace npcook.Ssh
2424
{
25-
public struct ConnectionSettings
25+
public class ConnectionSettings
2626
{
2727
public string ServerAddress
2828
{ get; set; }
@@ -97,6 +97,9 @@ public string KeyFilePassphrase
9797
public Connection Connection
9898
{ get; private set; }
9999

100+
public bool? Ok
101+
{ get; private set; }
102+
100103
public ConnectionDialog()
101104
{
102105
DataContext = this;
@@ -157,7 +160,7 @@ protected override void OnClosed(EventArgs e)
157160
{
158161
base.OnClosed(e);
159162

160-
if (DialogResult.GetValueOrDefault(false))
163+
if (Ok ?? false)
161164
{
162165
var store = IsolatedStorageFile.GetUserStoreForAssembly();
163166
try
@@ -209,7 +212,7 @@ private void save_Click(object sender, RoutedEventArgs e)
209212

210213
private void close_Click(object sender, RoutedEventArgs e)
211214
{
212-
DialogResult = false;
215+
Ok = false;
213216
Close();
214217
}
215218

@@ -218,41 +221,28 @@ void displayError(string message, string title)
218221
MessageBox.Show(this, message, title, MessageBoxButton.OK, MessageBoxImage.Error);
219222
}
220223

221-
void connect_Click(object sender, RoutedEventArgs e)
224+
async void connect_Click(object sender, RoutedEventArgs e)
222225
{
223226
IsEnabled = false;
224227

225-
Connection = new Connection();
226-
Connection.Connected += (_sender, _e) =>
228+
try
227229
{
228-
Dispatcher.Invoke(() =>
229-
{
230-
DialogResult = true;
231-
Close();
232-
});
233-
};
234-
Connection.Failed += (_sender, _e) =>
230+
Connection = await App.Current.MakeConnectionAsync(SelectedSettings, App.DefaultTerminalCols, App.DefaultTerminalRows);
231+
Ok = true;
232+
Close();
233+
}
234+
catch (ConnectException ex)
235235
{
236-
Dispatcher.Invoke(() =>
237-
{
238-
Connection = null;
239-
IsEnabled = true;
240-
displayError(_e.Message, "Could not connect");
241-
});
242-
};
243-
244-
var authList = new List<Authentication>();
245-
if (KeyFilePath != "")
246-
authList.Add(new KeyAuthentication(File.Open(KeyFilePath, FileMode.Open), KeyFilePassphrase));
247-
authList.Add(new PasswordAuthentication(Password));
248-
Connection.Connect(ServerAddress, ServerPort, Username, authList, App.DefaultTerminalCols, App.DefaultTerminalRows);
236+
IsEnabled = true;
237+
displayError(ex.Message, "Could not connect");
238+
}
249239
}
250240

251241
private void settingsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
252242
{
253243
if (e.AddedItems.Count > 0)
254244
{
255-
var settings = (e.AddedItems[0] as ConnectionSettings?).Value; // Let an exception happen if the items are not ConnectionSettings
245+
var settings = e.AddedItems[0] as ConnectionSettings; // Let an exception happen if the items are not ConnectionSettings
256246
serverAddress.Text = settings.ServerAddress;
257247
serverPort.Text = settings.ServerPort.ToString();
258248
username.Text = settings.Username;
@@ -275,7 +265,7 @@ private void settingsList_SelectionChanged(object sender, SelectionChangedEventA
275265

276266
private void settingsListItem_Delete(object sender, RoutedEventArgs e)
277267
{
278-
SavedSettings.Remove(((sender as MenuItem).Tag as ConnectionSettings?).Value);
268+
SavedSettings.Remove((sender as MenuItem).Tag as ConnectionSettings);
279269
}
280270
}
281271
}

Diff for: SSH/MainWindow.xaml

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:local="clr-namespace:npcook.Ssh"
77
xmlns:term="clr-namespace:npcook.Terminal.Controls;assembly=npcook.Terminal.Controls"
88
mc:Ignorable="d"
9-
Title="MainWindow"
9+
Title="{Binding Title}"
1010
FocusManager.FocusedElement="{Binding ElementName=terminalControl}"
1111
GotKeyboardFocus="OnKeyboardFocus" Height="309" Width="532">
1212
<Grid x:Name="root">
@@ -17,9 +17,19 @@
1717
<RowDefinition Height="Auto"/>
1818
<RowDefinition Height="*"/>
1919
</Grid.RowDefinitions>
20-
<ToolBar x:Name="toolBar" Grid.Row="0">
21-
<Button Content="Connect" Height="22" VerticalAlignment="Top" Width="75" Click="connect_click"/>
22-
</ToolBar>
20+
<Menu x:Name="menu" Grid.Row="0">
21+
<MenuItem Header="_File">
22+
<MenuItem Header="_New Session..." Command="{Binding NewSessionCommand}"/>
23+
<MenuItem Header="_Reopen Session" Command="{Binding ReopenSessionCommand}"/>
24+
<Separator/>
25+
<MenuItem Header="_Transfer Files" Command="{Binding TransferFilesCommand}"/>
26+
<Separator/>
27+
<MenuItem Header="E_xit" Command="{Binding ExitCommand}"/>
28+
</MenuItem>
29+
<MenuItem Header="_Tools">
30+
<MenuItem Header="_Options" Command="{Binding OptionsCommand}"/>
31+
</MenuItem>
32+
</Menu>
2333
<term:TerminalControl x:Name="terminalControl" FontFamily="Consolas" Focusable="True" FocusVisualStyle="{x:Null}"
2434
FontSize="13" Background="Black" Grid.Row="1" Width="Auto" Height="Auto"
2535
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SnapsToDevicePixels="True">

0 commit comments

Comments
 (0)