示例#1
0
文件: Program.cs 项目: tmbx/kwm-ng
        /// <summary>
        /// Backup the database, if it exists.
        /// </summary>
        private static void BackupDb()
        {
            String srcPath = KwmPath.GetKwmDbPath();
            String dstPath = srcPath + ".backup";

            if (File.Exists(srcPath))
            {
                File.Copy(srcPath, dstPath, true);
            }
        }
示例#2
0
        /// <summary>
        /// Create a listening socket and spawn ktlstunnel process.
        /// </summary>
        public void BeginTls(string extraParams)
        {
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, 0);

            Sock = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            Sock.Bind(endPoint);
            Sock.Listen(1);

            // Create a logging dir for ktlstunnel, if it does not exist.
            if (!Directory.Exists(KwmPath.GetKtlstunnelLogFilePath()))
            {
                Directory.CreateDirectory(KwmPath.GetKtlstunnelLogFilePath());
            }

            // Start ktlstunnel as such.
            // ktlstunnel localhost ((IPEndPoint)Listener.LocalEndPoint).Port Host Port [-r host:port]
            String loggingPath  = "-L " + "\"" + KwmPath.GetKtlstunnelLogFilePath() + "ktlstunnel-" + KwmPath.GetLogFileName() + "\" ";
            String loggingLevel = "";

            if (KwmCfg.Cur.KtlstunnelLoggingLevel == 1)
            {
                loggingLevel  = "-l minimal ";
                loggingLevel += loggingPath;
            }
            else if (KwmCfg.Cur.KtlstunnelLoggingLevel == 2)
            {
                loggingLevel  = "-l debug ";
                loggingLevel += loggingPath;
            }

            String startupLine = "\"" + KwmPath.KwmKtlstunnelPath + "\" " +
                                 loggingLevel +
                                 "localhost " + ((IPEndPoint)Sock.LocalEndPoint).Port.ToString() + " " +
                                 Host + " " + Port + " " + extraParams;

            KLogging.Log("Starting ktlstunnel.exe : " + startupLine);

            TunnelProcess = new KProcess(startupLine);
            TunnelProcess.InheritHandles = false;
            TunnelProcess.CreationFlags  = (uint)KSyscalls.CREATION_FLAGS.CREATE_NO_WINDOW;
            TunnelProcess.Start();
        }
示例#3
0
文件: Program.cs 项目: tmbx/kwm-ng
        /// <summary>
        /// Enter the main mode of the KWM. Return true if the application
        /// must continue.
        /// </summary>
        private static bool EnterMainMode()
        {
            // Perform early linking.
            Wm.LocalDbBroker.Relink(Wm.LocalDb);

            // FIXME.
#if true
            // Open a temporary console.
            ConsoleWindow foo = new ConsoleWindow();
            foo.Show();
            foo.OnConsoleClosing += delegate(Object sender, EventArgs args)
            {
                WmSm.RequestStop();
            };

            // Create an empty database.
            Wm.LocalDb.DeleteDb();
            Wm.LocalDb.OpenOrCreateDb(KwmPath.GetKwmDbPath());
            Wm.LocalDbBroker.InitDb();
            WmDeserializer ds = new WmDeserializer();
            ds.Deserialize();
            Debug.Assert(ds.Ex == null);
#else
            // Open or create the database.
            Wm.LocalDb.OpenOrCreateDb(KwmPath.GetKwmDbPath());
            Wm.LocalDbBroker.InitDb();

            // Try to deserialize.
            WmDeserializer ds = new WmDeserializer();
            ds.Deserialize();

            // The deserialization failed.
            if (ds.Ex != null)
            {
                // If the user doesn't want to recover, bail out.
                if (!TellUserAboutDsrFailure())
                {
                    return(false);
                }

                // Backup, delete and recreate a database.
                BackupDb();
                Wm.LocalDb.DeleteDb();
                Wm.LocalDb.OpenOrCreateDb(KwmPath.GetKwmDbPath());
                Wm.LocalDbBroker.InitDb();

                // Retry to deserialize.
                ds = new WmDeserializer();
                ds.Deserialize();
                if (ds.Ex != null)
                {
                    throw ds.Ex;
                }

                // Set the next internal workspace ID to a high value based on
                // the date to avoid conflicts with old KFS directories.
                ds.WmCd.NextKwsInternalID = (UInt64)(DateTime.Now - new DateTime(2010, 1, 1)).TotalSeconds;
            }
#endif

            // Relink the workspace manager object graphs.
            Wm.Relink(ds);

            // Open the lingering database transaction.
            Wm.LocalDb.BeginTransaction();

            // Serialize the WM state that has changed.
            Wm.Serialize();

            // Export the workspaces, then exit.
            if (ExportKwsPath != "")
            {
                WmKwsImportExport.ExportKws(ExportKwsPath, 0);
                return(false);
            }

            // Set the handle to the message window.
            SetKwmHandle(MsgWindow.Handle);

            // Pass the hand to the WM state machine.
            WmSm.RequestStart();

            return(true);
        }
示例#4
0
文件: Vnc.cs 项目: tmbx/kwm-ng
        /// <summary>
        /// Start the process specified.
        /// </summary>
        private void StartProcess(bool mainFlag)
        {
            // Kill any lingering process.
            if (mainFlag)
            {
                String lingeringName = ServerSessionFlag ? "kappserver" : "kappviewer";
                foreach (Process l in Process.GetProcessesByName(lingeringName))
                {
                    l.Kill();
                }
            }

            // Get the executable path and its arguments.
            String path = "\"" + KwmPath.GetKwmInstallationPath() + @"vnc\";

            path += ServerSessionFlag ? "kappserver.exe" : "kappviewer.exe";
            path += "\"";

            String args = "";

            if (ServerSessionFlag && !mainFlag)
            {
                if (WindowHandle == 0)
                {
                    args = " -shareall";
                }
                else
                {
                    args = " -sharehwnd " + WindowHandle;
                }
            }

            else if (!ServerSessionFlag)
            {
                args = " /shared /notoolbar /disableclipboard /encoding tight /compresslevel 9 localhost";
            }

            // Start the process.
            KProcess p = new KProcess(path + args);

            if (mainFlag)
            {
                MainProcess = p;
            }
            else
            {
                DummyProcess = p;
            }
            p.CreationFlags  = (uint)KSyscalls.CREATION_FLAGS.CREATE_NO_WINDOW;
            p.InheritHandles = false;
            KSyscalls.STARTUPINFO si = new KSyscalls.STARTUPINFO();
            // I don't know why this is set.
            if (ServerSessionFlag)
            {
                si.dwFlags = 1;
            }
            p.StartupInfo = si;
            p.ProcessEnd += OnProcessEnd;
            p.Start();

            // Poll the process until it starts.
            NbTimerEvent = 0;
            Timer.TimerWakeUpCallback = OnProcessPollEvent;
            Timer.Args = new object[] { p };
            Timer.WakeMeUp(0);
        }
示例#5
0
文件: KmodThread.cs 项目: tmbx/kwm-ng
        /// <summary>
        /// Start kmod and connect to it.
        /// </summary>
        private void StartKmod()
        {
            FileStream  file       = null;
            Socket      listenSock = null;
            RegistryKey kwmRegKey  = null;

            try
            {
                // Get the path to the kmod executable in the registry.
                kwmRegKey = KwmReg.GetKwmLMRegKey();
                String Kmod = "\"" + (String)kwmRegKey.GetValue("InstallDir", @"C:\Program Files\Teambox\Teambox Manager") + "\\kmod\\kmod.exe\"";

                // The directory where KMOD will save logs and its database for use with the kwm.
                String KmodDir = KwmPath.GetKmodDirPath();
                Directory.CreateDirectory(KmodDir);

                // Start listening for kmod to connect when it'll be started.
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, 0);
                listenSock = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                listenSock.Bind(endPoint);
                listenSock.Listen(1);
                int port = ((IPEndPoint)listenSock.LocalEndPoint).Port;

                // Start KMOD in debugging mode if our settings say so.
                String debug   = KwmCfg.Cur.KtlstunnelLoggingLevel > 0 ? " -l 3" : "";
                String args    = " -C kmod_connect -p " + port + debug + " -m 20000 -k \"" + KmodDir + "\"";
                String cmdLine = Kmod + args;
                KLogging.Log("About to start kmod.exe: " + cmdLine);
                m_kmodProc = new KProcess(cmdLine);
                m_kmodProc.InheritHandles = false;
                m_kmodProc.CreationFlags  = (uint)KSyscalls.CREATION_FLAGS.CREATE_NO_WINDOW;
                m_kmodProc.Start();

                // Wait for KMOD to connect for about 10 seconds.
                DateTime startTime = DateTime.Now;
                while (true)
                {
                    SelectSockets select = new SelectSockets();
                    select.AddRead(listenSock);
                    SetSelectTimeout(startTime, 10000, select, "no KMOD connection received");
                    Block(select);

                    if (select.InRead(listenSock))
                    {
                        m_kmodSock          = listenSock.Accept();
                        m_kmodSock.Blocking = false;
                        break;
                    }
                }

                // Read the authentication data.
                byte[] authSockData = new byte[32];
                byte[] authFileData = new byte[32];

                startTime = DateTime.Now;
                int nbRead = 0;
                while (nbRead != 32)
                {
                    SelectSockets select = new SelectSockets();
                    select.AddRead(m_kmodSock);
                    SetSelectTimeout(startTime, 2000, select, "no authentication data received");
                    Block(select);
                    int r = KSocket.SockRead(m_kmodSock, authSockData, nbRead, 32 - nbRead);
                    if (r > 0)
                    {
                        nbRead += r;
                    }
                }

                file = File.Open(KmodDir + "\\connect_secret", FileMode.Open);
                file.Read(authFileData, 0, 32);
                if (!KUtil.ByteArrayEqual(authFileData, authSockData))
                {
                    throw new Exception("invalid authentication data received");
                }

                // Set the transport.
                m_transport = new K3pTransport(m_kmodSock);
            }

            finally
            {
                if (file != null)
                {
                    file.Close();
                }
                if (listenSock != null)
                {
                    listenSock.Close();
                }
                if (kwmRegKey != null)
                {
                    kwmRegKey.Close();
                }
            }
        }