From 2f4564515ca0991dae5edddb920203918280a9e8 Mon Sep 17 00:00:00 2001 From: dudu Date: Sat, 31 Aug 2019 14:12:15 +0800 Subject: [PATCH 1/3] Change files encoding --- Enyim.Caching/Memcached/IAuthenticator.cs | 2 +- Enyim.Caching/Memcached/MemcachedNode.cs | 1 + Enyim.Caching/Memcached/Protocol/Binary/SaslContinue.cs | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Enyim.Caching/Memcached/IAuthenticator.cs b/Enyim.Caching/Memcached/IAuthenticator.cs index 75f7b461..1098995e 100644 --- a/Enyim.Caching/Memcached/IAuthenticator.cs +++ b/Enyim.Caching/Memcached/IAuthenticator.cs @@ -11,7 +11,7 @@ public interface IAuthenticator #region [ License information ] /* ************************************************************ * - * Copyright (c) 2010 Attila Kiskó, enyim.com + * Copyright (c) 2010 Attila Kisk? enyim.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/Enyim.Caching/Memcached/MemcachedNode.cs b/Enyim.Caching/Memcached/MemcachedNode.cs index 4c6c69f0..108d5ee8 100755 --- a/Enyim.Caching/Memcached/MemcachedNode.cs +++ b/Enyim.Caching/Memcached/MemcachedNode.cs @@ -369,6 +369,7 @@ private async Task CreateSocketAsync() return ps; } + private PooledSocket CreateSocket() { var ps = this.ownerNode.CreateSocket(); diff --git a/Enyim.Caching/Memcached/Protocol/Binary/SaslContinue.cs b/Enyim.Caching/Memcached/Protocol/Binary/SaslContinue.cs index c8f529d7..306f2844 100644 --- a/Enyim.Caching/Memcached/Protocol/Binary/SaslContinue.cs +++ b/Enyim.Caching/Memcached/Protocol/Binary/SaslContinue.cs @@ -33,7 +33,7 @@ protected override BinaryRequest Build() #region [ License information ] /* ************************************************************ * - * Copyright (c) 2010 Attila Kiskó, enyim.com + * Copyright (c) 2010 Attila Kisk? enyim.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 36e7de84948a117a4e9deb25098ffaf73b86051d Mon Sep 17 00:00:00 2001 From: dudu Date: Sat, 31 Aug 2019 14:19:39 +0800 Subject: [PATCH 2/3] Override CreateSocketAsync in BinaryNode --- Enyim.Caching/Memcached/MemcachedNode.cs | 1 + .../Memcached/Protocol/Binary/BinaryNode.cs | 43 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Enyim.Caching/Memcached/MemcachedNode.cs b/Enyim.Caching/Memcached/MemcachedNode.cs index 108d5ee8..13f11dbd 100755 --- a/Enyim.Caching/Memcached/MemcachedNode.cs +++ b/Enyim.Caching/Memcached/MemcachedNode.cs @@ -768,6 +768,7 @@ protected internal virtual PooledSocket CreateSocket() } } + protected internal virtual async Task CreateSocketAsync() { try diff --git a/Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs b/Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs index 2e5df816..10356fb4 100644 --- a/Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs +++ b/Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs @@ -7,6 +7,7 @@ using Enyim.Collections; using System.Security; using Microsoft.Extensions.Logging; +using System.Threading.Tasks; namespace Enyim.Caching.Memcached.Protocol.Binary { @@ -36,7 +37,21 @@ protected internal override PooledSocket CreateSocket() { var retval = base.CreateSocket(); - if (this.authenticationProvider != null && !this.Auth(retval)) + if (this.authenticationProvider != null && !Auth(retval)) + { + _logger.LogError("Authentication failed: " + this.EndPoint); + + throw new SecurityException("auth failed: " + this.EndPoint); + } + + return retval; + } + + protected internal override async Task CreateSocketAsync() + { + var retval = await base.CreateSocketAsync(); + + if (this.authenticationProvider != null && !(await AuthAsync(retval))) { _logger.LogError("Authentication failed: " + this.EndPoint); @@ -76,6 +91,32 @@ private bool Auth(PooledSocket socket) return true; } + + private async Task AuthAsync(PooledSocket socket) + { + SaslStep currentStep = new SaslStart(this.authenticationProvider); + + await socket.WriteAsync(currentStep.GetBuffer()); + + while (!(await currentStep.ReadResponseAsync(socket)).Success) + { + // challenge-response authentication + if (currentStep.StatusCode == 0x21) + { + currentStep = new SaslContinue(this.authenticationProvider, currentStep.Data); + await socket.WriteAsync(currentStep.GetBuffer()); + } + else + { + _logger.LogWarning("Authentication failed, return code: 0x{0:x}", currentStep.StatusCode); + + // invalid credentials or other error + return false; + } + } + + return true; + } } } From 49a1681511e0fe4915468983d9e5e7e8767ae631 Mon Sep 17 00:00:00 2001 From: dudu Date: Sat, 31 Aug 2019 14:27:12 +0800 Subject: [PATCH 3/3] Tweak UseEnyimMemcached --- Enyim.Caching/EnyimMemcachedApplicationBuilderExtensions.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Enyim.Caching/EnyimMemcachedApplicationBuilderExtensions.cs b/Enyim.Caching/EnyimMemcachedApplicationBuilderExtensions.cs index cf7ec189..644b9c8b 100644 --- a/Enyim.Caching/EnyimMemcachedApplicationBuilderExtensions.cs +++ b/Enyim.Caching/EnyimMemcachedApplicationBuilderExtensions.cs @@ -17,9 +17,7 @@ public static IApplicationBuilder UseEnyimMemcached(this IApplicationBuilder app var logger = app.ApplicationServices.GetService>(); try { - var client = app.ApplicationServices.GetRequiredService(); - client.GetValueAsync("UseEnyimMemcached").Wait(); - Console.WriteLine("EnyimMemcached connected memcached servers."); + var client = app.ApplicationServices.GetRequiredService(); } catch (Exception ex) {