예제 #1
0
        public async Task OnRequest(object sender, SessionEventArgs e)
        {
#if DEBUG
            #region 测试用

            /*
             * if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("steampowered.com"))
             * {
             *  var ip = Dns.GetHostAddresses("steampowered.com");
             *  e.HttpClient.UpStreamEndPoint = new IPEndPoint(IPAddress.Parse(ip[0].ToString()), 443);
             *  if (e.HttpClient.ConnectRequest?.ClientHelloInfo != null)
             *  {
             *      e.HttpClient.ConnectRequest.ClientHelloInfo.Extensions.Remove("server_name");
             *  }
             * }
             *
             * if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("steamcommunity.com"))
             * {
             *  var ip = Dns.GetHostAddresses("steamcommunity-a.akamaihd.net");
             *  e.HttpClient.UpStreamEndPoint = new IPEndPoint(IPAddress.Parse(ip[0].ToString()), 443);
             *  if (e.HttpClient.ConnectRequest?.ClientHelloInfo != null)
             *  {
             *      e.HttpClient.ConnectRequest.ClientHelloInfo.Extensions.Remove("server_name");
             *  }
             * }
             */
            #endregion
            Debug.WriteLine("OnRequest " + e.HttpClient.Request.RequestUri.AbsoluteUri);
            Debug.WriteLine("OnRequest HTTP " + e.HttpClient.Request.HttpVersion);
            //Logger.Info("OnRequest" + e.HttpClient.Request.RequestUri.AbsoluteUri);
#endif
            foreach (var item in ProxyDomains)
            {
                if (!item.IsEnable)
                {
                    continue;
                }
                foreach (var host in item.Domains)
                {
                    if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains(host))
                    {
                        if (e.HttpClient.Request.RequestUri.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
                        {
                            e.Redirect(e.HttpClient.Request.RequestUri.AbsoluteUri.Remove(0, 4).Insert(0, "https"));
                            return;
                        }
                        if (item.IsRedirect)
                        {
                            e.Redirect(e.HttpClient.Request.RequestUri.AbsoluteUri.Replace(e.HttpClient.Request.RequestUri.Host, item.ToDomain));
                            return;
                        }
                        IPAddress ip = null;
                        if (!string.IsNullOrEmpty(item.ProxyIPAddres))
                        {
                            ip = IPAddress.Parse(item.ProxyIPAddres);
                        }
                        else
                        {
                            var iPs = await Dns.GetHostAddressesAsync(item.ToDomain);

                            ip = iPs.FirstOrDefault();
                            //Logger.Info("Proxy IP: " + iP);
                        }
                        if (ip != null)
                        {
                            e.HttpClient.UpStreamEndPoint = new IPEndPoint(IPAddress.Parse(ip.ToString()), item.Port);
                        }
                        if (e.HttpClient.ConnectRequest?.ClientHelloInfo != null)
                        {
                            //Logger.Info("ClientHelloInfo Info: " + e.HttpClient.ConnectRequest.ClientHelloInfo);
                            if (!string.IsNullOrEmpty(item.ServerName))
                            {
                                var sni = e.HttpClient.ConnectRequest.ClientHelloInfo.Extensions["server_name"];
                                e.HttpClient.ConnectRequest.ClientHelloInfo.Extensions["server_name"] =
                                    new Titanium.Web.Proxy.StreamExtended.Models.SslExtension(sni.Value, sni.Name, item.ServerName, sni.Position);
                            }
                            else
                            {
                                e.HttpClient.ConnectRequest.ClientHelloInfo.Extensions.Remove("server_name");
                            }
                        }
                        return;
                    }
                }
            }

            await Dns.GetHostAddressesAsync(e.HttpClient.Request.Host).ContinueWith(s =>
            {
                //部分运营商将奇怪的域名解析到127.0.0.1 再此排除这些不支持的代理域名
                if (IPAddress.IsLoopback(s.Result.FirstOrDefault()) &&
                    ProxyDomains.Count(w => w.IsEnable && w.Hosts.Contains(e.HttpClient.Request.Host)) == 0)
                {
                    e.Ok($"URL : {e.HttpClient.Request.RequestUri.AbsoluteUri} \r\n not support proxy");
                    return;
                }
                Logger.Info("Steam++ IsLoopback OnRequest: " + e.HttpClient.Request.RequestUri.AbsoluteUri);
            });

            //没有匹配到的结果直接返回不支持,避免出现Loopback死循环内存溢出
            //e.Ok($"URL : {e.HttpClient.Request.RequestUri.AbsoluteUri} {Environment.NewLine}not support proxy");
            return;
        }
예제 #2
0
        public async Task OnRequest(object sender, SessionEventArgs e)
        {
#if DEBUG
            #region 测试用

            /*
             * if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("steampowered.com"))
             * {
             *  var ip = Dns.GetHostAddresses("steampowered.com");
             *  e.HttpClient.UpStreamEndPoint = new IPEndPoint(IPAddress.Parse(ip[0].ToString()), 443);
             *  if (e.HttpClient.ConnectRequest?.ClientHelloInfo != null)
             *  {
             *      e.HttpClient.ConnectRequest.ClientHelloInfo.Extensions.Remove("server_name");
             *  }
             * }
             *
             * if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains("steamcommunity.com"))
             * {
             *  var ip = Dns.GetHostAddresses("steamcommunity-a.akamaihd.net");
             *  e.HttpClient.UpStreamEndPoint = new IPEndPoint(IPAddress.Parse(ip[0].ToString()), 443);
             *  if (e.HttpClient.ConnectRequest?.ClientHelloInfo != null)
             *  {
             *      e.HttpClient.ConnectRequest.ClientHelloInfo.Extensions.Remove("server_name");
             *  }
             * }
             */
            #endregion
            Debug.WriteLine("OnRequest " + e.HttpClient.Request.RequestUri.AbsoluteUri);
            Debug.WriteLine("OnRequest HTTP " + e.HttpClient.Request.HttpVersion);
            Logger.Info("OnRequest" + e.HttpClient.Request.RequestUri.AbsoluteUri);
#endif
            await Dns.GetHostAddressesAsync(e.HttpClient.Request.Host).ContinueWith(s =>
            {
                //部分运营商将奇怪的域名解析到127.0.0.1 再此排除这些不支持的代理域名
                if (IPAddress.IsLoopback(s.Result.FirstOrDefault()) &&
                    ProxyDomains.Count(w => w.IsEnable && w.Hosts.Contains(e.HttpClient.Request.Host)) == 0)
                {
                    e.Ok($"URL : {e.HttpClient.Request.RequestUri.AbsoluteUri} \r\n not support proxy");
                    return;
                }
            });

            foreach (var item in ProxyDomains)
            {
                if (!item.IsEnable)
                {
                    continue;
                }
                foreach (var host in item.Domains)
                {
                    if (e.HttpClient.Request.RequestUri.AbsoluteUri.Contains(host))
                    {
                        IPAddress iP = null;
                        if (!string.IsNullOrEmpty(item.ProxyIPAddres))
                        {
                            iP = IPAddress.Parse(item.ProxyIPAddres);
                        }
                        else
                        {
                            var iPs = await Dns.GetHostAddressesAsync(item.ToDomain);

                            iP = iPs.FirstOrDefault();
                        }
                        if (iP != null)
                        {
                            e.HttpClient.UpStreamEndPoint = new IPEndPoint(IPAddress.Parse(iP.ToString()), item.Port);
                        }
                        if (e.HttpClient.ConnectRequest?.ClientHelloInfo != null)
                        {
                            if (!string.IsNullOrEmpty(item.ServerName))
                            {
                                var sni = e.HttpClient.ConnectRequest.ClientHelloInfo.Extensions["server_name"];
                                e.HttpClient.ConnectRequest.ClientHelloInfo.Extensions["server_name"] =
                                    new Titanium.Web.Proxy.StreamExtended.Models.SslExtension(sni.Value, sni.Name, item.ServerName, sni.Position);
                            }
                            else
                            {
                                e.HttpClient.ConnectRequest.ClientHelloInfo.Extensions.Remove("server_name");
                            }
                        }
                    }
                }
            }
        }