diff --git a/FastGithub.Dns/DnsInterceptor.cs b/FastGithub.Dns/DnsInterceptor.cs index 5a40a6f..d7433cb 100644 --- a/FastGithub.Dns/DnsInterceptor.cs +++ b/FastGithub.Dns/DnsInterceptor.cs @@ -22,7 +22,7 @@ namespace FastGithub.Dns private const string DNS_FILTER = "udp.DstPort == 53"; private readonly FastGithubConfig fastGithubConfig; private readonly ILogger logger; - private readonly TimeSpan ttl = TimeSpan.FromMinutes(1d); + private readonly TimeSpan ttl = TimeSpan.FromMinutes(2d); /// /// 刷新DNS缓存 @@ -118,14 +118,14 @@ namespace FastGithub.Dns } // 反转ip - var sourceAddress = ipPacket.SourceAddress; - ipPacket.SourceAddress = ipPacket.DestinationAddress; - ipPacket.DestinationAddress = sourceAddress; + var destAddress = ipPacket.DestinationAddress; + ipPacket.DestinationAddress = ipPacket.SourceAddress; + ipPacket.SourceAddress = destAddress; // 反转端口 - var sourcePort = udpPacket.SourcePort; - udpPacket.SourcePort = udpPacket.DestinationPort; - udpPacket.DestinationPort = sourcePort; + var destPort = udpPacket.DestinationPort; + udpPacket.DestinationPort = udpPacket.SourcePort; + udpPacket.SourcePort = destPort; // 设置dns响应 var response = Response.FromRequest(request); @@ -146,6 +146,8 @@ namespace FastGithub.Dns { winDivertAddress.Direction = WinDivertDirection.Inbound; } + + this.logger.LogInformation($"已拦截dns查询{domain}并伪造响应内容为{IPAddress.Loopback}"); } } } diff --git a/FastGithub.HttpServer/HttpProxyMiddleware.cs b/FastGithub.HttpServer/HttpProxyMiddleware.cs index aaffef1..54b78b1 100644 --- a/FastGithub.HttpServer/HttpProxyMiddleware.cs +++ b/FastGithub.HttpServer/HttpProxyMiddleware.cs @@ -49,26 +49,11 @@ namespace FastGithub.HttpServer { if (context.Request.Method == HttpMethods.Get && context.Request.Path == "/proxy.pac") { - var buidler = new StringBuilder(); - buidler.AppendLine("function FindProxyForURL(url, host){"); - buidler.AppendLine($" var proxy = 'PROXY {context.Request.Host}';"); - foreach (var domain in this.fastGithubConfig.GetDomainPatterns()) - { - buidler.AppendLine($" if (shExpMatch(host, '{domain}')) return proxy;"); - } - buidler.AppendLine(" return 'DIRECT';"); - buidler.AppendLine("}"); - var pacString = buidler.ToString(); - context.Response.ContentType = "application/x-ns-proxy-autoconfig"; + var pacString = this.GetProxyPacString(context); await context.Response.WriteAsync(pacString); } - else if (context.Request.Method != HttpMethods.Connect) - { - var destinationPrefix = $"{context.Request.Scheme}://{context.Request.Host}"; - await this.httpForwarder.SendAsync(context, destinationPrefix, this.httpClient); - } - else + else if (context.Request.Method == HttpMethods.Connect) { var endpoint = await this.GetTargetEndPointAsync(context.Request); using var targetSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); @@ -87,6 +72,31 @@ namespace FastGithub.HttpServer await Task.WhenAny(task1, task2); } } + else + { + var destinationPrefix = $"{context.Request.Scheme}://{context.Request.Host}"; + await this.httpForwarder.SendAsync(context, destinationPrefix, this.httpClient); + } + } + + + /// + /// 获取proxypac脚本 + /// + /// + /// + private string GetProxyPacString(HttpContext context) + { + var buidler = new StringBuilder(); + buidler.AppendLine("function FindProxyForURL(url, host){"); + buidler.AppendLine($" var proxy = 'PROXY {context.Request.Host}';"); + foreach (var domain in this.fastGithubConfig.GetDomainPatterns()) + { + buidler.AppendLine($" if (shExpMatch(host, '{domain}')) return proxy;"); + } + buidler.AppendLine(" return 'DIRECT';"); + buidler.AppendLine("}"); + return buidler.ToString(); } /// @@ -105,12 +115,13 @@ namespace FastGithub.HttpServer return new IPEndPoint(address, targetPort); } + // 不关心的域名,直接使用系统dns if (this.fastGithubConfig.IsMatch(targetHost) == false) { return new DnsEndPoint(targetHost, targetPort); } - // https,走反向代理中间人 + // 目标端口为443,走https代理中间人 if (targetPort == HTTPS_PORT) { return new IPEndPoint(IPAddress.Loopback, HttpsReverseProxyPort.Value);