private static void LoadSshOptionsFromUri(SshConnectionInfoViewModel vm, string optsString) { vm.SshOptions.Clear(); if (string.IsNullOrEmpty(optsString)) { return; } foreach (SshOptionViewModel option in ParseSshOptionsFromUri(optsString, ',')) { if (option.Name.Equals(IdentityFileOptionName, StringComparison.OrdinalIgnoreCase)) { vm.IdentityFile = option.Value; } else if (vm.SshOptions.Any(opt => string.Equals(opt.Name, option.Name, StringComparison.OrdinalIgnoreCase))) { throw new FormatException($"SSH option '{option.Name}' is defined more than once."); } else { vm.SshOptions.Add(option); } } }
private async void SshInfoDialog_OnPrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) { SshConnectionInfoViewModel vm = (SshConnectionInfoViewModel)DataContext; if (string.IsNullOrEmpty(vm.Username) || string.IsNullOrEmpty(vm.Host)) { args.Cancel = true; await new MessageDialog(I18N.Translate("UserAndHostMandatory"), I18N.Translate("InvalidInput")).ShowAsync(); SetupFocus(); return; } if (vm.SshPort == 0) { args.Cancel = true; await new MessageDialog(I18N.Translate("PortCannotBeZero"), I18N.Translate("InvalidInput")).ShowAsync(); SetupFocus(); return; } args.Cancel = false; }
private static string GetArgumentsString(SshConnectionInfoViewModel sshConnectionInfo) { var sb = new StringBuilder(); if (sshConnectionInfo.SshPort != SshConnectionInfoViewModel.DefaultSshPort) { sb.Append($"-p {sshConnectionInfo.SshPort:#####} "); } if (!string.IsNullOrEmpty(sshConnectionInfo.IdentityFile)) { sb.Append($"-i \"{sshConnectionInfo.IdentityFile}\" "); } foreach (SshOptionViewModel option in sshConnectionInfo.SshOptions) { sb.Append($"-o \"{option.Name}={option.Value}\" "); } sb.Append($"{sshConnectionInfo.Username}@{sshConnectionInfo.Host}"); if (sshConnectionInfo.UseMosh) { sb.Append($" {sshConnectionInfo.MoshPortFrom}:{sshConnectionInfo.MoshPortTo}"); } return(sb.ToString()); }
private static ShellProfile GetShellProfile(SshConnectionInfoViewModel sshConnectionInfo) => new ShellProfile { Arguments = GetArgumentsString(sshConnectionInfo), Location = sshConnectionInfo.UseMosh ? MoshExe : SshExeLocationLazy.Value, WorkingDirectory = string.Empty, LineEndingTranslation = sshConnectionInfo.LineEndingStyle };
private async void OnLoading(FrameworkElement sender, object args) { SshConnectionInfoViewModel vm = (SshConnectionInfoViewModel)DataContext; if (!string.IsNullOrEmpty(vm.Username)) { return; } vm.Username = await _trayProcessCommunicationService.GetUserName(); SetupFocus(); }
private void SetupFocus() { SshConnectionInfoViewModel vm = (SshConnectionInfoViewModel)DataContext; if (string.IsNullOrEmpty(vm.Username)) { UserTextBox.Focus(FocusState.Programmatic); } else if (string.IsNullOrEmpty(vm.Host)) { HostTextBox.Focus(FocusState.Programmatic); } else { Focus(FocusState.Programmatic); } }
private async void SaveLink_OnClick(object sender, RoutedEventArgs e) { SshConnectionInfoViewModel vm = (SshConnectionInfoViewModel)DataContext; var validationResult = vm.Validate(true); if (validationResult != SshConnectionInfoValidationResult.Valid) { await new MessageDialog(I18N.Translate($"{nameof(SshConnectionInfoValidationResult)}.{validationResult}"), I18N.Translate("InvalidInput")).ShowAsync(); return; } var content = string.Format(ShortcutFileFormat, _sshHelperService.ConvertToUri(vm)); var fileName = string.IsNullOrEmpty(vm.Username) ? $"{vm.Host}.url" : $"{vm.Username}@{vm.Host}.url"; var savePicker = new FileSavePicker { SuggestedFileName = fileName, SuggestedStartLocation = PickerLocationId.Desktop }; savePicker.FileTypeChoices.Add("Shortcut", new List <string> { ".url" }); StorageFile file = await savePicker.PickSaveFileAsync(); if (file == null) { return; } try { await _trayProcessCommunicationService.SaveTextFileAsync(file.Path, content); } catch (Exception ex) { await new MessageDialog(ex.Message, I18N.Translate("Error")).ShowAsync(); } }
private static SshConnectionInfoViewModel ParseSsh(Uri uri) { SshConnectionInfoViewModel vm = new SshConnectionInfoViewModel { Host = uri.Host, UseMosh = MoshUriScheme.Equals(uri.Scheme, StringComparison.OrdinalIgnoreCase) }; if (uri.Port >= 0) { vm.SshPort = (ushort)uri.Port; } if (!string.IsNullOrEmpty(uri.UserInfo)) { string[] parts = uri.UserInfo.Split(';'); if (parts.Length > 2) { throw new FormatException($"UserInfo part contains {parts.Length} elements."); } vm.Username = HttpUtility.UrlDecode(parts[0]); if (parts.Length > 1) { LoadSshOptionsFromUri(vm, parts[1]); } } if (string.IsNullOrEmpty(uri.Query)) { return(vm); } if (!vm.UseMosh) { throw new FormatException("Query parameters are not supported in SSH links."); } string queryString = uri.Query; if (queryString.StartsWith("?", StringComparison.Ordinal)) { queryString = queryString.Substring(1); } if (string.IsNullOrEmpty(queryString)) { return(vm); } foreach (SshOptionViewModel option in ParseSshOptionsFromUri(queryString, '&')) { if (ValidMoshPortsNames.Any(n => n.Equals(option.Name, StringComparison.OrdinalIgnoreCase))) { Match match = MoshRangeRx.Match(option.Value); if (!match.Success) { throw new FormatException($"Invalid mosh ports range '{option.Value}'."); } vm.MoshPortFrom = ushort.Parse(match.Groups["from"].Value); vm.MoshPortTo = ushort.Parse(match.Groups["to"].Value); } else { throw new FormatException($"Unknown query parameter '{option.Name}'."); } } return(vm); }