Last active
January 18, 2017 08:06
-
-
Save Horusiath/8f32ac2c9f1d0df2b67eb5ceb07060b4 to your computer and use it in GitHub Desktop.
DotNetty example of failing TLS layer support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Threading.Tasks; | |
using DotNetty.Buffers; | |
using DotNetty.Codecs; | |
using DotNetty.Common.Internal.Logging; | |
using DotNetty.Handlers.Logging; | |
using DotNetty.Handlers.Tls; | |
using DotNetty.Transport.Bootstrapping; | |
using DotNetty.Transport.Channels; | |
using DotNetty.Transport.Channels.Sockets; | |
using Microsoft.Extensions.Logging.Console; | |
namespace DotNettyTestClient | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Task.Run(RunClientAsync).Wait(); | |
} | |
private static async Task RunClientAsync() | |
{ | |
InternalLoggerFactory.DefaultFactory.AddProvider(new ConsoleLoggerProvider((s, level) => true, false)); | |
var clientEventLoopGroup = new MultithreadEventLoopGroup(); | |
var cert = new X509Certificate2("../../../test-cert", default(string), X509KeyStorageFlags.DefaultKeySet); | |
var targetHost = cert.GetNameInfo(X509NameType.DnsName, false); | |
try | |
{ | |
var bootstrap = new Bootstrap() | |
.Group(clientEventLoopGroup) | |
.Option(ChannelOption.SoReuseaddr, true) | |
.Option(ChannelOption.SoKeepalive, true) | |
.Option(ChannelOption.TcpNodelay, true) | |
.Handler(new LoggingHandler()) | |
.Option(ChannelOption.ConnectTimeout, TimeSpan.FromSeconds(15)) | |
.ChannelFactory(() => new TcpSocketChannel(AddressFamily.InterNetwork)) | |
.Handler(new ActionChannelInitializer<TcpSocketChannel>(channel => | |
{ | |
var pipeline = channel.Pipeline; | |
//comment this line to disable TLS | |
pipeline.AddLast("TlsHandler", TlsHandler.Client(targetHost, cert)); | |
pipeline.AddLast("FrameDecoder", new LengthFieldBasedFrameDecoder(128000, 0, 4, 0, 4)); | |
pipeline.AddLast("FrameEncoder", new LengthFieldPrepender(4, false)); | |
var handler = new TcpClientHandler(); | |
pipeline.AddLast("ClientHandler", handler); | |
})); | |
var bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 1337)); | |
var input = File.ReadAllBytes("../../../test-file.txt"); | |
var buffer = Unpooled.WrappedBuffer(input); | |
//HERE!: when TLS is enabled, this methods hangs forever | |
await bootstrapChannel.WriteAndFlushAsync(buffer); | |
await bootstrapChannel.CloseAsync(); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Exception occurred: " + e); | |
} | |
finally | |
{ | |
await clientEventLoopGroup.ShutdownGracefullyAsync(); | |
} | |
} | |
} | |
internal class TcpClientHandler : ChannelHandlerAdapter | |
{ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Sockets; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Text; | |
using System.Threading.Tasks; | |
using DotNetty.Buffers; | |
using DotNetty.Codecs; | |
using DotNetty.Common.Internal.Logging; | |
using DotNetty.Common.Utilities; | |
using DotNetty.Handlers.Logging; | |
using DotNetty.Handlers.Tls; | |
using DotNetty.Transport.Bootstrapping; | |
using DotNetty.Transport.Channels; | |
using DotNetty.Transport.Channels.Sockets; | |
using Microsoft.Extensions.Logging.Console; | |
namespace DotNettyTestServer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Task.Run(StartServerAsync).Wait(); | |
} | |
private static async Task StartServerAsync() | |
{ | |
InternalLoggerFactory.DefaultFactory.AddProvider(new ConsoleLoggerProvider((s, level) => true, false)); | |
var size = Math.Min(Math.Max(Environment.ProcessorCount, 2), 2); | |
var serverEventLoopGroup = new MultithreadEventLoopGroup(size); | |
var cert = new X509Certificate2("../../../test-cert", default(string), X509KeyStorageFlags.DefaultKeySet); | |
try | |
{ | |
var bootstrap = new ServerBootstrap() | |
.Group(serverEventLoopGroup) | |
.Option(ChannelOption.SoReuseaddr, true) | |
.Option(ChannelOption.SoKeepalive, true) | |
.Option(ChannelOption.TcpNodelay, true) | |
.Option(ChannelOption.ConnectTimeout, TimeSpan.FromSeconds(15)) | |
.Option(ChannelOption.SoBacklog, 4096) | |
.ChannelFactory(() => new TcpServerSocketChannel(AddressFamily.InterNetwork)) | |
.ChildHandler(new ActionChannelInitializer<TcpSocketChannel>(channel => | |
{ | |
var pipeline = channel.Pipeline; | |
// comment this line to disable TLS | |
pipeline.AddLast("TlsHandler", TlsHandler.Server(cert)); | |
pipeline.AddLast("FrameDecoder", new LengthFieldBasedFrameDecoder(128000, 0, 4, 0, 4)); | |
pipeline.AddLast("FrameEncoder", new LengthFieldPrepender(4, false)); | |
pipeline.AddLast("LoggingHandler", new LoggingHandler()); | |
var handler = new TcpServerHandler(); | |
pipeline.AddLast("ServerHandler", handler); | |
})); | |
var bootstrapChannel = await bootstrap.BindAsync(1337); | |
Console.ReadLine(); | |
await bootstrapChannel.CloseAsync(); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Exception occurred: " + e); | |
} | |
finally | |
{ | |
await serverEventLoopGroup.ShutdownGracefullyAsync(); | |
} | |
} | |
} | |
internal class TcpServerHandler : ChannelHandlerAdapter | |
{ | |
public override void ChannelRead(IChannelHandlerContext context, object message) | |
{ | |
var buf = (IByteBuffer)message; | |
if (buf.ReadableBytes > 0) | |
{ | |
var str = Encoding.UTF8.GetString(buf.Array, buf.ArrayOffset + buf.ReaderIndex, buf.ReadableBytes); | |
Console.WriteLine(str); | |
} | |
// decrease the reference count to 0 (releases buffer) | |
ReferenceCountUtil.SafeRelease(message); | |
base.ChannelRead(context, message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment