private void RemoteTelnetSession(ClientRequestInfo requestinfo)
{
if ((object)m_remoteCommandProcess == null && requestinfo.Request.Arguments.ContainsHelpRequest)
{
StringBuilder helpMessage = new StringBuilder();
helpMessage.Append("Allows for a telnet session to the service server.");
helpMessage.AppendLine();
helpMessage.AppendLine();
helpMessage.Append(" Usage:");
helpMessage.AppendLine();
helpMessage.Append(" Telnet -options");
helpMessage.AppendLine();
helpMessage.AppendLine();
helpMessage.Append(" Options:");
helpMessage.AppendLine();
helpMessage.Append(" -?".PadRight(20));
helpMessage.Append("Displays this help message");
helpMessage.AppendLine();
helpMessage.Append(" -connect".PadRight(20));
helpMessage.Append("Establishes a telnet session (requires password)");
helpMessage.AppendLine();
helpMessage.Append(" -disconnect".PadRight(20));
helpMessage.Append("Terminates established telnet session");
helpMessage.AppendLine();
helpMessage.AppendLine();
UpdateStatus(requestinfo.Sender.ClientID, UpdateType.Information, helpMessage.ToString());
}
else
{
bool connectSession = requestinfo.Request.Arguments.Exists("connect");
bool disconnectSession = requestinfo.Request.Arguments.Exists("disconnect");
if ((object)m_remoteCommandProcess == null && connectSession && !string.IsNullOrEmpty(requestinfo.Request.Arguments["connect"]))
{
// User wants to establish a remote command session.
string password = requestinfo.Request.Arguments["connect"];
if (password == m_telnetSessionPassword)
{
// Establish remote command session
m_remoteCommandProcess = new Process();
m_remoteCommandProcess.ErrorDataReceived += RemoteCommandProcess_ErrorDataReceived;
m_remoteCommandProcess.OutputDataReceived += RemoteCommandProcess_OutputDataReceived;
m_remoteCommandProcess.StartInfo.FileName = "cmd.exe";
m_remoteCommandProcess.StartInfo.UseShellExecute = false;
m_remoteCommandProcess.StartInfo.RedirectStandardInput = true;
m_remoteCommandProcess.StartInfo.RedirectStandardOutput = true;
m_remoteCommandProcess.StartInfo.RedirectStandardError = true;
m_remoteCommandProcess.Start();
m_remoteCommandProcess.BeginOutputReadLine();
m_remoteCommandProcess.BeginErrorReadLine();
UpdateStatus(UpdateType.Information, "Remote command session established - status updates are suspended.\r\n\r\n");
m_remoteCommandClientID = requestinfo.Sender.ClientID;
SendResponse(requestinfo.Sender.ClientID, new ServiceResponse("TelnetSession", "Established"));
}
else
{
UpdateStatus(requestinfo.Sender.ClientID, UpdateType.Alarm, "Failed to establish remote command session - Password is invalid.\r\n\r\n");
}
}
else if (string.Compare(requestinfo.Request.Command, "Telnet", StringComparison.OrdinalIgnoreCase) == 0 && (object)m_remoteCommandProcess != null && disconnectSession)
{
// User wants to terminate an established remote command session.
m_remoteCommandProcess.ErrorDataReceived -= RemoteCommandProcess_ErrorDataReceived;
m_remoteCommandProcess.OutputDataReceived -= RemoteCommandProcess_OutputDataReceived;
if (!m_remoteCommandProcess.HasExited)
m_remoteCommandProcess.Kill();
m_remoteCommandProcess.Dispose();
m_remoteCommandProcess = null;
m_remoteCommandClientID = Guid.Empty;
SendResponse(requestinfo.Sender.ClientID, new ServiceResponse("TelnetSession", "Terminated"));
UpdateStatus(UpdateType.Information, "Remote command session terminated - status updates are resumed.\r\n\r\n");
}
else if ((object)m_remoteCommandProcess != null)
{
// User has entered commands that must be redirected to the established command session.
string input = requestinfo.Request.Command + " " + requestinfo.Request.Arguments;
m_remoteCommandProcess.StandardInput.WriteLine(input);
}
else
{
// User has provided insufficient information.
requestinfo.Request = ClientRequest.Parse("Telnet /?");
RemoteTelnetSession(requestinfo);
}
}
}