Jump to content

AutoIt proxy-server


Recommended Posts

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=proxy.exe
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
TCPStartup()
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GUIEdit.au3>
#Include <Array.au3>
#Include <File.au3>
#include <WindowsConstants.au3>
#include <Base64.au3>

Opt("GUIOnEventMode" , 1)
Opt("TrayAutoPause",0)
Opt("TrayMenuMode",3)
Opt("TrayOnEventMode",1)

Global $ini = @ScriptDir & "\config.ini" 
Global $mainarray[30][10]
Global $proxysocket , $proxysocketssl
Global $GUISHOW = 1
Global $FUseProxy = False
Global $SProxyAuthCode = "" ; кодируем логин и пас

$mainarray[0][0] = "Hostport"
$mainarray[0][1] = "Clientsocket"
$mainarray[0][2] = "Hostsocket"
$mainarray[0][3] = "Request"
$mainarray[0][4] = "Hostname"
$mainarray[0][5] = "Timerhandle connection"
$mainarray[0][6] = "Blocked"
$mainarray[0][7] = "State"
$mainarray[0][8] = "connection type"
$mainarray[0][9] = "req count"



Global $gui = GUICreate("ProxyServer v0.2",700,700)
$DEBUG=GUICtrlCreateEdit("" , 5 , 30 , 500 ,670,BitOr($GUI_SS_DEFAULT_EDIT,$ES_READONLY))
GUICtrlSetLimit(-1 , 2000000000000000)
Global $button_debug_clear = GUICtrlCreateButton("Clear",5,5,90,20)
GUICtrlSetOnEvent($button_debug_clear,"_debug_clear")
GUICtrlCreateLabel("Listening IP:",515,40,70,20)
GUICtrlCreateLabel("Port HTTP:",515,60,70,20)
GUICtrlCreateLabel("Port HTTPS:",515,80,70,20)
Global $input_proxyip =  GUICtrlCreateInput("",590, 40,100,20)
Global $input_proxyport = GUICtrlCreateInput("",590,60,60,20)
Global $input_proxyportssl = GUICtrlCreateInput("",590,80,60,20)
Global $button_proxy_save = GUICtrlCreateButton("Save and Restart",515,140,180,20)
GUICtrlSetOnEvent($button_proxy_save,"_mainsocket_save")

Global $checkbox_debug = GUICtrlCreateCheckbox("Debugmode", 515 , 240, 120, 20)
Global $checkbox_debug_save = GUICtrlCreateCheckbox("Save Debug to file", 515 , 260, 120, 20)
Global $checkbox_debug_content = GUICtrlCreateCheckbox("Show traffic content", 515 , 280, 120, 20)
Global $button_debug_content = GUICtrlCreateButton("Binary to String",515,320,120,19)
GUICtrlSetOnEvent($button_debug_content,"_debug_binary")
Global $button_debug_array = GUICtrlCreateButton("show mainarray",515,340,120,19)
GUICtrlSetOnEvent(-1,"_show_array")


#Region ### START Koda GUI section ### 
Global $Proxy_Form = GUICreate("Settings", 209, 190, 192, 132)
Global $PF_IP = GUICtrlCreateInput("", 8, 24, 121, 24)
Global $PF_Port = GUICtrlCreateInput("", 136, 24, 57, 24)
Global $PF_Login = GUICtrlCreateInput("", 88, 65, 105, 24)
Global $PF_Pass = GUICtrlCreateInput("", 88, 96, 105, 24)
Global $PF_OK = GUICtrlCreateButton("ОК", 8, 152, 89, 25, 0)
GUICtrlSetOnEvent($PF_OK,"_close_proxy_settings")
Global $PF_Save = GUICtrlCreateButton("Сохранить", 104, 152, 89, 25, 0)
GUICtrlSetOnEvent($PF_Save,"_save_proxy_settings")
GUICtrlCreateLabel("Пароль", 16, 100, 54, 20)
GUICtrlCreateLabel("Имя", 16, 65, 33, 20)
GUICtrlCreateLabel("Адрес", 16, 3, 45, 20)
GUICtrlCreateLabel("Порт", 136, 3, 39, 20)
Global $PF_CheckboxUse = GUICtrlCreateCheckbox("Использовать", 16, 128, 137, 17)
GUISetState(@SW_HIDE, $Proxy_Form)
#EndRegion ### END Koda GUI section ###
GUISetOnEvent($GUI_EVENT_CLOSE,"_quit")

Global $tray_showgui = TrayCreateItem("Show gui")
Global $tray_showproxyform = TrayCreateItem("Настройки прокси")
TrayItemSetOnEvent($tray_showgui,"_gui_show")
TrayItemSetOnEvent($tray_showproxyform,"_proxy_settings")

GUISetOnEvent($GUI_EVENT_CLOSE,"_close_proxy_settings",$Proxy_Form )
GUISetState(@SW_SHOW,$gui)
_read_ini()



_mainsocket_create()

While 1
    $newclientsock  = TCPAccept($proxysocket)
    If $newclientsock <> -1 Then
        _save("] new HTTP clientconnection :" &_SocketToIP($newclientsock) &@CRLF) 
        _clientconnection_to_mainarray($newclientsock,80,"HTTP")
    EndIf
    $newclientsockssl   = TCPAccept($proxysocketssl)
    If $newclientsockssl <> -1 Then
        _save("] new HTTPS clientconnection :" &_SocketToIP($newclientsockssl) &@CRLF) 
        _clientconnection_to_mainarray($newclientsockssl,443,"HTTPS","start")
    EndIf
    ;--Recieving Req from Client
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] <> "" Then
            $request = TCPRecv($mainarray[$i][1],100000)
            If @error Then 
                
                _save("] Client " & $i & " closed connection after " & Floor(TimerDiff($mainarray[$i][5])) & " ms" & @CRLF)
                _mainarray_deleteclient($i)
            EndIf
            
            If $request <> "" Then
                _save("] Client " & $i & " requested" & @CRLF  & @CRLF & $request & @CRLF)
                $mainarray[$i][9] += 1 
                $mainarray[$i][3] = $request
                If GUICtrlRead($checkbox_debug_content) = 1 Then _save($request & @CRLF)
            EndIf   
        EndIf
    Next

    ;--connecting and sending Req to Host
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][3] <> "" Then
            If $mainarray[$i][2] = "" Then
                
                IF $FUseProxy Then
                    ;
                    $forward = _Get_Address($mainarray[$i][3])
                    $forward = $mainarray[$i][3] ;здесь можно добавить фильтр по домену
                    _save("]" & $i &" Отправляем запрос на прокси : " & $forward  & @CRLF)
                    ;
                Else
                    ;
                    $forward = _Get_Address($mainarray[$i][3]) ;здесь можно добавить фильтр по домену
                    _save("]" & $i &" Got Host Address : " & $forward  & @CRLF)
                    ;
                EndIf
       
                $mainarray[$i][6] = 0
                $mainarray[$i][4] = $forward
                If $forward = "0" Then
                    _Send_Response($mainarray[$i][1],400)
                    _save("]" & $i &" Host is 0 , Error 400" & @CRLF)
                Else
                        IF $FUseProxy Then
                            ;
                            $serverip   = GUICtrlRead($PF_IP)
                            $mainarray[$i][0]=GUICtrlRead($PF_Port)
                            ;
                        Else
                            ;
                            $serverip   = TCPNameToIP($forward)
                            ;
                        ENDIF

                        $serversock = TCPConnect($serverip,$mainarray[$i][0])
                        If $serversock <> -1 Then
                            _save("]" & $i &" Connected to Host: " & $forward & @CRLF)
                            $mainarray[$i][2] = $serversock
                            
                        Else
                            _save("]" & $i &" Not Connected to Host: " & $forward & @CRLF)
                            _Send_Response($mainarray[$i][1],400)
                            _mainarray_deleteclient($i)
                        EndIf
                EndIf   
            Else
                $forward = _Get_Address($mainarray[$i][3])
                If $forward <> $mainarray[$i][4] Then
                    _save("]" & $i &" Host Address changed from: " & $mainarray[$i][4] & " to: " & $forward & @CRLF)
                    $mainarray[$i][4] = $forward
                    TCPCloseSocket($mainarray[$i][2])
                    $mainarray[$i][2] = ""
                EndIf
            EndIf
            If $mainarray[$i][2] <> "" Then
                $prevlen = StringLen($mainarray[$i][3])
                $mainarray[$i][3] = _request_modify($mainarray[$i][3],$mainarray[$i][4])
                If $mainarray[$i][7] = "" Then 
                    ;$mainarray[$i][3] = _request_modify($mainarray[$i][3],$mainarray[$i][4])
                    TCPSend($mainarray[$i][2] , $mainarray[$i][3])
                        If Not @error Then
                            _save("]" & $i &" Sending HTTP to Host " & $mainarray[$i][4] & " successfull prevlen:" & $prevlen & " now:" & StringLen($mainarray[$i][3]) & @CRLF)
                            $mainarray[$i][3] = ""
                        Else
                            _save("]" & $i &" Error Sending HTTP to Host " & @CRLF)
                        EndIf
                EndIf
            EndIf
        EndIf
    Next
    ;--Waiting for Response from Host and Sending to client
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][2] <> "" Then
            $serverresponse     = TCPRecv($mainarray[$i][2],400000,1)   
            If @error Then 
                _mainarray_hostkillclient($i)
            EndIf
            If $serverresponse <> Binary("") Then
                If $mainarray[$i][6] = 0 Then 
                    _save("]" & $i &" Got Host response" & @CRLF)
                    If  $mainarray[$i][1] <> "" And $mainarray[$i][7] = "" Then
                        TCPSend($mainarray[$i][1],$serverresponse)
                        If Not @error Then
                            _save("]" & $i &" Sent data to Client " & @CRLF)

                            If GUICtrlRead($checkbox_debug_content) = 1 Then _save(BinaryToString($serverresponse) & @CRLF)
                        EndIf       
                    EndIf   
                ElseIf $mainarray[$i][6] = 1 Then 
                    TCPSend($mainarray[$i][1],"HTTP/1.1 " & "403")
                    _mainarray_hostkillclient($i)
                EndIf
            EndIf
        EndIf
    Next    
    ;---only for HTTPS
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] <> "" And $mainarray[$i][3] <> "" And $mainarray[$i][8] = "HTTPS" And $mainarray[$i][7] = "start" Then
            _Send_Response($mainarray[$i][1],200) ;,$data="")
            $mainarray[$i][3] = ""
            $mainarray[$i][7] = ""
        EndIf
    Next
    
WEnd    



Func _proxy_settings()
    ;
    ;_read_ini()
    GUISetState(@SW_SHOW, $Proxy_Form)
    ;
EndFunc

Func _close_proxy_settings()
    ;
    GUISetState(@SW_HIDE, $Proxy_Form)

    ;
EndFunc


Func _read_ini()
    ;
    _GUICtrlEdit_SetText($PF_Port, IniRead($ini, "PROXY", "PROXYPORT", ""))
    _GUICtrlEdit_SetText($PF_IP, IniRead($ini, "PROXY", "PROXYIP", ""))
    _GUICtrlEdit_SetText($PF_Login, IniRead($ini, "PROXY", "PROXYUSER", ""))
    _GUICtrlEdit_SetText($PF_Pass, IniRead($ini, "PROXY", "PROXYPASS", ""))
    IF IniRead($ini, "PROXY", "PROXYUSE", 0) = 1 Then
        ;
        GUICtrlSetState ($PF_CheckboxUse, $GUI_CHECKED)
        $FUseProxy=True
        $SProxyAuthCode=_Base64Encode(IniRead($ini, "PROXY", "PROXYUSER", "") & ":" & IniRead($ini, "PROXY", "PROXYPASS", ""))
        ;
    Else
        ;
        GUICtrlSetState ($PF_CheckboxUse, $GUI_UNCHECKED)
        $FUseProxy=False
        ;
    ENDIF
    ;
EndFunc

Func _save_proxy_settings()
    ;
    IniWrite($ini,"PROXY","PROXYPORT",GUICtrlRead($PF_Port))
    IniWrite($ini,"PROXY","PROXYIP",GUICtrlRead($PF_IP))
    IniWrite($ini,"PROXY","PROXYUSER",GUICtrlRead($PF_Login))
    IniWrite($ini,"PROXY","PROXYPASS",GUICtrlRead($PF_Pass ))

    ;
    IF GUICtrlRead($PF_CheckboxUse)=$GUI_CHECKED Then
        ;
        $FUseProxy=True
        IniWrite($ini,"PROXY","PROXYUSE",1)
        $SProxyAuthCode=_Base64Encode(GUICtrlRead($PF_Login) & ":" & GUICtrlRead($PF_Pass ))
        _mainsocket_create()
        ;
    Else
        ;
        $FUseProxy=False
        IniWrite($ini,"PROXY","PROXYUSE",0)
        _mainsocket_create()
        ;
    ENDIF
    ;
EndFunc

Func _show_array()
    _ArrayDisplay($mainarray)
EndFunc

Func _mainsocket_save()
    IniWrite($ini,"SYSTEM","PROXYPORT",GUICtrlRead($input_proxyport))
    IniWrite($ini,"SYSTEM","PROXYIP",GUICtrlRead($input_proxyip))
    IniWrite($ini,"SYSTEM","PROXYPORTSSL",GUICtrlRead($input_proxyportssl))
    _mainsocket_create()
EndFunc

Func _mainsocket_create()
    TCPShutdown()
    TCPStartup()
    $IP = IniRead($ini,"SYSTEM","PROXYIP","127.0.0.1")
    $PORT = IniRead($ini,"SYSTEM","PROXYPORT","8080")
    $PORTSSL = IniRead($ini,"SYSTEM","PROXYPORTSSL","8043")
    GUICtrlSetData($input_proxyip, $IP)
    GUICtrlSetData($input_proxyport,$PORT)
    GUICtrlSetData($input_proxyportssl,$PORTSSL)
    $proxysocket        = TCPListen($IP,$PORT)
    If @error Then 
        _GUICtrlEdit_AppendText($DEBUG , "] Error HTTP Proxy couldn't bind socket on IP :" & $IP & " Port :" & $PORT & @CRLF)
    Else
        _GUICtrlEdit_AppendText($DEBUG , "] HTTP Proxy listening on IP :" & $IP & " Port :" & $PORT & @CRLF)
    EndIf   
    $proxysocketssl     = TCPListen($IP,$PORTSSL)
    If @error Then 
        _GUICtrlEdit_AppendText($DEBUG , "] Error HTTPS Proxy couldn't bind socket on IP :" & $IP & " Port :" & $PORTSSL & @CRLF)
    Else
        _GUICtrlEdit_AppendText($DEBUG , "] HTTPS Proxy listening on IP :" & $IP & " Port :" & $PORTSSL & @CRLF)
    EndIf       
EndFunc


Func _Get_Address($text)
    If IsBinary($text) Then 
        $text = BinaryToString($text)
    EndIf
        $serversock=StringSplit($text , @CRLF,1)
        For $i=1 To $serversock[0]
            If StringLeft($serversock[$i],6)="Host: " Then
                Return StringTrimLeft($serversock[$i],6)
            EndIf
        Next
    Return 0        
EndFunc

Func _clientconnection_to_mainarray($sock,$port = 80,$type = "HTTP",$state = "")
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] = "" And $mainarray[$i][2] = "" Then
            $mainarray[$i][1] = $sock
            $mainarray[$i][0] = $port
            $mainarray[$i][5] = TimerInit()
            $mainarray[$i][7] = $state
            $mainarray[$i][8] = $type
            _save("] Added new client to mainarray , pos: " & $i & @CRLF)
            If $i + 5 > UBound($mainarray) Then ReDim $mainarray[UBound($mainarray)+5][10]
            Return
        EndIf   
    Next
EndFunc

Func _mainarray_deleteclient($line)
    TCPCloseSocket($mainarray[$line][1])
    $mainarray[$line][0] = ""
    $mainarray[$line][1] = ""
    $mainarray[$line][3] = ""
    $mainarray[$line][4] = ""
    $mainarray[$line][5] = ""
    $mainarray[$line][6] = ""
    $mainarray[$line][7] = ""
    $mainarray[$line][8] = ""
    $mainarray[$line][9] = ""
    If $mainarray[$line][2]<> "" Then
        TCPCloseSocket($mainarray[$line][2])
        $mainarray[$line][2] = ""
    EndIf
EndFunc

Func _mainarray_hostkillclient($line)
    _save("]" & $line &" Connection Closed by Host after " & Floor(TimerDiff($mainarray[$line][5])) & " ms" & @CRLF)
    $mainarray[$line][2] = ""
    $mainarray[$line][4] = ""
    $mainarray[$line][5] = ""
    If $mainarray[$line][1]<> "" Then
        TCPCloseSocket($mainarray[$line][1])
        _mainarray_deleteclient($line)
    EndIf   
EndFunc

Func _Send_Response($browsersock,$code,$data="")
    If $data="" Then
        $data=@CRLF & @CRLF 
    Else
        $data=@CRLF & $data & @CRLF & @CRLF 
    EndIf
    TCPSend($browsersock , "HTTP/1.0 " & $code & " Message" & $data)
EndFunc


Func _quit()
    TCPShutdown()
    Exit
EndFunc

Func _gui_show()
    If $GUISHOW = 0 Then
        Local $pw = InputBox("Enter Password","Enter Password","","*",150,130)
        If Not @error Then
            If $pw = "1234" Then 
                GUISetState(@SW_SHOW)
                $GUISHOW = 1
                Return
            EndIf   
        EndIf
    ElseIf $GUISHOW = 1 Then
        GUISetState(@SW_HIDE)
        $GUISHOW = 0
        Return      
    EndIf
EndFunc 

Func _save($text)
    If GUICtrlRead($checkbox_debug_save) = 1 Then
        $file = FileOpen(@ScriptDir & "\log\" & @Year & @MON & @MDAY & "-log.txt",9)
        FileWrite ($file,$text)
        FileClose($file)
    Endif
    If GUICtrlRead($checkbox_debug) = 1 Then _GUICtrlEdit_AppendText($DEBUG , $text)
Endfunc

Func _SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")
    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    $sockaddr = 0
    Return $aRet
EndFunc   ;==>SocketToIP

Func _debug_binary()
    Local $aSel = _GUICtrlEdit_GetSel($DEBUG)
    Local $text = StringMid(GUICtrlRead($DEBUG),$aSel[0]+1,$aSel[1]-$aSel[0])
    If $text <> "" Then 
        
    Else    
        $text = InputBox("Binary to String","Enter Binary Data") 
        
    EndIf
    If StringLower(StringLeft($text,2)) <> "0x" Then $text = "0x" & $text
    $tempfile = FileOpen(@ScriptDir & "\temp.txt",2)
    FileWrite($tempfile,BinaryToString($text))
    FileClose($tempfile)
    ShellExecute(@ScriptDir & "\temp.txt")
EndFunc 

Func _debug_clear()
    GUICtrlSetData($DEBUG,"")
EndFunc 



Func _request_modify($req,$host, $line)
Local $encoding = "Accept-Encoding: identity" ;deflate, gzip, compress,
Local $reqsplit
Local $modified_method = 0 , $modified_connection = 0 , $modified_encoding = 0
    ;

    If $req <> "" And Not IsBinary($req) Then
        $reqsplit = StringSplit($req,@CRLF,1)
        If IsArray($reqsplit) Then
            For $i = 1 to $reqsplit[0] - 1
                _save("] modifying :" & $i & " " & $reqsplit[$i])
                IF Not $FUseProxy Then ; если работаем без прокси
                    If StringLeft($reqsplit[$i],3) = "GET" Or StringLeft($reqsplit[$i],4) = "POST" Or StringLeft($reqsplit[$i],7) = "CONNECT" And $modified_method = 0 Then
                        $reqsplit[$i] = StringReplace($reqsplit[$i],$host,"")
                        $reqsplit[$i] = StringReplace($reqsplit[$i],"http://","")
                        $reqsplit[$i] = StringReplace($reqsplit[$i],":443","")
                        $modified_method = 1
                    EndIf
                    If StringInStr(Stringlower($reqsplit[$i]),"proxy-connection: keep-alive") And $modified_connection = 0 Then
                        $reqsplit[$i] = "Connection: keep-alive"
                        $modified_connection = 1
                    EndIf
                    If StringInStr(Stringlower($reqsplit[$i]),"accept-encoding") And $modified_encoding = 0 Then
                        $reqsplit[$i] = $encoding
                        $modified_encoding = 1
                    EndIf
                EndIf




                _save(" -> to :" & $reqsplit[$i] & @CRLF)
            Next
            $req = _ArrayToString($reqsplit, @CRLF , 1 , Ubound($reqsplit)-1)
            IF $FUseProxy Then  $req=StringReplace($req, @CRLF & @CRLF,  @CRLF & "Proxy-Authorization: Basic " & $SProxyAuthCode & @CRLF & @CRLF,-1) ; если работаем через прокси добавляем авторизацию
        EndIf
        If GUICtrlRead($checkbox_debug_content) = 1 Then
            _save("] Request modified to:" & @CRLF)
            _save($req &@CRLF)
        EndIf
    EndIf
    Return $req
EndFunc

I modified your code a bit. Now, it can work through another proxy.

<Base64.au3> lib is needed for proxy authorization line coding

There are may be some mistakes, I don't remember all functions which was changed.

It's only an idea.

Edited by Ka36ek
Link to comment
Share on other sites

So, HTTPS is working for me , you can try it.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=proxy.exe
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
TCPStartup()
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GUIEdit.au3>
#Include <Array.au3>
#Include <File.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode" , 1)
Opt("TrayAutoPause",0)
Opt("TrayMenuMode",3)
Opt("TrayOnEventMode",1)

Global $ini = @ScriptDir & "\config.ini" 
Global $mainarray[30][10]
Global $proxysocket , $proxysocketssl
Global $GUISHOW = 1

$mainarray[0][0] = "Hostport"
$mainarray[0][1] = "Clientsocket"
$mainarray[0][2] = "Hostsocket"
$mainarray[0][3] = "Request"
$mainarray[0][4] = "Hostname"
$mainarray[0][5] = "Timerhandle connection"
$mainarray[0][6] = "Blocked"
$mainarray[0][7] = "State"
$mainarray[0][8] = "connection type"
$mainarray[0][9] = "req count"



Global $gui = GUICreate("ProxyServer v0.2",700,700)
$DEBUG=GUICtrlCreateEdit("" , 5 , 30 , 500 ,670,BitOr($GUI_SS_DEFAULT_EDIT,$ES_READONLY))
GUICtrlSetLimit(-1 , 2000000000000000)
Global $button_debug_clear = GUICtrlCreateButton("Clear",5,5,90,20)
GUICtrlSetOnEvent($button_debug_clear,"_debug_clear")
GUICtrlCreateLabel("Listening IP:",515,40,70,20)
GUICtrlCreateLabel("Port HTTP:",515,60,70,20)
GUICtrlCreateLabel("Port HTTPS:",515,80,70,20)
Global $input_proxyip =  GUICtrlCreateInput("",590, 40,100,20)
Global $input_proxyport = GUICtrlCreateInput("",590,60,60,20)
Global $input_proxyportssl = GUICtrlCreateInput("",590,80,60,20)
Global $button_proxy_save = GUICtrlCreateButton("Save and Restart",515,140,180,20)
GUICtrlSetOnEvent($button_proxy_save,"_mainsocket_save")

Global $checkbox_debug = GUICtrlCreateCheckbox("Debugmode", 515 , 240, 120, 20)
Global $checkbox_debug_save = GUICtrlCreateCheckbox("Save Debug to file", 515 , 260, 120, 20)
Global $checkbox_debug_content = GUICtrlCreateCheckbox("Show traffic content", 515 , 280, 120, 20)
Global $button_debug_content = GUICtrlCreateButton("Binary to String",515,320,120,19)
GUICtrlSetOnEvent($button_debug_content,"_debug_binary")
Global $button_debug_array = GUICtrlCreateButton("show mainarray",515,340,120,19)
GUICtrlSetOnEvent(-1,"_show_array")


Global $tray_showgui = TrayCreateItem("Show gui")
TrayItemSetOnEvent($tray_showgui,"_gui_show")

GUISetOnEvent($GUI_EVENT_CLOSE,"_quit")
GUISetState(@SW_SHOW,$gui)

_mainsocket_create()

While 1
    $newclientsock  = TCPAccept($proxysocket)
    If $newclientsock <> -1 Then
        _save("] new HTTP clientconnection :" &_SocketToIP($newclientsock) &@CRLF) 
        _clientconnection_to_mainarray($newclientsock,80,"HTTP")
    EndIf
    $newclientsockssl   = TCPAccept($proxysocketssl)
    If $newclientsockssl <> -1 Then
        _save("] new HTTPS clientconnection :" &_SocketToIP($newclientsockssl) &@CRLF) 
        _clientconnection_to_mainarray($newclientsockssl,443,"HTTPS","start")
    EndIf
    ;--Recieving Req from Client
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] <> "" Then
            $request = TCPRecv($mainarray[$i][1],100000)
            If @error Then 
                
                _save("] Client " & $i & " closed connection after " & Floor(TimerDiff($mainarray[$i][5])) & " ms" & @CRLF)
                _mainarray_deleteclient($i)
            EndIf
            
            If $request <> "" Then
                _save("] Client " & $i & " requested" & @CRLF  & @CRLF & $request & @CRLF)
                $mainarray[$i][9] += 1 
                $mainarray[$i][3] = $request
                If GUICtrlRead($checkbox_debug_content) = 1 Then _save($request & @CRLF)
            EndIf   
        EndIf
    Next

    ;--connecting and sending Req to Host
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][3] <> "" Then
            If $mainarray[$i][2] = "" Then
                $forward = _Get_Address($mainarray[$i][3])
                _save("]" & $i &" Got Host Address : " & $forward  & @CRLF)         
                $mainarray[$i][6] = 0
                $mainarray[$i][4] = $forward
                If $forward = "0" Then
                    _Send_Response($mainarray[$i][1],400)
                    _save("]" & $i &" Host is 0 , Error 400" & @CRLF)
                Else
                        $serverip   = TCPNameToIP($forward)
                        $serversock = TCPConnect($serverip,$mainarray[$i][0])
                        If $serversock <> -1 Then
                            _save("]" & $i &" Connected to Host: " & $forward & @CRLF)
                            $mainarray[$i][2] = $serversock
                            
                        Else
                            _save("]" & $i &" Not Connected to Host: " & $forward & @CRLF)
                            _Send_Response($mainarray[$i][1],400)
                            _mainarray_deleteclient($i)
                        EndIf
                EndIf   
            Else
                $forward = _Get_Address($mainarray[$i][3])
                If $forward <> $mainarray[$i][4] Then
                    _save("]" & $i &" Host Address changed from: " & $mainarray[$i][4] & " to: " & $forward & @CRLF)
                    $mainarray[$i][4] = $forward
                    TCPCloseSocket($mainarray[$i][2])
                    $mainarray[$i][2] = ""
                EndIf
            EndIf
            If $mainarray[$i][2] <> "" Then
                $prevlen = StringLen($mainarray[$i][3])
                $mainarray[$i][3] = _request_modify($mainarray[$i][3],$mainarray[$i][4])
                If $mainarray[$i][7] = "" Then 
                    ;$mainarray[$i][3] = _request_modify($mainarray[$i][3],$mainarray[$i][4])
                    TCPSend($mainarray[$i][2] , $mainarray[$i][3])
                        If Not @error Then
                            _save("]" & $i &" Sending HTTP to Host " & $mainarray[$i][4] & " successfull prevlen:" & $prevlen & " now:" & StringLen($mainarray[$i][3]) & @CRLF)
                            $mainarray[$i][3] = ""
                        Else
                            _save("]" & $i &" Error Sending HTTP to Host " & @CRLF)
                        EndIf
                EndIf
            EndIf
        EndIf
    Next
    ;--Waiting for Response from Host and Sending to client
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][2] <> "" Then
            $serverresponse     = TCPRecv($mainarray[$i][2],400000,1)   
            If @error Then 
                _mainarray_hostkillclient($i)
            EndIf
            If $serverresponse <> Binary("") Then
                If $mainarray[$i][6] = 0 Then 
                    _save("]" & $i &" Got Host response" & @CRLF)
                    If  $mainarray[$i][1] <> "" And $mainarray[$i][7] = "" Then
                        TCPSend($mainarray[$i][1],$serverresponse)
                        If Not @error Then
                            _save("]" & $i &" Sent data to Client " & @CRLF)

                            If GUICtrlRead($checkbox_debug_content) = 1 Then _save(BinaryToString($serverresponse) & @CRLF)
                        EndIf       
                    EndIf   
                ElseIf $mainarray[$i][6] = 1 Then 
                    TCPSend($mainarray[$i][1],"HTTP/1.1 " & "403")
                    _mainarray_hostkillclient($i)
                EndIf
            EndIf
        EndIf
    Next    
    ;---only for HTTPS
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] <> "" And $mainarray[$i][3] <> "" And $mainarray[$i][8] = "HTTPS" And $mainarray[$i][7] = "start" Then
            _Send_Response($mainarray[$i][1],200) ;,$data="")
            $mainarray[$i][3] = ""
            $mainarray[$i][7] = ""
        EndIf
    Next
    
WEnd    

Func _show_array()
    _ArrayDisplay($mainarray)
EndFunc

Func _mainsocket_save()
    IniWrite($ini,"SYSTEM","PROXYPORT",GUICtrlRead($input_proxyport))
    IniWrite($ini,"SYSTEM","PROXYIP",GUICtrlRead($input_proxyip))
    IniWrite($ini,"SYSTEM","PROXYPORTSSL",GUICtrlRead($input_proxyportssl))
    _mainsocket_create()
EndFunc

Func _mainsocket_create()
    TCPShutdown()
    TCPStartup()
    $IP = IniRead($ini,"SYSTEM","PROXYIP","127.0.0.1")
    $PORT = IniRead($ini,"SYSTEM","PROXYPORT","8080")
    $PORTSSL = IniRead($ini,"SYSTEM","PROXYPORTSSL","8043")
    GUICtrlSetData($input_proxyip, $IP)
    GUICtrlSetData($input_proxyport,$PORT)
    GUICtrlSetData($input_proxyportssl,$PORTSSL)
    $proxysocket        = TCPListen($IP,$PORT)
    If @error Then 
        _GUICtrlEdit_AppendText($DEBUG , "] Error HTTP Proxy couldn't bind socket on IP :" & $IP & " Port :" & $PORT & @CRLF)
    Else
        _GUICtrlEdit_AppendText($DEBUG , "] HTTP Proxy listening on IP :" & $IP & " Port :" & $PORT & @CRLF)
    EndIf   
    $proxysocketssl     = TCPListen($IP,$PORTSSL)
    If @error Then 
        _GUICtrlEdit_AppendText($DEBUG , "] Error HTTPS Proxy couldn't bind socket on IP :" & $IP & " Port :" & $PORTSSL & @CRLF)
    Else
        _GUICtrlEdit_AppendText($DEBUG , "] HTTPS Proxy listening on IP :" & $IP & " Port :" & $PORTSSL & @CRLF)
    EndIf       
EndFunc


Func _Get_Address($text)
    If IsBinary($text) Then 
        $text = BinaryToString($text)
    EndIf
        $serversock=StringSplit($text , @CRLF,1)
        For $i=1 To $serversock[0]
            If StringLeft($serversock[$i],6)="Host: " Then
                Return StringTrimLeft($serversock[$i],6)
            EndIf
        Next
    Return 0        
EndFunc

Func _clientconnection_to_mainarray($sock,$port = 80,$type = "HTTP",$state = "")
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] = "" And $mainarray[$i][2] = "" Then
            $mainarray[$i][1] = $sock
            $mainarray[$i][0] = $port
            $mainarray[$i][5] = TimerInit()
            $mainarray[$i][7] = $state
            $mainarray[$i][8] = $type
            _save("] Added new client to mainarray , pos: " & $i & @CRLF)
            If $i + 5 > UBound($mainarray) Then ReDim $mainarray[UBound($mainarray)+5][10]
            Return
        EndIf   
    Next
EndFunc

Func _mainarray_deleteclient($line)
    TCPCloseSocket($mainarray[$line][1])
    $mainarray[$line][0] = ""
    $mainarray[$line][1] = ""
    $mainarray[$line][3] = ""
    $mainarray[$line][4] = ""
    $mainarray[$line][5] = ""
    $mainarray[$line][6] = ""
    $mainarray[$line][7] = ""
    $mainarray[$line][8] = ""
    $mainarray[$line][9] = ""
    If $mainarray[$line][2]<> "" Then
        TCPCloseSocket($mainarray[$line][2])
        $mainarray[$line][2] = ""
    EndIf
EndFunc

Func _mainarray_hostkillclient($line)
    _save("]" & $line &" Connection Closed by Host after " & Floor(TimerDiff($mainarray[$line][5])) & " ms" & @CRLF)
    $mainarray[$line][2] = ""
    $mainarray[$line][4] = ""
    $mainarray[$line][5] = ""
    If $mainarray[$line][1]<> "" Then
        TCPCloseSocket($mainarray[$line][1])
        _mainarray_deleteclient($line)
    EndIf   
EndFunc

Func _Send_Response($browsersock,$code,$data="")
    If $data="" Then
        $data=@CRLF & @CRLF 
    Else
        $data=@CRLF & $data & @CRLF & @CRLF 
    EndIf
    TCPSend($browsersock , "HTTP/1.0 " & $code & " Message" & $data)
EndFunc


Func _quit()
    TCPShutdown()
    Exit
EndFunc

Func _gui_show()
    If $GUISHOW = 0 Then
        Local $pw = InputBox("Enter Password","Enter Password","","*",150,130)
        If Not @error Then
            If $pw = "1234" Then 
                GUISetState(@SW_SHOW)
                $GUISHOW = 1
                Return
            EndIf   
        EndIf
    ElseIf $GUISHOW = 1 Then
        GUISetState(@SW_HIDE)
        $GUISHOW = 0
        Return      
    EndIf
EndFunc 

Func _save($text)
    If GUICtrlRead($checkbox_debug_save) = 1 Then
        $file = FileOpen(@ScriptDir & "\log\" & @Year & @MON & @MDAY & "-log.txt",9)
        FileWrite ($file,$text)
        FileClose($file)
    Endif
    If GUICtrlRead($checkbox_debug) = 1 Then _GUICtrlEdit_AppendText($DEBUG , $text)
Endfunc

Func _SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")
    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    $sockaddr = 0
    Return $aRet
EndFunc   ;==>SocketToIP

Func _debug_binary()
    Local $aSel = _GUICtrlEdit_GetSel($DEBUG)
    Local $text = StringMid(GUICtrlRead($DEBUG),$aSel[0]+1,$aSel[1]-$aSel[0])
    If $text <> "" Then 
        
    Else    
        $text = InputBox("Binary to String","Enter Binary Data") 
        
    EndIf
    If StringLower(StringLeft($text,2)) <> "0x" Then $text = "0x" & $text
    $tempfile = FileOpen(@ScriptDir & "\temp.txt",2)
    FileWrite($tempfile,BinaryToString($text))
    FileClose($tempfile)
    ShellExecute(@ScriptDir & "\temp.txt")
EndFunc 

Func _debug_clear()
    GUICtrlSetData($DEBUG,"")
EndFunc 

Func _request_modify($req,$host)
Local $encoding = "Accept-Encoding: identity" ;deflate, gzip, compress, 
Local $reqsplit
Local $modified_method = 0 , $modified_connection = 0 , $modified_encoding = 0
    If $req <> "" And Not IsBinary($req) Then
        $reqsplit = StringSplit($req,@CRLF,1)       
        If IsArray($reqsplit) Then
            For $i = 1 to $reqsplit[0] - 1
                _save("] modifying :" & $i & " " & $reqsplit[$i])
                If StringLeft($reqsplit[$i],3) = "GET" Or StringLeft($reqsplit[$i],4) = "POST" Or StringLeft($reqsplit[$i],7) = "CONNECT" And $modified_method = 0 Then
                        $reqsplit[$i] = StringReplace($reqsplit[$i],$host,"")
                        $reqsplit[$i] = StringReplace($reqsplit[$i],"http://","")
                        $reqsplit[$i] = StringReplace($reqsplit[$i],":443","")
                        $modified_method = 1
                EndIf 
                If StringInStr(Stringlower($reqsplit[$i]),"proxy-connection: keep-alive") And $modified_connection = 0 Then 
                    $reqsplit[$i] = "Connection: keep-alive"
                    $modified_connection = 1
                EndIf   
                If StringInStr(Stringlower($reqsplit[$i]),"accept-encoding") And $modified_encoding = 0 Then 
                    $reqsplit[$i] = $encoding
                    $modified_encoding = 1
                EndIf   
                _save(" -> to :" & $reqsplit[$i] & @CRLF)
            Next    
            $req = _ArrayToString($reqsplit, @CRLF , 1 , Ubound($reqsplit)-1)
        EndIf
        If GUICtrlRead($checkbox_debug_content) = 1 Then
            _save("] Request modified to:" & @CRLF)
            _save($req &@CRLF)
        EndIf
    EndIf
    Return $req
EndFunc

Enrico

please give me feedback if any host isn't working ( with hostname ) with proxy.

thanks

Wow :) This works perfect :(

I tested this from a computer in southern norway and used my computer as proxy(nothern norway)

Speed:

Ping - 185ms (Often 50-130ms)

Down: 1.6 Mb/s

Up: 0.3 Mb/s

My internet is 16 Mb/s down and 3-4 Mb/s up :)

http://www.speedtest.net/result/770901399.png

Edited by Chris86
Link to comment
Share on other sites

  • 4 months later...
  • 6 months later...

i managed to geet a universal http proxy going using the code available in this thread, tested on irc and http, works pretty damn good

and here's the code:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=c:\users\marshall\desktop\coolico.ico
#AutoIt3Wrapper_outfile=proxy.exe
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
TCPStartup()
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GUIEdit.au3>
#Include <Array.au3>
#Include <File.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode" , 1)
Opt("TrayAutoPause",0)
Opt("TrayMenuMode",3)
Opt("TrayOnEventMode",1)

Global $ini = @ScriptDir & "\config.ini"
Global $mainarray[1000][11]
Global $proxysocket , $proxysocketssl
Global $GUISHOW = 1
Global $z = 0
$mainarray[0][0] = "Hostport"
$mainarray[0][1] = "Clientsocket"
$mainarray[0][2] = "Hostsocket"
$mainarray[0][3] = "Request"
$mainarray[0][4] = "Hostname"
$mainarray[0][5] = "Timerhandle connection"
$mainarray[0][6] = "Blocked"
$mainarray[0][7] = "State"
$mainarray[0][8] = "connection type"
$mainarray[0][9] = "req count"
$mainarray[0][10] = "regular"



Global $gui = GUICreate("ProxyServer v0.2",700,700)
$DEBUG=GUICtrlCreateEdit("" , 5 , 30 , 500 ,670,BitOr($GUI_SS_DEFAULT_EDIT,$ES_READONLY))
GUICtrlSetLimit(-1 , 2000000000000000)
Global $button_debug_clear = GUICtrlCreateButton("Clear",5,5,90,20)
Global $button_blist = GUICtrlCreateButton("blacklist",100,5,90,20)
GUICtrlSetOnEvent($button_debug_clear,"_debug_clear")
GUICtrlCreateLabel("Listening IP:",515,40,70,20)
GUICtrlCreateLabel("Port HTTP:",515,60,70,20)
GUICtrlCreateLabel("Port HTTPS:",515,80,70,20)
Global $input_proxyip =  GUICtrlCreateInput("",590, 40,100,20)
Global $input_proxyport = GUICtrlCreateInput("",590,60,60,20)
Global $input_proxyportssl = GUICtrlCreateInput("",590,80,60,20)
Global $button_proxy_save = GUICtrlCreateButton("Save and Restart",515,140,180,20)
GUICtrlSetOnEvent($button_proxy_save,"_mainsocket_save")

Global $checkbox_debug = GUICtrlCreateCheckbox("Debugmode", 515 , 240, 120, 20)
Global $checkbox_debug_save = GUICtrlCreateCheckbox("Save Debug to file", 515 , 260, 120, 20)
Global $checkbox_debug_content = GUICtrlCreateCheckbox("Show traffic content", 515 , 280, 120, 20)
Global $button_debug_content = GUICtrlCreateButton("Binary to String",515,320,120,19)
GUICtrlSetOnEvent($button_debug_content,"_debug_binary")
GUICtrlSetOnEvent($button_blist,"_blist")
Global $button_debug_array = GUICtrlCreateButton("show mainarray",515,340,120,19)
GUICtrlSetOnEvent(-1,"_show_array")


Global $tray_showgui = TrayCreateItem("Show gui")
TrayItemSetOnEvent($tray_showgui,"_gui_show")

GUISetOnEvent($GUI_EVENT_CLOSE,"_quit")
GUISetState(@SW_SHOW,$gui)

_mainsocket_create()
Global $blarray[40]
Global $bn = ""
func _blist()
    $bword = InputBox("Enter blacklist word", "", "", "", -1, -1, 0, 0)
    $bn += 1
    $blarray[$bn] = $bword
EndFunc
While 1
    sleep(200)
    $newclientsock  = TCPAccept($proxysocket)
    If $newclientsock <> -1 Then
        _save("] new HTTP clientconnection :" &_SocketToIP($newclientsock) &@CRLF)
        _clientconnection_to_mainarray($newclientsock,80,"HTTP")
    EndIf
    $newclientsockssl   = TCPAccept($proxysocketssl)
    If $newclientsockssl <> -1 Then
        _save("] new HTTPS clientconnection :" &_SocketToIP($newclientsockssl) &@CRLF)
        _clientconnection_to_mainarray($newclientsockssl,443,"HTTPS","start")
    EndIf
    ;--Recieving Req from Client
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] <> "" Then
            $request = TCPRecv($mainarray[$i][1],100000)
            If @error Then

                _save("] Client " & $i & " closed connection after " & Floor(TimerDiff($mainarray[$i][5])) & " ms" & @CRLF)
                _mainarray_deleteclient($i)
            EndIf

            If $request <> "" Then
                _save("] Client " & $i & " requested" & @CRLF  & @CRLF & $request & @CRLF)
                ;msgbox(0,"",$request)
                $mainarray[$i][9] += 1
                $mainarray[$i][3] = $request
                If GUICtrlRead($checkbox_debug_content) = 1 Then _save($request & @CRLF)
            EndIf
        EndIf
    Next

    ;--connecting and sending Req to Host
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][3] <> "" Then
            If $mainarray[$i][2] = "" Then
                $forward = _Get_Address($mainarray[$i][3])
                _save("]" & $i &" Got Host Address : " & $forward  & @CRLF)
                $mainarray[$i][6] = 0
                $mainarray[$i][4] = $forward
                If $forward = "0" Then
                    ;_Send_Response($mainarray[$i][1],400)
                    _save("]" & $i &" Host is 0 , Error 400" & @CRLF)
                Else
                        $serverip   = TCPNameToIP($forward)
                        $serversock = TCPConnect($serverip,$mainarray[$i][0])
                        If $serversock <> -1 Then
                            _save("]" & $i &" Connected to Host: " & $forward & @CRLF)
                            $mainarray[$i][2] = $serversock

                        Else
                            _save("]" & $i &" Not Connected to Host: " & $forward & @CRLF)
                            _Send_Response($mainarray[$i][1],400)
                            _mainarray_deleteclient($i)
                        EndIf
                EndIf
            Else
                $forward = _Get_Address($mainarray[$i][3])
                If $forward <> $mainarray[$i][4] Then
                    _save("]" & $i &" Host Address changed from: " & $mainarray[$i][4] & " to: " & $forward & @CRLF)
                    $mainarray[$i][4] = $forward
                    TCPCloseSocket($mainarray[$i][2])
                    $mainarray[$i][2] = ""
                EndIf
            EndIf
            If $mainarray[$i][2] <> "" Then
                $prevlen = StringLen($mainarray[$i][3])
                $mainarray[$i][3] = _request_modify($mainarray[$i][3],$mainarray[$i][4])
                If $mainarray[$i][7] = "" Then
                    ;$mainarray[$i][3] = _request_modify($mainarray[$i][3],$mainarray[$i][4])
                    TCPSend($mainarray[$i][2] , $mainarray[$i][3])
                        If Not @error Then
                            _save("]" & $i &" Sending HTTP to Host " & $mainarray[$i][4] & " successfull prevlen:" & $prevlen & " now:" & StringLen($mainarray[$i][3]) & @CRLF)
                            $mainarray[$i][3] = ""
                        Else
                            _save("]" & $i &" Error Sending HTTP to Host " & @CRLF)
                        EndIf
                EndIf
            EndIf
        EndIf
    Next
    ;--Waiting for Response from Host and Sending to client
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][2] <> "" Then
            $serverresponse     = TCPRecv($mainarray[$i][2],400000,1)
            If @error Then
                _mainarray_hostkillclient($i)
            EndIf
            If $serverresponse <> Binary("") Then
                If $mainarray[$i][6] = 0 Then
                    _save("]" & $i &" Got Host response" & @CRLF)
                    If  $mainarray[$i][1] <> "" And $mainarray[$i][7] = "" Then
                        TCPSend($mainarray[$i][1],$serverresponse)
                        If Not @error Then
                            _save("]" & $i &" Sent data to Client " & @CRLF)

                            If GUICtrlRead($checkbox_debug_content) = 1 Then _save(BinaryToString($serverresponse) & @CRLF)
                        EndIf
                    EndIf
                ElseIf $mainarray[$i][6] = 1 Then
                    TCPSend($mainarray[$i][1],"HTTP/1.1 " & "403")
                    _mainarray_hostkillclient($i)
                EndIf
            EndIf
        EndIf
    Next
    ;---only for HTTPS
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] <> "" And $mainarray[$i][3] <> "" And $mainarray[$i][8] = "HTTPS" And $mainarray[$i][7] = "start" Then
            _Send_Response($mainarray[$i][1],200) ;,$data="")
            $mainarray[$i][3] = ""
            $mainarray[$i][7] = ""
        EndIf
    Next

WEnd

Func _show_array()
    _ArrayDisplay($mainarray)
EndFunc

Func _mainsocket_save()
    IniWrite($ini,"SYSTEM","PROXYPORT",GUICtrlRead($input_proxyport))
    IniWrite($ini,"SYSTEM","PROXYIP",GUICtrlRead($input_proxyip))
    IniWrite($ini,"SYSTEM","PROXYPORTSSL",GUICtrlRead($input_proxyportssl))
    _mainsocket_create()
EndFunc

Func _mainsocket_create()
    TCPShutdown()
    TCPStartup()
    $IP = @IPaddress1
    $PORT = IniRead($ini,"SYSTEM","PROXYPORT","8080")
    $PORTSSL = IniRead($ini,"SYSTEM","PROXYPORTSSL","8043")
    GUICtrlSetData($input_proxyip, $IP)
    GUICtrlSetData($input_proxyport,$PORT)
    GUICtrlSetData($input_proxyportssl,$PORTSSL)
    $proxysocket        = TCPListen(@IPaddress1,$port)
    clipput(@ipaddress1)
    If @error Then
        _GUICtrlEdit_AppendText($DEBUG , "] Error HTTP Proxy couldn't bind socket on IP :" & $IP & " Port :" & $PORT & @CRLF)
    Else
        _GUICtrlEdit_AppendText($DEBUG , "] HTTP Proxy listening on IP :" & $IP & " Port :" & $PORT & @CRLF)
    EndIf
    $proxysocketssl     = TCPListen(@IPAddress1,$PORTSSL)
    If @error Then
        _GUICtrlEdit_AppendText($DEBUG , "] Error HTTPS Proxy couldn't bind socket on IP :" & $IP & " Port :" & $PORTSSL & @CRLF)
    Else
        _GUICtrlEdit_AppendText($DEBUG , "] HTTPS Proxy listening on IP :" & $IP & " Port :" & $PORTSSL & @CRLF)
    EndIf
EndFunc


Func _Get_Address($text)
    $z = 0
    If IsBinary($text) Then
        $text = BinaryToString($text)
    EndIf
    $fcheck = StringSplit($text, " ")
    if $fcheck[1] = "Connect" Then
        $2 = stringsplit($fcheck[2], ":")
        For $b = 1 To UBound($mainarray) -1
            Tcpsend($mainarray[$b][1], "HTTP/1.0 200 Connection established" & @CRLF)
            Tcpsend($mainarray[$b][1], "Proxy-agent: niggerproxy 1.0" & @CRLF)
            $mainarray[$b][0] = $2[2]
            $mainarray[$b][10] = $mainarray[$b][1]
        Next
        Return $2[1]
        $z = 1
    EndIf
    if $z <> 1 Then
        $serversock=StringSplit($text , @CRLF,1)
        For $i=1 To $serversock[0]
            If StringLeft($serversock[$i],6)="Host: " Then
                Return StringTrimLeft($serversock[$i],6)
            EndIf
        Next
    EndIf
EndFunc

Func _clientconnection_to_mainarray($sock,$port = 80,$type = "HTTP",$state = "")
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] = "" And $mainarray[$i][2] = "" Then
            $mainarray[$i][1] = $sock
            $mainarray[$i][0] = $port
            $mainarray[$i][5] = TimerInit()
            $mainarray[$i][7] = $state
            $mainarray[$i][8] = $type
            _save("] Added new client to mainarray , pos: " & $i & @CRLF)
            If $i + 5 > UBound($mainarray) Then ReDim $mainarray[UBound($mainarray)+5][10]
            Return
        EndIf
    Next
EndFunc

Func _mainarray_deleteclient($line)
    TCPCloseSocket($mainarray[$line][1])
    $mainarray[$line][0] = ""
    $mainarray[$line][1] = ""
    $mainarray[$line][3] = ""
    $mainarray[$line][4] = ""
    $mainarray[$line][5] = ""
    $mainarray[$line][6] = ""
    $mainarray[$line][7] = ""
    $mainarray[$line][8] = ""
    $mainarray[$line][9] = ""
    If $mainarray[$line][2]<> "" Then
        TCPCloseSocket($mainarray[$line][2])
        $mainarray[$line][2] = ""
    EndIf
EndFunc

Func _mainarray_hostkillclient($line)
    _save("]" & $line &" Connection Closed by Host after " & Floor(TimerDiff($mainarray[$line][5])) & " ms" & @CRLF)
    $mainarray[$line][2] = ""
    $mainarray[$line][4] = ""
    $mainarray[$line][5] = ""
    If $mainarray[$line][1]<> "" Then
        TCPCloseSocket($mainarray[$line][1])
        _mainarray_deleteclient($line)
    EndIf
EndFunc

Func _Send_Response($browsersock,$code,$data="")
    For $x = 1 To UBound($mainarray) -1
        if $mainarray[$x][10] = $browsersock Then
            $dont = 1
        EndIf
    Next
    if $dont = 1 Then
    If $data="" Then
        $data=@CRLF & @CRLF
    Else
        $data=@CRLF & $data & @CRLF & @CRLF
    EndIf
    TCPSend($browsersock , $data)
    EndIf
EndFunc


Func _quit()
    TCPShutdown()
    Exit
EndFunc

Func _gui_show()
    If $GUISHOW = 0 Then
        Local $pw = InputBox("Enter Password","Enter Password","","*",150,130)
        If Not @error Then
            If $pw = "1234" Then
                GUISetState(@SW_SHOW)
                $GUISHOW = 1
                Return
            EndIf
        EndIf
    ElseIf $GUISHOW = 1 Then
        GUISetState(@SW_HIDE)
        $GUISHOW = 0
        Return
    EndIf
EndFunc

Func _save($text)
    ;msgbox(0,"",$text)
    Global $go = 1
    If GUICtrlRead($checkbox_debug_save) = 1 Then
        $spstr = stringsplit($text, " ")
        if $spstr[0] > 1 Then
         global $2 = $spstr[2]
    Else
        global $2 = 0
    EndIf
            if stringinstr($2, ".html") = 0 AND stringinstr($2, ".js") = 0 AND stringinstr($2, ".php") = 0 Then
                global $go = 0
            EndIf
        if $spstr[1] = "GET" OR $spstr[1] = "POST" AND $go = 1 Then
        $file = FileOpen(@ScriptDir & "\log\" & @Year & @MON & @MDAY & "-log.txt",9)
        FileWrite ($file,$text)
        FileClose($file)
        If GUICtrlRead($checkbox_debug) = 1 Then
            _GUICtrlEdit_AppendText($DEBUG , $text)
        EndIf
    EndIf
    EndIf
Endfunc

Func _SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")
    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    $sockaddr = 0
    Return $aRet
EndFunc   ;==>SocketToIP

Func _debug_binary()
    Local $aSel = _GUICtrlEdit_GetSel($DEBUG)
    Local $text = StringMid(GUICtrlRead($DEBUG),$aSel[0]+1,$aSel[1]-$aSel[0])
    If $text <> "" Then

    Else
        $text = InputBox("Binary to String","Enter Binary Data")

    EndIf
    If StringLower(StringLeft($text,2)) <> "0x" Then $text = "0x" & $text
    $tempfile = FileOpen(@ScriptDir & "\temp.txt",2)
    FileWrite($tempfile,BinaryToString($text))
    FileClose($tempfile)
    ShellExecute(@ScriptDir & "\temp.txt")
EndFunc

Func _debug_clear()
    GUICtrlSetData($DEBUG,"")
EndFunc

Func _request_modify($req,$host)
global $text = $req
Local $encoding = "Accept-Encoding: identity" ;deflate, gzip, compress,
Local $reqsplit
Local $modified_method = 0 , $modified_connection = 0 , $modified_encoding = 0
    If $req <> "" And Not IsBinary($req) Then
        $reqsplit = StringSplit($req,@CRLF,1)
      If IsArray($reqsplit) Then
            For $i = 1 to $reqsplit[0] - 1
                _save("] modifying :" & $i & " " & $reqsplit[$i])
                If StringLeft($reqsplit[$i],3) = "GET" Or StringLeft($reqsplit[$i],4) = "POST" And $modified_method = 0 Then
                        $reqsplit[$i] = StringReplace($reqsplit[$i],$host,"")
                        $reqsplit[$i] = StringReplace($reqsplit[$i],"http://","")
                        $reqsplit[$i] = StringReplace($reqsplit[$i],":443","")
                        $modified_method = 1
                EndIf
                If StringInStr(Stringlower($reqsplit[$i]),"proxy-connection: keep-alive") And $modified_connection = 0 Then
                    $reqsplit[$i] = "Connection: keep-alive"
                    $modified_connection = 1
                EndIf
                If StringInStr(Stringlower($reqsplit[$i]),"accept-encoding") And $modified_encoding = 0 Then
                    $reqsplit[$i] = $encoding
                    $modified_encoding = 1
                EndIf
                _save(" -> to :" & $reqsplit[$i] & @CRLF)
            Next
            $req = _ArrayToString($reqsplit, @CRLF , 1 , Ubound($reqsplit)-1)
        EndIf
        If GUICtrlRead($checkbox_debug_content) = 1 Then
            _save("] Request modified to:" & @CRLF)
            _save($req &@CRLF)
        EndIf
    EndIf
    For $z = 1 To UBound($mainarray) -1
    if $mainarray[$z][10] = $mainarray[$z][1] Then
     $con = 1
 Else
     $con = 0
 EndIf
 Next
   if $con = 0 Then
    Return $req
Else
    Return $text
EndIf
EndFunc

yes it could use some work, but this universal proxy does its job. anyone who wants to edit and make it neater etc can have at it.

Edited by lionfaggot
Link to comment
Share on other sites

  • 2 months later...
  • 3 months later...

Great Program, works fine. It has a small problem in Func _Send_Response : The $dont variable is evaluated without being initialized:

Func _Send_Response($browsersock,$code,$data="")

For $x = 1 To UBound($mainarray) -1

if $mainarray[$x][10] = $browsersock Then

$dont = 1

EndIf

Next

if $dont = 1 Then

If $data="" Then

$data=@CRLF & @CRLF

Else

$data=@CRLF & $data & @CRLF & @CRLF

EndIf

TCPSend($browsersock , $data)

EndIf

EndFunc

I inserted the initialization of the variable $dont.

Func _Send_Response($browsersock,$code,$data="")

$dont=0

For $x = 1 To UBound($mainarray) -1

if $mainarray[$x][10] = $browsersock Then

$dont = 1

EndIf

Next

if $dont = 1 Then

If $data="" Then

$data=@CRLF & @CRLF

Else

$data=@CRLF & $data & @CRLF & @CRLF

EndIf

TCPSend($browsersock , $data)

EndIf

EndFunc

Thanks

Link to comment
Share on other sites

  • 3 years later...

So, HTTPS is working for me , you can try it.

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=proxy.exe
#AutoIt3Wrapper_Compression=4
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
TCPStartup()
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <GUIEdit.au3>
#Include <Array.au3>
#Include <File.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode" , 1)
Opt("TrayAutoPause",0)
Opt("TrayMenuMode",3)
Opt("TrayOnEventMode",1)

Global $ini = @ScriptDir & "\config.ini" 
Global $mainarray[30][10]
Global $proxysocket , $proxysocketssl
Global $GUISHOW = 1

$mainarray[0][0] = "Hostport"
$mainarray[0][1] = "Clientsocket"
$mainarray[0][2] = "Hostsocket"
$mainarray[0][3] = "Request"
$mainarray[0][4] = "Hostname"
$mainarray[0][5] = "Timerhandle connection"
$mainarray[0][6] = "Blocked"
$mainarray[0][7] = "State"
$mainarray[0][8] = "connection type"
$mainarray[0][9] = "req count"



Global $gui = GUICreate("ProxyServer v0.2",700,700)
$DEBUG=GUICtrlCreateEdit("" , 5 , 30 , 500 ,670,BitOr($GUI_SS_DEFAULT_EDIT,$ES_READONLY))
GUICtrlSetLimit(-1 , 2000000000000000)
Global $button_debug_clear = GUICtrlCreateButton("Clear",5,5,90,20)
GUICtrlSetOnEvent($button_debug_clear,"_debug_clear")
GUICtrlCreateLabel("Listening IP:",515,40,70,20)
GUICtrlCreateLabel("Port HTTP:",515,60,70,20)
GUICtrlCreateLabel("Port HTTPS:",515,80,70,20)
Global $input_proxyip =  GUICtrlCreateInput("",590, 40,100,20)
Global $input_proxyport = GUICtrlCreateInput("",590,60,60,20)
Global $input_proxyportssl = GUICtrlCreateInput("",590,80,60,20)
Global $button_proxy_save = GUICtrlCreateButton("Save and Restart",515,140,180,20)
GUICtrlSetOnEvent($button_proxy_save,"_mainsocket_save")

Global $checkbox_debug = GUICtrlCreateCheckbox("Debugmode", 515 , 240, 120, 20)
Global $checkbox_debug_save = GUICtrlCreateCheckbox("Save Debug to file", 515 , 260, 120, 20)
Global $checkbox_debug_content = GUICtrlCreateCheckbox("Show traffic content", 515 , 280, 120, 20)
Global $button_debug_content = GUICtrlCreateButton("Binary to String",515,320,120,19)
GUICtrlSetOnEvent($button_debug_content,"_debug_binary")
Global $button_debug_array = GUICtrlCreateButton("show mainarray",515,340,120,19)
GUICtrlSetOnEvent(-1,"_show_array")


Global $tray_showgui = TrayCreateItem("Show gui")
TrayItemSetOnEvent($tray_showgui,"_gui_show")

GUISetOnEvent($GUI_EVENT_CLOSE,"_quit")
GUISetState(@SW_SHOW,$gui)

_mainsocket_create()

While 1
    $newclientsock  = TCPAccept($proxysocket)
    If $newclientsock <> -1 Then
        _save("] new HTTP clientconnection :" &_SocketToIP($newclientsock) &@CRLF) 
        _clientconnection_to_mainarray($newclientsock,80,"HTTP")
    EndIf
    $newclientsockssl   = TCPAccept($proxysocketssl)
    If $newclientsockssl <> -1 Then
        _save("] new HTTPS clientconnection :" &_SocketToIP($newclientsockssl) &@CRLF) 
        _clientconnection_to_mainarray($newclientsockssl,443,"HTTPS","start")
    EndIf
    ;--Recieving Req from Client
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] <> "" Then
            $request = TCPRecv($mainarray[$i][1],100000)
            If @error Then 
                
                _save("] Client " & $i & " closed connection after " & Floor(TimerDiff($mainarray[$i][5])) & " ms" & @CRLF)
                _mainarray_deleteclient($i)
            EndIf
            
            If $request <> "" Then
                _save("] Client " & $i & " requested" & @CRLF  & @CRLF & $request & @CRLF)
                $mainarray[$i][9] += 1 
                $mainarray[$i][3] = $request
                If GUICtrlRead($checkbox_debug_content) = 1 Then _save($request & @CRLF)
            EndIf   
        EndIf
    Next

    ;--connecting and sending Req to Host
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][3] <> "" Then
            If $mainarray[$i][2] = "" Then
                $forward = _Get_Address($mainarray[$i][3])
                _save("]" & $i &" Got Host Address : " & $forward  & @CRLF)         
                $mainarray[$i][6] = 0
                $mainarray[$i][4] = $forward
                If $forward = "0" Then
                    _Send_Response($mainarray[$i][1],400)
                    _save("]" & $i &" Host is 0 , Error 400" & @CRLF)
                Else
                        $serverip   = TCPNameToIP($forward)
                        $serversock = TCPConnect($serverip,$mainarray[$i][0])
                        If $serversock <> -1 Then
                            _save("]" & $i &" Connected to Host: " & $forward & @CRLF)
                            $mainarray[$i][2] = $serversock
                            
                        Else
                            _save("]" & $i &" Not Connected to Host: " & $forward & @CRLF)
                            _Send_Response($mainarray[$i][1],400)
                            _mainarray_deleteclient($i)
                        EndIf
                EndIf   
            Else
                $forward = _Get_Address($mainarray[$i][3])
                If $forward <> $mainarray[$i][4] Then
                    _save("]" & $i &" Host Address changed from: " & $mainarray[$i][4] & " to: " & $forward & @CRLF)
                    $mainarray[$i][4] = $forward
                    TCPCloseSocket($mainarray[$i][2])
                    $mainarray[$i][2] = ""
                EndIf
            EndIf
            If $mainarray[$i][2] <> "" Then
                $prevlen = StringLen($mainarray[$i][3])
                $mainarray[$i][3] = _request_modify($mainarray[$i][3],$mainarray[$i][4])
                If $mainarray[$i][7] = "" Then 
                    ;$mainarray[$i][3] = _request_modify($mainarray[$i][3],$mainarray[$i][4])
                    TCPSend($mainarray[$i][2] , $mainarray[$i][3])
                        If Not @error Then
                            _save("]" & $i &" Sending HTTP to Host " & $mainarray[$i][4] & " successfull prevlen:" & $prevlen & " now:" & StringLen($mainarray[$i][3]) & @CRLF)
                            $mainarray[$i][3] = ""
                        Else
                            _save("]" & $i &" Error Sending HTTP to Host " & @CRLF)
                        EndIf
                EndIf
            EndIf
        EndIf
    Next
    ;--Waiting for Response from Host and Sending to client
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][2] <> "" Then
            $serverresponse     = TCPRecv($mainarray[$i][2],400000,1)   
            If @error Then 
                _mainarray_hostkillclient($i)
            EndIf
            If $serverresponse <> Binary("") Then
                If $mainarray[$i][6] = 0 Then 
                    _save("]" & $i &" Got Host response" & @CRLF)
                    If  $mainarray[$i][1] <> "" And $mainarray[$i][7] = "" Then
                        TCPSend($mainarray[$i][1],$serverresponse)
                        If Not @error Then
                            _save("]" & $i &" Sent data to Client " & @CRLF)

                            If GUICtrlRead($checkbox_debug_content) = 1 Then _save(BinaryToString($serverresponse) & @CRLF)
                        EndIf       
                    EndIf   
                ElseIf $mainarray[$i][6] = 1 Then 
                    TCPSend($mainarray[$i][1],"HTTP/1.1 " & "403")
                    _mainarray_hostkillclient($i)
                EndIf
            EndIf
        EndIf
    Next    
    ;---only for HTTPS
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] <> "" And $mainarray[$i][3] <> "" And $mainarray[$i][8] = "HTTPS" And $mainarray[$i][7] = "start" Then
            _Send_Response($mainarray[$i][1],200) ;,$data="")
            $mainarray[$i][3] = ""
            $mainarray[$i][7] = ""
        EndIf
    Next
    
WEnd    

Func _show_array()
    _ArrayDisplay($mainarray)
EndFunc

Func _mainsocket_save()
    IniWrite($ini,"SYSTEM","PROXYPORT",GUICtrlRead($input_proxyport))
    IniWrite($ini,"SYSTEM","PROXYIP",GUICtrlRead($input_proxyip))
    IniWrite($ini,"SYSTEM","PROXYPORTSSL",GUICtrlRead($input_proxyportssl))
    _mainsocket_create()
EndFunc

Func _mainsocket_create()
    TCPShutdown()
    TCPStartup()
    $IP = IniRead($ini,"SYSTEM","PROXYIP","127.0.0.1")
    $PORT = IniRead($ini,"SYSTEM","PROXYPORT","8080")
    $PORTSSL = IniRead($ini,"SYSTEM","PROXYPORTSSL","8043")
    GUICtrlSetData($input_proxyip, $IP)
    GUICtrlSetData($input_proxyport,$PORT)
    GUICtrlSetData($input_proxyportssl,$PORTSSL)
    $proxysocket        = TCPListen($IP,$PORT)
    If @error Then 
        _GUICtrlEdit_AppendText($DEBUG , "] Error HTTP Proxy couldn't bind socket on IP :" & $IP & " Port :" & $PORT & @CRLF)
    Else
        _GUICtrlEdit_AppendText($DEBUG , "] HTTP Proxy listening on IP :" & $IP & " Port :" & $PORT & @CRLF)
    EndIf   
    $proxysocketssl     = TCPListen($IP,$PORTSSL)
    If @error Then 
        _GUICtrlEdit_AppendText($DEBUG , "] Error HTTPS Proxy couldn't bind socket on IP :" & $IP & " Port :" & $PORTSSL & @CRLF)
    Else
        _GUICtrlEdit_AppendText($DEBUG , "] HTTPS Proxy listening on IP :" & $IP & " Port :" & $PORTSSL & @CRLF)
    EndIf       
EndFunc


Func _Get_Address($text)
    If IsBinary($text) Then 
        $text = BinaryToString($text)
    EndIf
        $serversock=StringSplit($text , @CRLF,1)
        For $i=1 To $serversock[0]
            If StringLeft($serversock[$i],6)="Host: " Then
                Return StringTrimLeft($serversock[$i],6)
            EndIf
        Next
    Return 0        
EndFunc

Func _clientconnection_to_mainarray($sock,$port = 80,$type = "HTTP",$state = "")
    For $i = 1 To UBound($mainarray) -1
        If $mainarray[$i][1] = "" And $mainarray[$i][2] = "" Then
            $mainarray[$i][1] = $sock
            $mainarray[$i][0] = $port
            $mainarray[$i][5] = TimerInit()
            $mainarray[$i][7] = $state
            $mainarray[$i][8] = $type
            _save("] Added new client to mainarray , pos: " & $i & @CRLF)
            If $i + 5 > UBound($mainarray) Then ReDim $mainarray[UBound($mainarray)+5][10]
            Return
        EndIf   
    Next
EndFunc

Func _mainarray_deleteclient($line)
    TCPCloseSocket($mainarray[$line][1])
    $mainarray[$line][0] = ""
    $mainarray[$line][1] = ""
    $mainarray[$line][3] = ""
    $mainarray[$line][4] = ""
    $mainarray[$line][5] = ""
    $mainarray[$line][6] = ""
    $mainarray[$line][7] = ""
    $mainarray[$line][8] = ""
    $mainarray[$line][9] = ""
    If $mainarray[$line][2]<> "" Then
        TCPCloseSocket($mainarray[$line][2])
        $mainarray[$line][2] = ""
    EndIf
EndFunc

Func _mainarray_hostkillclient($line)
    _save("]" & $line &" Connection Closed by Host after " & Floor(TimerDiff($mainarray[$line][5])) & " ms" & @CRLF)
    $mainarray[$line][2] = ""
    $mainarray[$line][4] = ""
    $mainarray[$line][5] = ""
    If $mainarray[$line][1]<> "" Then
        TCPCloseSocket($mainarray[$line][1])
        _mainarray_deleteclient($line)
    EndIf   
EndFunc

Func _Send_Response($browsersock,$code,$data="")
    If $data="" Then
        $data=@CRLF & @CRLF 
    Else
        $data=@CRLF & $data & @CRLF & @CRLF 
    EndIf
    TCPSend($browsersock , "HTTP/1.0 " & $code & " Message" & $data)
EndFunc


Func _quit()
    TCPShutdown()
    Exit
EndFunc

Func _gui_show()
    If $GUISHOW = 0 Then
        Local $pw = InputBox("Enter Password","Enter Password","","*",150,130)
        If Not @error Then
            If $pw = "1234" Then 
                GUISetState(@SW_SHOW)
                $GUISHOW = 1
                Return
            EndIf   
        EndIf
    ElseIf $GUISHOW = 1 Then
        GUISetState(@SW_HIDE)
        $GUISHOW = 0
        Return      
    EndIf
EndFunc 

Func _save($text)
    If GUICtrlRead($checkbox_debug_save) = 1 Then
        $file = FileOpen(@ScriptDir & "\log\" & @Year & @MON & @MDAY & "-log.txt",9)
        FileWrite ($file,$text)
        FileClose($file)
    Endif
    If GUICtrlRead($checkbox_debug) = 1 Then _GUICtrlEdit_AppendText($DEBUG , $text)
Endfunc

Func _SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")
    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    $sockaddr = 0
    Return $aRet
EndFunc   ;==>SocketToIP

Func _debug_binary()
    Local $aSel = _GUICtrlEdit_GetSel($DEBUG)
    Local $text = StringMid(GUICtrlRead($DEBUG),$aSel[0]+1,$aSel[1]-$aSel[0])
    If $text <> "" Then 
        
    Else    
        $text = InputBox("Binary to String","Enter Binary Data") 
        
    EndIf
    If StringLower(StringLeft($text,2)) <> "0x" Then $text = "0x" & $text
    $tempfile = FileOpen(@ScriptDir & "\temp.txt",2)
    FileWrite($tempfile,BinaryToString($text))
    FileClose($tempfile)
    ShellExecute(@ScriptDir & "\temp.txt")
EndFunc 

Func _debug_clear()
    GUICtrlSetData($DEBUG,"")
EndFunc 

Func _request_modify($req,$host)
Local $encoding = "Accept-Encoding: identity" ;deflate, gzip, compress, 
Local $reqsplit
Local $modified_method = 0 , $modified_connection = 0 , $modified_encoding = 0
    If $req <> "" And Not IsBinary($req) Then
        $reqsplit = StringSplit($req,@CRLF,1)       
        If IsArray($reqsplit) Then
            For $i = 1 to $reqsplit[0] - 1
                _save("] modifying :" & $i & " " & $reqsplit[$i])
                If StringLeft($reqsplit[$i],3) = "GET" Or StringLeft($reqsplit[$i],4) = "POST" Or StringLeft($reqsplit[$i],7) = "CONNECT" And $modified_method = 0 Then
                        $reqsplit[$i] = StringReplace($reqsplit[$i],$host,"")
                        $reqsplit[$i] = StringReplace($reqsplit[$i],"http://","")
                        $reqsplit[$i] = StringReplace($reqsplit[$i],":443","")
                        $modified_method = 1
                EndIf 
                If StringInStr(Stringlower($reqsplit[$i]),"proxy-connection: keep-alive") And $modified_connection = 0 Then 
                    $reqsplit[$i] = "Connection: keep-alive"
                    $modified_connection = 1
                EndIf   
                If StringInStr(Stringlower($reqsplit[$i]),"accept-encoding") And $modified_encoding = 0 Then 
                    $reqsplit[$i] = $encoding
                    $modified_encoding = 1
                EndIf   
                _save(" -> to :" & $reqsplit[$i] & @CRLF)
            Next    
            $req = _ArrayToString($reqsplit, @CRLF , 1 , Ubound($reqsplit)-1)
        EndIf
        If GUICtrlRead($checkbox_debug_content) = 1 Then
            _save("] Request modified to:" & @CRLF)
            _save($req &@CRLF)
        EndIf
    EndIf
    Return $req
EndFunc
Enrico

please give me feedback if any host isn't working ( with hostname ) with proxy.

thanks

it dosent work for me :(>

Mozilla Firefox Error: The connection was reset 

Chrome Error: ERR_EMPTY_RESPONSE

Debug:

 

] HTTP Proxy listening on IP :192.168.1.9 Port :10000
] HTTPS Proxy listening on IP :192.168.1.9 Port :8043
] new HTTP clientconnection :192.168.1.7
] Added new client to mainarray , pos: 1
] Client 1 requested

GET http://www.kraloyun.com/ HTTP/1.1
Host: www.kraloyun.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive


GET http://www.kraloyun.com/ HTTP/1.1
Host: www.kraloyun.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive


]1 Got Host Address : www.kraloyun.com
]1 Connected to Host: www.kraloyun.com
] modifying :1 GET http://www.kraloyun.com/ HTTP/1.1 -> to :GET / HTTP/1.1
] modifying :2 Host: www.kraloyun.com -> to :Host: www.kraloyun.com
] modifying :3 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0 -> to :User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
] modifying :4 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 -> to :Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
] modifying :5 Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3 -> to :Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3
] modifying :6 Accept-Encoding: gzip, deflate -> to :Accept-Encoding: identity
] modifying :7 Connection: keep-alive -> to :Connection: keep-alive
] modifying :8  -> to :
] Request modified to:
GET / HTTP/1.1
Host: www.kraloyun.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: identity
Connection: keep-alive


]1 Sending HTTP to Host www.kraloyun.com successfull prevlen:334 now:306
]1 Connection Closed by Host after 397 ms
] new HTTP clientconnection :192.168.1.7
] Added new client to mainarray , pos: 1
] Client 1 requested

CONNECT safebrowsing.google.com:443 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
Proxy-Connection: keep-alive
Connection: keep-alive
Host: safebrowsing.google.com:443


CONNECT safebrowsing.google.com:443 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
Proxy-Connection: keep-alive
Connection: keep-alive
Host: safebrowsing.google.com:443


]1 Got Host Address : safebrowsing.google.com:443
]1 Not Connected to Host: safebrowsing.google.com:443 
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...