Skip to content

Commit 40eaf08

Browse files
Added provider field and exception handling for incorrect BIN database.
1 parent 687c39b commit 40eaf08

9 files changed

Lines changed: 180 additions & 253 deletions

File tree

IP2ProxyHTTPModule/IP2ProxyHTTPModule/IP2Proxy.vb

Lines changed: 144 additions & 176 deletions
Large diffs are not rendered by default.

IP2ProxyHTTPModule/IP2ProxyHTTPModule/IP2ProxyHTTPModule.vb

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
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
1615
Imports System.IO
1716
Imports System.Text.RegularExpressions
1817
Imports System.Web
19-
Imports System.Xml
2018
Imports System.Xml.Serialization
2119

2220
Public 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

222177
End Class

IP2ProxyHTTPModule/IP2ProxyHTTPModule/LogDebug.vb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
' URL : http://www.ip2location.com
44
' Email : sales@ip2location.com
55
'
6-
' Copyright (c) 2002-2020 IP2Location.com
6+
' Copyright (c) 2002-2021 IP2Location.com
77
'---------------------------------------------------------------------------
88

99
Imports System.IO
1010

1111
Public Class LogDebug
1212
Public Shared Sub WriteLog(ByVal mymesg As String)
13-
Dim myfilename As String = System.AppDomain.CurrentDomain.BaseDirectory & "ip2proxy_errLog.txt"
13+
Dim myfilename As String = AppDomain.CurrentDomain.BaseDirectory & "ip2proxy_errLog.txt"
1414
Dim fs As FileStream = Nothing
1515
Dim objWriter As StreamWriter = Nothing
1616

@@ -22,10 +22,10 @@ Public Class LogDebug
2222
Catch ex As Exception
2323
'just fail silently
2424
Finally
25-
If Not objWriter Is Nothing Then
25+
If objWriter IsNot Nothing Then
2626
objWriter.Close()
2727
End If
28-
If Not fs Is Nothing Then
28+
If fs IsNot Nothing Then
2929
fs.Close()
3030
End If
3131
End Try

IP2ProxyHTTPModule/IP2ProxyHTTPModule/My Project/AssemblyInfo.vb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Imports System.Runtime.InteropServices
1212
<Assembly: AssemblyDescription("")>
1313
<Assembly: AssemblyCompany("IP2Location.com")>
1414
<Assembly: AssemblyProduct("IP2ProxyHTTPModule")>
15-
<Assembly: AssemblyCopyright("Copyright © 2020")>
15+
<Assembly: AssemblyCopyright("Copyright © 2021")>
1616
<Assembly: AssemblyTrademark("")>
1717

1818
<Assembly: ComVisible(False)>
@@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
3131
' by using the '*' as shown below:
3232
' <Assembly: AssemblyVersion("1.0.*")>
3333

34-
<Assembly: AssemblyVersion("3.0.0.0")>
35-
<Assembly: AssemblyFileVersion("3.0.0.0")>
34+
<Assembly: AssemblyVersion("3.1.0.0")>
35+
<Assembly: AssemblyFileVersion("3.1.0.0")>

IP2ProxyHTTPModule/IP2ProxyHTTPModule/XML.vb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@
33
' URL : http://www.ip2location.com
44
' Email : sales@ip2location.com
55
'
6-
' Copyright (c) 2002-2020 IP2Location.com
6+
' Copyright (c) 2002-2021 IP2Location.com
77
'---------------------------------------------------------------------------
88

9-
Imports System.Xml
109
Imports System.Xml.Serialization
1110

12-
'NOTE: The XMLRootAttribute and XMLElement are for renaming the XML output tags.
11+
'NOTE: The XMLRoot and XMLElement are for renaming the XML output tags.
1312

14-
<XmlRootAttribute("IP2Proxy_Configuration")> _
13+
<XmlRoot("IP2Proxy_Configuration")>
1514
Public Class IP2ProxyConfig
1615
Public Settings As Settings
1716
End Class
1817

1918
Public Class Settings
20-
<XmlElement("BIN_File")> _
19+
<XmlElement("BIN_File")>
2120
Public BINFile As String
2221

23-
<XmlElement("Custom_IP_Server_Variable")> _
22+
<XmlElement("Custom_IP_Server_Variable")>
2423
Public CustomIPServerVariable As String
2524
End Class
Binary file not shown.

LICENSE.TXT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 IP2Location.com
3+
Copyright (c) 2021 IP2Location.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Below are the server variables set by the IP2Proxy HTTP Module. You can use any
134134
|HTTP_X_IP2PROXY_AS|Autonomous system name of the proxy.|
135135
|HTTP_X_IP2PROXY_LAST_SEEN|Number of days that the proxy was last seen.|
136136
|HTTP_X_IP2PROXY_THREAT|Threat type of the proxy.|
137+
|HTTP_X_IP2PROXY_PROVIDER|Provider of the proxy.|
137138

138139
___
139140

@@ -157,6 +158,7 @@ Private Sub ShowServerVariable()
157158
Response.Write(Request.ServerVariables("HTTP_X_IP2PROXY_AS") & "<br>")
158159
Response.Write(Request.ServerVariables("HTTP_X_IP2PROXY_LAST_SEEN") & "<br>")
159160
Response.Write(Request.ServerVariables("HTTP_X_IP2PROXY_THREAT") & "<br>")
161+
Response.Write(Request.ServerVariables("HTTP_X_IP2PROXY_PROVIDER") & "<br>")
160162
End Sub
161163
```
162164

@@ -179,6 +181,7 @@ private void ShowServerVariable()
179181
Response.Write(Request.ServerVariables("HTTP_X_IP2PROXY_AS") + "\n");
180182
Response.Write(Request.ServerVariables("HTTP_X_IP2PROXY_LAST_SEEN") + "\n");
181183
Response.Write(Request.ServerVariables("HTTP_X_IP2PROXY_THREAT") + "\n");
184+
Response.Write(Request.ServerVariables("HTTP_X_IP2PROXY_PROVIDER") + "\n");
182185
}
183186
```
184187

@@ -204,6 +207,7 @@ private void ShowServerVariable()
204207
<%=Request.ServerVariables("HTTP_X_IP2PROXY_AS") & "<br>"%>
205208
<%=Request.ServerVariables("HTTP_X_IP2PROXY_LAST_SEEN") & "<br>"%>
206209
<%=Request.ServerVariables("HTTP_X_IP2PROXY_THREAT") & "<br>"%>
210+
<%=Request.ServerVariables("HTTP_X_IP2PROXY_PROVIDER") & "<br>"%>
207211
</body>
208212
</html>
209213
```
@@ -231,6 +235,7 @@ private void ShowServerVariable()
231235
echo $_SERVER['HTTP_X_IP2PROXY_AS'] . "<br>";
232236
echo $_SERVER['HTTP_X_IP2PROXY_LAST_SEEN'] . "<br>";
233237
echo $_SERVER['HTTP_X_IP2PROXY_THREAT'] . "<br>";
238+
echo $_SERVER['HTTP_X_IP2PROXY_PROVIDER'] . "<br>";
234239
?>
235240
</body>
236241
</html>

dll/IP2ProxyHTTPModule.dll

1 KB
Binary file not shown.

0 commit comments

Comments
 (0)