Results 1 to 6 of 6

Thread: How can I display a MessageBox when a website being searched is blocked by host?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    51

    How can I display a MessageBox when a website being searched is blocked by host?

    I have an application which adds text to the hosts file from a .txt file full of blacklisted websites.


    It works, however, when the website is blocked, I want a Message to come up saying "Website Blocked". This is my code:


    Code:
     Public Function Initizalize() As String
            Dim systemroot As String
            Dim root As String
            systemroot = Environment.GetEnvironmentVariable("SystemRoot")
            root = (systemroot + "\system32\drivers\etc\hosts")
            Return root
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dir As String
            dir = Me.Initizalize
            Dim systemroot As String
            Dim root As String
            systemroot = Environment.GetEnvironmentVariable("SystemRoot")
            root = (systemroot + "\system32\drivers\etc\hosts")
            Dim line As String
            Dim sr = New StreamReader("whitelistsites.txt")
            line = sr.ReadLine
            Dim filePath = "C:\Windows\System32\drivers\etc\hosts"
    
            File.WriteAllLines(filePath, File.ReadAllLines(filePath).Where(Function(s) s <> line))
            File.AppendAllText(dir, (Environment.NewLine _
                            + (line + Environment.NewLine)))
    
            While ((Not (line) Is Nothing) _
                        AndAlso (line <> ""))
                MessageBox.Show(("Appeanding Line: " + line))
                File.AppendAllText(dir, (line + Environment.NewLine))
                line = sr.ReadLine
    
            End While
    
            sr.Close()
            MessageBox.Show("Done!")
        End Sub

    So, what I'm trying to say, is that when a website is searched [EDITED]FROM[/EDITED] the host file, it goes to the
    ERR_CONNECTION_REFUSED
    page on Chrome, then, once it goes to that page, I want a MessageBox to appear. Is there a way to do this?

    I know, I've mentioned the Initialize code twice, but, I'm just going to leave it. Please comment for any more info. I didn't explain this very well, I know.

    Maybe Google provides an API to do this? But what about for ALL browsers?
    Last edited by Modulus; Aug 3rd, 2018 at 05:41 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,936

    Re: How can I display a MessageBox when a website being searched is blocked by host?

    I have no idea on your actual question, and I doubt it is possible (because you aren't involved in the actual blocking at all). There may be some API for chrome (or some other way of detecting that it has gone to that page) or even a Windows API to detect the issue, but I haven't heard of anything like it.

    Quote Originally Posted by Modulus View Post
    I know, I've mentioned the Initialize code twice, but, I'm just going to leave it. Please comment for any more info. I didn't explain this very well, I know.
    Not only have you 'mentioned' it twice (never a good sign), but you also don't make use of it at all.

    It would be better to write it like this:
    Code:
        Public Function GetHostsPath() As String
            Dim systemroot As String
            Dim root As String
            systemroot = Environment.GetEnvironmentVariable("SystemRoot")
            root = (systemroot & "\system32\drivers\etc\hosts")
            Return root
        End Function
    (the name describes what it does, and the & is what you should always use to join strings together, because + does strange things in some situations)

    ...and then to use it, instead of this:
    Code:
            Dim filePath = "C:\Windows\System32\drivers\etc\hosts"
    ...do this:
    Code:
            Dim filePath = GetHostsPath

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    51

    Re: How can I display a MessageBox when a website being searched is blocked by host?

    Ok, thanks for the tip.


    Quick question, how do anti-viruses abort connections to malware sites and then show a message? This is what I'm trying to accomplish. See with Avast how they block website connections and then show the threat message, how do you do that?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,936

    Re: How can I display a MessageBox when a website being searched is blocked by host?

    I would assume that anti-viruses do it by including a full firewall within their application, and when an attempt is made to visit a blacklisted site, they report that the connection was aborted (to the user and/or the application) and/or instead of getting data from the site simply return their own HTML for a web page that shows an "aborted" type message.

    A firewall isn't something that is wise to write until you have a lot of experience, because even minor mistakes can cause big problems, including making the internet painfully slow (and some sites that use ajax wont return as much data, so you'll see less), or even disabling internet access until Windows is re-installed.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2018
    Posts
    51

    Re: How can I display a MessageBox when a website being searched is blocked by host?

    Ok, right.

    I've actually found something which may help me. Gonna test it outt: https://stackoverflow.com/questions/...lay-error-page

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,936

    Re: How can I display a MessageBox when a website being searched is blocked by host?

    I had a few minutes to spare, so I made some edits to the code you posted in #1 to reduce various problems:
    Code:
        Public Function GetHostsPath() As String
            Dim systemroot As String
            Dim root As String
            systemroot = Environment.GetEnvironmentVariable("SystemRoot")
            root = (systemroot & "\system32\drivers\etc\hosts")
            Return root
        End Function
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim sr = New StreamReader("a full path here!\whitelistsites.txt")
            Dim line As String 
            Dim filePath = GetHostsPath
    
            line = sr.ReadLine
            File.WriteAllLines(filePath, File.ReadAllLines(filePath).Where(Function(s) s <> line))
    
            While (Not line Is Nothing) _
                        AndAlso (line <> "")
                MessageBox.Show("Appeanding Line: " & line)
                File.AppendAllText(filePath, Environment.NewLine & line)
                line = sr.ReadLine
            End While
    
            sr.Close()
            MessageBox.Show("Done!")
        End Sub
    In addition to fixing the things I mentioned in post #2, this has eliminated lots of unnecessary brackets (the ones remaining on the While loop are a sensible style choice, and seems to be yours), highlighted an issue with the first file location (without a full path you are relying on luck to get the file you wanted), and eliminated one source of duplication you are causing in the hosts file (you were adding a line before the While loop, then adding the same line again inside the loop).

    I also reduced the amount of new lines added to the hosts file, because your program would add extra blank lines every time it ran. If you particularly want the extra blank lines, append just a blank line (either before or after the loop).

    This also doesn't eliminate most of the duplication you are causing, because you aren't doing what I recommended in your previous thread, which is to avoid writing duplicates in the first place: http://www.vbforums.com/showthread.p...t=#post5307033
    The WriteAllLines/ReadAllLines method you are using is a very poor alternative, and still would be even if you hadn't made a mistake (which I wont clarify, because I don't want to help you continue with bad code).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width