/// <summary> /// Check if db is existed and writable /// </summary> /// <returns> /// Tuple(is decrypted, error info) /// </returns> private static Tuple <bool, string> CheckIfDbIsWritable() { var path = SystemConfig.Instance.DataSecurity.DbPath; try { var fi = new FileInfo(SystemConfig.Instance.DataSecurity.DbPath); if (!Directory.Exists(fi.DirectoryName)) { Directory.CreateDirectory(fi.DirectoryName); } if (IOPermissionHelper.HasWritePermissionOnFile(path)) { Server.Init(); return(new Tuple <bool, string>(true, "")); } else { return(new Tuple <bool, string>(false, "TXT:db permission denied:" + " " + path)); } } catch (Exception e) { SimpleLogHelper.Error(e); SimpleLogHelper.Error(e.StackTrace); return(new Tuple <bool, string>(false, e.Message)); } }
public static void SetExtendedProperty(this AxHost axHost, string propertyName, object value) { try { ((IMsRdpExtendedSettings)axHost.GetOcx()).set_Property(propertyName, ref value); } catch (Exception ex) { SimpleLogHelper.Error(ex, ex.StackTrace); } }
public void ClosePutty() { DelPuttySessionInRegTable(); try { if (_puttyProcess?.HasExited == false) { _puttyProcess?.Kill(); } _puttyProcess = null; } catch (Exception e) { SimpleLogHelper.Error(e); } }
public void ShowRemoteHost(uint serverId) { Debug.Assert(serverId > 0); Debug.Assert(GlobalData.Instance.ServerList.Any(x => x.Id == serverId)); var server = GlobalData.Instance.ServerList.First(x => x.Id == serverId); // update last conn time server.LastConnTime = DateTime.Now; Server.AddOrUpdate(server); // is connected now! activate it then return. if (server.OnlyOneInstance && _protocolHosts.ContainsKey(serverId.ToString())) { if (_protocolHosts[serverId.ToString()].ParentWindow is TabWindow t) { var s = t.Vm?.Items?.First(x => x.Content?.ProtocolServer?.Id == serverId); if (s != null) { t.Vm.SelectedItem = s; } t.Activate(); } return; } // create new remote session TabWindow tab = null; try { if (server.IsConnWithFullScreen()) { // for those people using 2+ monitors in different scale factors, we will try "mstsc.exe" instead of "PRemoteM". if (Screen.AllScreens.Length > 1 && server is ProtocolServerRDP rdp && rdp.RdpFullScreenFlag == ERdpFullScreenFlag.EnableFullAllScreens) { int factor = (int)(new ScreenInfoEx(Screen.PrimaryScreen).ScaleFactor * 100); // check if screens are in different scale factors bool differentScaleFactorFlag = Screen.AllScreens.Select(screen => (int)(new ScreenInfoEx(screen).ScaleFactor * 100)).Any(factor2 => factor != factor2); if (differentScaleFactorFlag || true) { var tmp = Path.GetTempPath(); var rdpFileName = $"{rdp.DispName}_{rdp.Port}_{rdp.UserName}"; var invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); rdpFileName = invalid.Aggregate(rdpFileName, (current, c) => current.Replace(c.ToString(), "")); var rdpFile = Path.Combine(tmp, rdpFileName + ".rdp"); try { File.WriteAllText(rdpFile, rdp.ToRdpConfig().ToString()); var p = new Process { StartInfo = { FileName = "cmd.exe", UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; p.Start(); p.StandardInput.WriteLine("mstsc -admin \"" + rdpFile + "\""); p.StandardInput.WriteLine("exit"); } finally { // delete tmp rdp file, ETA 10s var t = new Task(() => { Thread.Sleep(1000 * 10); if (File.Exists(rdpFile)) { File.Delete(rdpFile); } }); t.Start(); } return; } } var host = ProtocolHostFactory.Get(server); host.OnClosed += OnProtocolClose; host.OnFullScreen2Window += OnFullScreen2Window; AddProtocolHost(host); MoveProtocolHostToFullScreen(host.ConnectionId); host.Conn(); SimpleLogHelper.Debug($@"Start Conn: {server.DispName}({server.GetHashCode()}) by host({host.GetHashCode()}) with full"); } else { switch (SystemConfig.Instance.General.TabMode) { case EnumTabMode.NewItemGoesToGroup: // work in tab by group mode if (_tabWindows.Any(x => x.Value.Vm.Tag == server.GroupName)) { tab = _tabWindows.First(x => x.Value.Vm.Tag == server.GroupName).Value; } break; case EnumTabMode.NewItemGoesToProtocol: // work in tab by protocol mode if (_tabWindows.Any(x => x.Value.Vm.Tag == server.ProtocolDisplayName)) { tab = _tabWindows.First(x => x.Value.Vm.Tag == server.ProtocolDisplayName).Value; } break; default: // work in tab by latest tab mode if (!string.IsNullOrEmpty(_lastTabToken) && _tabWindows.ContainsKey(_lastTabToken)) { tab = _tabWindows[_lastTabToken]; } break; } if (tab == null) { var token = DateTime.Now.Ticks.ToString(); AddTab(new TabWindow(token)); tab = _tabWindows[token]; tab.Show(); _lastTabToken = token; if (SystemConfig.Instance.General.TabMode == EnumTabMode.NewItemGoesToGroup) { tab.Vm.Tag = server.GroupName; } else if (SystemConfig.Instance.General.TabMode == EnumTabMode.NewItemGoesToProtocol) { tab.Vm.Tag = server.ProtocolDisplayName; } } tab.Activate(); var size = tab.GetTabContentSize(); var host = ProtocolHostFactory.Get(server, size.Width, size.Height); host.OnClosed += OnProtocolClose; host.OnFullScreen2Window += OnFullScreen2Window; host.ParentWindow = tab; tab.Vm.Items.Add(new TabItemViewModel() { Content = host, Header = server.DispName, }); tab.Vm.SelectedItem = tab.Vm.Items.Last(); host.Conn(); _protocolHosts.Add(host.ConnectionId, host); SimpleLogHelper.Debug($@"Start Conn: {server.DispName}({server.GetHashCode()}) by host({host.GetHashCode()}) with Tab({tab.GetHashCode()})"); SimpleLogHelper.Debug($@"ProtocolHosts.Count = {_protocolHosts.Count}, FullWin.Count = {_host2FullScreenWindows.Count}, _tabWindows.Count = {_tabWindows.Count}"); } } catch (Exception e) { CloseEmpytTab(); SimpleLogHelper.Error(e); MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
/// <summary> /// Check if db is(can) decrypted by the private key /// </summary> /// <returns> /// Tuple(is decrypted, error info) /// </returns> public Tuple <bool, string> CheckIfDbIsOk(string privateKeyPath = "") { var c1 = CheckIfDbIsWritable(); if (!c1.Item1) { return(c1); } var _privateKeyPath = DB.Config.RSA_PrivateKeyPath; if (!string.IsNullOrEmpty(privateKeyPath)) { _privateKeyPath = privateKeyPath; } // NO RSA if (string.IsNullOrEmpty(DB.Config.RSA_PublicKey) && string.IsNullOrEmpty(_privateKeyPath) && string.IsNullOrEmpty(DB.Config.RSA_SHA1)) { return(new Tuple <bool, string>(true, "")); } if (!File.Exists(_privateKeyPath)) { return(new Tuple <bool, string>(false, SystemConfig.Instance.Language.GetText("system_options_data_security_error_rsa_private_key_not_found"))); } RSA rsaPk = null; RSA rsaPpk = null; try { rsaPpk = new RSA(File.ReadAllText(_privateKeyPath), true); } catch (Exception) { return(new Tuple <bool, string>(false, SystemConfig.Instance.Language.GetText("system_options_data_security_error_rsa_private_key_not_match"))); } // make sure public key is PEM format key try { rsaPk = new RSA(DB.Config.RSA_PublicKey, true); } catch (Exception) { // try to fix public key if (rsaPpk.Verify("SHA1", DB.Config.RSA_SHA1, SystemConfig.AppName)) { DB.Config.RSA_PublicKey = rsaPpk.ToPEM_PKCS1(true); rsaPk = new RSA(File.ReadAllText(_privateKeyPath), true); } } // RSA private key is match public key? try { rsaPpk = new RSA(File.ReadAllText(_privateKeyPath), true); var sha1 = rsaPpk.Sign("SHA1", SystemConfig.AppName); if (!rsaPk.Verify("SHA1", sha1, SystemConfig.AppName)) { throw new Exception("RSA key is not match!"); } DB.Config.RSA_SHA1 = sha1; } catch (Exception e) { SimpleLogHelper.Error(e); return(new Tuple <bool, string>(false, SystemConfig.Instance.Language.GetText("system_options_data_security_error_rsa_private_key_not_match"))); } return(new Tuple <bool, string>(true, "")); }
public void ShowRemoteHost(uint id) { Debug.Assert(id > 0); Debug.Assert(GlobalData.Instance.ServerList.Any(x => x.Id == id)); var server = GlobalData.Instance.ServerList.First(x => x.Id == id); // update last conn time server.LastConnTime = DateTime.Now; Server.AddOrUpdate(server); // start conn if (server.OnlyOneInstance && _protocolHosts.ContainsKey(id.ToString())) { _protocolHosts[id.ToString()].ParentWindow?.Activate(); return; } TabWindow tab = null; try { if (server.IsConnWithFullScreen()) { // for those people using 2+ monitor which are in different scale factors, we will try "mstsc.exe" instead of "PRemoteM". if (Screen.AllScreens.Length > 1 && server is ProtocolServerRDP rdp && rdp.RdpFullScreenFlag == ERdpFullScreenFlag.EnableFullAllScreens) { int factor = (int)(new ScreenInfoEx(Screen.PrimaryScreen).ScaleFactor * 100); // check if screens are in different scale factors bool differentScaleFactorFlag = Screen.AllScreens.Select(screen => (int)(new ScreenInfoEx(screen).ScaleFactor * 100)).Any(factor2 => factor != factor2); if (differentScaleFactorFlag) { var tmp = Path.GetTempPath(); var dp = rdp.DispName; var invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); dp = invalid.Aggregate(dp, (current, c) => current.Replace(c.ToString(), "")); var rdpFile = Path.Combine(tmp, dp + ".rdp"); try { File.WriteAllText(rdpFile, rdp.ToRdpConfig().ToString()); var p = new Process { StartInfo = { FileName = "cmd.exe", UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; p.Start(); p.StandardInput.WriteLine("mstsc -admin " + rdpFile); p.StandardInput.WriteLine("exit"); } catch (Exception) { throw; } finally { var t = new Task(() => { Thread.Sleep(1000 * 10); if (File.Exists(rdpFile)) { File.Delete(rdpFile); } }); t.Start(); } return; } } var host = ProtocolHostFactory.Get(server); host.OnClosed += OnProtocolClose; host.OnFullScreen2Window += OnFullScreen2Window; AddProtocolHost(host); MoveProtocolHostToFullScreen(host.ConnectionId); host.Conn(); SimpleLogHelper.Debug($@"Start Conn: {server.DispName}({server.GetHashCode()}) by host({host.GetHashCode()}) with full"); } else { if (!string.IsNullOrEmpty(_lastTabToken) && _tabWindows.ContainsKey(_lastTabToken)) { tab = _tabWindows[_lastTabToken]; } else { var token = DateTime.Now.Ticks.ToString(); AddTab(new TabWindow(token)); tab = _tabWindows[token]; tab.Show(); _lastTabToken = token; } tab.Activate(); var size = tab.GetTabContentSize(); var host = ProtocolHostFactory.Get(server, size.Width, size.Height); host.OnClosed += OnProtocolClose; host.OnFullScreen2Window += OnFullScreen2Window; host.ParentWindow = tab; tab.Vm.Items.Add(new TabItemViewModel() { Content = host, Header = server.DispName, }); tab.Vm.SelectedItem = tab.Vm.Items.Last(); host.Conn(); _protocolHosts.Add(host.ConnectionId, host); SimpleLogHelper.Debug($@"Start Conn: {server.DispName}({server.GetHashCode()}) by host({host.GetHashCode()}) with Tab({tab.GetHashCode()})"); SimpleLogHelper.Debug($@"ProtocolHosts.Count = {_protocolHosts.Count}, FullWin.Count = {_host2FullScreenWindows.Count}, _tabWindows.Count = {_tabWindows.Count}"); } } catch (Exception e) { if (tab?.Vm != null && (tab.Vm?.Items?.Count ?? 0) == 0) { CloseTabWindow(tab.Vm.Token); } SimpleLogHelper.Error(e); MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }