by   February 16 2011   
How to detected a proxy connection using ASP Classic and WinHTTP.WinHTTPRequest.5.1. One thing that ASP Classic offered was limits on the programmer based on their creativity. Now, this article is old but when I read that WinHTTP style is coming back into fashion with MVC 3.0 and I've been a long time fan of WinHTTP I'll start posting up the scripts I used and when I have time the conversions in MVC.

How to detected a proxy connection using ASP Classic and WinHTTP.WinHTTPRequest.5.x . Great code to use during signup.

I was asked by another ASP forum owner how they can setup a test to determine if the connection the member was using during registration was a proxy.

I thought about all the websites online that offer test options but without the ability to install software on some of the shared hosting service I had to think of a free standing application that would check during the sign up if the connection was a proxy.

Now here comes the WinHTTP.WinHTTPRequest.5.1 Proxy API and how you can setup to check for proxy access during registration.

I'll just show you here the API setup then in the ASP Forums under  Modules I'll show you how to include this script into your Forums and run it during the registration process.

First of all your server needs to have the proper DLL which most shared and VPN servers have installed as the last Windows update.

Code is at the bottom the process follows.

Let's load up some ports.
Start with FTP ports to give your system a checksum.


Dim strKnownProxyPorts
strKnownProxyPorts = "21,25,80,88,110,443,444,808,1080,2003,
2680,3124,3127,3128,2232,3862,5555,5566,
6588,8000,8001,8008,8080,8081,8085,8086,
8087,8088,8090,8118,8135,8888,9000,9090,
9483,17941,46769,47859,48703"

Let's split these up.


KnownProxyPorts = Split(strKnownProxyPorts,",") for i = 0 to ubound( KnownProxyPorts )

Now set the destination site:


Dim strTestProxyURL_Port strTestProxyURL_Port = "www.google.com"
RSSURL2 = strTestProxyURL_Port &":"& KnownProxyPorts(i)
Dim url,strTestURL
strTestURL = "123.123.123.123" 'use your own method of pulling the IP.
url = "http://"&strTestURL

Using WinHTTPRequest.5.1

Set objHTTP = Server.CreateObject("WinHTTP.WinHTTPRequest.5.1")
objHTTP.SetProxy 2, RSSURL2
objHTTP.SetTimeouts 1000, 1000, 1000, 1000
objHTTP.open "GET",url,false
objHTTP.setRequestHeader "Content-Type", "text/html; charset=utf-8"
objHTTP.setRequestHeader "User-Agent", "xctm/1.0 (compatible; Win32; Proxy Check info / vs 1.0.1)"
On Error Resume Next 'See notes
objHTTP.send
objHTTP.WaitForResponse(4)
On Error Resume Next
status = objHTTP.status
If Len(status)=0 Then
status = proxy.status
End If
If status <> 200 Then
Response.Write("
No Proxy on "&RSSURL&" Server Status: "&status&"") Response.Flush Else RSSFeed = objHTTP.responseText Response.Write("
Yes Proxy on "&RSSURL&" Server Status: "&status&"") Response.Write("
Attempting to clear and clean page code to be on the safe side.
") Response.Write("
Shorting things up:
"&Left(ClearHTMLTags(RSSFeed,2),150)&"
") Response.Flush End If Set objHTTP = Nothing strTProxyCounter = strTProxyCounter + 1 Next Response.End

Note: The ClearHTMLTags is a function I'll post up as soon as I pull it off the dev system.

You might have noticed that I shorten the output in this example and run a very nice script to clear out the HTML.

This is the testing script and not the live script for a forum because of the output. You can setup a DB column to record if the Proxy hit was positive or record the port number.
For this example you can use this as an free standing application and develop on top of it.

The Function I use to clear out HTML tags is listed below.
This was added when I discovered that many proxy machines will display a webpage that contains viruses. This method clears out the HTML and shortens the HTML output to remove the possibility of downloading a virus from the proxy.

Here's J'hann code that is used in the function.

'[ClearHTMLTags]
'Coded by J'hann Haukur Gunnarsson
'joi@innn.is
' Purpose: This function clears all HTML tags from a
' string using Regular Expressions.
' Inputs: strHTML;
' A string to be cleared of HTML TAGS
' intWorkFlow;
' An integer that if equals to 0 runs only the RegExp filter
' .. 1 runs only the HTML source render filter
' .. 2 runs both the RegExp and the HTML source render
' .. &gt;2 defaults to 0
' Returns: A string that has been filtered by the function

Function ClearHTMLTags(strHTML, intWorkFlow)
'Variables used in the function
If Len(strHTML) = 0 Then
Exit Function
End If

dim regEx, strTagLess
If Len(strHTML) = 0 Then
Exit Function
End If
'---------------------------------------
strTagless = strHTML
'Move the string into a private variable
'within the function
'---------------------------------------
'regEx initialization
'---------------------------------------
set regEx = New RegExp
'Creates a regexp object
regEx.IgnoreCase = True
'Don't give frat about case sensitivity
regEx.Global = True
'Global applicability
'---------------------------------------
'Phase I
' "bye bye html tags"
if intWorkFlow <> 1 then
'---------------------------------------
regEx.Pattern = "]*
strTagLess = regEx.Replace(strTagLess, "
regEx.Pattern = "[>]"
'matches a single >
strTagLess = regEx.Replace(strTagLess, ">")
'---------------------------------------
end if
'Clean up
'---------------------------------------
set regEx = nothing
'Destroys the regExp object
'---------------------------------------
'---------------------------------------
ClearHTMLTags = strTagLess
'The results are passed back
'---------------------------------------
end function

The above might be messed up a bit because of my copy paste and reworked <> signs. If you use it be sure to make sure everthing related to the <> tags are to your liking.

To see the code in action visit the XCtM Proxy Test Page example.
(It's a very simple page but works like a charm.)

NOTES:
On Error will stop the check.
Because you are only checking for connection status you'll need the On Error Resume Next or you will see the following errors just about everytime.

quote:
WinHttp.WinHttpRequest error '80072f78'

The server returned an invalid or unrecognized response

How to detected a proxy connection using ASP Classic and WinHTTP.WinHTTPRequest.5.1. One thing that ASP Classic offered was limits on the programmer based on their creativity. Now, this article is old but when I read that WinHTTP style is coming back into fashion with MVC 3.0 and I've been a long time fan of WinHTTP I'll start posting up the scripts I used and when I have time the conversions in MVC.