如何阻止网页并在这样做时引发事件。
您正在阻止网站并被告知
每当用户访问被阻止的站点时。首先你阻止
主机文件中的网站告诉用户的计算机去
localhost (127.0.0.1) 而不是想要的网站。当你去
http://example.com/ 域的服务器看到您正在尝试
连接并为您提供相应的数据(通常是网站)。
在这种情况下,我们将运行我们自己的服务器,而不是向用户提供任何数据,我们将使用传入连接作为触发器来查看用户正在尝试访问被阻止的网站。
第 1 步:
使用 hosts 文件 (Windows) 阻止网站并将其重定向到 localhost (127.0.0.1)
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
这将使所有对http://example.com 的调用重定向到我们的本地机器端口 80。
为了拦截这些流量,我们在本地机器的 80 端口上设置了一个服务器。
第 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
每当用户尝试访问被阻止的站点时,此程序都会收到请求并引发事件。
在运行 Visual Studio 2012 的 Windows 7 x64 上测试。
注意。
然而,这有一个问题,至少是一个非常小的问题。
这样做的问题是,通过编辑主机文件,您只能重定向用户被重定向的位置 - 而不是重定向到哪个端口(让我们在端口 80 上运行服务器)。问题是,如果用户已经在 127.0.0.1 端口 80 上运行服务器怎么办?这是您可能需要考虑的事情,尽管它可能不会发生 - 有可能会发生。