Skip to content

Commit d6035cc

Browse files
authored
api calls hanging (#186)
* #178 Fix API calls hangs by checking Tls12 support or enforce Tls12 depending of the target: - net40 : check that (SecurityProtocolType)3072 is specified - net45 : check that SecurityProtocolType.Tls12 is specified - net46 : check that SecurityProtocolType.Tls12 is specified - net47 (target added) : check that SecurityProtocolType.Tls12 or SecurityProtocolType.UseDefault is specified - net471 (target added) : force usage of Tls12 in HttpClient ctor - netstandard1.3 : force usage of Tls12 in HttpClient ctor - netstandard2.0 : force usage of Tls12 in HttpClient ctor * #178 Update documentation about Tls 1.2 requirements
1 parent 5aefebc commit d6035cc

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

MegaApiClient.Tests/Context/TestContext.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5-
using System.Net.Http;
6-
using System.Threading;
7-
using System.Threading.Tasks;
85
using Xunit.Abstractions;
96

107
namespace CG.Web.MegaApiClient.Tests.Context
@@ -57,7 +54,7 @@ public void ClearLogger()
5754
protected virtual IMegaApiClient CreateClient()
5855
{
5956
Options = new Options(applicationKey: "ewZQFBBC");
60-
WebClient = new TestWebClient(new WebClient(WebTimeout, null, new TestMessageHandler(), false), MaxRetry, _logMessageAction);
57+
WebClient = new TestWebClient(new WebClient(WebTimeout, null, false), MaxRetry, _logMessageAction);
6158

6259
return new MegaApiClient(Options, WebClient);
6360
}
@@ -83,13 +80,5 @@ private void OnApiRequestFailed(object _, ApiRequestFailedEventArgs e)
8380
{
8481
_logMessageAction($"ApiRequestFailed: {e.ApiResult}, {e.ApiUrl}, {e.AttemptNum}, {e.RetryDelay}, {e.ResponseJson}, {e.Exception} {e.Exception?.Message}");
8582
}
86-
87-
private class TestMessageHandler : HttpClientHandler
88-
{
89-
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
90-
{
91-
return await base.SendAsync(request, cancellationToken);
92-
}
93-
}
9483
}
9584
}

MegaApiClient/MegaApiClient.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,33 @@
3131
</PropertyGroup>
3232

3333
<PropertyGroup>
34-
<TargetFrameworks>net40;net45;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
34+
<TargetFrameworks>net40;net45;net46;net47;net471;netstandard1.3;netstandard2.0</TargetFrameworks>
3535
<RootNamespace>CG.Web.MegaApiClient</RootNamespace>
3636
<SignAssembly>False</SignAssembly>
3737
<DocumentationFile>bin\docs\MegaApiClient.xml</DocumentationFile>
3838
<NoWarn>1591</NoWarn>
3939
</PropertyGroup>
4040

41-
<ItemGroup Condition="'$(TargetFramework)' == 'net40' Or '$(TargetFramework)' == 'net45' Or '$(TargetFramework)' == 'net46'">
41+
<ItemGroup Condition="$(TargetFramework.StartsWith('netstandard')) == 'false'">
4242
<Reference Include="System" />
4343
<Reference Include="System.Web" />
4444
</ItemGroup>
4545

46-
<ItemGroup Condition="'$(TargetFramework)' == 'net45' Or '$(TargetFramework)' == 'net46'">
46+
<ItemGroup Condition="$(TargetFramework.StartsWith('netstandard')) == 'false' And $(TargetFramework) != 'net40'">
4747
<Reference Include="System.Net.Http" />
4848
</ItemGroup>
4949

50-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3' Or '$(TargetFramework)' == 'netstandard2.0'">
50+
<ItemGroup Condition="$(TargetFramework.StartsWith('netstandard')) == 'true'">
5151
<PackageReference Include="System.Net.Http">
5252
<Version>4.3.4</Version>
5353
</PackageReference>
5454
</ItemGroup>
5555

56-
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
56+
<ItemGroup Condition="$(TargetFramework) == 'netstandard1.3'">
5757
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="1.1.2" />
5858
</ItemGroup>
5959

60-
<PropertyGroup Condition="'$(TargetFramework)' == 'net40' Or '$(TargetFramework)' == 'net45' Or '$(TargetFramework)' == 'net46'">
60+
<PropertyGroup Condition="$(TargetFramework.StartsWith('netstandard')) == 'false'">
6161
<SignAssembly>True</SignAssembly>
6262
<AssemblyOriginatorKeyFile>../key.snk</AssemblyOriginatorKeyFile>
6363
</PropertyGroup>
@@ -78,7 +78,7 @@
7878
<SourceRoot Include="$(MSBuildThisFileDirectory)/" />
7979
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
8080
<PrivateAssets>all</PrivateAssets>
81-
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
81+
<IncludeAssets>runtime;build;native;contentfiles;analyzers</IncludeAssets>
8282
</PackageReference>
8383
</ItemGroup>
8484

MegaApiClient/WebClient_HttpClient.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace CG.Web.MegaApiClient
77
using System.Reflection;
88
using System.Text;
99
using System.Threading;
10+
using System.Security.Authentication;
1011

1112
using System.Net.Http;
1213
using System.Net.Http.Headers;
@@ -18,14 +19,30 @@ public class WebClient : IWebClient
1819
private readonly HttpClient _httpClient;
1920

2021
public WebClient(int responseTimeout = DefaultResponseTimeout, ProductInfoHeaderValue userAgent = null)
21-
: this(responseTimeout, userAgent, null, false)
22+
: this(responseTimeout, userAgent, false)
2223
{
2324
}
2425

25-
internal WebClient(int responseTimeout, ProductInfoHeaderValue userAgent, HttpMessageHandler messageHandler, bool connectionClose)
26+
internal WebClient(int responseTimeout, ProductInfoHeaderValue userAgent, bool connectionClose)
2627
{
2728
BufferSize = Options.DefaultBufferSize;
28-
_httpClient = messageHandler == null ? new HttpClient() : new HttpClient(messageHandler);
29+
#if NET471 || NETSTANDARD
30+
_httpClient = new HttpClient(new HttpClientHandler { SslProtocols = SslProtocols.Tls12 }, true);
31+
#elif NET47
32+
if (!ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) && !ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.SystemDefault))
33+
{
34+
throw new NotSupportedException("mega.nz API requires support for TLS v1.2 or higher. Check https://gpailler.github.io/MegaApiClient/#compatibility for additional information");
35+
}
36+
37+
_httpClient = new HttpClient();
38+
#else
39+
if (!ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12))
40+
{
41+
throw new NotSupportedException("mega.nz API requires support for TLS v1.2 or higher. Check https://gpailler.github.io/MegaApiClient/#compatibility for additional information");
42+
}
43+
44+
_httpClient = new HttpClient();
45+
#endif
2946
_httpClient.Timeout = TimeSpan.FromMilliseconds(responseTimeout);
3047
_httpClient.DefaultRequestHeaders.UserAgent.Add(userAgent ?? GenerateUserAgent());
3148
_httpClient.DefaultRequestHeaders.ConnectionClose = connectionClose;

MegaApiClient/WebClient_HttpWebRequest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public class WebClient : IWebClient
1717

1818
public WebClient(int responseTimeout = DefaultResponseTimeout, string userAgent = null)
1919
{
20+
if (!ServicePointManager.SecurityProtocol.HasFlag((SecurityProtocolType)3072))
21+
{
22+
throw new NotSupportedException("mega.nz API requires support for TLS v1.2 or higher. Check https://gpailler.github.io/MegaApiClient/#compatibility for additional information");
23+
}
24+
2025
BufferSize = Options.DefaultBufferSize;
2126
_responseTimeout = responseTimeout;
2227
_userAgent = userAgent ?? GenerateUserAgent();

docs/index.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,28 @@ Getting started with MegaApiClient
1414

1515
#### Compatibility
1616

17-
The library supports .NET 4.0, .NET 4.5, .NET 4.6 and [.NET Standard 1.3](https://docs.microsoft.com/en-us/dotnet/standard/net-standard). Here is the list of all supported implementations:
17+
The library is built for the following targets: .NET 4.0, .NET 4.5, .NET 4.6, .NET 4.7, .NET 4.7.1, .NET Standard 1.3 and .NET Standard 2.0.
18+
Here is the list of all supported implementations:
1819

1920
| Implementation | Version |
2021
|----------------------------|-----------|
2122
| .NET Framework | 4.0 |
2223
| .NET Framework | >= 4.5 |
23-
| .NET Core | >= 1.0 |
24+
| .NET Core / .NET | >= 1.0 |
2425
| Mono | >= 4.6 |
2526
| Xamarin.iOS | >= 10.0 |
2627
| Xamarin.Mac | >= 3.0 |
2728
| Xamarin.Android | >= 7.0 |
2829
| Universal Windows Platform | >= 10.0 |
2930
| Unity* | >= 2018.2 |
3031

31-
⚠️ TLS 1.2 support should be configured explicitely when using .NET Framework <= 4.5 or all the API calls to Mega will hang.
32-
- For .NET Framework 4.5, add `ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;`
32+
---
33+
⚠️ TLS 1.2 support should be enforced when using .NET Framework <= 4.7 or all the API calls to Mega will hang.
34+
- For .NET Framework from 4.5 to 4.7.0, add `ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;`
3335
- For .NET Framework 4, add `ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;` (.NET 4.5 should be installed on the machine).
36+
- Alternatively, you can add relevant switches in your app.config https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls
3437

3538
---
36-
3739
\* Need a link.xml file if IL2CPP is used:
3840
```
3941
<linker>
@@ -44,7 +46,6 @@ The library supports .NET 4.0, .NET 4.5, .NET 4.6 and [.NET Standard 1.3](https:
4446
</assembly>
4547
</linker>
4648
```
47-
4849
---
4950

5051
MegaApiClient source is available on [GitHub](https://github.com/gpailler/MegaApiClient) and is released under [MIT](https://choosealicense.com/licenses/mit/) license.

0 commit comments

Comments
 (0)