Web Proxy (Creating One)

Can one create a web proxy using Visual Basic? If yes, I'll up the points for a "how" section to that answer!
Who is Participating?
mcriderConnect With a Mentor Commented:
When building a proxy server in VB basically, they work like this:

You have a winsock control that is running as a server.  It is listening on a specific port waiting for a connection.

Once this socket receives a connection (ACCEPT SOCKET), the code would open another connection using another socket to the "real destination" (PROXY SOCKET).

Usually, before the PROXY SOCKET is opened, information about where the "real destination" is located is passed from the client.  For example, in a HTTP proxy, URL sent contains the system name that the proxy server should connect to.

Depending on the protocol you are building the procy for, the code would "pump" everything it receives from the ACCEPT SOCKET to the PROXY SOCKET... and everything it recives from the PROXY SOCKET to the ACCEPT SOCKET.

FTP, for example is much more difficult to provide proxy services for that HTTP because HTTP only uses 1 socket for transmission, whereas, FTP uses 2 sockets...

I am assuming that you already know how to program using the winsock control... If not, take a look at the Help page and "Books online..." for the control.

Here's an example of a proxy for HTTP using the winsock controls that I put together... Remember, this is a BASIC EXAMPLE that I threw together.

1) Create a new project.

2) add 2 winsock controls to the form and change the INDEX property of both controls to 0

3) Add the following code to the DECLARATIONS SECTION of the form:

        Dim RollingSocket As Integer
    Private Sub Form_Load()
        Winsock1(0).LocalPort = 80
    End Sub
    Private Sub Winsock1_Close(Index As Integer)
        On Error Resume Next
        Unload Winsock1(Index)
        Unload Winsock2(Index)
    End Sub
    Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
        Dim iVal As Long
        RollingSocket = RollingSocket + 1
        If RollingSocket = 200 Then RollingSocket = 1
        Load Winsock1(RollingSocket)
        Winsock1(RollingSocket).Accept requestID
    End Sub
    Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
        Dim iVal As Long
        Dim jVal As Long
        Dim lBuf As String
        Dim lCmd As String
        Dim lFunct As String
        Dim lUrl As String
        Dim lVer As String
        Dim lHost As String
        Dim ProxyServer As Integer
        On Error Resume Next
        With Winsock1(Index)
            .GetData lBuf
            .Tag = .Tag + lBuf
            If Right$(.Tag, 4) = vbCrLf + vbCrLf Then
                iVal = InStr(1, .Tag, vbCrLf)
                lCmd = Left$(.Tag, iVal - 1)
                .Tag = Mid$(.Tag, iVal + 2)
                iVal = InStr(1, lCmd, " ")
                jVal = InStr(iVal + 1, lCmd, " ")
                lFunct = Left$(lCmd, iVal - 1)
                iVal = iVal + 1
                lUrl = Mid$(lCmd, iVal, jVal - iVal)
                lVer = Mid$(lCmd, jVal + 1)
                iVal = InStr(1, lUrl, "://") + 3
                jVal = InStr(iVal, lUrl, "/")
                lHost = Mid$(lUrl, iVal, jVal - iVal)
                lUrl = Mid$(lUrl, iVal + Len(lHost))
                .Tag = lFunct + " " + lUrl + " " + lVer + vbCrLf + .Tag
                'CALL THE PROXY SOCKET
                ProxyServer = Index
                Load Winsock2(ProxyServer)
                Winsock2(ProxyServer).RemoteHost = lHost
                Winsock2(ProxyServer).RemotePort = 80
            End If
        End With
    End Sub
    Private Sub Winsock2_Close(Index As Integer)
        On Error Resume Next
        Unload Winsock1(Index)
        Unload Winsock2(Index)
    End Sub
    Private Sub Winsock2_Connect(Index As Integer)
        On Error Resume Next
        Winsock2(Index).SendData Winsock1(Index).Tag
    End Sub
    Private Sub Winsock2_DataArrival(Index As Integer, ByVal bytesTotal As Long)
        Dim lByte() As Byte
        Winsock2(Index).GetData lByte, vbByte
        Winsock1(Index).SendData lByte
    End Sub

4) Run the program.

5) From another system, configure the browser proxy to be the system you're running this program on. (use port 80)

By the way, I think this is worth much more than 10 points... Don't you??  EE guidelines for asking questions are:

     50 points for an easy question
     100 points for a moderate question
     200 points for a hard question


While you might be able to create a web proxy in VB generally you wouldn't want to as you should put in a Linux box up instead of a Windoze OS. This would therefore eliminate *any* Micro$oft GUI product from being used as a proxy server development base.

Rather then bash Microsoft, I will tell you that it can be done.  Tell me more about what you wish to do and I might already have some code for you.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Not bashing M$. Simply saying that GUI is weak point in this type of app. You want *TIGHT*, *FAST* and *Rugged*. Windows/VB provides none of these and is inappropriate choice for this application. You can write it in VB on Windows, but system will *NOT* work reliably and will be easily breached. Is NOT how you want proxy server/firewall to behave. Why do you need a GUI on a box that will be locked in a closet? You could add a bath tub to a drag racing car but it wouldn't help it go any faster or run any better - so what is point?

Windows is inappropriate for tight realtime applications. You simply cannot write robust, highly I/O intensive code with all of overhead that the GUI implies. I've written quite a bit of firmware and embedded processor code and GUI overhead is not appropriate even if you *can* get it running.

How do you know how I/O intensive his application is going to be?  No time to argue though and there is no point to it until we know what he wants to do with it.

Take care,
ayrezyleAuthor Commented:
Adjusted points from 10 to 100
ayrezyleAuthor Commented:
Let me settle this one before someone draws blood :-) My case is a special one. Firstly, I am a C++ fan and also like nice & tight code. However, I don't have the time to learn VC++ which is my first choice of platform for this app. I'm already strong on VB using COM, which is why I want to do this in VB. Later on, I can migrate the component doing the actual work to a nice & tight VC++ COM component using ATL and all those niceties.

Why VB on NT? I want to create management features for a proxy; the proxy itself doesn't concern me much.

Regarding the performance issue: I'm running this proxy on a dual Pentium 2 350Mhz (200+MB RAM) servicing 40 people sharing a single 64kbps line. It won't break a sweat.

mcrider, you'll notice I've already upped the points :) You're #1 atm, I'll check out the code tonight.
ayrezyleAuthor Commented:
mcrider, I'm impressed!! Cut&Paste and it works! now I need to sit down and check out what you did!

I take it you've previously worked with TCP/IP headers?

One more thing... I assume that if I match the port numbers to the requested ports, I'll be able to use SSL and FTP too? (FTP will be a different header though; will SSL?)

How much memory does that control array take (per element)?

Where on the Net do I get a description of those headers?
>>mcrider, I'm impressed!! Cut&Paste and it works!

Glad I could help you... It's not very often that I post code that dies when you cut and paste it. ;-)

Yes, I have worked with TCP headers.  In fact, Secure Firewall Technology (not in VB) is where I've made my millions.  As for SSL, you should be able to change the ports, however, FTP is a multi-socket protocol and it is *MUCH* more difficult.  It requires 2 separate sockets... Ports 20 and 21.

I suggest that if you are going to be going deep in this subject, you look at the RFCs for each protocol.  You can find them by going to http://www.yahoo.com and doing a search for "+RFC +FTP" (don't use the quotes)


i know it's been nearly 3 years since this thread has started however i've just found it.

i've created the program and it runs semi well. I get inconsistent results.

if i point my broswer to the proxy and point my browser to a site such as www.google.com i get inconsitent results. The first time only half the page will come through, the second time even less then the next time the page will come in fully and so on.

Any thoughts?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.