1Imports System.Collections.Specialized
2Imports System.Configuration
3Imports System.IO
4Imports System.net
5Imports System.ServiceProcess
6Imports System.Text.RegularExpressions
7Imports System.Web
8Imports System.Web.Mail
9Imports System.Windows.Forms.Timer
10Imports System.Xml
11
12Public Class clsMain
13 Inherits System.ServiceProcess.ServiceBase
14 Private WithEvents Timer1 As New Timers.Timer()
15#Region " Component Designer generated code "
16
17 Public Sub New()
18 MyBase.New()
19
20 ' This call is required by the Component Designer.
21 InitializeComponent()
22
23 ' Add any initialization after the InitializeComponent() call
24
25 End Sub
26
27 'UserService overrides dispose to clean up the component list.
28 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
29 If disposing Then
30 If Not (components Is Nothing) Then
31 components.Dispose()
32 End If
33 End If
34 MyBase.Dispose(disposing)
35 End Sub
36
37 ' The main entry point for the process
38 <MTAThread()> _
39 Shared Sub Main()
40 Dim ServicesToRun() As System.ServiceProcess.ServiceBase
41
42 ' More than one NT Service may run within the same process. To add
43 ' another service to this process, change the following line to
44 ' create a second service object. For example,
45 '
46 ' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
47 '
48 ServicesToRun = New System.ServiceProcess.ServiceBase() {New clsMain()}
49
50 System.ServiceProcess.ServiceBase.Run(ServicesToRun)
51 End Sub
52
53 'Required by the Component Designer
54 Private components As System.ComponentModel.IContainer
55
56 ' NOTE: The following procedure is required by the Component Designer
57 ' It can be modified using the Component Designer.
58 ' Do not modify it using the code editor.
59 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
60 '
61 'IP Address Sender
62 '
63 Me.ServiceName = "IPAddressSender"
64
65 End Sub
66#End Region
67 Protected CLASS_NAME As String = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()
68 Protected EXECUTING_ASSEMBLY As String = System.Reflection.Assembly.GetExecutingAssembly.FullName()
69#Region "Private vars"
70 Private objStreamWriter As StreamWriter
71 Private mstrEnableLogging As String = Trim(ConfigurationSettings.AppSettings("EnableLogging"))
72#End Region
73#Region "OnStart"
74 Protected Overrides Sub OnStart(ByVal args() As String)
75 AddHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
76 'Update the timer interval
77 Timer1.Interval = ConfigurationSettings.AppSettings("Interval")
78 'Enable the timer
79 Timer1.Enabled = True
80 End Sub
81#End Region
82#Region "OnStop"
83 Protected Overrides Sub OnStop()
84 'Disable the timer
85 Timer1.Enabled = False
86 End Sub
87#End Region
88#Region "Timer1_Elapsed"
89 Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
90 Dim METHODNAME As String = System.Reflection.MethodBase.GetCurrentMethod().Name
91
92 'Instantiated variables
93 Dim objWebRequest As HttpWebRequest, objWebResponse As HttpWebResponse
94 Dim objProxy As WebProxy, objUri As Uri
95 Dim objSR As StreamReader, objSW As StreamWriter
96 Dim objMail As MailMessage
97 Try
98 'IP Address Storing Variables
99 Dim strNewIPAddress As String = ""
100 Dim strOldIPAddress As String = ""
101
102 'Get the current IP Address
103 Dim strRouterPage As String = ConfigurationSettings.AppSettings("RouterPage")
104 Dim strRouterUserName As String = ConfigurationSettings.AppSettings("RouterUserName")
105 Dim strRouterPassword As String = ConfigurationSettings.AppSettings("RouterPassword")
106 Dim strRouterPatternStart As String = ConfigurationSettings.AppSettings("RouterPatternStart")
107 Dim strRouterPatternEnd As String = ConfigurationSettings.AppSettings("RouterPatternEnd")
108 'Get the HTML form the Router's page
109 objWebRequest = CType(WebRequest.Create(strRouterPage), HttpWebRequest)
110 'Create a NetworkCredential object
111 objWebRequest.Credentials = New NetworkCredential(strRouterUserName, strRouterPassword)
112 'objWebRequest.Proxy = objProxy
113 objWebResponse = CType(objWebRequest.GetResponse(), HttpWebResponse)
114 'Get the response text
115 objSR = New StreamReader(objWebResponse.GetResponseStream())
116 Dim strHTML As String = objSR.ReadToEnd()
117 'IP Address pattern to match on
118 Dim strPattern As String = strRouterPatternStart & _
119 "([01]?\d\d|2[0-4]\d|25[0-5])\." & _
120 "([01]?\d\d|2[0-4]\d|25[0-5])\." & _
121 "([01]?\d\d|2[0-4]\d|25[0-5])\." & _
122 "([01]?\d\d|2[0-4]\d|25[0-5])" & strRouterPatternEnd
123 'Look for a match
124 Dim objMatch As Match = Regex.Match(strHTML, strPattern)
125 strNewIPAddress = objMatch.Value
126 strNewIPAddress = strNewIPAddress.Replace(strRouterPatternStart, "")
127 strNewIPAddress = strNewIPAddress.Replace(strRouterPatternEnd, "")
128
129 'Get the last IP Address
130 objSR = New StreamReader("IPAddress.txt")
131 strOldIPAddress = objSR.ReadLine
132 objSR.Close()
133 objSR = Nothing
134
135 'New IP Address? (Send email)
136 If strNewIPAddress <> strOldIPAddress Then
137 Dim objSMTP As SmtpMail
138 objMail = New MailMessage
139 Dim strSMTPServer As String = ConfigurationSettings.AppSettings("EmailHost")
140 objSMTP.SmtpServer = strSMTPServer
141 Dim strTo As String = ConfigurationSettings.AppSettings("EmailTo")
142 objMail.To = strTo
143 Dim strFromName As String = ConfigurationSettings.AppSettings("EmailFromName")
144 Dim strFromEmail As String = ConfigurationSettings.AppSettings("EmailFromEmail")
145 objMail.From = "\" & strFromName & "\ <" & strFromEmail & ">"
146 objMail.BodyFormat = MailFormat.Text
147 Dim strSubject As String = ConfigurationSettings.AppSettings("EmailSubject")
148 objMail.Subject = strSubject
149 objMail.Body = "New IP Address is " & strNewIPAddress
150 'Setup authentication
151 Dim strUserName As String = ConfigurationSettings.AppSettings("EmailUserName")
152 Dim strPassword As String = ConfigurationSettings.AppSettings("EmailPassword")
153 objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")
154 objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", strUserName)
155 objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", strPassword)
156 'Send the email
157 objSMTP.Send(objMail)
158
159 'Save the new IP Address to a physical file
160 objSW = New StreamWriter("IPAddress.txt")
161 objSW.Write(strNewIPAddress)
162 objSW.Close()
163 objSW = Nothing
164 End If
165
166 Catch ex As Exception
167 'Log to physical file
168 StartLogging()
169 'Collect debugging information
170 Dim colExtraInfo As NameValueCollection = New NameValueCollection
171 colExtraInfo.Add("Caller", System.Reflection.Assembly.GetCallingAssembly().FullName)
172 colExtraInfo.Add("Assembly Name", System.Reflection.Assembly.GetExecutingAssembly().FullName)
173 colExtraInfo.Add("Class Name", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString())
174 colExtraInfo.Add("Method Name", System.Reflection.MethodBase.GetCurrentMethod().Name)
175 Dim I As Int16, strBody As String = ""
176 For I = 0 To colExtraInfo.Count - 1
177 strBody &= colExtraInfo.GetKey(I) & " - " & colExtraInfo.Get(I) & "<br>"
178 Next
179 strBody &= ex.ToString() & vbCrLf
180 'Write to log
181 WriteStatEntry(strBody)
182 'Close log
183 StopLogging()
184
185 Finally
186 'Release memory
187 objMail = Nothing
188 objSR = Nothing
189 objSW = Nothing
190 objProxy = Nothing
191 objUri = Nothing
192 objWebRequest = Nothing
193 objWebResponse = Nothing
194 End Try
195 End Sub
196#End Region
197
198#Region "StartLogging"
199 Private Sub StartLogging()
200 'Prepare the message log
201 If mstrEnableLogging = "Y" Then
202 Try
203 Dim strDate As String = DateTime.Now.Date.Year.ToString & _
204 DateTime.Now.Date.Month.ToString & _
205 DateTime.Now.Date.Day.ToString
206 Dim strTime As String = Mid(CStr(DateTime.Now.TimeOfDay.ToString), 1, 8)
207 strDate = Replace(strDate, ":", ".")
208 strTime = Replace(strTime, ":", ".")
209 Dim strPath As String = Trim(ConfigurationSettings.AppSettings("AppPath")) + "Logs\"
210 objStreamWriter = New StreamWriter(strPath & strDate & "_" & strTime & ".txt")
211 Catch
212 'Do nothing
213 End Try
214 End If
215 End Sub
216#End Region
217
218#Region "WriteStatEntry"
219 Private Sub WriteStatEntry(ByVal strMessage As String)
220 If mstrEnableLogging = "Y" Then
221 Try
222 strMessage = CStr(DateTime.Now.ToString) & " - " & strMessage
223 objStreamWriter.WriteLine(strMessage)
224 Catch
225 'Do nothing...this is here to catch writing exceptions on exit
226 End Try
227 End If
228 End Sub
229#End Region
230
231#Region "StopLogging"
232 Private Sub StopLogging()
233 If mstrEnableLogging = "Y" Then
234 Try
235 'Close output file
236 objStreamWriter.Close()
237 Catch
238 'Do nothing
239 End Try
240 End If
241 End Sub
242#End Region
243
244End Class