From f374554c789bf5872a1987dc480d5ab435175bb6 Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Sun, 18 Jul 2021 12:24:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ResponseConfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Core/DomainConfig.cs | 13 +++++++---- FastGithub.Core/ResponseConfig.cs | 23 +++++++++++++++++++ .../ReverseProxyMiddleware.cs | 9 ++++++++ FastGithub/appsettings.json | 20 +++++++++------- 4 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 FastGithub.Core/ResponseConfig.cs diff --git a/FastGithub.Core/DomainConfig.cs b/FastGithub.Core/DomainConfig.cs index 7127059..345d1a7 100644 --- a/FastGithub.Core/DomainConfig.cs +++ b/FastGithub.Core/DomainConfig.cs @@ -5,22 +5,27 @@ namespace FastGithub /// /// 域名配置 /// - public class DomainConfig + public record DomainConfig { /// /// 是否发送SNI /// - public bool TlsSni { get; set; } + public bool TlsSni { get; init; } /// /// 请求超时时长 /// - public TimeSpan? Timeout { get; set; } + public TimeSpan? Timeout { get; init; } /// /// 目的地 /// 格式为相对或绝对uri /// - public Uri? Destination { get; set; } + public Uri? Destination { get; init; } + + /// + /// 自定义响应 + /// + public ResponseConfig? Response { get; init; } } } diff --git a/FastGithub.Core/ResponseConfig.cs b/FastGithub.Core/ResponseConfig.cs new file mode 100644 index 0000000..b547337 --- /dev/null +++ b/FastGithub.Core/ResponseConfig.cs @@ -0,0 +1,23 @@ +namespace FastGithub +{ + /// + /// 响应配置 + /// + public record ResponseConfig + { + /// + /// 状态码 + /// + public int StatusCode { get; init; } = 200; + + /// + /// 内容类型 + /// + public string ContentType { get; init; } = "text/plain;charset=utf-8"; + + /// + /// 内容的值 + /// + public string? ContentValue { get; init; } + } +} diff --git a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs index 0aafbf6..77e5b45 100644 --- a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs +++ b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs @@ -46,6 +46,15 @@ namespace FastGithub.ReverseProxy context.Response.ContentType = "text/html"; await context.Response.SendFileAsync(fallbackFile); } + else if (domainConfig.Response != null) + { + context.Response.StatusCode = domainConfig.Response.StatusCode; + context.Response.ContentType = domainConfig.Response.ContentType; + if (domainConfig.Response.ContentValue != null) + { + await context.Response.WriteAsync(domainConfig.Response.ContentValue); + } + } else { var destinationPrefix = GetDestinationPrefix(host, domainConfig.Destination); diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json index 294c484..992c03c 100644 --- a/FastGithub/appsettings.json +++ b/FastGithub/appsettings.json @@ -1,18 +1,18 @@ { "FastGithub": { - "PureDns": { // ڽDomainConfigs + "PureDns": { // 用于解析DomainConfigs的域名 "IPAddress": "127.0.0.1", - "Port": 5533 // 5533ָdnscrypt-proxy + "Port": 5533 // 5533指向dnscrypt-proxy }, - "FastDns": { // ڽDomainConfigs + "FastDns": { // 用于解析不在DomainConfigs的域名 "IPAddress": "114.114.114.114", "Port": 53 }, - "DomainConfigs": { // *ʾ0ַ + "DomainConfigs": { // 域名的*表示0到多个任意字符 "github.com": { - "TlsSni": false, // ָʾtlsʱǷSNI - "Timeout": null, // ʱʱʽΪ"00:02:00"ĬΪnull - "Destination": null // ĿĵأʽΪԻUriĬnull + "TlsSni": false, // 指示tls握手时是否发送SNI + "Timeout": null, // 请求超时时长,格式为"00:02:00",默认为null + "Destination": null // 请求目的地,格式为绝对或相对Uri,默认null }, "githubstatus.com": { "TlsSni": false @@ -60,7 +60,11 @@ "Destination": "https://fdn.geekzu.org/" }, "i.stack.imgur.com": { - "Destination": "http://localhost/" // ûжӦԴ + "Response": { // 直接响应 + "StatusCode": 404, + "ContentType": "text/plain;charset=utf-8", + "ContentValue": "阻断的请求" + } } } },