【问题标题】:Block webpages and display error page阻止网页并显示错误页面
【发布时间】:2014-12-29 08:55:18
【问题描述】:

我正在寻找一种方法来阻止计算机上的某些网页。如果用户访问,比如说http://www.google.com/,则会出现一个错误窗口(我不介意错误是在浏览器内部还是在单独的表单上)。一些防病毒/网络保护软件可以this:请注意,我只对阻止少量特定数量的页面感兴趣。

我已经在网上搜索过这个。我发现了很多例子,主要是关于 hosts 文件。 但是,当用户进入“禁止”页面时,不会显示错误页面。

编辑 hosts 文件的代码如下:

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

【问题讨论】:

  • 您可以尝试的一件事是非常有限且非常糟糕 - 但几乎唯一的方法是阅读互联网浏览器窗口的标题(在 chrome 中它表示提供的标题通过网页)并据此采取行动。 stackoverflow.com/questions/3131303/…
  • 我发现了其他可能是解决此问题的方法 - 修改主机文件,您显然可以重定向到某个站点。 (trishtech.com/2013/03/…) 这个“站点”可能是本地的。如果您熟悉 Web 开发,那么您可以编写要显示的错误页面 - 如果您希望它打开程序,您可以使网页重定向到使用称为 URI 协议 (stackoverflow.com/questions/389204/…) 的方式打开程序的链接.
  • 如果您需要帮助实现它,请告诉我。
  • 嗨,亚历克斯!感谢您的回复。我看过那些网站。第一个告诉我可以将浏览器重定向到另一个本地页面。它会像这样工作吗:C:\myfile.html badsite.comas HOSTS 线模型?再次感谢您的帮助
  • 看来我有点不正确,您可以像那样重定向 - 但不能重定向到文件,它必须是服务器。但是,您可以在本地计算机 (127.0.0.1) 上打开服务器并在 hosts 文件中写入:127.0.0.1 www.example.com。我会尝试寻找其他解决方案。

标签: vb.net


【解决方案1】:

如何阻止网页并在这样做时引发事件。

您正在阻止网站并被告知 每当用户访问被阻止的站点时。首先你阻止 主机文件中的网站告诉用户的计算机去 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 上运行服务器怎么办?这是您可能需要考虑的事情,尽管它可能不会发生 - 有可能会发生。

【讨论】:

  • Alex,这简直太棒了!它完美运行,非常感谢!我想问你最后一件事(仅在可能的情况下:)):我可以在警告消息框上显示已被阻止的地址吗?例如,如果我阻止 google.com,则 MessageBox 将是“您无法访问 google.com!”。无论如何,非常感谢您的非凡帮助! FWhite
  • 在我提出这个代码的测试中(基于网络服务器),您可以获得资源名称(即 www.example.com/this_is_the_resource),但不能获得请求的 url。
  • 非常感谢,你真的帮了我!
猜你喜欢
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 2020-10-26
  • 1970-01-01
相关资源
最近更新 更多