Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 12/01/2014 in all areas

  1. Thanks Manadar for your replay well, here a very primitive attempt (primitive but at least it works) to achieve the purpose (just a proof of concept). in short: added a new "Case" to the Switch $sFileType to catch .ha3 filetypes added _parseSource() at bottom added _ExecuteScript() at bottom For who wants to try: Save the modified server script and the 2 "web pages" in the same directory, run the server open a web browser and enter as address the IP of the machine where the server is running (a very basic page will open) click on the only link and a second page with embedded Autoit code will be parsed on the server and opened on the client. modified Basic_Server save it as Server.au3 #cs Resources: Internet Assigned Number Authority - all Content-Types: http://www.iana.org/assignments/media-types/ World Wide Web Consortium - An overview of the HTTP protocol: http://www.w3.org/Protocols/ Credits: Manadar for starting on the webserver. Alek for adding POST and some fixes Creator for providing the "application/octet-stream" MIME type. #ce ; // OPTIONS HERE // Local $sRootDir = @ScriptDir ; & "\www" ; The absolute path to the root directory of the server. Local $sIP = @IPAddress1 ; ip address as defined by AutoIt Local $iPort = 80 ; the listening port Local $sServerAddress = "http://" & $sIP & ":" & $iPort & "/" Local $iMaxUsers = 15 ; Maximum number of users who can simultaneously get/post Local $sServerName = "ManadarX/1.1 (" & @OSVersion & ") AutoIt " & @AutoItVersion ; // END OF OPTIONS // Local $aSocket[$iMaxUsers] ; Creates an array to store all the possible users Local $sBuffer[$iMaxUsers] ; All these users have buffers when sending/receiving, so we need a place to store those For $x = 0 To UBound($aSocket) - 1 ; Fills the entire socket array with -1 integers, so that the server knows they are empty. $aSocket[$x] = -1 Next TCPStartup() ; AutoIt needs to initialize the TCP functions $iMainSocket = TCPListen($sIP, $iPort) ;create main listening socket If @error Then ; if you fail creating a socket, exit the application MsgBox(0x20, "AutoIt Webserver", "Unable to create a socket on port " & $iPort & ".") ; notifies the user that the HTTP server will not run Exit ; if your server is part of a GUI that has nothing to do with the server, you'll need to remove the Exit keyword and notify the user that the HTTP server will not work. EndIf ConsoleWrite("Server created on " & $sServerAddress & @CRLF) ; If you're in SciTE, While 1 $iNewSocket = TCPAccept($iMainSocket) ; Tries to accept incoming connections If $iNewSocket >= 0 Then ; Verifies that there actually is an incoming connection For $x = 0 To UBound($aSocket) - 1 ; Attempts to store the incoming connection If $aSocket[$x] = -1 Then $aSocket[$x] = $iNewSocket ;store the new socket ExitLoop EndIf Next EndIf For $x = 0 To UBound($aSocket) - 1 ; A big loop to receive data from everyone connected If $aSocket[$x] = -1 Then ContinueLoop ; if the socket is empty, it will continue to the next iteration, doing nothing $sNewData = TCPRecv($aSocket[$x], 1024) ; Receives a whole lot of data if possible If @error Then ; Client has disconnected $aSocket[$x] = -1 ; Socket is freed so that a new user may join ContinueLoop ; Go to the next iteration of the loop, not really needed but looks oh so good ElseIf $sNewData Then ; data received $sBuffer[$x] &= $sNewData ;store it in the buffer If StringInStr(StringStripCR($sBuffer[$x]), @LF & @LF) Then ; if the request has ended .. $sFirstLine = StringLeft($sBuffer[$x], StringInStr($sBuffer[$x], @LF)) ; helps to get the type of the request $sRequestType = StringLeft($sFirstLine, StringInStr($sFirstLine, " ") - 1) ; gets the type of the request If $sRequestType = "GET" Then ; user wants to download a file or whatever .. $sRequest = StringTrimRight(StringTrimLeft($sFirstLine, 4), 11) ; let's see what file he actually wants If StringInStr(StringReplace($sRequest, "\", "/"), "/.") Then ; Disallow any attempts to go back a folder ;;~ _HTTP_SendError($aSocket[$x]) ; sends back an error _HTTP_SendFileNotFoundError($aSocket[$x]) ; sends back an error Else If $sRequest = "/" Then ; user has requested the root $sRequest = "/index.html" ; instead of root we'll give him the index page EndIf $sRequest = StringReplace($sRequest, "/", "\") ; convert HTTP slashes to windows slashes, not really required because windows accepts both If FileExists($sRootDir & "\" & $sRequest) Then ; makes sure the file that the user wants exists $sFileType = StringRight($sRequest, 4) ; determines the file type, so that we may choose what mine type to use Switch $sFileType ; -- just a pre alpha "proof of concept" ----------------------------------------- Case ".ha3" ; just for example extension .ha3 could be used -> (h)tml (a)utoit(3) $hFile = FileOpen($sRootDir & "\" & $sRequest);, 16) ; read file of web page $bFileData = FileRead($hFile) FileClose($hFile) ; now use _parseSource() to extract and execute embedded AutoIt code ; and then send result to client's browser _HTTP_SendData($aSocket[$x], _parseSource($bFileData), "text/html") ; ---------------------------------------------------------------------------- Case "html", ".htm" ; in case of normal HTML files _HTTP_SendFile($aSocket[$x], $sRootDir & $sRequest, "text/html") Case ".css" ; in case of style sheets _HTTP_SendFile($aSocket[$x], $sRootDir & $sRequest, "text/css") Case ".jpg", "jpeg" ; for common images _HTTP_SendFile($aSocket[$x], $sRootDir & $sRequest, "image/jpeg") Case ".png" ; another common image format _HTTP_SendFile($aSocket[$x], $sRootDir & $sRequest, "image/png") Case Else ; this is for .exe, .zip, or anything else that is not supported is downloaded to the client using a application/octet-stream _HTTP_SendFile($aSocket[$x], $sRootDir & $sRequest, "application/octet-stream") EndSwitch Else _HTTP_SendFileNotFoundError($aSocket[$x]) ; File does not exist, so we'll send back an error.. EndIf EndIf EndIf $sBuffer[$x] = "" ; clears the buffer because we just used to buffer and did some actions based on them $aSocket[$x] = -1 ; the socket is automatically closed so we reset the socket so that we may accept new clients EndIf EndIf Next Sleep(10) WEnd Func _HTTP_ConvertString(ByRef $sInput) ; converts any characters like %20 into space 8) $sInput = StringReplace($sInput, '+', ' ') StringReplace($sInput, '%', '') For $t = 0 To @extended $Find_Char = StringLeft(StringTrimLeft($sInput, StringInStr($sInput, '%')), 2) $sInput = StringReplace($sInput, '%' & $Find_Char, Chr(Dec($Find_Char))) Next EndFunc ;==>_HTTP_ConvertString Func _HTTP_SendHTML($hSocket, $sHTML, $sReply = "200 OK") ; sends HTML data on X socket _HTTP_SendData($hSocket, Binary($sHTML), "text/html", $sReply) EndFunc ;==>_HTTP_SendHTML Func _HTTP_SendFile($hSocket, $sFileLoc, $sMimeType, $sReply = "200 OK") ; Sends a file back to the client on X socket, with X mime-type Local $hFile, $sImgBuffer, $sPacket, $a ConsoleWrite("Sending " & $sFileLoc & @CRLF) $hFile = FileOpen($sFileLoc, 16) $bFileData = FileRead($hFile) FileClose($hFile) _HTTP_SendData($hSocket, $bFileData, $sMimeType, $sReply) EndFunc ;==>_HTTP_SendFile Func _HTTP_SendData($hSocket, $bData, $sMimeType, $sReply = "200 OK") $sPacket = Binary("HTTP/1.1 " & $sReply & @CRLF & _ "Server: " & $sServerName & @CRLF & _ "Connection: close" & @CRLF & _ "Content-Lenght: " & BinaryLen($bData) & @CRLF & _ "Content-Type: " & $sMimeType & @CRLF & _ @CRLF) TCPSend($hSocket, $sPacket) ; Send start of packet While BinaryLen($bData) ; Send data in chunks (most code by Larry) $a = TCPSend($hSocket, $bData) ; TCPSend returns the number of bytes sent $bData = BinaryMid($bData, $a + 1, BinaryLen($bData) - $a) WEnd $sPacket = Binary(@CRLF & @CRLF) ; Finish the packet TCPSend($hSocket, $sPacket) TCPCloseSocket($hSocket) EndFunc ;==>_HTTP_SendData Func _HTTP_SendFileNotFoundError($hSocket) ; Sends back a basic 404 error Local $s404Loc = $sRootDir & "\404.html" If (FileExists($s404Loc)) Then _HTTP_SendFile($hSocket, $s404Loc, "text/html") Else _HTTP_SendHTML($hSocket, "404 Error: " & @CRLF & @CRLF & "The file you requested could not be found.") EndIf EndFunc ;==>_HTTP_SendFileNotFoundError ; --- new pre alpha test functions --------------- ; ; following function grabbed from AuCGI.au3 ; from here: http://www.autoitscript.com/forum/topic/111133-autoit-cgi-handler-aucgi/ ; by Erik Pilsits (wraithdu), and Josh Rowe (JRowe) ; original author Matt Roth (theguy0000) <theguy0000@gmail.com> ; (slightly modified by me) ; ; parse source for <?au3 ?> code Func _parseSource($source) Local $idx = 1, $idx2, $lastidx = 1, $parsed = "", $chunk = "" Do ; get first code snippet $idx = StringInStr($source, "<?au3", 0, 1, $idx) ; position of "<?au3" in $idx If $idx Then If $idx > $lastidx Then ; we have html $chunk = StringMid($source, $lastidx, $idx - $lastidx) ; get it (get html) $parsed &= $chunk ;~ _splitHTML($chunk) ; write it to $parsed EndIf $idx += 5 ; start of code ; get end of code tag $idx2 = StringInStr($source, "?>", 0, 1, $idx) If $idx2 Then ; found end of code --------------------------------------------------------------+ $chunk = StringMid($source, $idx, $idx2 - $idx) ; get it (portion of AutoIt code) | ; --- execute autoit code and get back result --- | $chunk = _ExecuteScript($chunk) ; | $parsed &= $chunk & @CRLF ; | $lastidx = $idx2 + 2 ; new $lastidx value, set to position after end-code tag | $idx = $lastidx ; next search start location -------------------------------------+ Else ; parse error, get out ConsoleWrite("Error parsing source.") EndIf Else ; no code sections or last section of html $chunk = StringMid($source, $lastidx) ; get it (html) If $chunk Then $parsed &= $chunk ;~ _splitHTML($chunk) ; check we actually have something this time, write it EndIf Until Not $idx Return $parsed EndFunc ;==>_parseSource ; following function is by trancexx (slightly modified by me) ; from here: http://www.autoitscript.com/forum/topic/82461-how-to-get-access-for-a3x-encoded-files/?p=590906 Func _ExecuteScript($code) Local $return Local $TypeLib = ObjCreate("Scriptlet.TypeLib") Local $tmp = @TempDir & "\~" & $TypeLib.Guid & "tempexec.tmp" Local $hwnd = FileOpen($tmp, 26) FileWrite($hwnd, $code) FileClose($hwnd) $pid = Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $tmp & '"', "", "", 0x2) ; 0x2 ($STDOUT_CHILD) Do $return = StdoutRead($pid) Sleep(50) Until $return <> "" FileDelete($tmp) Return $return ; EndFunc ;==>_ExecuteScript First html page, save it in the same directory of the server as index.html <!DOCTYPE html> <html> <body> <a href="index.ha3">Try a web page with AutoiT code embedded!</a> </body> </html> Second html page with AutoIt code embedded. Save it in the same directory of the server as index.ha3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <?au3 $echo = "<br></br>" $echo &= "Hello World - from Manadar's web Server" $echo &= "<br></br>" $echo &= "Sever hosted on " & @ComputerName $echo &= "<br></br>" $echo &= "Server architecture is:" & "<br></br>" $echo &= "CPUArch: " & @CPUArch & "<br></br>" $echo &= "OSArch: " & @OSArch & "<br></br>" $echo &= "OSType: " & @OSType & "<br></br>" $echo &= "OSVersion: " & @OSVersion & "<br></br>" ConsoleWrite($echo) ?> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <title>Test</title> </head> <body> <?au3 $echo = "<br></br>" $echo &= "Server date is " & @MON & "/" & @MDAY & "/" & @YEAR $echo &= "<br></br>" $echo &= "Server Time is " & @HOUR & ":" & @MIN & ":" & @SEC ConsoleWrite($echo) ?> </body> </html>
    1 point
  2. No need to use the gui. There is a services au3 UDF in the Example Scripts forum.
    1 point
  3. I do not do ??, but it does Microsoft That is the maximum that can be done in this regard, the IsTextUnicode is a official function microsoft, is the best of the best even if you read on the web there are many complaints, but all notepad or text editor, refer and use the IsTextUnicode, in the official page of IsTextUnicode is everything well explained, microsoft himself says that in one point nothing is 100% suresave, but al notepad or text editor using the IsTextUnicode mod, so in general there some compatibility. however no one uses the UTF16-BE, both in Windows or C ++ or other (Wide Character are UTF16-LE) so in general with unicode means the UTF16-LE, so the UTF16-LE takes precedence Local $hFileOpen = FileOpen(@DesktopDir & "\text.txt", 26) If $hFileOpen = -1 Then ;Error Else FileWrite($hFileOpen, Binary("0x61000067")) FileClose($hFileOpen) EndIfand open the text.txt with notepad2.exe or Windows NotePad Ciao.
    1 point
  4. I like this UDF. It generates very fast and compact code. To see some working examples where it's used to optimize loops and fix x64 issues take a look at this example. See section 1.6 and 1.7. If you have downloaded the examples and want to take a closer look at one of the optimizations go to "OpenGL 1.1TexturesTunnelsutilitiesfasm". I have left the test files (with -a and -b in the names) to optimize loops in "Tunnel effect 2.au3". When I write assembler source I like to write it in a format like shown in the code boxes. This is an example with MessageBoxA: ; FASM assembler code ; MessageBoxA( hWnd, lpText, lpCaption, uType ) ; $tData = DllStructCreate( "byte[80];byte[16];handle;ptr[3]" ) ; Element 3, index 1, offset 96 ( 96) : handle hWnd ; Element 4, index 1, offset 100 (104) : ptr lpText ; Element 4, index 2, offset 104 (112) : ptr lpCaption ; Element 4, index 3, offset 108 (120) : ptr MessageBoxA ; (Numbers in brackets are x64 offsets) ; Parameters: ; [ebp + 08] : uType ; [ebp + 12] : $pData use32 push ebp mov ebp, esp mov eax, [ebp + 12] ; eax is pointer in $tData table ; Call MessageBoxA ;pusha ; Save registers on stack push dword [ebp + 08] ; 4. parameter: uType push dword [eax + 104] ; 3. parameter: lpCaption push dword [eax + 100] ; 2. parameter: lpText push dword [eax + 96] ; 1. parameter: hWnd call dword [eax + 108] ; Call MessageBoxA ;popa ; Restore registers pop ebp ret 08 And the x64 code: ; FASM assembler code ; MessageBoxA( hWnd, lpText, lpCaption, uType ) ; $tData = DllStructCreate( "byte[80];byte[16];handle;ptr[3]" ) ; Element 3, index 1, offset 96 ( 96) : handle hWnd ; Element 4, index 1, offset 100 (104) : ptr lpText ; Element 4, index 2, offset 104 (112) : ptr lpCaption ; Element 4, index 3, offset 108 (120) : ptr MessageBoxA ; (Numbers in brackets are x64 offsets) ; Parameters: ; rcx : uType ; rdx : $pData ; x64 function calling convention for parameter passing: ; First parameter: int/ptr -> rcx, float -> xmm0 ; Second parameter: int/ptr -> rdx, float -> xmm1 ; Third parameter: int/ptr -> r8, float -> xmm2 ; Fourth parameter: int/ptr -> r9, float -> xmm3 ; The rest goes onto the stack use64 mov rax, rdx ; rax is pointer in $tData table ; Call MessageBoxA sub rsp, 40 ; Stack space and alignment mov r9, rcx ; 4. parameter: uType mov rcx, qword [rax + 96] ; 1. parameter: hWnd mov rdx, qword [rax + 104] ; 2. parameter: lpText mov r8, qword [rax + 112] ; 3. parameter: lpCaption call qword [rax + 120] ; Call MessageBoxA add rsp, 40 ; Restore stack pointer ret Note that FASM.au3 cannot be run as a 64 bit program. But it certainly can generate 64 bit code. To generate the binary code run MessageBoxA-x64-mk-bin.au3. This is the AutoIt program to call the assembler functions: #include "Includes\Utilities.au3" Opt( "MustDeclareVars", 1 ) Global $hGui = GUICreate( "MessageBoxA demo with FASM", 400, 300 ) Global $pCode, $pData, $tData GetFasmCode() GUISetState() MessageBoxA1() MessageBoxA2() Func GetFasmCode() ; --- Fasm data --- ; MessageBoxA( hWnd, lpText, lpCaption, uType ) ; Use a struct to pass data from AutoIt to the assembler function. uType will be passed as a parameter. $tData = DllStructCreate( "byte[80];byte[16];handle;ptr[3]" ) ; To make things easier add extra space in tables in the structure until they are 8-bytes aligned. It's also ; nice to have a little bit of extra space. Then you can use this space for an intermediate result, if that ; should be necessary. Without the need to change all the offsets. It seems also to be easier to put the ; x86/x64 dependant data e.g. handle and ptr in the end of the structure. DllStructSetData( $tData, 3, $hGui ) ; Element 3, index 1, offset 96 ( 96) : handle hWnd DllStructSetData( $tData, 4, DllStructGetPtr( $tData, 1 ), 1 ) ; Element 4, index 1, offset 100 (104) : ptr lpText DllStructSetData( $tData, 4, DllStructGetPtr( $tData, 2 ), 2 ) ; Element 5, index 2, offset 104 (112) : ptr lpCaption Local $ptr = GetProcAddress( "user32.dll", "MessageBoxA" ) DllStructSetData( $tData, 4, $ptr, 3 ) ; Element 4, index 3, offset 108 (120) : ptr MessageBoxA ; (Numbers in brackets are x64 offsets) $pData = DllStructGetPtr( $tData ) ; --- Fasm code --- Local $sCode, $tCode If @AutoItX64 Then If Not ReadX64code( "MessageBoxA-x64.bin", "MessageBoxA-x64-mk-bin.au3", $sCode ) Then Exit Else Local $oFasm = FasmInit() If Not ReadFasmCode( "MessageBoxA.asm", $oFasm, $sCode ) Then Exit FasmExit( $oFasm ) EndIf $pCode = _MemVirtualAlloc( 0, 128, $MEM_COMMIT, $PAGE_EXECUTE_READWRITE ) $tCode = DllStructCreate( "byte[128]", $pCode ) DllStructSetData( $tCode, 1, $sCode ) EndFunc Func MessageBoxA1() SetTextCaption( "This is the first test.", "Test 1" ) DllCallAddress( "none", $pCode, "uint", 0, "ptr", $pData ) EndFunc Func MessageBoxA2() SetTextCaption( "This is the second test.", "Test 2" ) Local $aRet = DllCallAddress( "int", $pCode, "uint", 33, "ptr", $pData ), $sText If $aRet[0] = 1 Then ; 33 = OK + Cancel + Question-mark icon $sText = "OK button." Else $sText = "Cancel button (or close)." EndIf SetTextCaption( $sText, "Clicked" ) DllCallAddress( "none", $pCode, "uint", 0, "ptr", $pData ) EndFunc Func SetTextCaption( $sText, $sCaption ) ; MessageBoxA( hWnd, lpText, lpCaption, uType ) ; $tData = DllStructCreate( "byte[80];byte[16];uint[2];handle;ptr[3]" ) DllStructSetData( $tData, 1, $sText ) ; Element 1, index 1, offset 0 : byte $sText DllStructSetData( $tData, 1, 0, StringLen( $sText ) + 1 ) ; Null-terminated string DllStructSetData( $tData, 2, $sCaption ) ; Element 2, index 1, offset 80 : byte $sCaption DllStructSetData( $tData, 2, 0, StringLen( $sCaption ) + 1 ) ; Null-terminated string EndFunc You find the utility functions GetProcAddress, ReadFasmCode, MakeX64code, ReadX64code and the MessageBoxA files here: MessageBoxA.7z Copy BinaryCall.au3, FASM.au3 and MemoryDll.au3 from Ward's UDF to the Includes folder when you extract the 7z-file. Links flat assembler Programmer's Manual IntelĀ® 64 and IA-32 Architectures Software Developer Manuals Introduction to 64 Bit Intel Assembly Language x64 Software Conventions The Art of Assembly Language
    1 point
×
×
  • Create New...