Jump to content

Why my web proxy doen't fully load google.com? It loads half of it and then stops loading.


E1M1
 Share

Recommended Posts

I am trying to make proxy, but it freezes when google is loading. I am trying to make that proxy work so that when you connect to local host, you see google.com instead. So When I connect to http://127.0.0.1 Page loading indicator gets to half and then it stops moving, which means that page stops loading.

I cant find out reason why page stops loading at half. Why don't this page fully load?. Looks like connection freezes after X packets. Connection freezes where it says: To TCP Client:0x48.....

Does anyone have any idea about what might cause this?

#include <Array.au3>
#include <Constants.au3>
#include-once
Dim $array[100][2], $f, $incorrect, $user[100], $ReplySocet[100], $UdpSocket, $u, $UDPReplySocet
$GoiogleIP = "74.125.77.104"
$WebPort = 80

$remoteIP = $GoiogleIP
$Port = $WebPort

;Start up TCP and UDP
TCPStartup()
UDPStartup()

$MainSocket = TCPListen("127.0.0.1", $Port);Create a socket listening for an incoming connection.
If $MainSocket = -1 Then Exit ;Exit on errors

While 1
    For $r = 0 To UBound($array) - 1;Loop trought array, to see wether anyconnection handle has received any packets
        If $array[$r][0] <> "" Then;Check if current array element is connection handle
            $recv = TCPRecv($array[$r][0], 4096, 1)
            If $recv <> "" Then; check is client sent anything
                ConsoleWrite("To TCP Server:" & $recv & @CRLF)
                $ans = getServer($recv, $r);Forward Packet from client to server
                ;return value ($ans) is Server's reply
                If Not @error Then;If not any errors (@error is set by getServer if server doesnt respond in time)
                    ConsoleWrite("To TCP Client:" & $ans & @CRLF)
                    TCPSend($array[$r][0], $ans);Send answer sent by server to client
                Else;If server did not respond
                    ConsoleWrite("To TCP ERROR:" & @CRLF)
                EndIf
            EndIf
        EndIf
    Next
        If IsArray($UdpSocket) <> "" Then;If UDP connection exists
            $recv = UDPRecv($UdpSocket, 4096, 1)
            If $recv <> "" Then;Check if any packets sent by UDP server
                ConsoleWrite("To UDP Server:" & $recv & @CRLF)
                $ans = getUDPServer($recv);Forward client's UDP packet to server and
                ;return server's reply
                If Not @error Then; If server responded
                    ConsoleWrite("To UDP Client:" & $ans & @CRLF)
                    UDPSend($UdpSocket, Binary($ans));Forward server's response to client
                Else;If server did not respond
                    ConsoleWrite("To UDP ERROR:" & @CRLF)
                EndIf
            EndIf
        EndIf
    newTCPclient() ;Accept new TCP Client
    newUDPclient() ;Accept new UDP Client
WEnd
TCPShutdown()

Func newTCPclient()
    $ConnectedSocket = TCPAccept($MainSocket) ;$ConnectedSocket's value is Connection handle
    If $ConnectedSocket <> -1 Then
        ConsoleWrite("New TCP connection" & @CRLF)
        ConsoleWrite("Socet: "&$ConnectedSocket & @CRLF)
        $array[$f][0] = $ConnectedSocket; f'th element of array is connection handle
        $f += 1;Increase f by 1 so, when new TCP connection is created, new array element
        ;will get connection handle.
        ;This way we can support multiple connections.
    EndIf
EndFunc   ;==>newTCPclient

Func newUDPclient()
    $socket = UDPBind(@IPAddress1, 6112);$socket is array contains socket handle,ip, port
    ;$socket can be used to send/recv UDP
    If $socket[1] <> "" Then
        ConsoleWrite("New UDP connection" & @CRLF)
        ConsoleWrite("Socet: "&$socket[1] & @CRLF)
        $UdpSocket = $socket
;~      $u += 1
    EndIf
EndFunc   ;==>newUDPclient


Func getServer($recv, $r)
    If $ReplySocet[$r] = "" Then; If connection with server not created, create new
        $ReplySocet[$r] = TCPConnect($remoteIP, $Port)
    EndIf
    TCPSend($ReplySocet[$r], $recv);Send packet we got from Client to server
    $timer = TimerInit();Start Timer to avoid overhead time loss
    While 1
        If TimerDiff($timer) > 500 Then ;If server doent respond in  500 ms, return empty string and set error 1
            SetError(1)
            Return
        EndIf
        $reply = TCPRecv($ReplySocet[$r], 4096, 1) ;Get Server reply for packet
        If $reply <> "" Then Return $reply; If server replied, Return server's reply.
    WEnd
EndFunc   ;==>getServer

Func getUDPServer($recv)
    If $UDPReplySocet = "" Then ;If UDP connection between Server doesnt't exist
        $UDPReplySocet = UDPOpen($remoteIP, $Port); Create new UDP connection
        ConsoleWrite("UDP Remote Connection Started:"&$UDPReplySocet[1]&@CRLF)
    EndIf
    UDPSend($UDPReplySocet, $recv);Forward packet received from client to server
    If @error Then MsgBox(0,0,"ERROR");Notify if sending packet failed.
    $timer = TimerInit();Start timer
    While 1
        If TimerDiff($timer) > 500 Then; If no answer in 500 ms
            SetError(1);set @error so we wont send empty packet to client.
            Return
        EndIf
        $reply = UDPRecv($UDPReplySocet, 4096, 1)
        If $reply <> "" Then Return $reply;If UDP server respnded, Return server's response.
    WEnd
EndFunc   ;==>getUDPServer
Edited by E1M1

edited

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...