|
@@ -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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取proxypac脚本
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="context"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ 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();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -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);
|