Sign in to follow this  
Followers 0
olivarra1

My own remote control :3

1 post in this topic

#1 ·  Posted (edited)

Hey there :3

During the first days of these week i've been thinking about developing a remote control, so i could control my computer from any device.

At first, i was programming while i was brainstorming. I know i shouldn't do that, but i did :huh2:. So the code got a little messy... Then these last 2 days i've remade it, and i think the code looks much clearer now.

The server [what you put into the machine you want to control] is made with autoit. For flash clients, there's a second autoit, PolicyServer, that sends the policy file [policy.xml] to whichever client asks for it.

The client is made with Flex 4 SDK. I put the FlashDevelop project folder inside the attachment.

If you want to use it, you will have to look the code, and customize it so it feeds your needs [i.e., setting the IP / port, that's fixed in Constants]. Also, BE AWARE that this software is not secure, it doesn't use any encription method, so anyone could access your machine and do whatever with it.

There's a little bit of security but it's not enough: i made that the client has to send a password [in raw text! D:] to be able to send commands. This way not anyone can access the computer, but anyone that puts a packet sniffer between you and your machine. I think this can be solved only by putting some Public Key criptography... but i don't have time to implement it.

#include <GDIPlus.au3>
#include <ScreenCapture.au3>

Opt("MouseClickDragDelay", 50)

Dim $deviceScreenWidth = 480
Dim $deviceScreenHeight = 770
Dim $deviceRatio = $deviceScreenWidth / $deviceScreenHeight

Const $DEBUG = False ; If true, waits 1 second before executing any command that depends of the state of the remote machine [window active]
Const $RESIZE_TO_BIGGER = False
        #cs If true, if the area to capture is smaller than the device screen, the image will be resized to fit the
        device screen and then sent. If false, it won't resize it, send it, and the client will resize to fit the image. True requires more
        bandwidth, but the quality of the resized image is a lot better than False.
        #ce

Const $CLICK = "1"
Const $DOWN = "2"
Const $UP = "3"
Const $RIGHT = "4"
Const $LEFT = "5"
Const $SCREEN = "6"
Const $WINDOW = "7"

Const $LOGIN = "a"
Const $RESOLUTION = "b"
Const $PASSWORD = "c"
Const $AUTOIT = "d"
Const $RAW = "e"

Const $storedPassword = "myprivatepassword"

Global $regionStartX
Global $regionStartY
Global $regionEndX
Global $regionEndY

Func getRegionWidth()
    Return $regionEndX - $regionStartX
EndFunc
Func getRegionHeight()
    Return $regionEndY - $regionStartY
EndFunc

_ScreenCapture_SetBMPFormat(0)

TCPStartup()

Dim $srvSocket, $cliSocket
$srvSocket = TCPListen("127.0.0.1", 1045) ; Localhost connection
;$srvSocket = TCPListen("192.168.1.10", 1045) ; LAN connection
;$srvSocket = TCPListen("192.168.0.2", 1045) ; LAN connection
If $srvSocket == -1 Then
    Exit
EndIf

Dim $error = True
Dim $ratio = 0
Dim $allowed
While 1
    If $error Then
        Do
            $cliSocket = TCPAccept($srvSocket)
            $allowed = False
            $regionStartX = 0
            $regionStartY = 0
            $regionEndX = $regionStartX + @DesktopWidth ; @DesktopWidth
            $regionEndY = $regionStartY + @DesktopHeight ; @DesktopHeight
        Until $cliSocket <> -1
    EndIf

    ;size(2)code(a-zA-Z0-9)data
    $s = TCPRecv($cliSocket, 2)
    $size = Int($s)
    If $size > 0 Then
        $str = TCPRecv($cliSocket, $size)
        While StringLen($str) < $size
            $str = $str & TCPRecv($cliSocket, $size - StringLen($str))
        WEnd
        $codi = StringLeft($str, 1)
        $dades = StringTrimLeft($str, 1)


        If $codi == $LOGIN Then
            If $dades == "thisismypassword" Then
                If $DEBUG Then sleep(1000)
                $allowed = True
            EndIf
        ElseIf $codi == $RESOLUTION Then
            $str = StringSplit($dades, ",", 2)
            $deviceScreenWidth = Int($str[0])
            $deviceScreenHeight = Int($str[1])
            $deviceRatio = $deviceScreenWidth / $deviceScreenHeight
            ;TrayTip("", $deviceScreenWidth & ", " & $deviceScreenHeight, 5)
        ElseIf $codi == $PASSWORD And $allowed Then
            Send($storedPassword)
        ElseIf $codi == $AUTOIT And $allowed Then
            If $DEBUG Then sleep(1000)
            Send($dades, 0)
        ElseIf $codi == $RAW And $allowed Then
            If $DEBUG Then sleep(1000)
            Send($dades, 1)
        ElseIf $codi == $CLICK And $allowed Then
            $str = StringSplit($dades, ",", 2)
            $x0 = $str[1] / $ratio + $regionStartX
            $y0 = $str[2] / $ratio + $regionStartY
            $xf = $str[3] / $ratio + $regionStartX
            $yf = $str[4] / $ratio + $regionStartY
            If $str[0] == "r" Then ; right
                MouseClickDrag("secondary", $x0, $y0, $xf, $yf, 3)
            ElseIf $str[0] == "l" Then ; left
                MouseClickDrag("primary", $x0, $y0, $xf, $yf, 3)
            EndIf
        ElseIf $codi == $DOWN Then
            $val = Int($dades)
            $regionEndY = $regionEndY + $val / $ratio
        ElseIf $codi == $UP Then
            $val = Int($dades)
            $regionStartY = $regionStartY + $val / $ratio
        ElseIf $codi == $RIGHT Then
            $val = Int($dades)
            $regionEndX = $regionEndX + $val / $ratio
        ElseIf $codi == $LEFT Then
            $val = Int($dades)
            $regionStartX = $regionStartX + $val / $ratio
        ElseIf $codi == $SCREEN Then
            $regionStartX = 0
            $regionStartY = 0
            $regionEndX = $regionStartX + @DesktopWidth
            $regionEndY = $regionStartY + @DesktopHeight
        ElseIf $codi == $WINDOW Then
            If $DEBUG Then sleep(1000)
            $arr = WinGetPos("[ACTIVE]")
            $regionStartX = $arr[0]
            $regionStartY = $arr[1]
            $regionEndX = $regionStartX + $arr[2]
            $regionEndY = $regionStartY + $arr[3]
        EndIf
    EndIf

    $w = getRegionWidth()
    $h = getRegionHeight()
    $regionRatio = $w / $h

    If ($deviceRatio > 1 And $regionRatio > 1) Or ($deviceRatio < 1 And $regionRatio < 1) Then ; The device won't flip the image
        $ratio = $deviceScreenWidth / $w
        if $ratio * $h > $deviceScreenHeight Then $ratio = $deviceScreenHeight / $h
    Else ; The device will flip the image
        $ratio = $deviceScreenWidth / $h
        if $ratio * $w > $deviceScreenHeight Then $ratio = $deviceScreenHeight / $w
    EndIf
    If $ratio < 1 Or $RESIZE_TO_BIGGER Then
        $deviceW = $w * $ratio
        $deviceH = $h * $ratio
    Else
        $deviceW = $w
        $deviceH = $h
    EndIf

    If $allowed Then
        $img = _ScreenCapture_Capture("", $regionStartX, $regionStartY, $regionEndX, $regionEndY)
        _ImageResize($img, @ScriptDir & "\tmp.jpg", $deviceW, $deviceH)
        _WinAPI_DeleteObject($img)
    Else
        $img = _ScreenCapture_Capture("", 0, 0, 0, 0)
        _ImageResize($img, @ScriptDir & "\tmp.jpg", $deviceScreenWidth, $deviceScreenHeight)
        _WinAPI_DeleteObject($img)
    EndIf

    $size = FileGetSize(@ScriptDir & "\tmp.jpg")
    $oFile = FileOpen(@ScriptDir & "\tmp.jpg", 0)
    $bin = FileRead($oFile)
    FileClose($oFile)

    SetError(0)
    $str = to10chars(String($size))
    TCPSend($cliSocket, $str)
    TCPSend($cliSocket, $bin)
    If @error Then
        $error = True
    Else
        $error = False
    EndIf
WEnd

Func to10chars($str)
    $num = 9 - StringLen($str)
    For $i = 1 To $num
        $str = "0" & $str
    Next
    $str = "s" & $str
    Return $str
EndFunc

Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $hWnd, $hDC, $hBMP, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0


    ;OutFile path, to use later on.
    Local $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

    ;OutFile name, to use later on.
    Local $sOF = StringMid($sOutImage, StringInStr($sOutImage, "\", 0, -1) + 1)

    ;OutFile extension , to use for the encoder later on.
    Local $Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1))

    ; Win api to create blank bitmap at the width and height to put your resized image on.
    $hWnd = _WinAPI_GetDesktopWindow()
    $hDC = _WinAPI_GetDC($hWnd)
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iW, $iH)
    _WinAPI_ReleaseDC($hWnd, $hDC)

    ;Start GDIPlus
    _GDIPlus_Startup()

    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)

    ;Load the image you want to resize.
    If IsString($sInImage) Then
        $hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)
    Else
        $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP($sInImage) ; This function is modified here, so we don't have to write the bmp to the hard disk
    EndIf

    ;Get the graphic context of the blank bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext ($hImage1)

    ;Draw the loaded image onto the blank bitmap at the size you want
    _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $iW, $iH)

    ;Get the encoder of to save the resized image in the format you want.
    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)

    ;Generate a number for out file that doesn't already exist, so you don't overwrite an existing image.
    Do
        $i += 1
    Until (Not FileExists($sOP & $i & "_" & $sOF))

    ;Prefix the number to the begining of the output filename
    ;$sOutImage = $sOP & $i & "_" & $sOF
    $sOutImage = $sOP & $sOF

    ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose ($hGraphic)
    _WinAPI_DeleteObject($hBMP)
    _GDIPlus_Shutdown()
EndFunc

The function _ImageResize($sInImage, $sOutImage, $iW, $iH) is taken from this forum, but it's a bit modified. On my version, $sInImage can either be an image handle or a String.

edit: and i know this already exists, but for me it was fun to do it and.... now there's 1 more in the world : )

Edited by olivarra1

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