Hostednetwork显示登录页面

时间:2017-04-27 17:17:44

标签: vb.net wifi hotspot

我正在编写一个打开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

有什么想法吗?

0 个答案:

没有答案