Skip to content

Commit 7e68dec

Browse files
committed
接口重命名
1 parent f887a0b commit 7e68dec

27 files changed

+184
-199
lines changed

Directory.Build.props

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,8 @@
22
<PropertyGroup>
33
<Version>1.0.0</Version>
44
<Nullable>enable</Nullable>
5-
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
6-
<ANCMPreConfiguredForIIS>true</ANCMPreConfiguredForIIS>
7-
<IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
8-
<Description>公网反向代理到局域网的web服务器</Description>
9-
</PropertyGroup>
10-
11-
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
12-
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
13-
</PropertyGroup>
14-
15-
<PropertyGroup Condition="'$(Configuration)'=='Release'">
16-
<DebugType>none</DebugType>
17-
<DebugSymbols>false</DebugSymbols>
18-
</PropertyGroup>
5+
<TargetFramework>net5.0</TargetFramework>
6+
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
7+
<Description>http公网反向代理到内网的服务端与客户端库</Description>
8+
</PropertyGroup>
199
</Project>

HttpMouse.Client/HttpMouseClientServiceCollectionExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
using Microsoft.Extensions.Configuration;
1+
using HttpMouse.Client.Implementions;
2+
using Microsoft.Extensions.Configuration;
23
using Microsoft.Extensions.DependencyInjection;
34
using System;
45

56
namespace HttpMouse.Client
67
{
8+
/// <summary>
9+
/// HttpMouseClient扩展
10+
/// </summary>
711
public static class HttpMouseClientServiceCollectionExtensions
812
{
913
/// <summary>

HttpMouse.Client/HttpMouseClient.cs renamed to HttpMouse.Client/Implementions/HttpMouseClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Threading;
1010
using System.Threading.Tasks;
1111

12-
namespace HttpMouse.Client
12+
namespace HttpMouse.Client.Implementions
1313
{
1414
/// <summary>
1515
/// 客户端
@@ -78,7 +78,7 @@ private async Task<uint> ReadConnectionIdAsync(CancellationToken cancellationTok
7878
/// <summary>
7979
/// 绑定上下游的连接进行双向传输
8080
/// </summary>
81-
/// <param name="reverseConnectionId"></param>
81+
/// <param name="connectionId"></param>
8282
/// <param name="cancellationToken"></param>
8383
private async void TransportAsync(uint connectionId, CancellationToken cancellationToken)
8484
{

HttpMouse.Client/HttpMouseClientFactory.cs renamed to HttpMouse.Client/Implementions/HttpMouseClientFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Threading;
55
using System.Threading.Tasks;
66

7-
namespace HttpMouse.Client
7+
namespace HttpMouse.Client.Implementions
88
{
99
/// <summary>
1010
/// 客户端工厂

HttpMouse.ServerHost/localhost.pfx

-2.55 KB
Binary file not shown.

HttpMouse.sln

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpMouse", "HttpMouse\Http
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpMouse.Client", "HttpMouse.Client\HttpMouse.Client.csproj", "{CB280978-D61C-4833-B907-044CEF3A914D}"
99
EndProject
10-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpMouse.ClientHost", "HttpMouse.ClientHost\HttpMouse.ClientHost.csproj", "{5610D3BF-3800-4CCD-9227-7087D7C1DD67}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpMouse.ClientHost", "samples\HttpMouse.ClientHost\HttpMouse.ClientHost.csproj", "{DCA3F32A-7E3A-44C6-8FA3-35DB695BB9ED}"
1111
EndProject
12-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpMouse.ServerHost", "HttpMouse.ServerHost\HttpMouse.ServerHost.csproj", "{1D99B9FB-6998-45FB-87D1-167B87D07AC4}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpMouse.ServerHost", "samples\HttpMouse.ServerHost\HttpMouse.ServerHost.csproj", "{C71A773E-64C2-479D-832A-F80F61F80C83}"
13+
EndProject
14+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{3E09F4C8-86A0-427D-BC27-710572D61400}"
1315
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -25,18 +27,22 @@ Global
2527
{CB280978-D61C-4833-B907-044CEF3A914D}.Debug|Any CPU.Build.0 = Debug|Any CPU
2628
{CB280978-D61C-4833-B907-044CEF3A914D}.Release|Any CPU.ActiveCfg = Release|Any CPU
2729
{CB280978-D61C-4833-B907-044CEF3A914D}.Release|Any CPU.Build.0 = Release|Any CPU
28-
{5610D3BF-3800-4CCD-9227-7087D7C1DD67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29-
{5610D3BF-3800-4CCD-9227-7087D7C1DD67}.Debug|Any CPU.Build.0 = Debug|Any CPU
30-
{5610D3BF-3800-4CCD-9227-7087D7C1DD67}.Release|Any CPU.ActiveCfg = Release|Any CPU
31-
{5610D3BF-3800-4CCD-9227-7087D7C1DD67}.Release|Any CPU.Build.0 = Release|Any CPU
32-
{1D99B9FB-6998-45FB-87D1-167B87D07AC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33-
{1D99B9FB-6998-45FB-87D1-167B87D07AC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
34-
{1D99B9FB-6998-45FB-87D1-167B87D07AC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
35-
{1D99B9FB-6998-45FB-87D1-167B87D07AC4}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{DCA3F32A-7E3A-44C6-8FA3-35DB695BB9ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{DCA3F32A-7E3A-44C6-8FA3-35DB695BB9ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{DCA3F32A-7E3A-44C6-8FA3-35DB695BB9ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{DCA3F32A-7E3A-44C6-8FA3-35DB695BB9ED}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{C71A773E-64C2-479D-832A-F80F61F80C83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{C71A773E-64C2-479D-832A-F80F61F80C83}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{C71A773E-64C2-479D-832A-F80F61F80C83}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{C71A773E-64C2-479D-832A-F80F61F80C83}.Release|Any CPU.Build.0 = Release|Any CPU
3638
EndGlobalSection
3739
GlobalSection(SolutionProperties) = preSolution
3840
HideSolutionNode = FALSE
3941
EndGlobalSection
42+
GlobalSection(NestedProjects) = preSolution
43+
{DCA3F32A-7E3A-44C6-8FA3-35DB695BB9ED} = {3E09F4C8-86A0-427D-BC27-710572D61400}
44+
{C71A773E-64C2-479D-832A-F80F61F80C83} = {3E09F4C8-86A0-427D-BC27-710572D61400}
45+
EndGlobalSection
4046
GlobalSection(ExtensibilityGlobals) = postSolution
4147
SolutionGuid = {A7445E87-EBB8-45A9-BF16-D2C841D1B7A2}
4248
EndGlobalSection

HttpMouse/HttpMouseExtensions.cs

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
using HttpMouse.Abstractions;
33
using HttpMouse.Implementions;
44
using Microsoft.AspNetCore.Builder;
5-
using Microsoft.AspNetCore.Http;
6-
using Microsoft.AspNetCore.Routing;
75
using Microsoft.Extensions.Configuration;
8-
using Microsoft.Extensions.Options;
96
using System;
107
using System.Net.Http;
118
using System.Threading.Tasks;
@@ -21,61 +18,52 @@ namespace Microsoft.Extensions.DependencyInjection
2118
public static class HttpMouseExtensions
2219
{
2320
/// <summary>
24-
/// 添加HttpMouse
25-
/// </summary>
26-
/// <param name="services"></param>
27-
/// <returns></returns>
28-
public static IServiceCollection AddHttpMouse(this IServiceCollection services)
29-
{
30-
services
31-
.AddReverseProxy()
32-
.AddClientDomainOptionsTransform();
33-
34-
return services
35-
.AddSingleton<IProxyConfigProvider, MomoryConfigProvider>()
36-
.AddSingleton<IMainConnectionService, MainConnectionService>()
37-
.AddSingleton<IMainConnectionAuthenticator, MainConnectionAuthenticator>()
38-
.AddSingleton<IReverseConnectionService, ReverseConnectionService>()
39-
.AddSingleton<IForwarderHttpClientFactory, ReverseForwarderHttpClientFactory>();
40-
}
41-
42-
/// <summary>
43-
/// 配置HttpMouse
21+
/// 注册HttpMouse相关服务
4422
/// </summary>
4523
/// <param name="services"></param>
4624
/// <param name="configuration"></param>
4725
/// <returns></returns>
48-
public static IServiceCollection ConfigureHttpMouse(this IServiceCollection services, IConfiguration configuration)
26+
public static IReverseProxyBuilder AddHttpMouse(this IServiceCollection services, IConfiguration configuration)
4927
{
50-
return services.Configure<HttpMouseOptions>(configuration);
28+
return services.Configure<HttpMouseOptions>(configuration).AddHttpMouse();
5129
}
5230

5331
/// <summary>
54-
/// 配置HttpMouse
32+
/// 注册HttpMouse相关服务
5533
/// </summary>
5634
/// <param name="services"></param>
5735
/// <param name="configureOptions"></param>
5836
/// <returns></returns>
59-
public static IServiceCollection ConfigureHttpMouse(this IServiceCollection services, Action<HttpMouseOptions> configureOptions)
37+
public static IReverseProxyBuilder AddHttpMouse(this IServiceCollection services, Action<HttpMouseOptions> configureOptions)
6038
{
61-
return services.Configure(configureOptions);
39+
return services.Configure(configureOptions).AddHttpMouse();
6240
}
6341

64-
6542
/// <summary>
66-
/// 添加ClientDomain的options
43+
/// 注册HttpMouse相关服务
6744
/// </summary>
68-
/// <param name="builder"></param>
45+
/// <param name="services"></param>
6946
/// <returns></returns>
70-
private static IReverseProxyBuilder AddClientDomainOptionsTransform(this IReverseProxyBuilder builder)
47+
public static IReverseProxyBuilder AddHttpMouse(this IServiceCollection services)
7148
{
7249
var optionsKey = new HttpRequestOptionsKey<string>("ClientDomain");
73-
return builder.AddTransforms(ctx => ctx.AddRequestTransform(request =>
74-
{
75-
var clientDomain = request.HttpContext.Request.Host.Host;
76-
request.ProxyRequest.Options.Set(optionsKey, clientDomain);
77-
return ValueTask.CompletedTask;
78-
}));
50+
var builder = services
51+
.AddReverseProxy()
52+
.AddTransforms(ctx => ctx.AddRequestTransform(request =>
53+
{
54+
var clientDomain = request.HttpContext.Request.Host.Host;
55+
request.ProxyRequest.Options.Set(optionsKey, clientDomain);
56+
return ValueTask.CompletedTask;
57+
}));
58+
59+
services
60+
.AddSingleton<IProxyConfigProvider, MomoryConfigProvider>()
61+
.AddSingleton<IMainConnectionHandler, MainConnectionHandler>()
62+
.AddSingleton<IMainConnectionAuthenticator, MainConnectionAuthenticator>()
63+
.AddSingleton<IReverseConnectionProvider, ReverseConnectionProvider>()
64+
.AddSingleton<IForwarderHttpClientFactory, ReverseForwarderHttpClientFactory>();
65+
66+
return builder;
7967
}
8068

8169
/// <summary>
@@ -85,29 +73,14 @@ private static IReverseProxyBuilder AddClientDomainOptionsTransform(this IRevers
8573
/// <returns></returns>
8674
public static IApplicationBuilder UseHttpMouse(this IApplicationBuilder builder)
8775
{
88-
var mainConnectionService = builder.ApplicationServices.GetRequiredService<IMainConnectionService>();
89-
var reverseConnectionService = builder.ApplicationServices.GetRequiredService<IReverseConnectionService>();
76+
var mainConnectionHandler = builder.ApplicationServices.GetRequiredService<IMainConnectionHandler>();
77+
var reverseConnectionProvider = builder.ApplicationServices.GetRequiredService<IReverseConnectionProvider>();
9078

9179
builder.UseWebSockets();
92-
builder.Use(mainConnectionService.HandleConnectionAsync);
93-
builder.Use(reverseConnectionService.HandleConnectionAsync);
80+
builder.Use(mainConnectionHandler.HandleConnectionAsync);
81+
builder.Use(reverseConnectionProvider.HandleConnectionAsync);
9482

9583
return builder;
9684
}
97-
98-
/// <summary>
99-
/// 映射反向代理回退
100-
/// </summary>
101-
/// <param name="endpoints"></param>
102-
public static void MapReverseProxyFallback(this IEndpointRouteBuilder endpoints)
103-
{
104-
endpoints.MapFallback(context =>
105-
{
106-
var fallback = context.RequestServices.GetRequiredService<IOptionsMonitor<HttpMouseOptions>>().CurrentValue.Fallback;
107-
context.Response.StatusCode = fallback.StatusCode;
108-
context.Response.ContentType = fallback.ContentType;
109-
return context.Response.SendFileAsync(fallback.ContentFile);
110-
});
111-
}
11285
}
11386
}

HttpMouse/HttpMouseOptions.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,5 @@ public class HttpMouseOptions
1717
/// 客户端域名的请求配置
1818
/// </summary>
1919
public Dictionary<string, ForwarderRequestConfig> HttpRequest { get; set; } = new();
20-
21-
/// <summary>
22-
/// 错误回退配置
23-
/// </summary>
24-
public FallbackConfig Fallback { get; set; } = new FallbackConfig();
25-
26-
/// <summary>
27-
/// 错误回退配置
28-
/// </summary>
29-
public class FallbackConfig
30-
{
31-
/// <summary>
32-
/// 响应状态码
33-
/// </summary>
34-
public int StatusCode { get; set; } = 503;
35-
36-
/// <summary>
37-
/// 响应内容类型
38-
/// </summary>
39-
public string ContentType { get; set; } = "application/problem+json";
40-
41-
/// <summary>
42-
/// 响应内容文件路径
43-
/// </summary>
44-
public string ContentFile { get; set; } = "fallback.json";
45-
}
4620
}
4721
}

HttpMouse/IMainConnection.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ public interface IMainConnection
2020
/// </summary>
2121
Uri Upstream { get; }
2222

23-
2423
/// <summary>
25-
/// 转换为ClusterConfig
24+
/// 转换为RouteConfig
2625
/// </summary>
2726
/// <returns></returns>
28-
ClusterConfig ToClusterConfig();
27+
RouteConfig ToRouteConfig();
2928

3029
/// <summary>
31-
/// 转换为RouteConfig
30+
/// 转换为ClusterConfig
3231
/// </summary>
3332
/// <returns></returns>
34-
RouteConfig ToRouteConfig();
33+
ClusterConfig ToClusterConfig();
34+
3535

3636
/// <summary>
3737
/// 发送创建反向连接指令
3838
/// </summary>
39-
/// <param name="reverseConnectionId"></param>
39+
/// <param name="connectionId"></param>
4040
/// <param name="cancellationToken"></param>
4141
/// <returns></returns>
42-
Task SendCreateReverseConnectionAsync(uint reverseConnectionId, CancellationToken cancellationToken);
42+
Task SendCreateReverseConnectionAsync(uint connectionId, CancellationToken cancellationToken);
4343

4444
/// <summary>
4545
/// 由于异常而关闭

HttpMouse/IMainConnectionService.cs renamed to HttpMouse/IMainConnectionHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
namespace HttpMouse
77
{
88
/// <summary>
9-
/// 主连接服务接口
9+
/// 主连接处理者
1010
/// </summary>
11-
public interface IMainConnectionService
11+
public interface IMainConnectionHandler
1212
{
1313
/// <summary>
1414
/// 主连接变化后
1515
/// </summary>
16-
event Action<IMainConnection[]>? ConnectionChanged;
16+
event Action<IMainConnection[]>? ConnectionsChanged;
1717

1818
/// <summary>
1919
/// 处理连接

HttpMouse/IReverseConnectionService.cs renamed to HttpMouse/IReverseConnectionProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
namespace HttpMouse
88
{
99
/// <summary>
10-
/// 返回连接服务接口
10+
/// 反向连接提值者
1111
/// </summary>
12-
public interface IReverseConnectionService
12+
public interface IReverseConnectionProvider
1313
{
1414
/// <summary>
1515
/// 创建一个反向连接
1616
/// </summary>
1717
/// <param name="clientDomain">客户端域名</param>
18-
/// <param name="cancellation"></param>
18+
/// <param name="cancellationToken"></param>
1919
/// <returns></returns>
20-
ValueTask<Stream> CreateReverseConnectionAsync(string clientDomain, CancellationToken cancellation);
20+
ValueTask<Stream> CreateAsync(string clientDomain, CancellationToken cancellationToken);
2121

2222
/// <summary>
2323
/// 处理连接

HttpMouse/Implementions/MainConnection.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ sealed class MainConnection : IMainConnection
3535
/// <param name="domain"></param>
3636
/// <param name="upstream"></param>
3737
/// <param name="webSocket"></param>
38+
/// <param name="options"></param>
3839
public MainConnection(
3940
string domain,
4041
Uri upstream,
@@ -92,13 +93,13 @@ public RouteConfig ToRouteConfig()
9293
/// <summary>
9394
/// 发送创建反向连接指令
9495
/// </summary>
95-
/// <param name="reverseConnectionId"></param>
96+
/// <param name="connectionId"></param>
9697
/// <param name="cancellationToken"></param>
9798
/// <returns></returns>
98-
public Task SendCreateReverseConnectionAsync(uint reverseConnectionId, CancellationToken cancellationToken)
99+
public Task SendCreateReverseConnectionAsync(uint connectionId, CancellationToken cancellationToken)
99100
{
100101
var channelIdBuffer = new byte[sizeof(uint)];
101-
BinaryPrimitives.WriteUInt32BigEndian(channelIdBuffer, reverseConnectionId);
102+
BinaryPrimitives.WriteUInt32BigEndian(channelIdBuffer, connectionId);
102103
return this.webSocket.SendAsync(channelIdBuffer, WebSocketMessageType.Binary, true, cancellationToken);
103104
}
104105

0 commit comments

Comments
 (0)