Cookie优化

pull/1/head
吴顺明 2 years ago
parent b27a44d44e
commit e723eb8ba2

@ -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";

@ -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";

@ -331,7 +331,8 @@ namespace UMC.Proxy
}
}
}
List<String> OuterCookies = new List<string>();
HashSet<String> OuterCookies = new HashSet<string>();
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<Hashtable>(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;

@ -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))

@ -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("[");

@ -1,5 +1,6 @@
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{title}</title>
@ -13,85 +14,7 @@
padding: 0;
}
</style>
<script>UMC(function ($) {
var data = {fields};
var feilds = $('#feilds');
var htmls = [];
for (var k = 0; k < data.length; k++) {
var v = data[k];
var select = $(document.createElement('select')).addClass('el-input__inner')
.attr('required', 'true').attr('name', v.name)
.attr('forchange', v.change);
var ops = v.data || [{ Text: ['--请选择', v.title, '--'].join(''), Value: '' }]
for (var i = 0; i < ops.length; i++) {
select.append(new Option(ops[i].Text, ops[i].Value));
}
$(document.createElement('div')).addClass('el-form-item el-input')
.append(select).appendTo(feilds);
$(document.createElement('input')).attr('type', 'hidden')
.attr('name', v.name + '_Text').val(ops[0].Text).appendTo(feilds);
}
var fm = $('form').submit(function () { return $(this).val(); });
fm.find('input,select').change(function () {
var me = $(this);
var name = this.name;
if (me.is('select')) {
fm.find('input[name=' + name + '_Text]').val(this.options[this.selectedIndex].text);
}
var fmValue = {};
fm.find('input,select').each(function () {
if (this.value) {
fmValue[this.name] = this.value
}
});
fm.find('select')
.each(function () {
var cv = $(this).attr('forchange');
if (cv) {
var nv = cv.split(',');
var isOk = 0;
for (var i = 0; i < nv.length; i++) {
if (!fmValue[nv[i]]) return;
if (!isOk) {
isOk = nv[i] == name;
}
}
if (isOk) {
delete fmValue[this.name];
while (this.options.length > 1) {
this.remove(this.options.length - 1);
}
var slt = $(this);
fetch('/UMC.Login/' + this.name, {
credentials: 'include',
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: $.query(fmValue)
}).then(function (r) { return r.json() }).then(function (json) {
for (var i = 0; i < json.length; i++) {
var vk = json[i];
slt.append(new Option(vk.Text, vk.Value));
}
if (json.length) {
slt[0].selectedIndex = 1;
slt.change();
}
});
}
}
})
}).eq(0).change().length ? $('button').text('确认对接') : fm.submit();
});</script>
<script src="/UMC.js/umc.login.js"></script>
</head>
<body>
@ -106,11 +29,12 @@
</div>
{user}
<div id="feilds">
</div>
{home}
<button type="submit" class="el-button el-button--primary el-button--default" style="width: 100%; margin-bottom: 30px;">···正在核验···</button>
<script id="feildItems" type="text/plain">{fields}</script>
<button type="submit" class="el-button el-button--primary el-button--default"
style="width: 100%; margin-bottom: 30px;">···正在核验···</button>
</form>
</div>
</body>
</html>
Loading…
Cancel
Save