public static void Main(string[] args) { string hostname = "localhost"; int port = 5556; long time1 = DateTime.UtcNow.Ticks; /* * Note: This is the default PSK identity for 'openssl s_server' testing, the server must be * started with "-psk 6161616161" to make the keys match, and possibly the "-psk_hint" * option should be present. */ //string psk_identity = "Client_identity"; //byte[] psk = new byte[]{ 0x61, 0x61, 0x61, 0x61, 0x61 }; // These correspond to the configuration of MockPskTlsServer string psk_identity = "client"; byte[] psk = Strings.ToUtf8ByteArray("TLS_TEST_PSK"); BasicTlsPskIdentity pskIdentity = new BasicTlsPskIdentity(psk_identity, psk); MockPskTlsClient client = new MockPskTlsClient(null, pskIdentity); TlsClientProtocol protocol = OpenTlsConnection(hostname, port, client); protocol.Close(); long time2 = DateTime.UtcNow.Ticks; Console.WriteLine("Elapsed 1: " + (time2 - time1) / TimeSpan.TicksPerMillisecond + "ms"); client = new MockPskTlsClient(client.GetSessionToResume(), pskIdentity); protocol = OpenTlsConnection(hostname, port, client); long time3 = DateTime.UtcNow.Ticks; Console.WriteLine("Elapsed 2: " + (time3 - time2) / TimeSpan.TicksPerMillisecond + "ms"); byte[] req = Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\n\r\n"); Stream tlsStream = protocol.Stream; tlsStream.Write(req, 0, req.Length); tlsStream.Flush(); StreamReader reader = new StreamReader(tlsStream); String line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(">>> " + line); } protocol.Close(); }
private static void DoTestClientServer(bool fragment) { SecureRandom secureRandom = new SecureRandom(); TlsClientProtocol clientProtocol = new TlsClientProtocol(secureRandom); TlsServerProtocol serverProtocol = new TlsServerProtocol(secureRandom); clientProtocol.Connect(new MockTlsClient(null)); serverProtocol.Accept(new MockTlsServer()); // pump handshake bool hadDataFromServer = true; bool hadDataFromClient = true; while (hadDataFromServer || hadDataFromClient) { hadDataFromServer = PumpData(serverProtocol, clientProtocol, fragment); hadDataFromClient = PumpData(clientProtocol, serverProtocol, fragment); } // send data in both directions byte[] data = new byte[1024]; secureRandom.NextBytes(data); WriteAndRead(clientProtocol, serverProtocol, data, fragment); WriteAndRead(serverProtocol, clientProtocol, data, fragment); // close the connection clientProtocol.Close(); PumpData(clientProtocol, serverProtocol, fragment); serverProtocol.CloseInput(); CheckClosed(serverProtocol); CheckClosed(clientProtocol); }
private static void DoTestClientServer(bool fragment) { SecureRandom secureRandom = new SecureRandom(); TlsClientProtocol clientProtocol = new TlsClientProtocol(secureRandom); TlsServerProtocol serverProtocol = new TlsServerProtocol(secureRandom); clientProtocol.Connect(new MockTlsClient(null)); serverProtocol.Accept(new MockTlsServer()); // pump handshake bool hadDataFromServer = true; bool hadDataFromClient = true; while (hadDataFromServer || hadDataFromClient) { hadDataFromServer = PumpData(serverProtocol, clientProtocol, fragment); hadDataFromClient = PumpData(clientProtocol, serverProtocol, fragment); } // send data in both directions byte[] data = new byte[1024]; secureRandom.NextBytes(data); WriteAndRead(clientProtocol, serverProtocol, data, fragment); WriteAndRead(serverProtocol, clientProtocol, data, fragment); // close the connection clientProtocol.Close(); PumpData(clientProtocol, serverProtocol, fragment); CheckClosed(serverProtocol); CheckClosed(clientProtocol); }
public static void RunMainTests(string[] args) { string hostname = "localhost"; int port = 5556; long time1 = DateTime.UtcNow.Ticks; MockTlsClient client = new MockTlsClient(null); TlsClientProtocol protocol = OpenTlsConnection(hostname, port, client); protocol.Close(); long time2 = DateTime.UtcNow.Ticks; Console.WriteLine("Elapsed 1: " + (time2 - time1) / TimeSpan.TicksPerMillisecond + "ms"); client = new MockTlsClient(client.GetSessionToResume()); protocol = OpenTlsConnection(hostname, port, client); long time3 = DateTime.UtcNow.Ticks; Console.WriteLine("Elapsed 2: " + (time3 - time2) / TimeSpan.TicksPerMillisecond + "ms"); byte[] req = Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\n\r\n"); Stream tlsStream = protocol.Stream; tlsStream.Write(req, 0, req.Length); tlsStream.Flush(); StreamReader reader = new StreamReader(tlsStream); String line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(">>> " + line); } protocol.Close(); }
public override async Task CloseAsync() { if (State == ChannelState.Closed || State == ChannelState.ClosedReceived) { return; } State = ChannelState.ClosedReceived; try { if (protocol != null) { protocol.Close(); } } catch { } protocol = null; if (client != null && client.Client != null && (client.Client.Connected && client.Client.Poll(10, SelectMode.SelectRead))) { if (client.Client.UseOnlyOverlappedIO) { client.Client.DuplicateAndClose(Process.GetCurrentProcess().Id); } else { client.Close(); } } client = null; if (readConnection != null) { readConnection.Dispose(); } if (writeConnection != null) { writeConnection.Dispose(); } State = ChannelState.Closed; OnClose?.Invoke(this, new ChannelCloseEventArgs(Id)); await Task.CompletedTask; }
public HttpResponse Do(HttpRequest req) { using (var tcpClient = new TcpClient(req.RequestUri.Host, req.RequestUri.Port)) { if (req.RequestUri.Scheme == Uri.UriSchemeHttps) { var protocol = new TlsClientProtocol(tcpClient.GetStream(), new SecureRandom()); var tlsClient = new MyTlsClient(); protocol.Connect(tlsClient); var stream = protocol.Stream; WriteRequest(req, stream); var res = ReadResponse(stream); protocol.Close(); return(res); } else { var stream = tcpClient.GetStream(); WriteRequest(req, stream); var res = ReadResponse(stream); return(res); } } }