using AllBase; using AllBase.Helper; using AllBase.Model.Result; using CefSharp; using CefSharp.WinForms; using TemuKJD.KJDForms.BaseForms; using NaiBa.CefHelper; using NaiBa.CefHelper.Handler; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Work; using Work.BLL.Tools; namespace TemuKJD.KJDForms.ShopForms { public partial class ShopAuthForm : LRBaseForm { public int CloseAfterSecond { get; set; } public string shopAuthUrl = "https://fuwu.jinritemai.com/authorize?service_id=815"; private ChromiumWebBrowser cefWebBrowser = null; private DouShopLoginInfo shop = null; private bool isCefLoad = false; private string currentUrl = ""; public ShopAuthForm(DouShopLoginInfo shopInfo) { InitializeComponent(); this.shop = shopInfo; this.txtUserName.Text = shopInfo.LoginName; this.txtPassWord.Text = DataOpenSSL.DecryptString(shopInfo.Password); Task.Run(() => { var url = BaseClass.BuildObjString("shop&act=add_shop_agent_name"); var data = UserLogin.GetSignaturex() + "&shop_id=" + shop.ShopId + "&agent_name=" + BaseClass.AgentName; var result = GetHtml.PostUtf8(url, data); }); } private void ShopLoginForm_Load(object sender, EventArgs e) { if (CloseAfterSecond > 0) { Task.Run(() => { Thread.Sleep(1000 * CloseAfterSecond); if (this.Created) { Invoke(new Action(() => { this.Close(); })); } }); } Task.Run(() => { bool isAuth = false; while (this.Created) { Thread.Sleep(1000); if (this.shop != null && !isAuth && currentUrl.StartsWith("https://fuwu.jinritemai.com/authorize?service_id=")) { // 勾选授权选项 Thread.Sleep(Randomx.GetId(1000, 1200)); var jsStr = "document.getElementsByClassName('auxo-checkbox-input')[0].click();"; var response = cefWebBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr); Thread.Sleep(Randomx.GetId(500, 800)); jsStr = "function clickElement(txt) {\r\n" + "var child = document.body.firstElementChild;\r\n" + "var element1 = null;\r\n" + " while (child) {\r\n" + " if (child.innerHTML.indexOf(txt) >= 0) {\r\n" + " element1 = child;\r\n" + " if (child.firstElementChild) {\r\n" + " child = child.firstElementChild; continue;\r\n" + " } else break;\r\n" + " }\r\n" + " child = child.nextElementSibling;\r\n" + " }\r\n" + " if (element1) {\r\n" + " element1.click(); return 'success';\r\n" + " } else return 'no_pop';\r\n" + "}\r\n" + "clickElement('确认授权');\r\n"; response = cefWebBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr); if (response.Result?.Result != null) { isAuth = true; } } // 授权成功自动关闭 if (this.shop != null && currentUrl.StartsWith("http://new.qixiaoke.com/doudian.php?code=")) { if (!cefWebBrowser.GetBrowser().MainFrame.IsValid) return; var jsStr = "function getShopName() { return document.body.innerHTML; } getShopName();"; var response = cefWebBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr); response.Wait(1000); var res = response.Result.Result?.ToString(); if (res != null && res.StartsWith("授权成功") && res.EndsWith(shop.ShopAccount)) { //shop.ApiAuthStatus = "授权成功"; ShopListForm.UpdateShopTokens(GlobalConfig.ShopLoginList.Where(s => s.ShopAccount == shop.ShopAccount).ToList()); StartUp.SaveLocalShopLoginInfo(); OnSaveComplete(shop); if (this.Created && this.chkCloseAfterLogin.Checked) { Thread.Sleep(2000); Invoke(new Action(() => { this.Close(); })); } } } // 授权成功自动关闭 if (this.shop != null && this.currentUrl.StartsWith("http://ddxnt.nai8.net/#/login?code=")) { Thread.Sleep(3000); while (cefWebBrowser.IsLoading) Thread.Sleep(100); Thread.Sleep(2000); if (this.Created && this.chkCloseAfterLogin.Checked) { ShopListForm.UpdateShopTokens(GlobalConfig.ShopLoginList.Where(s => s.ShopAccount == shop.ShopAccount).ToList()); StartUp.SaveLocalShopLoginInfo(); OnSaveComplete(shop); Thread.Sleep(2000); Invoke(new Action(() => { this.Close(); })); } } } }); if (cefWebBrowser == null) { //https://fuwu.jinritemai.com/detail?service_id=20974 if (!shopAuthUrl.Contains("service_id=20896")) { var url = BaseClass.BuildObjString("config&act=get_buy_service"); var data = UserLogin.GetSignaturex() + "&app_key=" + shop.AppKey; var result = GetHtml.PostUtf8(url, data); if (!result.Contains("\"code\":200")) result = GetHtml.PostUtf8(url, data); if (!result.Contains("\"code\":200")) { BaseMsgForm.ShowError("获取授权地址失败!请关闭窗口重试。"); return; } shopAuthUrl = BaseRegex.GetStringJson(ref result, "data"); } isCefLoad = false; cefWebBrowser = CEFBrowser.CreateChromiumWebBrowser(shopAuthUrl, false); CustomResourceRequestHandler resourceRequestHandler = new CustomResourceRequestHandler(); resourceRequestHandler.ResourceLoadCompleteEvent += RequestHandler_ResourceLoadComplete; cefWebBrowser.RequestHandler = new CustomRequestHandler(resourceRequestHandler); cefWebBrowser.KeyboardHandler = new KeyboardHandler(); cefWebBrowser.FrameLoadEnd += CefWebBrowser_FrameLoadEnd; cefWebBrowser.AddressChanged += CefWebBrowser_AddressChanged; cefWebBrowser.LoadError += CefWebBrowser_LoadError; cefWebBrowser.Dock = DockStyle.Fill; var t = CEFBrowser.SetCookieAsync("jinritemai.com", shop.Cookies, cefWebBrowser.RequestContext.GetCookieManager(null)); splitContainer1.Panel1.Controls.Add(cefWebBrowser); } else { CEFBrowser.DeleteCookie(cefWebBrowser.RequestContext.GetCookieManager(null)); CEFBrowser.SetCookieAsync("jinritemai.com", shop.Cookies, cefWebBrowser.RequestContext.GetCookieManager(null)); } } private void CefWebBrowser_LoadError(object sender, CefSharp.LoadErrorEventArgs e) { } private void CefWebBrowser_AddressChanged(object sender, CefSharp.AddressChangedEventArgs e) { Invoke(new Action(() => { currentUrl = e.Address; txtAddress.Text = e.Address; })); } private void CefWebBrowser_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e) { //if (!cefWebBrowser.IsBrowserInitialized) return; //currentUrl = e.Url; //if (currentUrl.StartsWith("https://fxg.jinritemai.com/ffa/multiShop")) //{ // Thread.Sleep(Randomx.GetId(500, 800)); //} //if (currentUrl.StartsWith("https://fxg.jinritemai.com/ffa/mshop/homepage/index")) //{ // Task.Run(() => // { // // }); //} } private void RequestHandler_ResourceLoadComplete(object sender, string url, string postData, string responseData) { if (url.StartsWith("https://fuwu.jinritemai.com/api/order/list")) { var data = responseData; Task.Run(() => { }); } if (url.StartsWith("https://fxg.jinritemai.com/common/index/index")) { Task.Run(() => { var data = responseData; if (this.shop != null && data.Contains("\"code\":0")) { shop.ShopId = BaseRegex.GetStringJson(ref data, "id"); shop.ShopToken = BaseRegex.GetStringJson(ref data, "token"); shop.ShopAccount = BaseRegex.GetStringJson(ref data, "shop_name"); shop.Mobile = BaseRegex.GetStringJson(ref data, "mobile"); } }); } if (url.Contains("_signature")) { Task.Run(() => { var _url = url; var data = responseData; var signature = url.Substring(url.IndexOf("_signature=") + "_signature=".Length).Split('&')[0]; if (shop != null) { shop.Signature = signature; } }); } } /// /// 自动登录 /// /// /// /// private void InputUserNameAndPassword(ChromiumWebBrowser webBrowser, string _account, string _password) { string jsStr = "var btn = document.getElementsByClassName('account-center-switch-button switch-switch email false')[0]; if (btn) btn.click();"; var response = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr); response.Wait(1000); Thread.Sleep(Randomx.GetId(500, 1000)); string jsSetValue = @" function keyboardInput(dom, st) { var evt = new InputEvent('input', { //var evt = new Event('input', { // cef57 inputType: 'insertText', data: st, dataTransfer: null, isComposing: false }); dom.value = st; dom.dispatchEvent(evt); dom.focus(); };"; string jsInputKey = @" function fireKeyEvent(el, evtType, keyCode) { var evtObj; if (document.createEvent) { if (window.KeyEvent) { //firefox 浏览器下模拟事件 evtObj = document.createEvent('KeyEvents'); evtObj.initKeyEvent(evtType, true, true, window, true, false, false, false, keyCode, 0); } else { //chrome 浏览器下模拟事件 evtObj = document.createEvent('UIEvents'); evtObj.initUIEvent(evtType, true, true, window, 1); delete evtObj.keyCode; if (typeof evtObj.keyCode === 'undefined') { //为了模拟keycode Object.defineProperty(evtObj, 'keyCode', { value: keyCode }); } else { evtObj.key = String.fromCharCode(keyCode); } if (typeof evtObj.ctrlKey === 'undefined') { //为了模拟ctrl键 Object.defineProperty(evtObj, 'ctrlKey', { value: true }); } else { evtObj.ctrlKey = true; } } el.dispatchEvent(evtObj); } else if (document.createEventObject) { //IE 浏览器下模拟事件 evtObj = document.createEventObject(); evtObj.keyCode = keyCode el.fireEvent('on' + evtType, evtObj); } }"; // 输入用户名 jsStr = @"var txt1 = document.getElementsByName('email')[0]; txt1.focus(); txt1.click(); keyboardInput(txt1, '" + _account + "');" + "var uname = '" + _account + "';" + "for(var i = 0; i < uname.length; i++) {" + " cCode = uname.charCodeAt(i);" + " fireKeyEvent(txt1, 'keydown', cCode);" + " fireKeyEvent(txt1, 'keypress', cCode);" + " fireKeyEvent(txt1, 'keyup', cCode);" + "}"; response = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsSetValue + jsInputKey + jsStr); response.Wait(2000); Thread.Sleep(Randomx.GetId(400, 800)); // 输入密码 jsStr = @"var txt2 = document.getElementsByName('password')[0]; txt2.focus(); txt2.click(); keyboardInput(txt2, '" + _password + "');" + "var pwd = '" + _password + "';" + "for(var i = 0; i < pwd.length; i++) {" + " cCode = pwd.charCodeAt(i);" + " fireKeyEvent(txt2, 'keydown', cCode);" + " fireKeyEvent(txt2, 'keypress', cCode);" + " fireKeyEvent(txt2, 'keyup', cCode);" + "}"; response = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsInputKey + jsStr); response.Wait(2000); Thread.Sleep(Randomx.GetId(400, 800)); // 点击登录按钮 jsStr = "var btn = document.getElementsByClassName('account-center-action-button')[0]; btn.click(); //btn.dispatchEvent(new Event('change'));"; response = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr); response.Wait(2000); Thread.Sleep(1500 + Randomx.GetId(1000)); #region 获取验证码 int getImageCount = 0; getImageAgain: jsStr = "function getImg() { return document.getElementsByClassName('captcha_verify_img--wrapper')[0].innerHTML; }"; jsStr += "getImg();"; response = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr); response.Wait(2000); if (response.Result?.Result == null) { if (getImageCount < 50) { Thread.Sleep(2500); getImageCount++; goto getImageAgain; } } if (response.Result?.Result != null) { var imgStr = response.Result.Result.ToString(); var src = BaseRegex.GetMatchRegexString(ref imgStr, "src=\"([\\s\\S]*?)\""); Bitmap bt = (Bitmap)GetHtml.LoadImage(src); if (bt != null) { int Stopxt = 0; int dragButtonWidth = 42; //Stopxt = new Base64ChangeImgUrl().CalculateNick(bt, dragButtonWidth); Thread.Sleep(500 + Randomx.GetId(1000)); Stopxt = new Base64ChangeImgUrl().CalculateNickNew(bt, dragButtonWidth); if (Stopxt > 0) { jsStr = "function getImgPos() { return document.getElementsByClassName('secsdk-captcha-drag-icon')[0].getBoundingClientRect().left + '|' " + " + document.getElementsByClassName('secsdk-captcha-drag-icon')[0].getBoundingClientRect().top;} getImgPos();"; response = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr); response.Wait(2000); if (response.Result?.Result != null) { var xy = response.Result?.Result.ToString(); var xyArr = xy.Split('|'); if (xyArr.Length == 2) { try { int x = (int)Convert.ToDecimal(xyArr[0]) + 25; int y = (int)Convert.ToDecimal(xyArr[1]) + 25; // 自动滑块图形验证 if (GlobalConfig.DouShopSettingParam.IsAutoDragLogin) { ShopLoginForm.CefMouseDragDou(cefWebBrowser, x, y, Convert.ToInt32((x + Stopxt * 1.2319m) - 4), y); for (int i = 0; i < 1; i++) Thread.Sleep(2000 + Randomx.GetId(1000)); response = webBrowser.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr); response.Wait(2000); if (response.Result?.Result != null && getImageCount < 50) { getImageCount++; goto getImageAgain; } } } catch { } } } for (int i = 0; i < 1; i++) Thread.Sleep(2000 + Randomx.GetId(1000)); } } } Thread.Sleep(1500 + Randomx.GetId(1000)); #endregion } // 鼠标左键按下,移动,移动...左键抬起。 public static void CefMouseDragDou(ChromiumWebBrowser webBrowser, int x1, int y1, int x2, int y2) { Thread.Sleep(100); var host = webBrowser.GetBrowser().GetHost(); var rx = -6 + Randomx.GetId(13); x2 = x2 + rx; // 随机按下点的位置(正负6像素),提高仿真性 int x = x1 + rx; int y = y1 - 6 + Randomx.GetId(13); // 按下鼠标左键 host.SendMouseClickEvent(x, y, MouseButtonType.Left, false, 1, CefEventFlags.None); Thread.Sleep(100 + Randomx.GetId(300)); // 滑动光标 int times = -20; while (x < x2 + 4 - Randomx.GetId(9)) { times++; x += (15 + Randomx.GetId(15)) / 8; if (times % 5 == 0) y = y - 1 + Randomx.GetId(3); Thread.Sleep(6 + Math.Abs(times) / 5 + Randomx.GetId(16)); host.SendMouseMoveEvent((int)x, (int)y, false, CefEventFlags.LeftMouseButton); } if (x < x2 - 1) while (x < x2) { times++; x += (8 + Randomx.GetId(8)) / 8; if (times % 5 == 0) y = y - 1 + Randomx.GetId(3); Thread.Sleep(6 + Math.Abs(times) / 5 + Randomx.GetId(16)); host.SendMouseMoveEvent((int)x, (int)y, false, CefEventFlags.LeftMouseButton); } else if (x > x2 + 1) while (x > x2) { times++; x -= (8 + Randomx.GetId(8)) / 8; if (times % 5 == 0) y = y - 1 + Randomx.GetId(3); Thread.Sleep(6 + Math.Abs(times) / 5 + Randomx.GetId(16)); host.SendMouseMoveEvent((int)x, (int)y, false, CefEventFlags.LeftMouseButton); } // 如果最后位置偏差较多,再做一次调整 x = x2 - 1 + Randomx.GetId(3); x = x2; Thread.Sleep(18 + Math.Abs(times) / 5 + Randomx.GetId(16)); Thread.Sleep(18 + Math.Abs(times) / 5 + Randomx.GetId(16)); host.SendMouseMoveEvent((int)x, (int)y, false, CefEventFlags.LeftMouseButton); // 抬起鼠标左键 Thread.Sleep(300 + Randomx.GetId(500)); host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.None); host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 1, CefEventFlags.None); } private void btnStop_Click(object sender, EventArgs e) { } private void btnCopyUserName_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtUserName.Text)) Clipboard.SetDataObject(txtUserName.Text); } private void btnCopyPassword_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtPassWord.Text)) Clipboard.SetDataObject(txtPassWord.Text); } private void btnReload_Click(object sender, EventArgs e) { if (cefWebBrowser != null) { cefWebBrowser.Load(txtAddress.Text); } } private void ShopAuthForm_FormClosed(object sender, FormClosedEventArgs e) { //OnSaveComplete(shop); } private void btnAuthInWeb_Click(object sender, EventArgs e) { GetHtml.OpenUrl(this.shopAuthUrl); } private void btnBuyAuth_Click(object sender, EventArgs e) { if (cefWebBrowser != null) { var buyUrl = shopAuthUrl; if (!shopAuthUrl.Contains("service_id=20896")) { var url = BaseClass.BuildObjString("config&act=get_buy_service"); var data = UserLogin.GetSignaturex(); var result = GetHtml.PostUtf8(url, data); if (!result.Contains("\"code\":200")) result = GetHtml.PostUtf8(url, data); if (!result.Contains("\"code\":200")) { BaseMsgForm.ShowError("获取授权地址失败!请关闭窗口重试。"); return; } buyUrl = BaseRegex.GetStringJson(ref result, "data"); } cefWebBrowser.Load(buyUrl.Replace("authorize", "detail")); } } } }