Apply Factory Pattern to ProxyCmd
This commit is contained in:
7
VAR.Toolbox/Code/IOutputHandler.cs
Normal file
7
VAR.Toolbox/Code/IOutputHandler.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace VAR.Toolbox.Code
|
||||
{
|
||||
public interface IOutputHandler
|
||||
{
|
||||
void OutputLine(string line);
|
||||
}
|
||||
}
|
||||
7
VAR.Toolbox/Code/IProxyCmdExecutor.cs
Normal file
7
VAR.Toolbox/Code/IProxyCmdExecutor.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace VAR.Toolbox.Code
|
||||
{
|
||||
public interface IProxyCmdExecutor
|
||||
{
|
||||
bool ExecuteCmd(string cmd, IOutputHandler outputHandler);
|
||||
}
|
||||
}
|
||||
11
VAR.Toolbox/Code/ProxyCmdExecutorDummy.cs
Normal file
11
VAR.Toolbox/Code/ProxyCmdExecutorDummy.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace VAR.Toolbox.Code
|
||||
{
|
||||
public class ProxyCmdExecutorDummy : IProxyCmdExecutor
|
||||
{
|
||||
public bool ExecuteCmd(string cmdString, IOutputHandler outputHandler)
|
||||
{
|
||||
outputHandler.OutputLine(string.Format("DummyExecution: {0}", cmdString));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
VAR.Toolbox/Code/ProxyCmdExecutorFactory.cs
Normal file
14
VAR.Toolbox/Code/ProxyCmdExecutorFactory.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace VAR.Toolbox.Code
|
||||
{
|
||||
public class ProxyCmdExecutorFactory
|
||||
{
|
||||
public static IProxyCmdExecutor CreateFromConfig(string config)
|
||||
{
|
||||
if (string.IsNullOrEmpty(config))
|
||||
{
|
||||
return new ProxyCmdExecutorDummy();
|
||||
}
|
||||
return new ProxyCmdExecutorThroughSQLServer(config);
|
||||
}
|
||||
}
|
||||
}
|
||||
32
VAR.Toolbox/Code/ProxyCmdExecutorThroughSQLServer.cs
Normal file
32
VAR.Toolbox/Code/ProxyCmdExecutorThroughSQLServer.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace VAR.Toolbox.Code
|
||||
{
|
||||
public class ProxyCmdExecutorThroughSQLServer : IProxyCmdExecutor
|
||||
{
|
||||
private string _connectionString = null;
|
||||
|
||||
public ProxyCmdExecutorThroughSQLServer(string connectionString)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
}
|
||||
|
||||
public bool ExecuteCmd(string cmdString, IOutputHandler outputHandler)
|
||||
{
|
||||
SqlConnection cnx = new SqlConnection(_connectionString);
|
||||
SqlCommand cmd = cnx.CreateCommand();
|
||||
cmd.CommandText = "exec master.dbo.xp_cmdshell @cmd";
|
||||
cmd.Parameters.Add(new SqlParameter("cmd", cmdString));
|
||||
cnx.Open();
|
||||
SqlDataReader reader = cmd.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
string output = Convert.ToString(reader[0]);
|
||||
outputHandler.OutputLine(output);
|
||||
}
|
||||
cnx.Close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,17 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using VAR.Toolbox.Code;
|
||||
|
||||
namespace VAR.Toolbox.UI
|
||||
{
|
||||
public partial class FrmProxyCmd : Form
|
||||
public partial class FrmProxyCmd : Form, IOutputHandler
|
||||
{
|
||||
public FrmProxyCmd()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void txtOutput_AppendLine(string line)
|
||||
{
|
||||
BeginInvoke(new MethodInvoker(delegate
|
||||
{
|
||||
txtOutput.AppendText(line);
|
||||
txtOutput.AppendText(Environment.NewLine);
|
||||
Application.DoEvents();
|
||||
}));
|
||||
}
|
||||
|
||||
private object _executionLock = new object();
|
||||
|
||||
private void txtInput_KeyDown(object sender, KeyEventArgs e)
|
||||
@@ -32,17 +22,27 @@ namespace VAR.Toolbox.UI
|
||||
Application.DoEvents();
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.KeyCode == Keys.Return)
|
||||
{
|
||||
e.Handled = true;
|
||||
Application.DoEvents();
|
||||
string cmd = txtInput.Text.Replace("\n", "").Replace("\r", "");
|
||||
txtOutput_AppendLine(cmd);
|
||||
txtInput.Text = string.Empty;
|
||||
Application.DoEvents();
|
||||
txtInput.Text = string.Empty;
|
||||
OutputLine(cmd);
|
||||
new Thread(() => ExecuteCmd(cmd)).Start();
|
||||
return;
|
||||
}
|
||||
if(e.KeyCode == Keys.Enter)
|
||||
{
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
if (e.KeyCode == Keys.LineFeed)
|
||||
{
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
if (e.KeyCode == Keys.Up)
|
||||
{
|
||||
e.Handled = true;
|
||||
@@ -55,22 +55,35 @@ namespace VAR.Toolbox.UI
|
||||
}
|
||||
}
|
||||
|
||||
private IProxyCmdExecutor _proxyCmdExecutor = null;
|
||||
|
||||
private void ExecuteCmd(string cmdString)
|
||||
{
|
||||
Monitor.Enter(_executionLock);
|
||||
SqlConnection cnx = new SqlConnection(Properties.Settings.Default.ProxyCmdConfig);
|
||||
SqlCommand cmd = cnx.CreateCommand();
|
||||
cmd.CommandText = "exec master.dbo.xp_cmdshell @cmd";
|
||||
cmd.Parameters.Add(new SqlParameter("cmd", cmdString));
|
||||
cnx.Open();
|
||||
SqlDataReader reader = cmd.ExecuteReader();
|
||||
while (reader.Read())
|
||||
if (_proxyCmdExecutor == null)
|
||||
{
|
||||
string output = Convert.ToString(reader[0]);
|
||||
txtOutput_AppendLine(output);
|
||||
_proxyCmdExecutor = ProxyCmdExecutorFactory.CreateFromConfig(Properties.Settings.Default.ProxyCmdConfig);
|
||||
}
|
||||
Monitor.Enter(_executionLock);
|
||||
try
|
||||
{
|
||||
_proxyCmdExecutor.ExecuteCmd(cmdString, this);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Log(ex);
|
||||
OutputLine(ex.Message);
|
||||
}
|
||||
cnx.Close();
|
||||
Monitor.Exit(_executionLock);
|
||||
}
|
||||
|
||||
public void OutputLine(string line)
|
||||
{
|
||||
BeginInvoke(new MethodInvoker(delegate
|
||||
{
|
||||
txtOutput.AppendText(line);
|
||||
txtOutput.AppendText(Environment.NewLine);
|
||||
Application.DoEvents();
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,8 +84,13 @@
|
||||
<Compile Include="Code\DirectShow\Uuids.cs" />
|
||||
<Compile Include="Code\DirectShow\Win32.cs" />
|
||||
<Compile Include="Code\GDI32.cs" />
|
||||
<Compile Include="Code\IOutputHandler.cs" />
|
||||
<Compile Include="Code\IProxyCmdExecutor.cs" />
|
||||
<Compile Include="Code\Logger.cs" />
|
||||
<Compile Include="Code\Mouse.cs" />
|
||||
<Compile Include="Code\ProxyCmdExecutorFactory.cs" />
|
||||
<Compile Include="Code\ProxyCmdExecutorDummy.cs" />
|
||||
<Compile Include="Code\ProxyCmdExecutorThroughSQLServer.cs" />
|
||||
<Compile Include="Code\Screenshooter.cs" />
|
||||
<Compile Include="Code\User32.cs" />
|
||||
<Compile Include="Code\Webcam.cs" />
|
||||
|
||||
Reference in New Issue
Block a user