-
Notifications
You must be signed in to change notification settings - Fork 0
/
Service1.cs
174 lines (151 loc) · 6.8 KB
/
Service1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// <copyright file="Service1.cs" company="Chris Pont">
// Copyright 2011, Chris Pont
// [Copyright]
// </copyright>
namespace FirewallService
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Net;
using System.ServiceProcess;
using System.Text;
using System.Threading;
/// <summary>
/// The Firewall Service
/// </summary>
public partial class Service1 : ServiceBase
{
/// <summary>
/// The last IP address found
/// </summary>
private string lastIPAddress = string.Empty;
/// <summary>
/// Configuration Settings Object
/// </summary>
private ConfigurationSettings config;
/// <summary>
/// The timer which fires to check the IP Address
/// </summary>
private System.Timers.Timer timer1;
/// <summary>
/// A mutex object
/// </summary>
private Mutex mutex;
/// <summary>
/// Initializes a new instance of the <see cref="Service1"/> class.
/// </summary>
public Service1()
{
InitializeComponent();
}
/// <summary>
/// When implemented in a derived class, executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to take when the service starts.
/// </summary>
/// <param name="args">Data passed by the start command.</param>
protected override void OnStart(string[] args)
{
System.Configuration.AppSettingsReader appReader = new System.Configuration.AppSettingsReader();
this.timer1 = new System.Timers.Timer(Convert.ToDouble(appReader.GetValue("Interval", typeof(string))));
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = System.AppDomain.CurrentDomain.BaseDirectory;
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.config";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
this.mutex = new Mutex(false);
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.Timer1_Elapsed);
this.timer1.Start();
string ipToAdd = appReader.GetValue("Host", typeof(string)).ToString();
EventLog.WriteEntry("Firewall Updater monitoring host '" + ipToAdd + "'");
}
/// <summary>
/// When implemented in a derived class, executes when a Stop command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service stops running.
/// </summary>
protected override void OnStop()
{
EventLog.WriteEntry("Firewall Updater exiting.");
}
/// <summary>
/// Called when [changed].
/// </summary>
/// <param name="source">The source.</param>
/// <param name="e">The <see cref="System.IO.FileSystemEventArgs"/> instance containing the event data.</param>
private static void OnChanged(object source, FileSystemEventArgs e)
{
ConfigurationManager.RefreshSection("appSettings");
}
/// <summary>
/// Handles the Elapsed event of the Timer1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param>
private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.mutex.WaitOne();
try
{
// EventLog.WriteEntry("Tick");
System.Configuration.AppSettingsReader appReader = new System.Configuration.AppSettingsReader();
string ipToAdd = appReader.GetValue("Host", typeof(string)).ToString();
IPAddress[] addresslist = Dns.GetHostAddresses(ipToAdd);
if (addresslist.Length > 0)
{
if (this.lastIPAddress != addresslist[0].ToString())
{
this.RunScript("netsh advfirewall firewall delete rule name=\"" + appReader.GetValue("RuleName", typeof(string)).ToString() + "\" protocol=TCP");
string scripttext = "netsh advfirewall firewall add rule name=\"" + appReader.GetValue("RuleName", typeof(string)).ToString() + "\" dir=in action=allow protocol=TCP remoteip=" + addresslist[0];
this.RunScript(scripttext);
EventLog.WriteEntry("Changed firewall rule for RDP. New IP Address: " + addresslist[0]);
this.lastIPAddress = addresslist[0].ToString();
}
}
}
finally
{
this.mutex.ReleaseMutex();
}
}
/// <summary>
/// Runs the script.
/// </summary>
/// <param name="scriptText">The script text.</param>
/// <returns>The output of the script run</returns>
private string RunScript(string scriptText)
{
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
pipeline.Commands.Add("Out-String");
// execute the script
Collection<PSObject> results = pipeline.Invoke();
// close the runspace
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
}
}