From e723eb8ba22e5b98699af4b14d0958375032c939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E9=A1=BA=E6=98=8E?= Date: Sun, 30 Jul 2023 08:56:54 +0800 Subject: [PATCH] =?UTF-8?q?Cookie=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bridge/HttpBridgeMime.cs | 20 +++------ Mime/HttpMimeSocket.cs | 21 +++------- Proxy/HttpProxy.cs | 80 ++++++++++++++++-------------------- Proxy/SiteActivity.cs | 5 ++- Proxy/SiteConfig.cs | 14 ++++--- Resources/login.html | 88 +++------------------------------------- 6 files changed, 62 insertions(+), 166 deletions(-) diff --git a/Bridge/HttpBridgeMime.cs b/Bridge/HttpBridgeMime.cs index 1a7815d..aa579b7 100644 --- a/Bridge/HttpBridgeMime.cs +++ b/Bridge/HttpBridgeMime.cs @@ -71,25 +71,15 @@ namespace UMC.Host try { var url = webRequest.RequestUri; - if (webRequest.CookieContainer != null) + + var cookie = webRequest.CookieContainer?.GetCookieHeader(url); + if (String.IsNullOrEmpty(cookie) == false) { - String cookie; - if (webRequest.CookieContainer is Net.NetCookieContainer) - { - cookie = ((Net.NetCookieContainer)webRequest.CookieContainer).GetCookieHeader(url); - } - else - { - cookie = webRequest.CookieContainer.GetCookieHeader(url); - } - if (String.IsNullOrEmpty(cookie) == false) - { - webRequest.Headers[HttpRequestHeader.Cookie] = cookie; - } + webRequest.Headers[HttpRequestHeader.Cookie] = cookie; } if (String.IsNullOrEmpty(webRequest.Headers[HttpRequestHeader.Host])) { - webRequest.Headers[HttpRequestHeader.Host] = webRequest.Host; + webRequest.Headers[HttpRequestHeader.Host] = url.Host; } webRequest.Headers["Connection"] = "Upgrade"; diff --git a/Mime/HttpMimeSocket.cs b/Mime/HttpMimeSocket.cs index 77f624d..4c54f8e 100644 --- a/Mime/HttpMimeSocket.cs +++ b/Mime/HttpMimeSocket.cs @@ -156,25 +156,16 @@ namespace UMC.Host try { var url = webRequest.RequestUri; - if (webRequest.CookieContainer != null) + + var cookie = webRequest.CookieContainer?.GetCookieHeader(url); + + if (String.IsNullOrEmpty(cookie) == false) { - String cookie; - if (webRequest.CookieContainer is Net.NetCookieContainer) - { - cookie = ((Net.NetCookieContainer)webRequest.CookieContainer).GetCookieHeader(url); - } - else - { - cookie = webRequest.CookieContainer.GetCookieHeader(url); - } - if (String.IsNullOrEmpty(cookie) == false) - { - webRequest.Headers[HttpRequestHeader.Cookie] = cookie; - } + webRequest.Headers[HttpRequestHeader.Cookie] = cookie; } if (String.IsNullOrEmpty(webRequest.Headers[HttpRequestHeader.Host])) { - webRequest.Headers[HttpRequestHeader.Host] = webRequest.Host; + webRequest.Headers[HttpRequestHeader.Host] = url.Host; } webRequest.Headers["Connection"] = "Upgrade"; diff --git a/Proxy/HttpProxy.cs b/Proxy/HttpProxy.cs index 4464464..20abe10 100644 --- a/Proxy/HttpProxy.cs +++ b/Proxy/HttpProxy.cs @@ -331,7 +331,8 @@ namespace UMC.Proxy } } } - List OuterCookies = new List(); + + HashSet OuterCookies = new HashSet(); public int StaticModel { get; @@ -846,7 +847,12 @@ namespace UMC.Proxy var res = (this.Domain.Host.Equals(getUrl.Host) ? this.Reqesut(webr) : webr).Net(Method, valResult); var cs = res.Headers.GetValues("Set-Cookie"); if (cs != null) - OuterCookies.AddRange(cs); + { + foreach (var c in cs) + { + OuterCookies.Add(c); + } + } if (this.IsLog == true) { this.Loger.Write(Method); @@ -900,9 +906,13 @@ namespace UMC.Proxy var res2 = (this.Domain.Host.Equals(getUrl.Host) ? this.Reqesut(webr2) : webr2).Get(); var cs2 = res2.Headers.GetValues("Set-Cookie"); if (cs2 != null) - OuterCookies.AddRange(cs2); + { + foreach (var c in cs2) + { + OuterCookies.Add(c); + } + } - // OuterCookies.AddRange(res2.Headers.GetValues("Set-Cookie")); if (this.IsLog == true) { @@ -1857,7 +1867,6 @@ namespace UMC.Proxy } else { - // var cookie = this.Cookies(key); var cookie = this.Context.Cookies[key]; if (String.IsNullOrEmpty(cookie) == false) { @@ -2142,7 +2151,12 @@ namespace UMC.Proxy } var cs = httpResponse.Headers.GetValues("Set-Cookie"); if (cs != null) - OuterCookies.AddRange(cs); + { + foreach (var c in cs) + { + OuterCookies.Add(c); + } + } if (this.IsLog == true) { this.Loger.WriteLine("{0} {1} {2}", httpResponse.ProtocolVersion, (int)httpResponse.StatusCode, httpResponse.StatusDescription); @@ -2405,7 +2419,7 @@ namespace UMC.Proxy } if (login.ContainsKey("IsNotCookieClear") == false) { - this.Cookies = new NetCookieContainer(); + this.Cookies = new CookieContainer(); } var feildConfig = UMC.Data.JSON.Deserialize(this.SiteCookie.Config) ?? new Hashtable(); @@ -2789,30 +2803,17 @@ namespace UMC.Proxy bool SaveCookie() { - // if (IsChangeUser == false || this.StaticModel == 0) - // { - // return false; - // } - - var siteCookie = new Entities.Cookie + if (this.IsChangeUser == true) { - Domain = this.Site.Root, - Time = DateTime.Now, - user_id = this.SiteCookie.user_id, - IndexValue = this.SiteCookie.IndexValue + var siteCookie = new Entities.Cookie + { + Domain = this.Site.Root, + Time = DateTime.Now, + user_id = this.SiteCookie.user_id, + IndexValue = this.SiteCookie.IndexValue - }; - // String strCol = UMC.Data.JSON.Serialize(this.Cookies); - // var isSaveCookie = false; - // if (String.Equals(strCol, this.SiteCookie.Cookies) == false) - // { - // siteCookie.Cookies = strCol; - // this.SiteCookie.Cookies = strCol; - // isSaveCookie = true; - // } + }; - if (this.IsChangeUser == true) - { var nUP = String.Format("{0}{1}", this.SiteCookie.Account, this.Password); @@ -2841,19 +2842,6 @@ namespace UMC.Proxy siteCookie.Config = this.SiteCookie.Config; siteCookie.LoginTime = UMC.Data.Utility.TimeSpan(); this.IsChangeUser = null; - // isSaveCookie = true; - DataFactory.Instance().Put(siteCookie); - return true; - - } - // if (isSaveCookie) - // { - // DataFactory.Instance().Put(siteCookie); - // return true; - // } - // else - if (this.SiteCookie.Time < DateTime.Now.AddSeconds(-300)) - { DataFactory.Instance().Put(siteCookie); return true; @@ -3116,10 +3104,10 @@ namespace UMC.Proxy LogWrite(this.Context, this.Site, this.Context.StatusCode, String.Format("{0} {1}", Context.HttpMethod, this.RawUrl), this.SiteCookie.Account, (int)(UMC.Data.Reflection.TimeSpanMilli(DateTime.Now) - StartTime), m_HttpHeaders, _AttachmentFile); - if (this.Site.Site.AuthType > WebAuthType.All) - { - this.SaveCookie(); - } + // if (this.Site.Site.AuthType > WebAuthType.All) + // { + this.SaveCookie(); + // } if (this.IsLog && User.IsAuthenticated) { this.Loger.WriteLine("Cookie:{0}", this.Cookies.GetCookieHeader(this.Domain)); @@ -3280,7 +3268,7 @@ namespace UMC.Proxy break; } } - this.Cookies = new NetCookieContainer(); + this.Cookies = new CookieContainer(); this.Login(true, false, form, String.Empty); this.ProcessEnd(); return; diff --git a/Proxy/SiteActivity.cs b/Proxy/SiteActivity.cs index c8a4993..c54367f 100644 --- a/Proxy/SiteActivity.cs +++ b/Proxy/SiteActivity.cs @@ -50,6 +50,7 @@ namespace UMC.Proxy.Activities } void Market(Site site) { + // System.Net.Cache.HttpCacheAgeControl var secret = UMC.Data.WebResource.Instance().Provider["appSecret"]; if (String.IsNullOrEmpty(secret)) { @@ -261,7 +262,7 @@ namespace UMC.Proxy.Activities { var from = new Web.UIFormDialog() { Title = "内容转化配置" }; from.AddTextarea("转化路径", "HostReConf", site.HostReConf).PlaceHolder("字典配置格式").Put("Rows", 8).NotRequired(); - from.AddFooter("值可为rp、rm、in、cdn、CDN,其中rp表示替换域名、rm表示移除域名、in表示提交内容域名转化,cdn表示静态资源加速,CDN表示以资源标签加速"); + from.AddFooter("值可为rp、rm、in其中rp表示替换域名、rm表示移除域名、in表示提交内容域名转化"); from.Submit("确认", "Site.Config"); return from; @@ -1135,7 +1136,7 @@ namespace UMC.Proxy.Activities ui2 = ui.NewSection().AddCell("内容转化", String.IsNullOrEmpty(site.HostReConf) ? "未设置" : "已设置", new UIClick(new WebMeta(request.Arguments).Put(g, "HostReConf")).Post(request.Model, request.Command)); - ui2.AddCell("透传会话", String.IsNullOrEmpty(site.OutputCookies) ? "未设置" : "已设置", new UIClick(new WebMeta(request.Arguments).Put(g, "OutputCookie")).Post(request.Model, request.Command)); + // ui2.AddCell("透传会话", String.IsNullOrEmpty(site.OutputCookies) ? "未设置" : "已设置", new UIClick(new WebMeta(request.Arguments).Put(g, "OutputCookie")).Post(request.Model, request.Command)); var m = "标准模式"; switch ((site.UserModel ?? UserModel.Standard)) diff --git a/Proxy/SiteConfig.cs b/Proxy/SiteConfig.cs index 8bf3f54..b4fa7d0 100644 --- a/Proxy/SiteConfig.cs +++ b/Proxy/SiteConfig.cs @@ -196,7 +196,7 @@ namespace UMC.Proxy if (String.IsNullOrEmpty(timeout) == false) { - Net.NetProxy.KeepAlives[new Uri(tUrl).Authority] = Utility.IntParse(timeout, 60); + Net.NetProxy.KeepAlives(new Uri(tUrl), Utility.IntParse(timeout, 60)); } } else @@ -234,10 +234,6 @@ namespace UMC.Proxy test[tUrl.Trim()] = new TestUrl { Auths = tAuth.ToArray(), Users = tUsers.ToArray(), Url = tUrl }; } - // if (String.IsNullOrEmpty(timeout) == false) - // { - // Net.NetProxy.KeepAlives[new Uri(tUrl).Authority] = Utility.IntParse(timeout, 0); - // } } else { @@ -255,7 +251,7 @@ namespace UMC.Proxy } if (String.IsNullOrEmpty(timeout) == false) { - Net.NetProxy.KeepAlives[new Uri(tUrl).Authority] = Utility.IntParse(timeout, 0); + Net.NetProxy.KeepAlives(new Uri(tUrl), Utility.IntParse(timeout, 60)); } } @@ -592,6 +588,12 @@ namespace UMC.Proxy var url = String.Empty; if (String.IsNullOrEmpty(dName2) == false) { + var tIndex = dName2.IndexOf('~'); + if (tIndex > 0) + { + dName2 = dName2.Substring(0, tIndex).Trim(); + } + if (dName2.EndsWith("]")) { var vin = dName2.LastIndexOf("["); diff --git a/Resources/login.html b/Resources/login.html index 10a8b47..361cdce 100644 --- a/Resources/login.html +++ b/Resources/login.html @@ -1,5 +1,6 @@  + {title} @@ -13,85 +14,7 @@ padding: 0; } - + @@ -106,11 +29,12 @@ {user}
-
- {home} - + + + \ No newline at end of file