Proxy Server

Everything else that doesn't fall into one of the other PB categories.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Proxy Server

Post by Killswitch »

Upon the advice of some people I've started writing a proxy server. I;ve been testing it with my browser and it hasn't been going very well to be perfectly honest. the program gets the request from a browser, connects to the right site, downloads the information then sends it back to the browser. I've had almost no success although some text has been set in the browswer (it looks a bit like a http header).

Can anyone help?

Code: Select all

;\\------------------------------
;\\-Title------------------------
;\\------------------------------

  InitNetwork()

;\\-Notes--
; Started:
; Completed:
; Version:
; Authors: Peter Marsh
; Type: Program
;
; COPYRIGHT ZERO POINT FIVE AND A HALF 2005
;\\--------

;\\-Constants--------------------
  
  ;\\Version
  #Version="Beta"  
  
  ;\\Buffer
  #Buffer=10000
  
;\\------------------------------

;\\-Structures-------------------

;\\------------------------------

;\\-Variables--------------------

  ;\\Server
  Global Port
    Port=6000
  
  ;\\Buffers
  *Buffer=AllocateMemory(#Buffer)

;\\------------------------------

;\\-Includes---------------------

;\\------------------------------

;\\-Declares---------------------

  ;\\Misc
  Declare Error()
    OnErrorGosub(@Error())
  
  ;\\Parse
  Declare Parse(ID,*Buffer)
  
;\\------------------------------

;\\-Main-------------------------
  
  If CreateNetworkServer(Port)
  
  Else
    MessageRequester("Alert","Could not create server on port: "+Str(Port)+".")
    End
  EndIf
  
  Repeat
  
    Select NetworkServerEvent()
      Case 1 ;Connection
        
      Case 2 ;Data
        ID=NetworkClientID()
        ReceiveNetworkData(ID,*Buffer,#Buffer)
        Parse(ID,*Buffer)
        FreeMemory(*Buffer)
        *Buffer=AllocateMemory(*Buffer)
      
      Case 4 ;Disconnection
    
    EndSelect
    
    Delay(10)
    
  Until EndLoop=1

;\\------------------------------

;\\-Procedures-------------------

  ;\\Parse
  
  Procedure Parse(ID,*Buffer)
    
    Request.s=PeekS(*Buffer)
    
    If Mid(Request,1,3)="GET" : Command.s="GET" : ElseIf Mid(Request,1,4)="POST" : Command.s="POST" : EndIf

    URL.s=Mid(Request,Len(Command)+9,FindString(Request," HTTP/1.0",1)-12)
    If Right(URL,1)="/" : URL=Mid(URL,1,Len(URL)-1) : EndIf

    Select Command
      
      Case "GET"
        
        
        Connection=OpenNetworkConnection(URL,80)       
        
        If Connection<>0
          Debug 1
          SendNetworkData(Connection,*Buffer,Len(Request))
          Repeat
            Select NetworkClientEvent(Connection)
            
              Case 2
                *SubBuffer=AllocateMemory(#Buffer)
                Result=ReceiveNetworkData(Connection,*SubBuffer,#Buffer)
                                
                If Result<#Buffer
                  BuffLen+Result
                  *Total+*SubBuffer
                  Endit=1
                Else
                  BuffLen+Result
                  *Total+*SubBuffer
                EndIf
                
                FreeMemory(*SubBuffer)
                  
            EndSelect
          Until Endit=1
          
          CloseNetworkConnection(Connection)
          SendNetworkData(ID,*Total,BuffLen+1)
          
          Debug BuffLen
          
        Else
        
        EndIf
        
        
      Case "POST"
      
    EndSelect
    
  EndProcedure
  
  ;\\Error

  Procedure Error()
    MessageRequester("Error",GetErrorDescription())
    End
  EndProcedure

;\\------------------------------

  End

;\\-End--------------------------
~I see one problem with your reasoning: the fact is thats not a chicken~
Tommeh
Enthusiast
Enthusiast
Posts: 149
Joined: Sun Aug 29, 2004 2:25 pm
Location: United Kingdom

Post by Tommeh »

I'll look over the code later, (school now :s) but don't forget, you have to edit every page you recive and send back to point to the proxy again
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

You have to edit the page...?
~I see one problem with your reasoning: the fact is thats not a chicken~
Tommeh
Enthusiast
Enthusiast
Posts: 149
Joined: Sun Aug 29, 2004 2:25 pm
Location: United Kingdom

Post by Tommeh »

Yeah... its the way proxy servers work..

Ok, lets say, your proxy server is http://localhost/proxy.exe, going to that will bring up the proxy page.

But, if we go to like, http://localhost/proxy.exe?page=google.com/ it would being up google.

Now, lets say it needs to go to google, and the code on that page is...

Code: Select all

<html><img src="http://google.com/images/logo.jpg"></html>
That will have to be read by the proxy, then transformed into...

Code: Select all

<html><img src="http://localhost/proxy.exe?page=google.com/images/logo.jpg"></html>
So when the page is opened, and returned to the browser, it goes.. ok now i need the image, and it will send of that URL to the proxy server, and the proxy server does it all over again.

Otherwise, say if google is banned, and it gets the HTML from google, and dosnt change the links to point to intself, then the image will not be displayed. This also applys for any links, CSS, images, page links etc.


Not all small task ;)
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

:shock:

Lot's of work to do...
~I see one problem with your reasoning: the fact is thats not a chicken~
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

@tommeh: im not 100% sure thats correct. Proxy is like a filter.. well ..

browser requests http: goes through proxy server, proxy sends back the info

ok i know im hard to understand :) i just think that its not needed to do it that way as you just specify proxy by port at firefox etc.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

I'm horribly confused now. Is there any need to alter the data if you set IE or FireFox to work through a proxy server? If so how can this be acheived, if not what can I do to make this code work?

I'd really like to get this working as Proxy Servers are mentioned a lot in the forums but there is no working example.
~I see one problem with your reasoning: the fact is thats not a chicken~
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

There is two kinds of proxys really. external (as seems to be mentioned above).
And local. (I think local prxys are called something else, but I can't remember).

Virus checkers do this a lot, firewalls too.

They act as thefool said above.
This is what you want.

Make a program, or ntservice (looks elsewhere where some cool guy just posted some easy nt service code).
That basically sit there intercepting port 80 outgoing.

Another variation is also a nt service, but as a deamon instead.
In this case you set the browser proixy settings to localhost,
this would be a lot easier as you can just use PureBasic network commands to run a simple server on port 80.

In both cases your program/service/deamon/proxy/whatever,
will fetch the pages and images and whatever else the browser requests.
Then you can easily control/check all urls, and even content.
Easily block urls and ip's, or react to bad words and well, anything really.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

I've looked for the NT code (and found it) but I have no idea what it does, what a NT is or how to use one.

Sorry for being annoying, but can someone answer these questions for me please?
~I see one problem with your reasoning: the fact is thats not a chicken~
Tommeh
Enthusiast
Enthusiast
Posts: 149
Joined: Sun Aug 29, 2004 2:25 pm
Location: United Kingdom

Post by Tommeh »

thefool:

Yes, this is exactly what has to happen.
Proxys are used for several things:

To anonymise their browsing, either to the computer administrator they are on, or to the site they are going to.

To evade website bans.

To bypass firewall restrictions.

All of the above have to stop ALL direct traffic to the web site, unless you are making a proxy for services, such as accessing IRC via port 80, but then again, all data has to go through the proxy server, none directly, so the proxy acts as a middle man.

If you open a page that has an image on it, and that image is NOT redirected to go via the proxy, the admin will either see it, the site owner or it will be blocked from being viewed.

If you are looking for a good example, look for CGIProxy or PHProxy, the name suggests what lanauge they are in, the first one, being one of the best around is recommened. If you use the proxy, you will see all links point back to the proxy its self.

Example: http://www.oldrubbish.info/C8prox/index ... 2e636f6d2f

That site uses CGIproxy, and points to google.com, yet you can not see the URL in clear text (to stop the site being viewed being blocked) but when you go on there. you will see all the links on the site point back to the proxy. If you click a link, you still get the right page, but it goes via the proxy. Hope this has helped :p
Post Reply