Proxy server..
Hej jeg er ved at prøve at lave en proxy server..men jeg kan ikke få ssl del til at virker og er lidt usikker på hvordan jeg skal løse denne opgave..
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Security.Cryptography.X509Certificates
Imports System.Net.Security
Imports System.Threading
Module ProxyServer
' Liste over godkendte websteder
Private approvedWebsites As List(Of String) = New List(Of String) From {
"anotherexample.com"
}
Sub Main()
' Start listening for incoming connection requests
Dim listener As New TcpListener(IPAddress.Any, 8080)
listener.Start()
Console.WriteLine("Proxy server started on port 8080...")
While True
' Accept incoming client connection
Dim client As TcpClient = listener.AcceptTcpClient()
Dim thread As New Thread(Sub() HandleClient(client))
thread.Start()
End While
End Sub
Private Sub HandleClient(ByVal client As TcpClient)
Try
' Get the client stream
Dim clientStream As NetworkStream = client.GetStream()
Dim reader As New StreamReader(clientStream)
Dim writer As New StreamWriter(clientStream)
' Read the request line
Dim requestLine As String = reader.ReadLine()
Dim requestParts As String() = requestLine.Split(" "c)
Dim method As String = requestParts(0)
Dim url As String = requestParts(1)
Dim version As String = requestParts(2)
' Parse the URL
Dim uri As New Uri(url)
Dim host As String = uri.Host
' Check if the host is approved
If Not IsApprovedWebsite(host) Then
' Send blocking page
SendBlockingPage(writer)
client.Close()
Return
End If
Dim port As Integer = If(uri.Scheme = "https", 443, 80)
' Establish a connection to the target server
Dim targetClient As New TcpClient(host, port)
Dim targetStream As NetworkStream = targetClient.GetStream()
Dim targetStream2
' Handle SSL/TLS if needed
If uri.Scheme = "https" Then
Dim sslStream As New SslStream(targetStream, False, New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate), Nothing)
sslStream.AuthenticateAsClient(host)
targetStream2 = sslStream
End If
' Forward the request to the target server
Dim targetWriter As New StreamWriter(targetStream)
targetWriter.WriteLine(requestLine)
While True
Dim headerLine As String = reader.ReadLine()
If String.IsNullOrEmpty(headerLine) Then
Exit While
End If
targetWriter.WriteLine(headerLine)
End While
targetWriter.WriteLine()
targetWriter.Flush()
' Forward the response to the client
Dim targetReader As New StreamReader(targetStream)
While True
Dim responseLine As String = targetReader.ReadLine()
If responseLine Is Nothing Then
Exit While
End If
writer.WriteLine(responseLine)
writer.Flush()
End While
' Close the connections
client.Close()
targetClient.Close()
Catch ex As Exception
Console.WriteLine("Error: " & ex.Message)
End Try
End Sub
' Check if the website is approved
Private Function IsApprovedWebsite(ByVal host As String) As Boolean
Return approvedWebsites.Contains(host.ToLower())
End Function
' Send a blocking page to the client
Private Sub SendBlockingPage(ByVal writer As StreamWriter)
writer.WriteLine("HTTP/1.1 403 Forbidden")
writer.WriteLine("Content-Type: text/html;charset=UTF-8")
writer.WriteLine("Connection: close")
writer.WriteLine()
writer.WriteLine("<html><head><title>403 Forbidden</title></head>")
writer.WriteLine("<body><h1>Access Denied</h1>")
writer.WriteLine("<p>You do not have permission to access this site.</p>")
writer.WriteLine("</body></html>")
writer.Flush()
End Sub
' The following method is invoked by the RemoteCertificateValidationCallback delegate.
Public Function ValidateServerCertificate(sender As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean
' Implement your own certificate validation logic here
Return True
End Function
End Module
få fejl i denne
' Handle SSL/TLS if needed
If uri.Scheme = "https" Then
Dim sslStream As New SslStream(targetStream, False, New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate), Nothing)
sslStream.AuthenticateAsClient(host)
targetStream = sslStream <-- her..
End If
lige pt kan jeg kun få ikke http sider til at køre..