|
1 | 1 | using Microsoft.Extensions.Logging;
|
| 2 | +using System; |
| 3 | +using System.IO; |
| 4 | +using System.Net; |
2 | 5 | using System.Net.Http;
|
| 6 | +using System.Net.Security; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
3 | 9 | using Yarp.ReverseProxy.Forwarder;
|
4 | 10 |
|
5 | 11 | namespace HttpMouse.Implementions
|
6 | 12 | {
|
7 | 13 | /// <summary>
|
8 | 14 | /// 反向连接的HttpClient工厂
|
9 | 15 | /// </summary>
|
10 |
| - sealed class ReverseHttpClientFactory : ForwarderHttpClientFactory, IForwarderHttpClientFactory |
| 16 | + sealed class ReverseHttpClientFactory : IForwarderHttpClientFactory, IDisposable |
11 | 17 | {
|
| 18 | + private readonly SocketsHttpHandler httpHandler; |
12 | 19 | private readonly IReverseConnectionService reverseConnectionService;
|
| 20 | + private readonly ILogger<ReverseHttpClientFactory> logger; |
13 | 21 |
|
14 | 22 | /// <summary>
|
15 | 23 | /// 反向连接的HttpClient工厂
|
16 | 24 | /// </summary>
|
17 | 25 | /// <param name="reverseConnectionService"></param>
|
18 | 26 | /// <param name="logger"></param>
|
19 | 27 | public ReverseHttpClientFactory(
|
20 |
| - IReverseConnectionService reverseConnectionService, |
21 |
| - ILogger<ForwarderHttpClientFactory> logger) |
22 |
| - : base(logger) |
| 28 | + IReverseConnectionService reverseConnectionService, ILogger<ReverseHttpClientFactory> logger) |
23 | 29 | {
|
24 | 30 | this.reverseConnectionService = reverseConnectionService;
|
| 31 | + this.logger = logger; |
| 32 | + |
| 33 | + this.httpHandler = new SocketsHttpHandler |
| 34 | + { |
| 35 | + UseProxy = false, |
| 36 | + UseCookies = false, |
| 37 | + AllowAutoRedirect = false, |
| 38 | + AutomaticDecompression = DecompressionMethods.None, |
| 39 | + ConnectCallback = this.ConnectCallback, |
| 40 | + SslOptions = new SslClientAuthenticationOptions |
| 41 | + { |
| 42 | + RemoteCertificateValidationCallback = delegate { return true; } |
| 43 | + } |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// 连接回调 |
| 49 | + /// </summary> |
| 50 | + /// <param name="context"></param> |
| 51 | + /// <param name="cancellation"></param> |
| 52 | + /// <returns></returns> |
| 53 | + private async ValueTask<Stream> ConnectCallback(SocketsHttpConnectionContext context, CancellationToken cancellation) |
| 54 | + { |
| 55 | + try |
| 56 | + { |
| 57 | + return await this.reverseConnectionService.CreateReverseConnectionAsync(context, cancellation); |
| 58 | + } |
| 59 | + catch (Exception ex) |
| 60 | + { |
| 61 | + this.logger.LogWarning(ex.Message); |
| 62 | + throw; |
| 63 | + } |
25 | 64 | }
|
26 | 65 |
|
27 | 66 | /// <summary>
|
28 |
| - /// 配置连接 |
| 67 | + /// 创建HttpMessageInvoker |
29 | 68 | /// </summary>
|
30 | 69 | /// <param name="context"></param>
|
31 |
| - /// <param name="handler"></param> |
32 |
| - protected override void ConfigureHandler(ForwarderHttpClientContext context, SocketsHttpHandler handler) |
| 70 | + /// <returns></returns> |
| 71 | + public HttpMessageInvoker CreateClient(ForwarderHttpClientContext context) |
| 72 | + { |
| 73 | + return new HttpMessageInvoker(this.httpHandler, disposeHandler: false); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// 释放资源 |
| 78 | + /// </summary> |
| 79 | + public void Dispose() |
33 | 80 | {
|
34 |
| - base.ConfigureHandler(context, handler); |
35 |
| - handler.ConnectCallback = this.reverseConnectionService.CreateReverseConnectionAsync; |
| 81 | + this.httpHandler.Dispose(); |
36 | 82 | }
|
37 | 83 | }
|
38 | 84 | }
|
0 commit comments