Jump to content

Safe browser


JohnOne
 Share

Recommended Posts

Warning: if you are going to run this code, it will close any instances of Internet explorer.

If you wish to avoid this, comment out the line "_CheckBrowsers()" in the main while loop.

Firstly, this is a very rudimentary and crude code example, but nonetheless I think it demonstrates what it is supposed to.

The Idea is for a simple safe browser. I know there are plenty of net nanny programs, and I'm not sure how they work, but I think they block nasty sites.

The Idea here, is not to have a massive list of dodgy websites to block, but to only allow navigation to selected websites.

The point was for lazy, ignorant or stupid parents, who can not/do not supervise their children using the internet.

The project is far from complete, but functional and any and all comments, advises, suggestions or criticisms are welcome.

The Code (not well commented)

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <File.au3>
#include <Array.au3>
#include <String.au3>

Global $aUrl
Global $homepage = @ScriptDir & 'index.htm'
Global $encodedhomepage = StringReplace($homepage, '', '/')
$encodedhomepage = StringReplace($encodedhomepage, ' ', '%20') ; sometimes the address appears like this, I don't know why
Global $urlcount = 0

_FileReadToArray(@ScriptDir & 'SafeUrl.txt', $aUrl)

Global $oIE = _IECreateEmbedded()
_IEErrorHandlerRegister("MyErrFunc")

ConsoleWrite(@DesktopWidth & @LF)
ConsoleWrite(@DesktopHeight & @LF)
ConsoleWrite(@DesktopWidth - 1315 & @LF)

Global $hGUI = GUICreate('IBsafE', @DesktopWidth, @DesktopHeight - 50, 0, 0, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX))
Global $WebBrowser = GUICtrlCreateObj($oIE, 0, 0, @DesktopWidth - 150, @DesktopHeight)
Global $hButtonBack = GUICtrlCreateButton('Back', @DesktopWidth - 125, 20, 80, 80)
Global $hButtonForward = GUICtrlCreateButton('Forward', @DesktopWidth - 125, 120, 80, 80)
Global $hButtonHome = GUICtrlCreateButton('Home', @DesktopWidth - 125, 220, 80, 80)
$oIE.Navigate2(@ScriptDir & 'index.htm')
Global $oEvent = ObjEvent($oIE, "_Evt_", "DWebBrowserEvents2")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            $oIE = 0
            Exit
        Case $hButtonBack
            _SafeNav('back')
        Case $hButtonForward
            _SafeNav('forward')
        Case $hButtonHome
            _SafeNav('home')
    EndSwitch
    _CheckBrowsers()
    $urlcount += 1
    If $urlcount > 20 Then
        _CheckURL()
        $urlcount = 0
    EndIf
WEnd

Func _SafeNav($sURL)
    Switch $sURL
        Case 'back'
            $oIE.GoBack()
        Case 'forward'
            $oIE.GoForward()
        Case 'home'
            $oIE.Navigate2(@ScriptDir & 'index.htm')
        Case Else
            $oIE.Navigate2(@ScriptDir & 'index.htm')
    EndSwitch
EndFunc ;==>_SafeNav

Func _Evt_BeforeNavigate2($IEpDisp, $IEURL)
    _DisableButtons()
    $oIE.Stop()
    $oEvent.Stop()
    If _AllowedSite($IEURL) Then
        _IENavigate($oIE, $IEURL)
        _IELoadWait($oIE)
    Else
        MsgBox(48, "OOPS! something went wrong", _
                "Sorry, that site is not allowed." & @CRLF & _
                "Please ask your parent if you are" & @CRLF & _
                "allowed to visit that website.", 7, $hGUI)
        ConsoleWrite('! ' & $IEURL & @LF)
    EndIf
    $oEvent = ObjEvent($oIE, "_Evt_", "DWebBrowserEvents2")
    _EnableButtons()
EndFunc ;==>_Evt_BeforeNavigate2

Func _AllowedSite($url)
    If StringInStr($url, $homepage) Or StringInStr($url, $encodedhomepage) Or StringInStr($url, 'about:blank') Then
        ; The url is allowed
        ConsoleWrite("+ " & $url & @LF)
        Return True
    EndIf

    $dom = _StringBetween($url, '//', '/'); attempt to get the url domain
    If Not IsArray($dom) Then
        Return False
    EndIf
    ;_ArrayDisplay($dom)
    ConsoleWrite("- " & $dom[0] & @LF)
    For $i = 1 To $aUrl[0]
        If StringInStr($url, $aUrl[$i]) Then
            ; site is allowed
            ConsoleWrite("+ " & $url & @LF)
            Return True
            ExitLoop
        EndIf
    Next
    ;site is not allowed
    ConsoleWrite("! " & $url & @LF)

    Return False
EndFunc ;==>_AllowedSite

Func _EnableButtons()
    GUICtrlSetState($WebBrowser, $GUI_SHOW)
    GUICtrlSetState($hButtonBack, $GUI_ENABLE)
    GUICtrlSetState($hButtonForward, $GUI_ENABLE)
    GUICtrlSetState($hButtonHome, $GUI_ENABLE)
EndFunc ;==>_EnableButtons

Func _DisableButtons()
    GUICtrlSetState($WebBrowser, $GUI_HIDE)
    GUICtrlSetState($hButtonBack, $GUI_DISABLE)
    GUICtrlSetState($hButtonForward, $GUI_DISABLE)
    GUICtrlSetState($hButtonHome, $GUI_DISABLE)
EndFunc ;==>_DisableButtons

;This function is an example of surpressing other browser launches.
Func _CheckBrowsers()
    If ProcessExists('iexplore.exe') Then
        While ProcessExists('iexplore.exe')
            ProcessClose('iexplore.exe')
        WEnd
        MsgBox(48, 'Not Allowed', 'Sorry, You are not allowed to do that.', 3, $hGUI)
    EndIf
EndFunc ;==>_CheckBrowsers

;This function is meant to serve as a failsafe
;although It does significantly increase CUP load
Func _CheckURL()
    Local $locationurl = _IEPropertyGet($oIE, 'locationurl')
    If _AllowedSite($locationurl) Then
        Return
    EndIf
    $oIE.Stop() ; Stop navigation
    ConsoleWrite("! " & $locationurl & @LF)
    _DisableButtons() ; disable gui buttons
    $oEvent.Stop() ; Stop receiving events
    $oIE.Navigate2($homepage) ; navigate to homepage
    MsgBox(48,'DisAllowed','Unautherized website',5, $hGUI)
    $oEvent = ObjEvent($oIE, "_Evt_", "DWebBrowserEvents2") ; Re-register events
    _EnableButtons() ; enable gui buttons
EndFunc ;==>_CheckURL

Func MyErrFunc()
    ; Important: the error object variable MUST be named $oIEErrorHandler
    ;GUICtrlSetState($WebBrowser, $GUI_HIDE)
    MsgBox(48, "OOPS! something went wrong", _
            "Sorry, that action has caused an error." & @CRLF & _
            "Don't worry, you can try something else.", 5, $hGUI)
    ;GUICtrlSetState($WebBrowser, $GUI_SHOW)
    Return
    Local $ErrorScriptline = $oIEErrorHandler.scriptline
    Local $ErrorNumber = $oIEErrorHandler.number
    Local $ErrorNumberHex = Hex($oIEErrorHandler.number, 8)
    Local $ErrorDescription = StringStripWS($oIEErrorHandler.description, 2)
    Local $ErrorWinDescription = StringStripWS($oIEErrorHandler.WinDescription, 2)
    Local $ErrorSource = $oIEErrorHandler.Source
    Local $ErrorHelpFile = $oIEErrorHandler.HelpFile
    Local $ErrorHelpContext = $oIEErrorHandler.HelpContext
    Local $ErrorLastDllError = $oIEErrorHandler.LastDllError
    Local $ErrorOutput = ""
    $ErrorOutput &= "--> COM Error Encountered in " & @ScriptName & @CR
    $ErrorOutput &= "----> $ErrorScriptline = " & $ErrorScriptline & @CR
    $ErrorOutput &= "----> $ErrorNumberHex = " & $ErrorNumberHex & @CR
    $ErrorOutput &= "----> $ErrorNumber = " & $ErrorNumber & @CR
    $ErrorOutput &= "----> $ErrorWinDescription = " & $ErrorWinDescription & @CR
    $ErrorOutput &= "----> $ErrorDescription = " & $ErrorDescription & @CR
    $ErrorOutput &= "----> $ErrorSource = " & $ErrorSource & @CR
    $ErrorOutput &= "----> $ErrorHelpFile = " & $ErrorHelpFile & @CR
    $ErrorOutput &= "----> $ErrorHelpContext = " & $ErrorHelpContext & @CR
    $ErrorOutput &= "----> $ErrorLastDllError = " & $ErrorLastDllError
    MsgBox(0, "COM Error", $ErrorOutput)
    SetError(1)
    Return
EndFunc ;==>MyErrFunc

SafeUrl.txt (needed in script folder) this is where you add allowed urls (would not be here in a final product)

google.co.uk
google.com
autoitscript.com
disney.co.uk

Index.htm (needed in script folder)

<title>Project Emulate speed dail</title>
</head>
<body>
<table align="center">
<tr>

<td><div id="lnk1" style="width:180px; height:100px; background-color:green; text-align:center;"

onclick=window.location="http://www.google.com/"><img src="http://www.google.com/favicon.ico"

alt="Google"><em>Google</em></div></td>

<td><div id="lnk2" style="width:180px; height:100px; background-color:green; text-align:center;"

onclick=window.location="http://www.google.com/"><img src="http://www.google.com/favicon.ico"

alt="Google"><em>Google</em></div></td>

<td><div id="lnk3" style="width:180px; height:100px; background-color:red; text-align:center;"

onclick=window.location="http://www.divx.com/"><img src="http://www.google.com/flavicon.ico"

alt="Divx.com"><em>DivX</em></div></td>

<td><div id="lnk4" style="width:180px; height:100px; background-color:green; text-align:center;"

onclick=window.location="http://www.google.com/"><img src="http://www.google.com/favicon.ico"

alt="Google"><em>Google</em></div></td>

<td><div id="lnk5" style="width:180px; height:100px; background-color:green; text-align:center;"

onclick=window.location="http://www.google.com/"><img src="http://www.google.com/favicon.ico"

alt="Google"><em>Google</em></div></td>

<td><div id="lnk6" style="width:180px; height:100px; background-color:green; text-align:center;"

onclick=window.location="http://www.google.com/"><img src="http://www.google.com/favicon.ico"

alt="Google"><em>Google</em></div></td>

</tr>
</table>
</body>
</html>

That's about it I think :unsure:

EDIT:

Thanks to the contributors to these threads

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

If you just open the html file, does that work for you?

Not exactly. I get one table with 6 cells. They look pretty odd. There's no encoding information tags or whatever in the html and both IE and firefox don't seem to understand it. Running XP SP3. My version of IE is pretty ancient.... version 8.0.6001, I hardly ever use it.

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