88' URL : http://www.ip2location.com
99' Email : sales@ip2location.com
1010'
11- ' Copyright (c) 2002-2020 IP2Location.com
11+ ' Copyright (c) 2002-2021 IP2Location.com
1212'
1313'---------------------------------------------------------------------------
1414
15- Imports System
1615Imports System.IO
1716Imports System.Text.RegularExpressions
1817Imports System.Web
19- Imports System.Xml
2018Imports System.Xml.Serialization
2119
2220Public Class HTTPModule : Implements IHttpModule
@@ -28,29 +26,23 @@ Public Class HTTPModule : Implements IHttpModule
2826 Private baseDir As String = ""
2927 Public whitespace As Regex
3028 Public proxyDatabasePath As String = ""
31- Private version As String = "2.3" 'follow the IP2Proxy version
29+ Private version As String = "3.1"
3230
3331 Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
3432 LogDebug.WriteLog( "Exiting IP2Proxy HTTP Module" )
35-
36- If Not proxy Is Nothing Then
37- proxy = Nothing
38- End If
39-
40- If Not config Is Nothing Then
41- config = Nothing
42- End If
33+ proxy = Nothing
34+ config = Nothing
4335 End Sub
4436
4537 Public Sub Init(context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
4638 Dim mydirectory As String = ""
4739
4840 globalConfig = Environment.GetEnvironmentVariable( "IP2ProxyHTTPModuleConfig" )
49- If Not globalConfig Is Nothing Then 'server level mode
41+ If globalConfig IsNot Nothing Then 'server level mode
5042 LogDebug.WriteLog( "Global config: " & globalConfig)
5143 mydirectory = globalConfig
5244 If Not mydirectory.EndsWith( "\" ) Then
53- mydirectory = mydirectory & "\"
45+ mydirectory &= "\"
5446 End If
5547 Else 'website level mode
5648 baseDir = AppDomain.CurrentDomain.BaseDirectory
@@ -61,12 +53,10 @@ Public Class HTTPModule : Implements IHttpModule
6153 LogDebug.WriteLog( "Starting IP2Proxy HTTP Module " & version)
6254 whitespace = New Regex( "\s" )
6355
64- 'CreateConfig(mydirectory & configFile) ' for testing only
65-
6656 config = ReadConfig(mydirectory & configFile)
6757
6858 'Set BIN file path
69- If Not globalConfig Is Nothing Then
59+ If globalConfig IsNot Nothing Then
7060 proxyDatabasePath = config.Settings.BINFile 'global BIN is always full path
7161 Else
7262 proxyDatabasePath = baseDir & config.Settings.BINFile 'website BIN is always relative to website root folder
@@ -86,18 +76,15 @@ Public Class HTTPModule : Implements IHttpModule
8676 Dim app As HttpApplication = DirectCast (sender, HttpApplication)
8777 Dim request As HttpRequest = app.Context.Request
8878 Dim response As HttpResponse = app.Context.Response
89- Dim myIP As String = ""
9079 Dim myurl As String = request.Url.AbsoluteUri
9180
81+ Dim myIP As String
9282 If config.Settings.CustomIPServerVariable.Trim <> "" Then
9383 myIP = request.ServerVariables.Item(config.Settings.CustomIPServerVariable.Trim)
9484 Else
9585 myIP = request.UserHostAddress
9686 End If
9787
98- ' debug only
99- 'myIP = "8.8.8.8"
100-
10188 ' output extra info so we know it is working
10289 LogDebug.WriteLog( "Querying IP: " & myIP)
10390 result = proxy.GetAll(myIP)
@@ -117,44 +104,18 @@ Public Class HTTPModule : Implements IHttpModule
117104 request.ServerVariables.Item( "HTTP_X_IP2PROXY_AS" ) = result.AS
118105 request.ServerVariables.Item( "HTTP_X_IP2PROXY_LAST_SEEN" ) = result.Last_Seen
119106 request.ServerVariables.Item( "HTTP_X_IP2PROXY_THREAT" ) = result.Threat
107+ request.ServerVariables.Item( "HTTP_X_IP2PROXY_PROVIDER" ) = result.Provider
120108 End Sub
121109
122- ''USING THIS FOR TESTING AND GENERATING A CONFIG FILE TEMPLATE
123- 'Private Sub CreateConfig(ByVal filename As String)
124- ' ' Create an instance of the XmlSerializer class;
125- ' ' specify the type of object to serialize.
126- ' Dim serializer As New XmlSerializer(GetType(IP2ProxyConfig))
127- ' Dim writer As New StreamWriter(filename)
128- ' Dim config As New IP2ProxyConfig()
129-
130- ' Dim mydirectory As String = "bin\"
131- ' Dim mybin As String = mydirectory & "IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL.BIN"
132- ' Dim mycustomipservervariable As String = ""
133-
134- ' 'SETTINGS OBJECT
135- ' Dim mysettings As New Settings
136- ' mysettings.BINFile = mybin
137- ' mysettings.CustomIPServerVariable = mycustomipservervariable
138-
139- ' 'attach to config
140- ' config.Settings = mysettings
141-
142- ' serializer.Serialize(writer, config)
143- ' writer.Close()
144- 'End Sub
145-
146110 Private Function ReadConfig( ByVal filename As String ) As IP2ProxyConfig
147111 ' Create an instance of the XmlSerializer class;
148112 ' specify the type of object to be deserialized.
149113 Dim serializer As New XmlSerializer( GetType (IP2ProxyConfig))
150114 ' If the XML document has been altered with unknown
151115 ' nodes or attributes, handle them with the
152116 ' UnknownNode and UnknownAttribute events.
153- AddHandler serializer.UnknownNode, AddressOf serializer_UnknownNode
154- AddHandler serializer.UnknownAttribute, AddressOf serializer_UnknownAttribute
155-
156- ' A FileStream is needed to read the XML document.
157- 'Dim fs As FileStream = Nothing
117+ AddHandler serializer.UnknownNode, AddressOf SerializerUnknownNode
118+ AddHandler serializer.UnknownAttribute, AddressOf SerializerUnknownAttribute
158119
159120 ' All these just to make sure XML is following our case sensitivity.
160121 Dim line As String
@@ -183,40 +144,34 @@ Public Class HTTPModule : Implements IHttpModule
183144 line = Regex.Replace(line, elem, "$1" & elem2 & "$3" , RegexOptions.IgnoreCase)
184145 Next
185146
186- 'fs = New FileStream(filename, FileMode.Open)
187147 ' Declare an object variable of the type to be deserialized.
188148 Dim config As IP2ProxyConfig
149+
189150 ' Use the Deserialize method to restore the object's state with
190151 ' data from the XML document.
191- 'config = CType(serializer.Deserialize(fs), IP2LocationConfig)
192-
193152 sr2 = New StringReader(line)
194153
195154 config = CType (serializer.Deserialize(sr2), IP2ProxyConfig)
196155
197156 Return config
198157 Catch ex As Exception
199- 'LogDebug.WriteLog(ex.Message & vbNewLine & ex.StackTrace)
200158 LogDebug.WriteLog(ex.Message)
201159 Throw 'special case so need to throw here to stop the main process
202160 Finally
203- 'If Not fs Is Nothing Then
204- ' fs.Close()
205- 'End If
206- If Not sr2 Is Nothing Then
161+ If sr2 IsNot Nothing Then
207162 sr2.Close()
208163 End If
209164 End Try
210165 End Function
211166
212- Private Sub serializer_UnknownNode (sender As Object , e As XmlNodeEventArgs)
167+ Private Sub SerializerUnknownNode (sender As Object , e As XmlNodeEventArgs)
213168 LogDebug.WriteLog( "Unknown Node:" & e.Name & vbTab & e.Text)
214- End Sub 'serializer_UnknownNode
169+ End Sub
215170
216171
217- Private Sub serializer_UnknownAttribute (sender As Object , e As XmlAttributeEventArgs)
218- Dim attr As System. Xml.XmlAttribute = e.Attr
172+ Private Sub SerializerUnknownAttribute (sender As Object , e As XmlAttributeEventArgs)
173+ Dim attr As Xml.XmlAttribute = e.Attr
219174 LogDebug.WriteLog( "Unknown attribute " & attr.Name & "='" & attr.Value & "'" )
220- End Sub 'serializer_UnknownAttribute
175+ End Sub
221176
222177End Class
0 commit comments