如何阻止网页并引发一个事件这样做的时候。
无论何时用户访问被阻止的网站,您都可以很好地阻止某个网站并收到 。首先,您在主机文件中阻止 网站,告诉用户的计算机去 localhost(127.0.0.1)而不是通缉网站。当您访问 http://example.com/时,域的服务器发现您正在尝试连接并向您提供相应的数据(通常是网站)。
在这种情况下,我们将运行我们自己的服务器,而不是向用户提供任何数据,我们将使用传入连接作为触发器来查看用户正在尝试访问被阻止的网站。
第1步:
块的网站,并将其重定向使用主机文件(Windows)
C(127.0.0.1)为localhost:\ 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上运行服务器,该怎么办?这是你可能想要考虑的事情,虽然它可能不会发生 - 它可能会有机会。
有一两件事你可以尝试,这是非常有限的,非常糟糕 - 但几乎做到这一点的唯一方法,就是阅读的互联网浏览器窗口的标题(在Chrome它说,通过网页提供的标题)并基于此采取行动。 http://stackoverflow.com/questions/3131303/how-to-get-the-window-title-of-a-process-using-vb-net – Alex 2014-11-02 19:54:12
我发现别的东西可能是解决这个问题的方法 - 修改主机文件,你显然可以重定向到某个网站。 (http://www.trishtech.com/2013/03/redirect-or-block-web-sites-using-hosts-file/)这个“网站”可能是本地的。如果您熟悉Web开发,那么您可以编写显示的错误页面 - 如果您希望它打开一个程序,您可以使网页重定向到一个链接,该链接使用称为URI协议(http:// http:// stackoverflow.com/questions/389204/how-do-i-create-my-own-url-protocol-eg-so)。 – Alex 2014-11-02 20:07:54
如果您需要帮助实施它,请告诉我。 – Alex 2014-11-02 20:08:52