-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathProgram.cs
242 lines (204 loc) · 7.26 KB
/
Program.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
using Newtonsoft.Json.Linq;
using PuppeteerSharp;
using StackExchange.Redis;
using System.Text.Json;
const int TIMEOUT_MS = 60_000;
Conf _conf = Deserialize<Conf>(GetEnvValue("CONF"));
HttpClient _scClient = new();
#region redis
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect($"{_conf.RdsServer},password={_conf.RdsPwd},name=Note163Checkin,defaultDatabase=0,allowadmin=true,abortConnect=false");
IDatabase db = redis.GetDatabase();
bool isRedis = db.IsConnected("test");
Console.WriteLine("redis:{0}", isRedis ? "有效" : "无效");
#endregion
Console.WriteLine("有道云笔记签到开始运行...");
for (int i = 0; i < _conf.Users.Length; i++)
{
User user = _conf.Users[i];
string title = $"账号 {i + 1}: {user.Task} ";
Console.WriteLine($"共 {_conf.Users.Length} 个账号,正在运行{title}...");
#region 获取cookie
string cookie = string.Empty;
bool isInvalid = true; string result = string.Empty;
if (!string.IsNullOrWhiteSpace(user.Cookie))
{
Console.WriteLine("json-cookie存在,开始验证...");
cookie = user.Cookie;
(isInvalid, result) = await IsInvalid(cookie);
Console.WriteLine("json-cookie状态:{0}", isInvalid ? "无效" : "有效");
}
if (isInvalid)
{
string redisKey = $"Note163_{user.Username}";
if (isRedis)
{
var redisValue = await db.StringGetAsync(redisKey);
if (redisValue.HasValue)
{
cookie = redisValue.ToString();
(isInvalid, result) = await IsInvalid(cookie);
Console.WriteLine("redis获取cookie,状态:{0}", isInvalid ? "无效" : "有效");
}
}
if (isInvalid)
{
cookie = await GetCookie(user);
(isInvalid, result) = await IsInvalid(cookie);
Console.WriteLine("login获取cookie,状态:{0}", isInvalid ? "无效" : "有效");
if (isInvalid)
{//Cookie失效
await Notify($"{title}Cookie失效,请检查登录状态!", true);
continue;
}
}
if (isRedis)
{
Console.WriteLine($"redis更新cookie:{await db.StringSetAsync(redisKey, cookie)}");
}
}
#endregion
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "ynote-android");
client.DefaultRequestHeaders.Add("Cookie", cookie);
long space = 0;
space += Deserialize<YdNoteRsp>(result).RewardSpace;
//签到
result = await (await client.PostAsync("https://note.youdao.com/yws/mapi/user?method=checkin", null))
.Content.ReadAsStringAsync();
space += Deserialize<YdNoteRsp>(result).Space;
//看广告
for (int j = 0; j < 3; j++)
{
result = await (await client.PostAsync("https://note.youdao.com/yws/mapi/user?method=adPrompt", null))
.Content.ReadAsStringAsync();
space += Deserialize<YdNoteRsp>(result).Space;
}
//看视频广告
for (int j = 0; j < 3; j++)
{
result = await (await client.PostAsync("https://note.youdao.com/yws/mapi/user?method=adRandomPrompt", null))
.Content.ReadAsStringAsync();
space += Deserialize<YdNoteRsp>(result).Space;
}
await Notify($"有道云笔记{title}签到成功,共获得空间 {space / 1048576} M");
}
Console.WriteLine("签到运行完毕");
async Task<(bool isInvalid, string result)> IsInvalid(string cookie)
{
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "ynote-android");
client.DefaultRequestHeaders.Add("Cookie", cookie);
//每日打开客户端(即登陆)
string result = await (await client.PostAsync("https://note.youdao.com/yws/api/daupromotion?method=sync", null))
.Content.ReadAsStringAsync();
return (result.Contains("error", StringComparison.OrdinalIgnoreCase), result);
}
async Task<string> GetCookie(User user)
{
var launchOptions = new LaunchOptions
{
Headless = false,
DefaultViewport = null,
ExecutablePath = @"/usr/bin/google-chrome"
};
var browser = await Puppeteer.LaunchAsync(launchOptions);
IPage page = await browser.DefaultContext.NewPageAsync();
await page.GoToAsync(_conf.LoginUrl, TIMEOUT_MS);
bool isLogin = false;
string cookie = "fail";
try
{
#region 登录
//登录
_ = Login(page, user);
int totalDelayMs = 0, delayMs = 100;
while (true)
{
if ((isLogin = IsLogin(page))
|| totalDelayMs > TIMEOUT_MS)
{
break;
}
await Task.Delay(delayMs);
totalDelayMs += delayMs;
}
if (isLogin)
{
var client = await page.Target.CreateCDPSessionAsync();
var ckObj = await client.SendAsync("Network.getAllCookies");
var cks = ckObj.Value<JArray>("cookies")
.Where(p => p.Value<string>("domain").Contains("note.youdao.com"))
.Select(p => $"{p.Value<string>("name")}={p.Value<string>("value")}");
cookie = string.Join(';', cks);
}
#endregion
}
catch (Exception ex)
{
cookie = "ex";
Console.WriteLine($"处理Page时出现异常!{ex.Message};{ex.StackTrace}");
}
finally
{
await browser.DisposeAsync();
}
return cookie;
}
async Task Login(IPage page, User user)
{
try
{
string js = await _scClient.GetStringAsync(_conf.JsUrl);
await page.EvaluateExpressionAsync(js.Replace("@U", user.Username).Replace("@P", user.Password));
}
catch (Exception ex)
{
Console.WriteLine($"Login时出现异常!{ex.Message}. {ex.StackTrace}");
}
}
bool IsLogin(IPage page) => !page.Url.Contains(_conf.LoginStr, StringComparison.OrdinalIgnoreCase);
async Task Notify(string msg, bool isFailed = false)
{
Console.WriteLine(msg);
if (_conf.ScType == "Always" || (isFailed && _conf.ScType == "Failed"))
{
await _scClient.GetAsync($"https://sc.ftqq.com/{_conf.ScKey}.send?text={msg}");
}
}
T Deserialize<T>(string json) => JsonSerializer.Deserialize<T>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
ReadCommentHandling = JsonCommentHandling.Skip
});
string GetEnvValue(string key) => Environment.GetEnvironmentVariable(key);
#region Conf
class Conf
{
public User[] Users { get; set; }
public string ScKey { get; set; }
public string ScType { get; set; }
public string RdsServer { get; set; }
public string RdsPwd { get; set; }
public string LoginUrl { get; set; } = "https://note.youdao.com/mobileSignIn/login_mobile.html?&back_url=https://note.youdao.com/web/&from=web";
public string LoginStr { get; set; } = "signIn";
public string JsUrl { get; set; } = "https://github.com/BlueHtml/pub/raw/main/code/js/note163login.js";
}
class User
{
public string Task { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Cookie { get; set; }
}
#endregion
class YdNoteRsp
{
/// <summary>
/// Sync奖励空间
/// </summary>
public int RewardSpace { get; set; }
/// <summary>
/// 其他奖励空间
/// </summary>
public int Space { get; set; }
}