Jump to content

Proxy Server


Recommended Posts

Hello,

I am trying to write a easy proxy server. I've made something by myself but it works very slow and sometimes doesn't work correctly.

My code (tested on Mozilla Firefox):

#include <Array.au3>
TCPStartup()

;Variables
Global $FirefoxRecv = "", $ServerRecv = ""
Global $Server[1] = [""], $Socket[1] = [""], $Recv[1] = [0]
Global $RecvMode = 1

;Port: 8080 - you can change if you want
$MainFirefoxSocket = TCPListen(@IPAddress1, 8080)
If @error Then MsgBox(16, "Error", "1")
ConsoleWrite(@CR & "Server started...")

Global $FirefoxSocket = -1
Do
    $FirefoxSocket = TCPAccept($MainFirefoxSocket)
Until $FirefoxSocket > -1
ConsoleWrite(@CR & "Browser connected.")

While 1
If $RecvMode = 1 Then
    $FirefoxRecv = TCPRecv($FirefoxSocket, 1024 * 100)
    If $FirefoxRecv <> "" Then
        $Line = StringSplit($FirefoxRecv, @CR)
        $Host = StringTrimLeft($Line[2], 7)
        $IP = TCPNameToIP($Host)
        $ServerIndex = _ArraySearch($Server, $IP, 1)
        If $ServerIndex > -1 Then
            TCPSend($Socket[$ServerIndex], $FirefoxRecv)
            $Recv[$ServerIndex] = 1
        Else
            $NewSocket = TCPConnect($IP, 80)
            If @error Then MsgBox(16, "Error2", @error)
            ConsoleWrite(@CR & "Connected: " & $IP & " (" & $Host & ")")
            TCPSend($NewSocket, $FirefoxRecv)
            ConsoleWrite(@CR & "Send: [" & $IP & "]: " & StringLen($FirefoxRecv) & " bytes.")
            _ArrayAdd($Server, $IP)
            _ArrayAdd($Socket, $NewSocket)
            _ArrayAdd($Recv, 1)
            $RecvMode = 0
        EndIf
    EndIf
EndIf
    For $i = 1 To UBound($Server) - 1
        If $Recv[$i] = 1 Then
            $ServerRecv = TCPRecv($Socket[$i], 1024 ^ 2)
            If @error Then
            $Socket[$i] = - 1
                ConsoleWrite(@CR & "[" & $Socket[$i] & "] Socket close.")
            EndIf
            If $ServerRecv <> "" Then
                ConsoleWrite(@CR & "Recv: [" & $Server[$i] & "]: " & StringLen($ServerRecv) & " bytes")
                TCPSend($FirefoxSocket, $ServerRecv)
                $Recv[$i] = 0
                $RecvMode = 1
            EndIf
        EndIf
    Next


Do
$BreakLoop = 0
For $i = 1 to UBound($Socket) - 1
If $Socket[$i] = - 1 Then
    TCPCloseSocket($Server[$i])
                _ArrayDelete($Server, $i)
                _ArrayDelete($Socket, $i)
                _ArrayDelete($Recv, $i)
            $BreakLoop = 1
    ExitLoop
EndIf
Next
Until $BreakLoop = 0

WEnd

Can I fix something?

Edited by Adrian777
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...