Sign in to follow this  
Followers 0
Dhilip89

My Web Server V0.1a

8 posts in this topic

#1 ·  Posted (edited)

;==============================================
; UDF Libary [include]
;==============================================
#include <GUIConstants.au3>
#include <Array.au3>
#include <INet.au3>
#include <File.au3>
;==============================================
; Variables
;==============================================
Global $IP_Address = _GetIP()
If @error Then
    $IP_Address = @IPAddress1
EndIf
Global $Port = 80
Global $TCP_Listen
Global $Socket
Global $Recv
Global $Header
Global $HTML
Global Const $TEXTAREA = IniRead(@ScriptDir & "\autoproxy.ini", "Server", "textarea", "command")
Global $Session = 1
Global $GetVar = ""
Global $ReturnVar = ""
;==============================================
; Mode
;==============================================
Opt("TrayMenuMode", 1)
;Opt("GUIOnEventMode", 1)
;==============================================
; GUI
;==============================================
$Form1 = GUICreate("Dhilip's Web Server v0.1a", 329, 231, 352, 307)
GUICtrlCreateLabel("_____________________________________________________", 4, 176, 322, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button1 = GUICtrlCreateButton("Start", 80, 200, 75, 25, 0)
$Button2 = GUICtrlCreateButton("Stop", 160, 200, 75, 25, 0)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button3 = GUICtrlCreateButton("Close", 248, 200, 75, 25, 0)
GUICtrlCreateLabel("IP Address:", 4, 8, 58, 13)
$Input1 = GUICtrlCreateInput($IP_Address, 4, 28, 121, 21)
GUICtrlCreateLabel("Port:", 128, 8, 26, 13)
$Input2 = GUICtrlCreateInput($Port, 128, 28, 45, 21)
GUICtrlCreateLabel("______________________________________________________", 0, -11, 328, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$Edit1 = GUICtrlCreateEdit("", 4, 76, 321, 109)
GUICtrlCreateLabel("Logging:", 4, 56, 45, 17)
GUISetState(@SW_SHOW)
$MenuItem1 = GUICtrlCreateMenu("&Web Server")
$MenuItem4 = GUICtrlCreateMenuItem("&Start Server", $MenuItem1)
$MenuItem5 = GUICtrlCreateMenuItem("S&top Server", $MenuItem1)
GUICtrlSetState(-1, $GUI_DISABLE)
$MenuItem6 = GUICtrlCreateMenuItem("E&xit", $MenuItem1)
$MenuItem2 = GUICtrlCreateMenu("&Options")
$MenuItem7 = GUICtrlCreateMenuItem("O&ptions", $MenuItem2)
$MenuItem3 = GUICtrlCreateMenu("&Help")
$MenuItem8 = GUICtrlCreateMenuItem("H&elp", $MenuItem3)
$MenuItem9 = GUICtrlCreateMenuItem("&About", $MenuItem3)
TraySetClick("9")
$MenuItem10 = TrayCreateItem("Start Server")
$MenuItem11 = TrayCreateItem("Stop Server")
$MenuItem12 = TrayCreateItem("Exit")
;==============================================
; Main Code
;==============================================
GUISetState(@SW_SHOW, $Form1)
$MODE = 0
While 1
    $nMsg = GUIGetMsg(1)
    If $nMsg[1] = $Form1 Then
   ;Switch $nMsg[1]
        Select
            Case $nMsg[0] = $GUI_EVENT_CLOSE
                TCPCloseSocket($Socket)
                TCPCloseSocket($TCP_Listen)
                TCPShutdown()
                Exit
           ;_OnExit()
            Case $nMsg[0] = $Button3
                TCPCloseSocket($Socket)
                TCPCloseSocket($TCP_Listen)
                TCPShutdown()
                Exit
            Case $nMsg[0] = $Button1
                GUICtrlSetState($Button1, $GUI_DISABLE)
                TCPStartup()
                $IP = GUICtrlRead($Input1)
                $nPort = GUICtrlRead($Input2)
                $TCP_Listen = TCPListen($IP, $nPort, 100)
                If $TCP_Listen = -1 Then
                    $Error = @error
                    GUICtrlSetState($Button1, $GUI_ENABLE)
                    MsgBox(16, "Error", "Unable to connect." & @CRLF & @CRLF & "dec: " & $Error & @CRLF & "hex: 0x" & Hex($Error, 8))
                Else
                    GUICtrlSetState($Button2, $GUI_ENABLE)
                EndIf
                $MODE = 1
            Case $nMsg[0] = $Button2
                TCPCloseSocket($Socket)
                TCPCloseSocket($TCP_Listen)
                TCPShutdown()
                GUICtrlSetState($Button1, $GUI_ENABLE)
                GUICtrlSetState($Button2, $GUI_DISABLE)
           ;EndSwitch
        EndSelect
    EndIf
    If $MODE = 1 Then
        $Socket = TCPAccept($TCP_Listen)
        If $Socket >= 0 Then
            $Recv = _SockRecv($Socket)
            If StringLeft($Recv, 11) <> "GET / HTTP/" Then _GET($Recv, $TEXTAREA)
            _CheckReturn()
            $Send = _SockSend($Socket, $Header & _VarsInterpret($HTML))
            _ResetNormal()
            $Session += 1
            TCPCloseSocket($Socket)
            _ReduceMemory()
        EndIf
    EndIf
WEnd
;==============================================
; Functions
;==============================================
Func _ReduceMemory()
    DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
EndFunc ;==>_ReduceMemory

Func mError($sText, $iFatal = 0, $sTitle = "Error", $iOpt = 0)
    Local $ret = MsgBox(48 + 4096 + 262144 + $iOpt, $sTitle, $sText)
    If $iFatal Then Exit
    Return $ret
EndFunc ;==>mError

Func _ResetNormal()
    _ResetHeader()
    _LoadHTML(@ScriptDir & "\index.htm")
EndFunc ;==>_ResetNormal

Func _ResetHeader($sType = "text/html")
    $Header = 'HTTP/1.0 200 OK' & @CRLF & 'Content-Type: ' & $sType & @CRLF & @CRLF
EndFunc ;==>_ResetHeader

Func _CheckFiles()
    Local $aFiles = StringSplit("400.htm|403.htm|404.htm|405.htm|406.htm|410.htm|500.htm|501.htm|autoproxy.ini|back.gif|favicon.i


co|functions.au3|index.htm|pagerror.gif|refresh.gif|search.gif", "|")
    For $i = 1 To $aFiles[0]
        If Not FileExists(@ScriptDir & "\" & $aFiles[$i]) Then mError("Missing file:" & @CRLF & @ScriptDir & "\" & $aFiles[$i], 1)
    Next
EndFunc ;==>_CheckFiles

Func _CheckReturn()
    Local $sFile = @ScriptDir & "\return.txt", $aTemp, $sTemp, $sExt
    If Not FileExists($sFile) Then Return
    $ReturnVar = FileRead($sFile)
    FileDelete($sFile)
    $aTemp = StringSplit($ReturnVar, ":")
    If @error Then Return
    $sTemp = @ScriptDir & "\" & StringTrimLeft($ReturnVar, StringLen($aTemp[1]) + 1)
    If Not FileExists($sTemp) Or StringInStr(FileGetAttrib($sTemp), "D") Then Return
    Switch $aTemp[1]
        Case "goto"
            $sExt = StringSplit($sTemp, ".")
            _ResetHeader(IniRead(@ScriptDir & "\autoproxy.ini", "Extentions", $sExt[$sExt[0]], "*/*"))
            _LoadHTML($sTemp)
    EndSwitch
EndFunc ;==>_CheckReturn

Func _LoadHTML($sFile)
    $HTML = FileRead($sFile)
EndFunc ;==>_LoadHTML

Func _VarsInterpret($sData)
    Local $sFile = @ScriptDir & "\autoproxy.ini", $aVars = IniReadSection($sFile, "Variables")
    If @error Then
        SetError(1)
        Return $sData
    EndIf
    For $i = 1 To $aVars[0][0]
        $sData = StringReplace($sData, $aVars[$i][0], Execute($aVars[$i][1]))
    Next
    Return $sData
EndFunc ;==>_VarsInterpret

Func _GET($sData, $sTextArea)
    $sData = StringSplit($sData, @CRLF, 1)
    If @error Or Not IsArray($sData) Then
        _ResetHeader()
        _LoadHTML(@ScriptDir & "\400.htm")
        $GetVar = ""
        Return
    EndIf
    $sData = StringTrimRight(StringTrimLeft($sData[1], 5), 9)
    If $sData == "?" Or $sData == "?=" Then
        _ResetHeader()
        _LoadHTML(@ScriptDir & "\400.htm")
        $GetVar = ""
        Return
    EndIf
    Local $sGetFile = @ScriptDir & "\" & StringReplace(_URLUnformat($sData), "/", "\")
    If FileExists($sGetFile) And Not StringInStr(FileGetAttrib($sGetFile), "D") Then
        Local $sExt = StringSplit($sGetFile, ".")
        _ResetHeader(IniRead(@ScriptDir & "\autoproxy.ini", "Extentions", $sExt[$sExt[0]], "*/*"))
        _LoadHTML($sGetFile)
        Return
    Else
        If StringLeft($sData, 1) == "?" Then
            $sData = StringSplit(StringTrimLeft($sData, 1), $sTextArea & "=", 1)
            If @error Or Not IsArray($sData) Then
                _ResetNormal()
                $GetVar = ""
                Return
            EndIf
            $GetVar = _URLUnformat($sData[$sData[0]])
            _CmdInterpret()
            _ResetNormal()
            Return
        Else
            If $sData == "" Then
                _ResetNormal()
                Return
            Else
                _ResetHeader()
                _LoadHTML(@ScriptDir & "\404.htm")
                Return
            EndIf
        EndIf
    EndIf
EndFunc ;==>_GET

Func _URLUnformat($sURL)
    Local $sNew = ''
    $sURL = StringSplit(StringReplace($sURL, "+", " "), "")
    For $i = 1 To $sURL[0]
        If $sURL[$i] = "%" Then
            $sNew &= Chr('0x' & $sURL[$i + 1] & $sURL[$i + 2])
            $i += 2
        Else
            $sNew &= $sURL[$i]
        EndIf
    Next
    Return $sNew
EndFunc ;==>_URLUnformat

Func _CmdInterpret()
    Local $aSplit = StringSplit($GetVar, ":"), $text = StringTrimLeft($GetVar, StringLen($aSplit[1]) + 1), $sFile = @ScriptDir & "\autoproxy.ini", $sCmd = IniRead($sFile, "Commands", $aSplit[1], "error")
    If @error Or $sCmd = "error" Then Return
    Local $script = @ScriptDir & "\" & Random(0x0, 0xFFFFFFF, 1) & ".au3"
    FileWrite($script, '#NoTrayIcon' & @CRLF & 'Opt("RunErrorsFatal", 0)' & @CRLF & '#include "functions.au3"' & @CRLF & '$text = _UnHex("' & _Hex($text) & '")' & @CRLF & '$recv = _UnHex("' & _Hex($Recv) & '")' & @CRLF & StringReplace($sCmd, "\n", @CRLF))
    _AutoExecute($script, 0)
    Sleep(500)
    FileDelete($script)
EndFunc ;==>_CmdInterpret

Func _AutoExecute($sScript, $bRunWait = 0)
    If $bRunWait Then Return RunWait('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sScript & '"', @ScriptDir, @SW_SHOWDEFAULT)
    Return Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sScript & '"', @ScriptDir, @SW_SHOWDEFAULT)
EndFunc ;==>_AutoExecute

Func _Hex($sText)
    Local $aText = StringSplit($sText, ""), $sRet = '', $i
    For $i = 1 To $aText[0]
        $sRet = $sRet & Hex(Asc($aText[$i]), 2)
    Next
    Return $sRet
EndFunc ;==>_Hex

Func _UnHex($sHex)
    Local $aSplit = StringSplit($sHex, ""), $sRet = "", $i
    For $i = 1 To $aSplit[0] Step 2
        $sRet = $sRet & Chr(Dec($aSplit[$i] & $aSplit[$i + 1]))
    Next
    Return $sRet
EndFunc ;==>_UnHex

Func _SockRecv($iSocket, $iBytes = 2048)
    Local $sData = ""
    While $sData = ""
        $sData = TCPRecv($iSocket, $iBytes)
    WEnd
    Return $sData
EndFunc ;==>_SockRecv

Func _SockSend($iSocket, $sData)
    Return TCPSend($iSocket, $sData)
EndFunc ;==>_SockSend

Func OnAutoItExit()
    _OnExit()
EndFunc ;==>OnAutoItExit

Func _OnExit()
    TCPCloseSocket($Socket)
    TCPCloseSocket($TCP_Listen)
    TCPShutdown()
    Exit
EndFunc ;==>_OnExit

Edit:

Update - Bugs fixed by nfwu

Edited by Dhilip

[u]My Projects[/u]:General:WinShell (Version 1.6)YouTube Video Downloader Core (Version 2.0)Periodic Table Of Chemical Elements (Version 1.0)Web-Based:Directory Listing Script Written In AutoIt3 (Version 1.9 RC1)UDFs:UnicodeURL UDFHTML Entity UDF[u]My Website:[/u]http://dhilip89.hopto.org/[u]Closed Sources:[/u]YouTube Video Downloader (Version 1.3)[quote]If 1 + 1 = 10, then 1 + 1 ≠ 2[/quote]

Share this post


Link to post
Share on other sites



You appear to misunderstand While loops. Here is a corrected version:

(Also Idented you code)

;==============================================
; UDF Libary [include]
;==============================================
#include <GUIConstants.au3>
#include <Array.au3>
#include <INet.au3>
#include <File.au3>
;==============================================
; Variables
;==============================================
Global $IP_Address = _GetIP()
If @error Then
    $IP_Address = @IPAddress1
EndIf
Global $Port = 80
Global $TCP_Listen
Global $Socket
Global $Recv
Global $Header
Global $HTML
Global Const $TEXTAREA = IniRead(@ScriptDir & "\autoproxy.ini", "Server", "textarea", "command")
Global $Session = 1
Global $GetVar = ""
Global $ReturnVar = ""
;==============================================
; Mode
;==============================================
Opt("TrayMenuMode", 1)
;Opt("GUIOnEventMode", 1)
;==============================================
; GUI
;==============================================
$Form1 = GUICreate("Dhilip's Web Server v0.1a", 329, 231, 352, 307)
GUICtrlCreateLabel("_____________________________________________________", 4, 176, 322, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button1 = GUICtrlCreateButton("Start", 80, 200, 75, 25, 0)
$Button2 = GUICtrlCreateButton("Stop", 160, 200, 75, 25, 0)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button3 = GUICtrlCreateButton("Close", 248, 200, 75, 25, 0)
GUICtrlCreateLabel("IP Address:", 4, 8, 58, 13)
$Input1 = GUICtrlCreateInput($IP_Address, 4, 28, 121, 21)
GUICtrlCreateLabel("Port:", 128, 8, 26, 13)
$Input2 = GUICtrlCreateInput($Port, 128, 28, 45, 21)
GUICtrlCreateLabel("______________________________________________________", 0, -11, 328, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$Edit1 = GUICtrlCreateEdit("", 4, 76, 321, 109)
GUICtrlCreateLabel("Logging:", 4, 56, 45, 17)
GUISetState(@SW_SHOW)
$MenuItem1 = GUICtrlCreateMenu("&Web Server")
$MenuItem4 = GUICtrlCreateMenuItem("&Start Server", $MenuItem1)
$MenuItem5 = GUICtrlCreateMenuItem("S&top Server", $MenuItem1)
GUICtrlSetState(-1, $GUI_DISABLE)
$MenuItem6 = GUICtrlCreateMenuItem("E&xit", $MenuItem1)
$MenuItem2 = GUICtrlCreateMenu("&Options")
$MenuItem7 = GUICtrlCreateMenuItem("O&ptions", $MenuItem2)
$MenuItem3 = GUICtrlCreateMenu("&Help")
$MenuItem8 = GUICtrlCreateMenuItem("H&elp", $MenuItem3)
$MenuItem9 = GUICtrlCreateMenuItem("&About", $MenuItem3)
TraySetClick("9")
$MenuItem10 = TrayCreateItem("Start Server")
$MenuItem11 = TrayCreateItem("Stop Server")
$MenuItem12 = TrayCreateItem("Exit")
;==============================================
; Main Code
;==============================================
GUISetState(@SW_SHOW, $Form1)
$MODE = 0
While 1
    $nMsg = GUIGetMsg(1)
    If $nMsg[1] = $Form1 Then
    ;Switch $nMsg[1]
        Select
            Case $nMsg[0] = $GUI_EVENT_CLOSE
                TCPCloseSocket($Socket)
                TCPCloseSocket($TCP_Listen)
                TCPShutdown()
                Exit
            ;_OnExit()
            Case $nMsg[0] = $Button3
                TCPCloseSocket($Socket)
                TCPCloseSocket($TCP_Listen)
                TCPShutdown()
                Exit
            Case $nMsg[0] = $Button1
                GUICtrlSetState($Button1, $GUI_DISABLE)
                TCPStartup()
                $IP = GUICtrlRead($Input1)
                $nPort = GUICtrlRead($Input2)
                $TCP_Listen = TCPListen($IP, $nPort, 100)
                If $TCP_Listen = -1 Then
                    $Error = @error
                    GUICtrlSetState($Button1, $GUI_ENABLE)
                    MsgBox(16, "Error", "Unable to connect." & @CRLF & @CRLF & "dec: " & $Error & @CRLF & "hex: 0x" & Hex($Error, 8))
                Else
                    GUICtrlSetState($Button2, $GUI_ENABLE)
                EndIf
                $MODE = 1
            Case $nMsg[0] = $Button2
                TCPCloseSocket($Socket)
                TCPCloseSocket($TCP_Listen)
                TCPShutdown()
                GUICtrlSetState($Button1, $GUI_ENABLE)
                GUICtrlSetState($Button2, $GUI_DISABLE)
            ;EndSwitch
        EndSelect
    EndIf
    If $MODE = 1 Then
        $Socket = TCPAccept($TCP_Listen)
        If $Socket >= 0 Then
            $Recv = _SockRecv($Socket)
            If StringLeft($Recv, 11) <> "GET / HTTP/" Then _GET($Recv, $TEXTAREA)
            _CheckReturn()
            $Send = _SockSend($Socket, $Header & _VarsInterpret($HTML))
            _ResetNormal()
            $Session += 1
            TCPCloseSocket($Socket)
            _ReduceMemory()
        EndIf
    EndIf
WEnd
;==============================================
; Functions
;==============================================
Func _ReduceMemory()
    DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
EndFunc  ;==>_ReduceMemory

Func mError($sText, $iFatal = 0, $sTitle = "Error", $iOpt = 0)
    Local $ret = MsgBox(48 + 4096 + 262144 + $iOpt, $sTitle, $sText)
    If $iFatal Then Exit
    Return $ret
EndFunc  ;==>mError

Func _ResetNormal()
    _ResetHeader()
    _LoadHTML(@ScriptDir & "\index.htm")
EndFunc  ;==>_ResetNormal

Func _ResetHeader($sType = "text/html")
    $Header = 'HTTP/1.0 200 OK' & @CRLF & 'Content-Type: ' & $sType & @CRLF & @CRLF
EndFunc  ;==>_ResetHeader

Func _CheckFiles()
    Local $aFiles = StringSplit("400.htm|403.htm|404.htm|405.htm|406.htm|410.htm|500.htm|501.htm|autoproxy.ini|back.gif|favicon.i


co|functions.au3|index.htm|pagerror.gif|refresh.gif|search.gif", "|")
    For $i = 1 To $aFiles[0]
        If Not FileExists(@ScriptDir & "\" & $aFiles[$i]) Then mError("Missing file:" & @CRLF & @ScriptDir & "\" & $aFiles[$i], 1)
    Next
EndFunc  ;==>_CheckFiles

Func _CheckReturn()
    Local $sFile = @ScriptDir & "\return.txt", $aTemp, $sTemp, $sExt
    If Not FileExists($sFile) Then Return
    $ReturnVar = FileRead($sFile)
    FileDelete($sFile)
    $aTemp = StringSplit($ReturnVar, ":")
    If @error Then Return
    $sTemp = @ScriptDir & "\" & StringTrimLeft($ReturnVar, StringLen($aTemp[1]) + 1)
    If Not FileExists($sTemp) Or StringInStr(FileGetAttrib($sTemp), "D") Then Return
    Switch $aTemp[1]
        Case "goto"
            $sExt = StringSplit($sTemp, ".")
            _ResetHeader(IniRead(@ScriptDir & "\autoproxy.ini", "Extentions", $sExt[$sExt[0]], "*/*"))
            _LoadHTML($sTemp)
    EndSwitch
EndFunc  ;==>_CheckReturn

Func _LoadHTML($sFile)
    $HTML = FileRead($sFile)
EndFunc  ;==>_LoadHTML

Func _VarsInterpret($sData)
    Local $sFile = @ScriptDir & "\autoproxy.ini", $aVars = IniReadSection($sFile, "Variables")
    If @error Then
        SetError(1)
        Return $sData
    EndIf
    For $i = 1 To $aVars[0][0]
        $sData = StringReplace($sData, $aVars[$i][0], Execute($aVars[$i][1]))
    Next
    Return $sData
EndFunc  ;==>_VarsInterpret

Func _GET($sData, $sTextArea)
    $sData = StringSplit($sData, @CRLF, 1)
    If @error Or Not IsArray($sData) Then
        _ResetHeader()
        _LoadHTML(@ScriptDir & "\400.htm")
        $GetVar = ""
        Return
    EndIf
    $sData = StringTrimRight(StringTrimLeft($sData[1], 5), 9)
    If $sData == "?" Or $sData == "?=" Then
        _ResetHeader()
        _LoadHTML(@ScriptDir & "\400.htm")
        $GetVar = ""
        Return
    EndIf
    Local $sGetFile = @ScriptDir & "\" & StringReplace(_URLUnformat($sData), "/", "\")
    If FileExists($sGetFile) And Not StringInStr(FileGetAttrib($sGetFile), "D") Then
        Local $sExt = StringSplit($sGetFile, ".")
        _ResetHeader(IniRead(@ScriptDir & "\autoproxy.ini", "Extentions", $sExt[$sExt[0]], "*/*"))
        _LoadHTML($sGetFile)
        Return
    Else
        If StringLeft($sData, 1) == "?" Then
            $sData = StringSplit(StringTrimLeft($sData, 1), $sTextArea & "=", 1)
            If @error Or Not IsArray($sData) Then
                _ResetNormal()
                $GetVar = ""
                Return
            EndIf
            $GetVar = _URLUnformat($sData[$sData[0]])
            _CmdInterpret()
            _ResetNormal()
            Return
        Else
            If $sData == "" Then
                _ResetNormal()
                Return
            Else
                _ResetHeader()
                _LoadHTML(@ScriptDir & "\404.htm")
                Return
            EndIf
        EndIf
    EndIf
EndFunc  ;==>_GET

Func _URLUnformat($sURL)
    Local $sNew = ''
    $sURL = StringSplit(StringReplace($sURL, "+", " "), "")
    For $i = 1 To $sURL[0]
        If $sURL[$i] = "%" Then
            $sNew &= Chr('0x' & $sURL[$i + 1] & $sURL[$i + 2])
            $i += 2
        Else
            $sNew &= $sURL[$i]
        EndIf
    Next
    Return $sNew
EndFunc  ;==>_URLUnformat

Func _CmdInterpret()
    Local $aSplit = StringSplit($GetVar, ":"), $text = StringTrimLeft($GetVar, StringLen($aSplit[1]) + 1), $sFile = @ScriptDir & "\autoproxy.ini", $sCmd = IniRead($sFile, "Commands", $aSplit[1], "error")
    If @error Or $sCmd = "error" Then Return
    Local $script = @ScriptDir & "\" & Random(0x0, 0xFFFFFFF, 1) & ".au3"
    FileWrite($script, '#NoTrayIcon' & @CRLF & 'Opt("RunErrorsFatal", 0)' & @CRLF & '#include "functions.au3"' & @CRLF & '$text = _UnHex("' & _Hex($text) & '")' & @CRLF & '$recv = _UnHex("' & _Hex($Recv) & '")' & @CRLF & StringReplace($sCmd, "\n", @CRLF))
    _AutoExecute($script, 0)
    Sleep(500)
    FileDelete($script)
EndFunc  ;==>_CmdInterpret

Func _AutoExecute($sScript, $bRunWait = 0)
    If $bRunWait Then Return RunWait('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sScript & '"', @ScriptDir, @SW_SHOWDEFAULT)
    Return Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sScript & '"', @ScriptDir, @SW_SHOWDEFAULT)
EndFunc  ;==>_AutoExecute

Func _Hex($sText)
    Local $aText = StringSplit($sText, ""), $sRet = '', $i
    For $i = 1 To $aText[0]
        $sRet = $sRet & Hex(Asc($aText[$i]), 2)
    Next
    Return $sRet
EndFunc  ;==>_Hex

Func _UnHex($sHex)
    Local $aSplit = StringSplit($sHex, ""), $sRet = "", $i
    For $i = 1 To $aSplit[0] Step 2
        $sRet = $sRet & Chr(Dec($aSplit[$i] & $aSplit[$i + 1]))
    Next
    Return $sRet
EndFunc  ;==>_UnHex

Func _SockRecv($iSocket, $iBytes = 2048)
    Local $sData = ""
    While $sData = ""
        $sData = TCPRecv($iSocket, $iBytes)
    WEnd
    Return $sData
EndFunc  ;==>_SockRecv

Func _SockSend($iSocket, $sData)
    Return TCPSend($iSocket, $sData)
EndFunc  ;==>_SockSend

Func OnAutoItExit()
    _OnExit()
EndFunc  ;==>OnAutoItExit

Func _OnExit()
    TCPCloseSocket($Socket)
    TCPCloseSocket($TCP_Listen)
    TCPShutdown()
    Exit
EndFunc  ;==>_OnExit

Hope that helps!

#)

Share this post


Link to post
Share on other sites

You appear to misunderstand While loops. Here is a corrected version:

(Also Idented you code)

;==============================================
; UDF Libary [include]
;==============================================
#include <GUIConstants.au3>
#include <Array.au3>
#include <INet.au3>
#include <File.au3>
;==============================================
; Variables
;==============================================
Global $IP_Address = _GetIP()
If @error Then
    $IP_Address = @IPAddress1
EndIf
Global $Port = 80
Global $TCP_Listen
Global $Socket
Global $Recv
Global $Header
Global $HTML
Global Const $TEXTAREA = IniRead(@ScriptDir & "\autoproxy.ini", "Server", "textarea", "command")
Global $Session = 1
Global $GetVar = ""
Global $ReturnVar = ""
;==============================================
; Mode
;==============================================
Opt("TrayMenuMode", 1)
;Opt("GUIOnEventMode", 1)
;==============================================
; GUI
;==============================================
$Form1 = GUICreate("Dhilip's Web Server v0.1a", 329, 231, 352, 307)
GUICtrlCreateLabel("_____________________________________________________", 4, 176, 322, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button1 = GUICtrlCreateButton("Start", 80, 200, 75, 25, 0)
$Button2 = GUICtrlCreateButton("Stop", 160, 200, 75, 25, 0)
GUICtrlSetState(-1, $GUI_DISABLE)
$Button3 = GUICtrlCreateButton("Close", 248, 200, 75, 25, 0)
GUICtrlCreateLabel("IP Address:", 4, 8, 58, 13)
$Input1 = GUICtrlCreateInput($IP_Address, 4, 28, 121, 21)
GUICtrlCreateLabel("Port:", 128, 8, 26, 13)
$Input2 = GUICtrlCreateInput($Port, 128, 28, 45, 21)
GUICtrlCreateLabel("______________________________________________________", 0, -11, 328, 17)
GUICtrlSetState(-1, $GUI_DISABLE)
$Edit1 = GUICtrlCreateEdit("", 4, 76, 321, 109)
GUICtrlCreateLabel("Logging:", 4, 56, 45, 17)
GUISetState(@SW_SHOW)
$MenuItem1 = GUICtrlCreateMenu("&Web Server")
$MenuItem4 = GUICtrlCreateMenuItem("&Start Server", $MenuItem1)
$MenuItem5 = GUICtrlCreateMenuItem("S&top Server", $MenuItem1)
GUICtrlSetState(-1, $GUI_DISABLE)
$MenuItem6 = GUICtrlCreateMenuItem("E&xit", $MenuItem1)
$MenuItem2 = GUICtrlCreateMenu("&Options")
$MenuItem7 = GUICtrlCreateMenuItem("O&ptions", $MenuItem2)
$MenuItem3 = GUICtrlCreateMenu("&Help")
$MenuItem8 = GUICtrlCreateMenuItem("H&elp", $MenuItem3)
$MenuItem9 = GUICtrlCreateMenuItem("&About", $MenuItem3)
TraySetClick("9")
$MenuItem10 = TrayCreateItem("Start Server")
$MenuItem11 = TrayCreateItem("Stop Server")
$MenuItem12 = TrayCreateItem("Exit")
;==============================================
; Main Code
;==============================================
GUISetState(@SW_SHOW, $Form1)
$MODE = 0
While 1
    $nMsg = GUIGetMsg(1)
    If $nMsg[1] = $Form1 Then
;Switch $nMsg[1]
        Select
            Case $nMsg[0] = $GUI_EVENT_CLOSE
                TCPCloseSocket($Socket)
                TCPCloseSocket($TCP_Listen)
                TCPShutdown()
                Exit
        ;_OnExit()
            Case $nMsg[0] = $Button3
                TCPCloseSocket($Socket)
                TCPCloseSocket($TCP_Listen)
                TCPShutdown()
                Exit
            Case $nMsg[0] = $Button1
                GUICtrlSetState($Button1, $GUI_DISABLE)
                TCPStartup()
                $IP = GUICtrlRead($Input1)
                $nPort = GUICtrlRead($Input2)
                $TCP_Listen = TCPListen($IP, $nPort, 100)
                If $TCP_Listen = -1 Then
                    $Error = @error
                    GUICtrlSetState($Button1, $GUI_ENABLE)
                    MsgBox(16, "Error", "Unable to connect." & @CRLF & @CRLF & "dec: " & $Error & @CRLF & "hex: 0x" & Hex($Error, 8))
                Else
                    GUICtrlSetState($Button2, $GUI_ENABLE)
                EndIf
                $MODE = 1
            Case $nMsg[0] = $Button2
                TCPCloseSocket($Socket)
                TCPCloseSocket($TCP_Listen)
                TCPShutdown()
                GUICtrlSetState($Button1, $GUI_ENABLE)
                GUICtrlSetState($Button2, $GUI_DISABLE)
        ;EndSwitch
        EndSelect
    EndIf
    If $MODE = 1 Then
        $Socket = TCPAccept($TCP_Listen)
        If $Socket >= 0 Then
            $Recv = _SockRecv($Socket)
            If StringLeft($Recv, 11) <> "GET / HTTP/" Then _GET($Recv, $TEXTAREA)
            _CheckReturn()
            $Send = _SockSend($Socket, $Header & _VarsInterpret($HTML))
            _ResetNormal()
            $Session += 1
            TCPCloseSocket($Socket)
            _ReduceMemory()
        EndIf
    EndIf
WEnd
;==============================================
; Functions
;==============================================
Func _ReduceMemory()
    DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
EndFunc ;==>_ReduceMemory

Func mError($sText, $iFatal = 0, $sTitle = "Error", $iOpt = 0)
    Local $ret = MsgBox(48 + 4096 + 262144 + $iOpt, $sTitle, $sText)
    If $iFatal Then Exit
    Return $ret
EndFunc ;==>mError

Func _ResetNormal()
    _ResetHeader()
    _LoadHTML(@ScriptDir & "\index.htm")
EndFunc ;==>_ResetNormal

Func _ResetHeader($sType = "text/html")
    $Header = 'HTTP/1.0 200 OK' & @CRLF & 'Content-Type: ' & $sType & @CRLF & @CRLF
EndFunc ;==>_ResetHeader

Func _CheckFiles()
    Local $aFiles = StringSplit("400.htm|403.htm|404.htm|405.htm|406.htm|410.htm|500.htm|501.htm|autoproxy.ini|back.gif|favicon.i


co|functions.au3|index.htm|pagerror.gif|refresh.gif|search.gif", "|")
    For $i = 1 To $aFiles[0]
        If Not FileExists(@ScriptDir & "\" & $aFiles[$i]) Then mError("Missing file:" & @CRLF & @ScriptDir & "\" & $aFiles[$i], 1)
    Next
EndFunc ;==>_CheckFiles

Func _CheckReturn()
    Local $sFile = @ScriptDir & "\return.txt", $aTemp, $sTemp, $sExt
    If Not FileExists($sFile) Then Return
    $ReturnVar = FileRead($sFile)
    FileDelete($sFile)
    $aTemp = StringSplit($ReturnVar, ":")
    If @error Then Return
    $sTemp = @ScriptDir & "\" & StringTrimLeft($ReturnVar, StringLen($aTemp[1]) + 1)
    If Not FileExists($sTemp) Or StringInStr(FileGetAttrib($sTemp), "D") Then Return
    Switch $aTemp[1]
        Case "goto"
            $sExt = StringSplit($sTemp, ".")
            _ResetHeader(IniRead(@ScriptDir & "\autoproxy.ini", "Extentions", $sExt[$sExt[0]], "*/*"))
            _LoadHTML($sTemp)
    EndSwitch
EndFunc ;==>_CheckReturn

Func _LoadHTML($sFile)
    $HTML = FileRead($sFile)
EndFunc ;==>_LoadHTML

Func _VarsInterpret($sData)
    Local $sFile = @ScriptDir & "\autoproxy.ini", $aVars = IniReadSection($sFile, "Variables")
    If @error Then
        SetError(1)
        Return $sData
    EndIf
    For $i = 1 To $aVars[0][0]
        $sData = StringReplace($sData, $aVars[$i][0], Execute($aVars[$i][1]))
    Next
    Return $sData
EndFunc ;==>_VarsInterpret

Func _GET($sData, $sTextArea)
    $sData = StringSplit($sData, @CRLF, 1)
    If @error Or Not IsArray($sData) Then
        _ResetHeader()
        _LoadHTML(@ScriptDir & "\400.htm")
        $GetVar = ""
        Return
    EndIf
    $sData = StringTrimRight(StringTrimLeft($sData[1], 5), 9)
    If $sData == "?" Or $sData == "?=" Then
        _ResetHeader()
        _LoadHTML(@ScriptDir & "\400.htm")
        $GetVar = ""
        Return
    EndIf
    Local $sGetFile = @ScriptDir & "\" & StringReplace(_URLUnformat($sData), "/", "\")
    If FileExists($sGetFile) And Not StringInStr(FileGetAttrib($sGetFile), "D") Then
        Local $sExt = StringSplit($sGetFile, ".")
        _ResetHeader(IniRead(@ScriptDir & "\autoproxy.ini", "Extentions", $sExt[$sExt[0]], "*/*"))
        _LoadHTML($sGetFile)
        Return
    Else
        If StringLeft($sData, 1) == "?" Then
            $sData = StringSplit(StringTrimLeft($sData, 1), $sTextArea & "=", 1)
            If @error Or Not IsArray($sData) Then
                _ResetNormal()
                $GetVar = ""
                Return
            EndIf
            $GetVar = _URLUnformat($sData[$sData[0]])
            _CmdInterpret()
            _ResetNormal()
            Return
        Else
            If $sData == "" Then
                _ResetNormal()
                Return
            Else
                _ResetHeader()
                _LoadHTML(@ScriptDir & "\404.htm")
                Return
            EndIf
        EndIf
    EndIf
EndFunc ;==>_GET

Func _URLUnformat($sURL)
    Local $sNew = ''
    $sURL = StringSplit(StringReplace($sURL, "+", " "), "")
    For $i = 1 To $sURL[0]
        If $sURL[$i] = "%" Then
            $sNew &= Chr('0x' & $sURL[$i + 1] & $sURL[$i + 2])
            $i += 2
        Else
            $sNew &= $sURL[$i]
        EndIf
    Next
    Return $sNew
EndFunc ;==>_URLUnformat

Func _CmdInterpret()
    Local $aSplit = StringSplit($GetVar, ":"), $text = StringTrimLeft($GetVar, StringLen($aSplit[1]) + 1), $sFile = @ScriptDir & "\autoproxy.ini", $sCmd = IniRead($sFile, "Commands", $aSplit[1], "error")
    If @error Or $sCmd = "error" Then Return
    Local $script = @ScriptDir & "\" & Random(0x0, 0xFFFFFFF, 1) & ".au3"
    FileWrite($script, '#NoTrayIcon' & @CRLF & 'Opt("RunErrorsFatal", 0)' & @CRLF & '#include "functions.au3"' & @CRLF & '$text = _UnHex("' & _Hex($text) & '")' & @CRLF & '$recv = _UnHex("' & _Hex($Recv) & '")' & @CRLF & StringReplace($sCmd, "\n", @CRLF))
    _AutoExecute($script, 0)
    Sleep(500)
    FileDelete($script)
EndFunc ;==>_CmdInterpret

Func _AutoExecute($sScript, $bRunWait = 0)
    If $bRunWait Then Return RunWait('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sScript & '"', @ScriptDir, @SW_SHOWDEFAULT)
    Return Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sScript & '"', @ScriptDir, @SW_SHOWDEFAULT)
EndFunc ;==>_AutoExecute

Func _Hex($sText)
    Local $aText = StringSplit($sText, ""), $sRet = '', $i
    For $i = 1 To $aText[0]
        $sRet = $sRet & Hex(Asc($aText[$i]), 2)
    Next
    Return $sRet
EndFunc ;==>_Hex

Func _UnHex($sHex)
    Local $aSplit = StringSplit($sHex, ""), $sRet = "", $i
    For $i = 1 To $aSplit[0] Step 2
        $sRet = $sRet & Chr(Dec($aSplit[$i] & $aSplit[$i + 1]))
    Next
    Return $sRet
EndFunc ;==>_UnHex

Func _SockRecv($iSocket, $iBytes = 2048)
    Local $sData = ""
    While $sData = ""
        $sData = TCPRecv($iSocket, $iBytes)
    WEnd
    Return $sData
EndFunc ;==>_SockRecv

Func _SockSend($iSocket, $sData)
    Return TCPSend($iSocket, $sData)
EndFunc ;==>_SockSend

Func OnAutoItExit()
    _OnExit()
EndFunc ;==>OnAutoItExit

Func _OnExit()
    TCPCloseSocket($Socket)
    TCPCloseSocket($TCP_Listen)
    TCPShutdown()
    Exit
EndFunc ;==>_OnExit

Hope that helps!

#)

Many Thanks :)

[u]My Projects[/u]:General:WinShell (Version 1.6)YouTube Video Downloader Core (Version 2.0)Periodic Table Of Chemical Elements (Version 1.0)Web-Based:Directory Listing Script Written In AutoIt3 (Version 1.9 RC1)UDFs:UnicodeURL UDFHTML Entity UDF[u]My Website:[/u]http://dhilip89.hopto.org/[u]Closed Sources:[/u]YouTube Video Downloader (Version 1.3)[quote]If 1 + 1 = 10, then 1 + 1 ≠ 2[/quote]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

wow. This is cool and simple. What about PHP, or Javascript, ect.

and other things like. password protection and directory listing??

BTW: there is still something wrong, it wont stop the server or exit

Edited by zerocool60544

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Share this post


Link to post
Share on other sites

wow. This is cool and simple. What about PHP, or Javascript, ect.

and other things like. password protection and directory listing??

BTW: there is still something wrong, it wont stop the server or exit

just use the fixed code by nfwu (on 2nd post#)

I'll re-design the GUI and add more functions.


[u]My Projects[/u]:General:WinShell (Version 1.6)YouTube Video Downloader Core (Version 2.0)Periodic Table Of Chemical Elements (Version 1.0)Web-Based:Directory Listing Script Written In AutoIt3 (Version 1.9 RC1)UDFs:UnicodeURL UDFHTML Entity UDF[u]My Website:[/u]http://dhilip89.hopto.org/[u]Closed Sources:[/u]YouTube Video Downloader (Version 1.3)[quote]If 1 + 1 = 10, then 1 + 1 ≠ 2[/quote]

Share this post


Link to post
Share on other sites

just use the fixed code by nfwu (on 2nd post#)

I'll re-design the GUI and add more functions.

Great thanks. Could you replace the code for the first post and fix the stop and exit. Thanks

What features are you adding? I can't wait


[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Share this post


Link to post
Share on other sites

How about this GUI design ?

AboutBox only:

Posted Image


[u]My Projects[/u]:General:WinShell (Version 1.6)YouTube Video Downloader Core (Version 2.0)Periodic Table Of Chemical Elements (Version 1.0)Web-Based:Directory Listing Script Written In AutoIt3 (Version 1.9 RC1)UDFs:UnicodeURL UDFHTML Entity UDF[u]My Website:[/u]http://dhilip89.hopto.org/[u]Closed Sources:[/u]YouTube Video Downloader (Version 1.3)[quote]If 1 + 1 = 10, then 1 + 1 ≠ 2[/quote]

Share this post


Link to post
Share on other sites

How about this GUI design ?

AboutBox only:

Posted Image

very professional

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Share this post


Link to post
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
Sign in to follow this  
Followers 0