Skip to content

Commit ca66131

Browse files
committed
Added support for TCP Listener IP Verification/Whitelist
1 parent 74dfe8c commit ca66131

File tree

6 files changed

+108
-21
lines changed

6 files changed

+108
-21
lines changed

taskt/Core/ApplicationSettings.cs

+15
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,28 @@ public class LocalListenerSettings
179179
public bool RequireListenerAuthenticationKey { get; set; }
180180
public int ListeningPort { get; set; }
181181
public string AuthKey { get; set; }
182+
public bool EnableWhitelist { get; set; }
183+
public string IPWhiteList { get; set; }
182184
public LocalListenerSettings()
183185
{
184186
StartListenerOnStartup = false;
185187
LocalListeningEnabled = false;
186188
RequireListenerAuthenticationKey = false;
189+
EnableWhitelist = false;
187190
ListeningPort = 19312;
188191
AuthKey = Guid.NewGuid().ToString();
192+
IPWhiteList = "";
189193
}
190194
}
195+
196+
[Serializable]
197+
public class WhiteListIP
198+
{
199+
string _value;
200+
public WhiteListIP(string s)
201+
{
202+
_value = s;
203+
}
204+
public string Value { get { return _value; } set { _value = value; } }
205+
}
191206
}

taskt/Core/Automation/Commands/RemoteAPICommand.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public class RemoteAPICommand : ScriptCommand
3838
[Attributes.PropertyAttributes.Remarks("")]
3939
public string v_ParameterType { get; set; }
4040

41+
[XmlAttribute]
42+
[Attributes.PropertyAttributes.PropertyDescription("Request Timeout (ms)")]
43+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
44+
[Attributes.PropertyAttributes.InputSpecification("Enter the length of time to wait before the request times out ")]
45+
[Attributes.PropertyAttributes.Remarks("")]
46+
public string v_RequestTimeout { get; set; }
47+
4148
[XmlAttribute]
4249
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the response")]
4350
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
@@ -52,6 +59,7 @@ public RemoteAPICommand()
5259
this.SelectionName = "Remote API";
5360
this.CommandEnabled = true;
5461
this.CustomRendering = true;
62+
this.v_RequestTimeout = "5000";
5563
}
5664

5765
public override void RunCommand(object sender)
@@ -61,8 +69,9 @@ public override void RunCommand(object sender)
6169
{
6270
var server = v_BaseURL.ConvertToUserVariable(sender);
6371
var paramType = v_ParameterType.ConvertToUserVariable(sender);
72+
var timeout = v_RequestTimeout.ConvertToUserVariable(sender);
6473

65-
var response = Server.LocalTCPListener.SendAutomationTask(server, paramType);
74+
var response = Server.LocalTCPListener.SendAutomationTask(server, paramType, timeout);
6675

6776
response.StoreInUserVariable(sender, v_userVariableName);
6877

@@ -78,6 +87,7 @@ public override List<Control> Render(frmCommandEditor editor)
7887
base.Render(editor);
7988
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_BaseURL", this, editor));
8089
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_ParameterType", this, editor));
90+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_RequestTimeout", this, editor));
8191

8292
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_userVariableName", this));
8393
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_userVariableName", this).AddVariableNames(editor);

taskt/Core/Automation/Commands/RemoteTaskCommand.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public class RemoteTaskCommand : ScriptCommand
5656
[Attributes.PropertyAttributes.Remarks("")]
5757
public string v_Parameter { get; set; }
5858

59+
[XmlAttribute]
60+
[Attributes.PropertyAttributes.PropertyDescription("Request Timeout (ms)")]
61+
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
62+
[Attributes.PropertyAttributes.InputSpecification("Enter the length of time to wait before the request times out ")]
63+
[Attributes.PropertyAttributes.Remarks("")]
64+
public string v_RequestTimeout { get; set; }
65+
5966
[XmlAttribute]
6067
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the response")]
6168
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
@@ -71,6 +78,7 @@ public RemoteTaskCommand()
7178
this.CommandEnabled = true;
7279
this.CustomRendering = true;
7380
this.v_ExecuteAwait = "Continue Execution";
81+
this.v_RequestTimeout = "120000";
7482
}
7583

7684
public override void RunCommand(object sender)
@@ -82,8 +90,9 @@ public override void RunCommand(object sender)
8290
var paramType = v_ParameterType.ConvertToUserVariable(sender);
8391
var parameter = v_Parameter.ConvertToUserVariable(sender);
8492
var awaitPreference = v_ExecuteAwait.ConvertToUserVariable(sender);
93+
var timeout = v_RequestTimeout.ConvertToUserVariable(sender);
8594

86-
var response = Server.LocalTCPListener.SendAutomationTask(server, paramType, parameter, awaitPreference);
95+
var response = Server.LocalTCPListener.SendAutomationTask(server, paramType, timeout, parameter, awaitPreference);
8796

8897
response.StoreInUserVariable(sender, v_userVariableName);
8998

@@ -101,6 +110,7 @@ public override List<Control> Render(frmCommandEditor editor)
101110
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_ParameterType", this, editor));
102111
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_Parameter", this, editor));
103112
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_ExecuteAwait", this, editor));
113+
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_RequestTimeout", this, editor));
104114

105115
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_userVariableName", this));
106116
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_userVariableName", this).AddVariableNames(editor);

taskt/Core/Server/LocalTCPListener.cs

+32-14
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static void StartAutomationListener(int port)
9494
// Start listening for client requests.
9595
automationListener.Start();
9696

97-
automationLogger.Information($"Automation Listener endpoint started at {automationListener.LocalEndpoint}");
97+
automationLogger.Information($"Automation Listener Endpoint started at {automationListener.LocalEndpoint}");
9898

9999
// Buffer for reading data
100100
Byte[] bytes = new Byte[2048];
@@ -130,6 +130,35 @@ public static void StartAutomationListener(int port)
130130
//break out request content
131131
var messageContent = data.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
132132

133+
if (listenerSettings.EnableWhitelist)
134+
{
135+
automationLogger.Information($"Listener requires IP Verification (Whitelist)");
136+
137+
//verify that client is allowed to connect
138+
var clientAddress = (((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
139+
140+
//get list of ip
141+
var enabledIPs = listenerSettings.IPWhiteList.Split(',');
142+
143+
if (enabledIPs.Any( s => s.Trim().Contains(clientAddress)))
144+
{
145+
automationLogger.Information($"Client '{clientAddress}' verified from WhiteList '{listenerSettings.IPWhiteList}'");
146+
}
147+
else
148+
{
149+
automationLogger.Information($"Closing Client Connection due to IP verification failure");
150+
SendResponse(ResponseCode.Unauthorized, $"Unauthorized", stream);
151+
return;
152+
}
153+
154+
}
155+
else
156+
{
157+
automationLogger.Information($"Listener does not require IP Verification");
158+
}
159+
160+
161+
133162
if (listenerSettings.RequireListenerAuthenticationKey)
134163
{
135164
string authKey = "";
@@ -374,7 +403,7 @@ public static void StopAutomationListener()
374403
{
375404
automationListener.Stop();
376405
}
377-
public static string SendAutomationTask(string endpoint, string parameterType, string scriptData = "", string awaitPreference = "")
406+
public static string SendAutomationTask(string endpoint, string parameterType, string timeout, string scriptData = "", string awaitPreference = "")
378407
{
379408

380409
if (!endpoint.StartsWith("http://"))
@@ -468,18 +497,7 @@ public static string SendAutomationTask(string endpoint, string parameterType, s
468497
request.Resource = "/RestartTaskt";
469498
}
470499

471-
472-
473-
474-
if (awaitPreference == "Await For Result")
475-
{
476-
request.Timeout = 120000;
477-
}
478-
else
479-
{
480-
request.Timeout = 5000;
481-
}
482-
500+
request.Timeout = int.Parse(timeout);
483501

484502

485503
RestSharp.IRestResponse resp = client.Execute(request);

taskt/UI/Forms/frmSettings.Designer.cs

+30-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

taskt/UI/Forms/frmSettings.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ private void frmSettings_Load(object sender, EventArgs e)
7474
chkRequireListenerKey.DataBindings.Add("Checked", listenerSettings, "RequireListenerAuthenticationKey", false, DataSourceUpdateMode.OnPropertyChanged);
7575
txtListeningPort.DataBindings.Add("Text", listenerSettings, "ListeningPort", false, DataSourceUpdateMode.OnPropertyChanged);
7676
txtAuthListeningKey.DataBindings.Add("Text", listenerSettings, "AuthKey", false, DataSourceUpdateMode.OnPropertyChanged);
77+
chkEnableWhitelist.DataBindings.Add("Checked", listenerSettings, "EnableWhitelist", false, DataSourceUpdateMode.OnPropertyChanged);
78+
txtWhiteList.DataBindings.Add("Text", listenerSettings, "IPWhiteList", false, DataSourceUpdateMode.OnPropertyChanged);
79+
7780
SetupListeningUI();
7881

7982
var clientSettings = newAppSettings.ClientSettings;
@@ -507,8 +510,12 @@ private void btnStopListening_Click(object sender, EventArgs e)
507510
Core.Server.LocalTCPListener.StopAutomationListener();
508511
}
509512

510-
511-
513+
private void btnWhiteList_Click(object sender, EventArgs e)
514+
{
515+
//newAppSettings.ListenerSettings.IPWhiteList.Add(new Core.WhiteListIP("127.0.0.1"));
516+
//Supplement_Forms.frmGridView frmWhitelist = new Supplement_Forms.frmGridView(newAppSettings.ListenerSettings.IPWhiteList);
517+
//frmWhitelist.ShowDialog();
518+
}
512519

513520
}
514521
}

0 commit comments

Comments
 (0)