1

我目前正在编写一个打开 wifi 热点并启用 Internet 连接共享的程序。我已经完成了所有这些工作netsh wlan set hostednetwork以及一些 WinAPI 调用。

现在我希望能够在客户端连接或阻止某些网站/端口/等时显示登录页面。

我对此的想法是监视我(接入点)和客户端之间的连接,并且每当端口 80 上的数据包发生时,我将我的登录页面或“网站被阻止”页面发送回而不是真实页面。目前该程序能够监控连接,但我不知道如何将其发送回客户端。

这是我的代码(有点长):

Dim myIpaddress As IPAddress
Dim theSocket As Socket
Dim receiveBuffer(4096) As Byte

Dim content As String = ""
Dim protocol As String = ""
Dim ipFrom As IPAddress
Dim ipTo As IPAddress
Dim portFrom As UInteger
Dim portTo As UInteger

Sub Listen()
    theSocket = New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)
    For Each int As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces
        If int.Description.Contains("Hosted") Then
            For Each adress As UnicastIPAddressInformation In int.GetIPProperties.UnicastAddresses
                If adress.Address.AddressFamily = AddressFamily.InterNetwork Then
                    myIpaddress = adress.Address
                    BindSocket()
                End If
            Next
        End If
    Next

End Sub

Sub BindSocket()
    Try
        theSocket.Bind(New IPEndPoint(myIpaddress, 0))
        theSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, True)
        theSocket.IOControl(IOControlCode.ReceiveAll, {1, 0, 0, 0}, {1, 0, 0, 0})
        receiveBuffer = New Byte(theSocket.ReceiveBufferSize) {}
        theSocket.BeginReceive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
    Catch ex As Exception
    End Try
End Sub

Private Sub OnReceive(ByVal asyncresult As IAsyncResult)

    'Get Length of packet (including header)
    Dim readlength As UInteger = BitConverter.ToUInt16(Byteswap(receiveBuffer, 2), 0)
    portTo = BitConverter.ToUInt16(Byteswap(receiveBuffer, 22), 0)
    portFrom = BitConverter.ToUInt16(Byteswap(receiveBuffer, 24), 0)

    'Get Protocol Type
    If receiveBuffer(9) = 6 Then
        protocol = "TCP"
    ElseIf receiveBuffer(9) = 17 Then
        protocol = "UDP"
    Else
        protocol = "???"
    End If

    'Get IP from and to
    ipFrom = New IPAddress(BitConverter.ToUInt32(receiveBuffer, 12))
    ipTo = New IPAddress(BitConverter.ToUInt32(receiveBuffer, 16))

    content = ""
    For i = 26 To readlength - 1
        If Char.IsLetterOrDigit(Chr(receiveBuffer(i))) = True Then
            content = content & Chr(receiveBuffer(i))
        Else
            content = content & "."
        End If
    Next


    'TODO how to send my content to the client?

    'Restart the Receiving
    theSocket.BeginReceive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
End Sub

Private Function Byteswap(ByVal bytez() As Byte, ByVal index As UInteger)
    Dim result(1) As Byte
    result(0) = bytez(index + 1)
    result(1) = bytez(index)
    Return result
End Function

有任何想法吗?

4

0 回答 0