/// <summary> /// Disconnect and close ktlstunnel. /// </summary> public void Terminate() { Disconnect(); if (TunnelProcess != null) { TunnelProcess.Terminate(); TunnelProcess = null; } }
/// <summary> /// Clean up the KMOD process and socket. /// </summary> private void CleanUpKmod() { if (m_kmodProc != null) { m_kmodProc.Terminate(); m_kmodProc = null; } if (m_kmodSock != null) { m_kmodSock.Close(); m_kmodSock = null; } }
/// <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(Misc.GetKtlstunnelLogFilePath())) { Directory.CreateDirectory(Misc.GetKtlstunnelLogFilePath()); } // Start ktlstunnel as such. // ktlstunnel localhost ((IPEndPoint)Listener.LocalEndPoint).Port Host Port [-r host:port] String loggingPath = "-L " + "\"" + Misc.GetKtlstunnelLogFilePath() + "ktlstunnel-" + Base.GetLogFileName() + "\" "; String loggingLevel = ""; #if false if (Misc.ApplicationSettings.ktlstunnelLoggingLevel == 1) { loggingLevel = "-l minimal "; loggingLevel += loggingPath; } else if (Misc.ApplicationSettings.ktlstunnelLoggingLevel == 2) { loggingLevel = "-l debug "; loggingLevel += loggingPath; } #endif loggingLevel = "-l minimal "; loggingLevel += loggingPath; string startupLine = "\"" + Base.GetKwmInstallationPath() + @"ktlstunnel\ktlstunnel.exe" + "\" " + loggingLevel + "localhost " + ((IPEndPoint)Sock.LocalEndPoint).Port.ToString() + " " + Host + " " + Port + " " + extraParams; Logging.Log("Starting ktlstunnel.exe : " + startupLine); TunnelProcess = new RawProcess(startupLine); TunnelProcess.InheritHandles = false; TunnelProcess.CreationFlags = (uint)Syscalls.CREATION_FLAGS.CREATE_NO_WINDOW; TunnelProcess.Start(); }
/// <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 = Base.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 = Misc.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 = Misc.ApplicationSettings.ktlstunnelLoggingLevel > 0 ? " -l 3" : ""; String debug = "-l 3"; String args = " -C kmod_connect -p " + port + debug + " -m 20000 -k \"" + KmodDir + "\""; String cmdLine = Kmod + args; Logging.Log("About to start kmod.exe: " + cmdLine); m_kmodProc = new RawProcess(cmdLine); m_kmodProc.InheritHandles = false; m_kmodProc.CreationFlags = (uint)Syscalls.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 = Base.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 (!Base.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(); } } }