Page 1 of 1

Proxy help?

Posted: Mon Mar 28, 2005 10:15 pm
by Kaiser
Hey people :D

Well <.< I know, I'm a programming addict XD but can't help sometimes when I don't have the enough knowledge of it (when translating to PB code) so here it goes...

I'm trying to make a proxy, but I dunno how. It's with a client program already made. According to some VB code I have, it makes the program to connect to 127.0.0.1 (local computer), while then in the local computer I send data received by the client, and receive data from the server and send to the client, you know o_o I hope you get it. Anyhow, I was wondering if any of you had a small example on how to do that?

Thanks in advance :)

(And yeah, I tried tons of times, but could just got to connect to the server and make the client to connect to my local ip (127.0.0.1) but not to relay the data between the two)

Posted: Thu Mar 31, 2005 2:09 pm
by Kaiser
Uh... hello? :(

Posted: Thu Mar 31, 2005 4:03 pm
by dagcrack
Well I always wondered... how can a proxy (like analogx very very simple proxy) get the messages when the client (in this case internet explorer) needs the "blabla.com" page... and later deliver it.

okey theres direct connection -say lan via ethernet- between the client pc and the server pc (with the proxy for the clients) but I still dont get some points.

Posted: Thu Mar 31, 2005 4:23 pm
by HeX0R
Here is a proxy-server in pb:
viewtopic.php?t=13960

Here is the Client:
viewtopic.php?t=13520

Not sure which one you are in need off..

Posted: Thu Mar 31, 2005 8:43 pm
by Kaiser
YAY :D dude, those are awesome o_o;;

Though actually, the first one is the one I'm searching for, to know how to, once I connect the app to 127.0.0.1 , to handle the data received / sent , using the PB code as listener, just like you did in your program ^^ if you don't want to release the source it's okay :) but maybe some little help (or at least a small example) could work? :)

Posted: Thu Mar 31, 2005 8:51 pm
by HeX0R
Well i took my own Client-Connect-Snippet and reworked it to the opposite.

You can use the Client-Snippet then to connect to your own proxy-test-server.
At least that was the way i did it.
If you see no land at all, feel free to post again. I will then try to provide some kind of working example (but not so fast).

Posted: Fri Apr 08, 2005 5:53 am
by Kaiser
Well... I tried reworking it as you said... I learned a few things, but still no results :-/ ... it took long time for me to answer I know, but I've just been wanting to make things easier for you but I couldn't, so yeah, could you please make that example? I'm sorry if I'm being such an annoyance, if I am :-/

Edit: I even made this code, I could get t receive the data from the client program, but I can't send it to the client program, though :(

Code: Select all

Global Buffer
InitNetwork()
proxy=CreateNetworkServer(6000)
server=OpenNetworkConnection("<my server>",6000)
RunProgram("<my app>","","<my path>")
Repeat
  If NetworkServerEvent()<>0
    cid=NetworkClientID()
    Buffer=AllocateMemory(1000)
    ReceiveNetworkData(cid,Buffer,1000)
    FromClient$=PeekS(Buffer)
    Debug "Client to me: " + FromClient$
  EndIf
  If NetworkClientEvent(server)<>0
    Buffer2=AllocateMemory(1000)
    ReceiveNetworkData(server,Buffer2,1000)
    FromServer$=PeekS(Buffer2) 
    Debug "Server to me: " + FromServer$
  EndIf 
ForEver

Posted: Sat Apr 09, 2005 3:05 pm
by HeX0R

Code: Select all

;+-------------------------+
;|                         |
;|      Proxy-Server       |
;|                         |
;|   only http supported   |
;|     in this example     |
;|  (for SOCKs use brain)  |
;|                         |
;|      (c)HeX0R 2005      |
;|                         |
;+-------------------------+

Structure _clients_
  Mode.l
  ClientID.l
  OUT_Port.l
  OUT_IP.s
  OUT_ID.l
  IN_Rest_String.s
  OUT_Rest_String.s
EndStructure

Global IN_Username.s ;<-possible Username for proxy
Global IN_Password.s ;<-possible Password for proxy
Global *buffer

NewList Clients._clients_()

*buffer = AllocateMemory(20000)

If InitNetwork() = 0
  End
EndIf

NetID.l = CreateNetworkServer(8080)
If NetID = 0
  Debug "Can't Create Listen-Server!"
  End
EndIf

Procedure.l SelectClient(Client.l)
  found.l = #False
  ForEach Clients()
    If Clients()\ClientID = Client
      found = #True
      Break
    EndIf
  Next
  ProcedureReturn found
EndProcedure

Procedure CheckInput(From_ID.l, To_ID.l, mode.b)
  ;mode Bit0 = 1  -> Data from Client (OUT)
  ;mode Bit0 = 0  -> Data from Server (IN)
  If mode & 1
    Rest.s = Clients()\OUT_Rest_String
    What.s = "OUT"
  Else
    Rest.s = Clients()\IN_Rest_String
    What.s = "IN"
  EndIf
  
  length.l = ReceiveNetworkData(From_ID, *buffer, 10000)
  
  ;Normal
  text$ = Rest + PeekS(*buffer, length)
  count.l = CountString(text$, Chr(10))
  For i = 1 To count
    line$ = StringField(text$, i, Chr(10)) + Chr(10)
    line$ = ReplaceString(ReplaceString(line$, Chr(10), "[lf]"), Chr(13), "[cr]")
    PrintN(What + "->" + line$)    
  Next i
  If mode & 1
    Clients()\OUT_Rest_String = StringField(text$, i, Chr(10))
  Else
    Clients()\IN_Rest_String = StringField(text$, i, Chr(10))
  EndIf
  If To_ID
    SendNetworkData(To_ID, *buffer, length)
  EndIf
EndProcedure

Procedure.l ProxyCheckConnect()
  If Clients()\Mode = -1
    ProcedureReturn 0
  EndIf
  ;====================
  ;-HTTP - Proxy
  ;====================
  length.l = ReceiveNetworkData(Clients()\ClientID, *buffer, 5000)
  If length = 0
    ProcedureReturn 0
  EndIf
  PokeB(*buffer + length, 0)
  text$ = PeekS(*buffer)
  accept$ = "200"
  If LCase(Left(text$, 8)) = "connect "
    ;Application-Tunneling!
    text$ = Right(text$, Len(text$) - 8)
    ip_port.s = StringField(text$, 1, " ")
    Clients()\OUT_IP = StringField(text$, 1, ":")
    Clients()\OUT_Port = Val(StringField(text$, 2, ":"))
    l.l = FindString(text$, #CRLF$, 1)
    If l
      text$ = Right(text$, Len(text$) - l - 1)
    EndIf
    If IN_Username
      *newbuffer = AllocateMemory(1024)
      user$ = IN_Username + ":" + IN_Password
      length2.l = Len(user$) * 2
      If length2 < 64
        length2 = 64
      EndIf
      Base64Encoder(@user$, Len(user$), *newbuffer, length2)
      encoded$ = PeekS(*newbuffer)
      FreeMemory(*newbuffer)
      If FindString(text$, encoded$ + #CRLF$, 1)
        Clients()\Mode = 10
      Else
        accept$ = "401 Authorization Required"
        PrintN("Error - Wrong username/password")
      EndIf
    Else
      Clients()\Mode = 10
    EndIf
    If Clients()\Mode = 10
      server$ = Clients()\OUT_IP + ":" + Str(Clients()\OUT_Port)
      PrintN("Connecting to " + server$ + "...")
      Clients()\OUT_ID = OpenNetworkConnection(Clients()\OUT_IP, Clients()\OUT_Port)
      If Clients()\OUT_ID = 0
        accept$ = "411"
        PrintN("Unable to reach external Server " + server$)
      Else
        PrintN("Connected to " + server$)
      EndIf
    EndIf
  Else
    accept$ = "402"
    Clients()\Mode = -1
  EndIf
  text$ = "HTTP/1.1 " + accept$ + Chr(10) + "O.K." + Chr(10)
  SendNetworkData(Clients()\ClientID, @text$, Len(text$))
  If accept$ <> "200"
    Clients()\Mode = -1
  EndIf
EndProcedure


If OpenConsole()
  PrintN("Server started and listening on Port 8080")
  Repeat
    key_pressed$ = Inkey()
    Select NetworkServerEvent()
      Case 0
        Delay(5)
      Case 1
        Client.l = NetworkClientID()
        AddElement(Clients())
        Clients()\ClientID = NetworkClientID()
        Clients()\Mode = 0
        PrintN("Client Connected")
      Case 2
        ;Data Received
        Client.l = NetworkClientID()
        If SelectClient(Client)
          If Clients()\Mode < 10
            ProxyCheckConnect()
          Else
            CheckInput(Clients()\ClientID, Clients()\OUT_ID, #True)
          EndIf
        EndIf
      Case 4
        ;Disconnect
        Client.l = NetworkClientID()
        If SelectClient(Client)
          DeleteElement(Clients())
          PrintN("Client Disconnected")
        EndIf
    EndSelect
    ForEach Clients()
      If Clients()\OUT_ID
        Select NetworkClientEvent(Clients()\OUT_ID)
          Case 2
            CheckInput(Clients()\OUT_ID, Clients()\ClientID, #False)
        EndSelect
      EndIf
    Next
  Until Left(key_pressed$, 1) = Chr(27)
EndIf

CloseNetworkServer()
CloseConsole()
FreeMemory(*buffer)

End

Posted: Sun Apr 10, 2005 2:15 am
by Kaiser
Wow, the code actually works and such, it really sent the data to the client, that's what Ive really wanting to do, but I wonder.. thta code is for more http-related like, and I've tried in my code to send the data to the clientID and it never receives it evene when I detect it with the NetworkClientID().. I wonder, would it be possible for allowing the code I just put to actually send the data? I dunno, yours works like hell.. but it uses too much buffers and such, and more checks for http-related stuff :-/

Posted: Sun Apr 10, 2005 11:02 am
by HeX0R
This is an example of how to do a REAL proxy, with the correct (http)format.
Those 'Buffer-things' are needed to make sure this proxy-connection is correctly established!

Of course you can change it for your needs if you don't want this thing to be a correct proxy, then you can Delete the ProxyCheckConnect() - Procedure and change the source as wished.

Not sure where your problems are now, this little example should show you the trick.

Posted: Sun Apr 10, 2005 8:40 pm
by Kaiser
Actually, it did! :D your code gave me an awesome idea that now works great!!!! :D :D thanks a ton!! :D!!