-
Notifications
You must be signed in to change notification settings - Fork 54
/
Form1.cs
432 lines (366 loc) · 13.5 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using CefSharp;
using CefSharp.DevTools.Network;
using CefSharp.WinForms;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Forms;
namespace JD_Get
{
public partial class Form1 : Form
{
public QLHelp ql { set; get; }
public List<string> needCookieName { set; get; }
//https://bean.m.jd.com/bean/signIndex.action
public string LoginUrl = "https://home.m.jd.com/myJd/home.action";
public bool Auto = false;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
GetQLConfig();
}
public void GetQLConfig()
{
ql = new QLHelp( );
}
private void Form1_Load(object sender, EventArgs e)
{
string auto = ConfigHelp.GetConfig("Auto");
if (!string.IsNullOrEmpty(auto))
{
Auto = Convert.ToBoolean(auto);
}
this.checkBox1.Checked = Auto;
LoginInitAsync();
InitAccount();
this.Location = Properties.Settings.Default.FormLocation;
this.Size = Properties.Settings.Default.FormSize;
this.Text = this.Text + "V" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
#if DEBUG
this.label1.Text = "123";
#endif
}
private void LoginInitAsync()
{
this.chromiumWebBrowser1.LoadUrlAsync(LoginUrl);
string script = "";
script += $@"setTimeout(function() {{
{GetLoginScript()}
}},2000)";
this.chromiumWebBrowser1.ExecuteScriptAsyncWhenPageLoaded(script);
}
/// <summary>
/// 获取cookie
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
GetCookies();
}
/// <summary>
/// cookie 格式化
/// 因为是异步获取所以采用追加到文本框的方式
/// </summary>
/// <param name="cookie"></param>
private void visitor_SendCookie(CefSharp.Cookie cookie)
{
if (cookie.Name == "pt_key" || cookie.Name == "pt_pin") {
this.textBox1.Text += $"{cookie.Name}={cookie.Value};";
}
if(cookie.Name == "pt_pin")
{
this.label1.Text = cookie.Value;
}
}
private class CookieVisitor : ICookieVisitor
{
//public List<CefSharp.Cookie> AllCookies { get; } = new List<CefSharp.Cookie>();
public event Action<CefSharp.Cookie> SendCookie;
public bool Visit(CefSharp.Cookie cookie, int count, int total, ref bool deleteCookie)
{
deleteCookie = false;
if (SendCookie != null)
{
SendCookie(cookie);
}
//AllCookies.Add(cookie);
return true;
}
public void Dispose()
{
// Dispose相关资源
}
}
private void button2_Click(object sender, EventArgs e)
{
string pt_pin = this.label1.Text;
if (string.IsNullOrEmpty(ql.ClientSecret) || string.IsNullOrEmpty(ql.ClientID) || string.IsNullOrEmpty(ql.Url))
{
MessageBox.Show("青龙面板配置不完整,点击青龙配置按钮,输入完成参数");
return;
}
if (string.IsNullOrEmpty(pt_pin))
{
MessageBox.Show("未获取到cookie 请先登录后点击获取Cookies按钮");
return;
}
string value = this.textBox1.Text;
if (comboBox1.SelectedItem != null)
{
var selected = (AccountHelp.Account)comboBox1.SelectedItem;
if (selected.Login != null)
{
value += $";from_jdget={selected.Login};";
}
}
var res = Send(pt_pin, value);
this.textBox2.Text += (res + "\r\n");
}
/// <summary>
/// 发送cookies到青龙
/// </summary>
/// <param name="pin"></param>
/// <param name="key"></param>
private string Send(string pin, string key)
{
try
{
if (string.IsNullOrEmpty(ql.Token))
{
ql.Login();
}
string id = ql.GetEnvs(pin);
if (string.IsNullOrEmpty(id))
{
ql.AddEnvs(pin,key);
}
else
{
ql.UpdateEnvs(id, key);
ql.EnableEnvs(new List<string>() { id });
}
MessageBox.Show("发送成功");
return $"[{DateTime.Now.ToString("HH:mm:ss")}] pt_pin为{ pin }发送成功";
}
catch(Exception e)
{
ql.Token = "";
LogHelper.Error(e,"发送日志:");
MessageBox.Show("发送失败:"+e.Message);
//return "pin为" + pin + "发送失败 失败原因"+ e.Message;
return $"[{DateTime.Now.ToString("HH:mm:ss")}] pt_pin为{ pin }发送失败 请查看日志";
}
}
private void ClearCookie()
{
// 清除所有cookies
Cef.GetGlobalCookieManager().DeleteCookies("", "");
}
private void button3_Click(object sender, EventArgs e)
{
try
{
var ShowReLoginSure = ConfigHelp.GetConfig("ShowReLoginSure");
if (string.IsNullOrEmpty(ShowReLoginSure))
{
DialogResult AF = MessageBox.Show("您确定重新登录?", "确认框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (AF == DialogResult.OK)
{
ClearCookie();
LoginInitAsync();
this.textBox1.Text = "";
this.label1.Text = "";
}
}
else
{
ClearCookie();
LoginInitAsync();
this.textBox1.Text = "";
this.label1.Text = "";
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button4_Click(object sender, EventArgs e)
{
EditQL popup = new EditQL(this);
popup.StartPosition = FormStartPosition.CenterParent;
popup.ShowDialog(this);
}
private void button5_Click(object sender, EventArgs e)
{
AccountForm popup = new AccountForm(this);
popup.StartPosition = FormStartPosition.CenterParent;
popup.ShowDialog(this);
}
public void InitAccount()
{
var accounts= AccountHelp.GetAccounts();
//comboBox1.DisplayMember = "Login";
//comboBox1.ValueMember = "Login";
// 将整个列表绑定到ComboBox的DataSource
comboBox1.DataSource = accounts;
//comboBox1.SelectedIndex = -1;
}
/// <summary>
/// 直接输入账号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//CompleteAP();
var select = (System.Windows.Forms.ComboBox)sender;
if (this.checkBox1.Checked)
{
if (select.SelectedIndex > 0)
{
ClearCookie();
LoginInitAsync();
this.textBox1.Text = "";
this.label1.Text = "";
}
}
}
public void CompleteAP() {
var LoginScript = GetLoginScript();
if (!string.IsNullOrEmpty(LoginScript))
{
try
{
this.chromiumWebBrowser1.ExecuteScriptAsync(LoginScript);
}
catch (Exception ex)
{
LogHelper.Error(ex, "自动输入账号");
}
}
}
public string GetLoginScript()
{
var account = (AccountHelp.Account)this.comboBox1.SelectedItem;
if (account != null)
{
String execJs = "(function() {";
execJs += "if(!document.getElementsByClassName('policy_tip-checkbox')[0].checked) { document.getElementsByClassName('policy_tip-checkbox')[0].click(); }";
execJs += "if(document.getElementById('username').closest('div').style.display=='none'){ document.getElementsByClassName('planBLogin')[0].click(); }";
execJs += "var account='" + account.Login + "';";
execJs += "var password='" + account.Password + "';";
execJs += "var evt=new InputEvent('input',{inputType:'insertText',data:account,dataTransfer:null,isComposing:false});";
execJs += "document.getElementById('username').value=account;";
execJs += "document.getElementById('username').dispatchEvent(evt);";
execJs += @"
var evt=new InputEvent('input',{inputType:'insertText',data:password,dataTransfer:null,isComposing:false});
document.getElementById('pwd').value=password;
document.getElementById('pwd').dispatchEvent(evt);
";
execJs += "document.querySelector('#app>div>a').click();";
//execJs += "alert('ok');";
execJs += "})();";
return execJs;
}
return "";
}
/// <summary>
/// 登录后自动获取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chromiumWebBrowser1_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
//var brow = (CefSharp.WinForms.ChromiumWebBrowser)sender;
//var addr = brow.Address;
//if(addr== "https://home.m.jd.com/myJd/home.action")
//{
// GetCookies();
//}
}
/// <summary>
/// 获取cookie
/// </summary>
private void GetCookies()
{
this.textBox1.Text = "";
this.label1.Text = "";
var cookieManager = this.chromiumWebBrowser1.GetCookieManager();
var visitor = new CookieVisitor();
visitor.SendCookie += visitor_SendCookie;
cookieManager.VisitAllCookies(visitor);
}
/// <summary>
/// 保存窗口属性
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.Location.X > 0 && this.Location.Y > 0)
{
Properties.Settings.Default.FormLocation = this.Location;
}
if (this.Size.Width > 0 && this.Size.Height > 0)
{
Properties.Settings.Default.FormSize = this.Size;
}
Properties.Settings.Default.Save();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Auto = checkBox1.Checked;
ConfigHelp.SetSetting("Auto", checkBox1.Checked.ToString());
}
private void button6_Click(object sender, EventArgs e)
{
var form = new Form1();
form.Show();
}
private void button7_Click(object sender, EventArgs e)
{
CompleteAP();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void button8_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(ql.Token))
{
ql.Login();
}
var all = ql.GetAllJDCookieEnvs();
string pattern = @"from_jdget=(.+);";
List<string> allDisable = new List<string>();
foreach (var item in all.Where(n => n.status == 1))
{
//item.value.re
Match match = Regex.Match(item.value, pattern);
if (match.Success)
{
allDisable.Add("账号:" + match.Groups[1]);
}
else
{
allDisable.Add("cookie:" + item.value);
}
}
textBox3.Text = string.Join("\r\n\r\n", allDisable);
}
}
}