我正在寻找一种方法来阻止计算机上的某些网页。如果用户访问,请说出http://www.google.com/,然后会出现一个错误窗口(如果错误在浏览器内或单独的表单上,我不介意)。某些防病毒/网络保护软件会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")
我的应用程序具有管理员权限,可以复制/删除系统文件。
我正在使用 VB.NET WinForms ,Visual Studio 2013。
谢谢,
FWhite
答案 0 :(得分:0)
您正准备阻止某个网站并获得通知 用户访问被阻止的网站时。首先你阻止了 您的主机文件中的网站告诉用户的计算机去 localhost(127.0.0.1)而不是想要的网站。当你去 http://example.com/域名服务器看到您正在尝试 连接并提供相应的数据(通常是网站)。
在这种情况下,我们将运行自己的服务器,而不是向用户提供任何数据,我们将使用传入连接作为触发器,以查看用户是否正在尝试访问被阻止的网站。
第1步:
使用hosts文件阻止网站并将其重定向到localhost(127.0.0.1)
<强> C:\ Windows \ System32下\驱动程序\等强>
# 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上测试。
注意。强> 然而,这有一个问题,至少是一个非常小的问题。 这样做的问题在于,通过编辑hosts文件,您只能重定向用户被重定向的位置 - 而不是重定向到哪个端口(使我们在端口80上运行服务器)。问题是,如果用户已经在127.0.0.1端口80上运行服务器该怎么办?这是你可能想要考虑的事情,虽然它可能不会发生 - 它可能会发生。