Reputation: 141
I'm looking for a way to block some webpages on a computer. If the user visits, let's say, http://www.google.com/, then an error window will appear (I don't mind if the error is inside the browser or on a separate form). Some antiviruses/web protection softwares do this: please note that I'm interested only in blocking a little and specific number of pages.
I've already searched on the web for this. I found lots of examples, mostly about hosts file. However, nothing on displaying an error page when the user goes into a "forbidden" page.
The code to edit the hosts file is this:
Dim s As String = My.Computer.FileSystem.ReadAllText("C:\Windows\system32\drivers\etc\hosts")
Dim finalFile As String = s & VbCrLf & "127.0.0.1 http://www.example.com/"
My.Computer.FileSystem.WriteAllText(finalFile, "z:\desktop\hosts")
My.Computer.FileSystem.DeleteFile("C:\Windows\system32\drivers\etc\hosts")
My.Computer.FileSystem.CopyFile("z:\desktop\hosts", "C:\Windows\system32\drivers\etc\hosts")
My application has administrator privileges to copy/delete system files.
I'm working with VB.NET WinForms, Visual Studio 2013.
Thanks,
FWhite
Upvotes: 0
Views: 3055
Reputation: 636
You are well on your way to blocking a website and getting told whenever the user visits a blocked site. Firstly you block the website in your hosts file telling the user's computer to go to localhost (127.0.0.1) instead of the wanted website. When you go to http://example.com/ the domain's server sees that you're trying to connect and feed you the corresponding data ( usually a website ).
In this case we will run our own server and instead of feeding the user any data, we will just use the incoming connection as a triggerer to see that the user is trying to access a blocked website.
Step 1:
Block the website and redirect it to localhost (127.0.0.1) using the hosts file ( Windows )
C:\Windows\System32\drivers\etc
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
# This is our blocked site
127.0.0.1 example.com
This will make all the calls to http://example.com redirected to our local machine, port 80.
To intercept this traffic we setup a server running on the local machine, port 80.
Step 2:
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Public Class Form1
Private Sub StartBlocker()
'Declare a BlockListener
Dim blocker As BlockListener
'Declare a thread to run the server on
Dim thread As Thread
'Set the blocker to be a new "Block Listener"
blocker = New BlockListener
'Set the current thread to be a new thread ( so that there is no noticable performance drop for the main window )
'It runs the function blocker.listen ( further described below )
thread = New Thread(New ThreadStart(AddressOf blocker.listen))
'Start the thread to run the function
thread.Start()
'Add a handler to handle a function whenever a user is blocked ( connected )
'The event's name is Blocked and the callback function is Restart
AddHandler blocker.Blocked, AddressOf User_Blocked
End Sub
'The function that handles whenever a user is blocked ( connected )
Private Sub User_Blocked()
'This function is called whenever a user is blocked
MessageBox.Show("Blocked")
'NOTE
'For some reasons, sometimes there are more than one connection meaning
'that the Blocked event is raised more than once in turn resulting in this function being called multiple times
'usually 2-3 when the user actually only connects once.
'To deal with this you can simply wait one second before you listen for connections again
End Sub
End Class
'The class to act as the blocker
Public Class BlockListener
'The block to run the server on ( must be 80 to work with hosts file and browsers, 80 is default for webservers )
Private port As Integer = 80
'Declare a TcpListener called listener to act as the actual server
Private listener As TcpListener
'Declare a boolean for turning the server's blocking capabilities on or off
Private BlockUsers As Boolean = True
'Declare the event to be called whenever a user is blocked ( connected )
Public Event Blocked As EventHandler
'The main function of the BlockListener : listen | listen for incoming connections
Public Sub listen()
'Create a new listener to act as a server on the localhost with 80 as port
listener = New TcpListener(IPAddress.Parse("127.0.0.1"), port)
'Start the listener
listener.Start()
'For as long as BlockUsers is true / for as long as you should block users
While (BlockUsers)
'Create a connection to the user and wait for the user to connect
Dim clientConnection As TcpClient = listener.AcceptTcpClient
'Whenever the user connects, close the connection directly
clientConnection.Close()
'Raise the "Blocked" event with no event arguments, so that you can easily handle your blocking elsewhere
RaiseEvent Blocked(Me, EventArgs.Empty)
End While
'When the BlockListener should no longer listen for incoming connections - stop the server ( for performance and compatibility )
listener.Stop()
End Sub
End Class
Whenever a user tries to access a blocked site, this program will get the request and raise an event. Tested on Windows 7 x64 running Visual Studio 2012.
Note. There are, however one problem with this, well at least a very small one. The problem with this is that by editing the hosts file, you can only redirect where the user gets redirected - not to which port ( making us run the server on port 80 ). The problem with this is, what if the user is already running a server on 127.0.0.1 port 80 already? This is something that you might want to take into account, although it probably won't happen - there might be a chance it will.
Upvotes: 1