forked from ManifestHub/ManifestHub
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProgram.cs
209 lines (179 loc) · 7.17 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
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using ManifestHub;
using CommandLine;
using Newtonsoft.Json;
using SteamKit2;
using SteamKit2.Authentication;
using System.Security.Cryptography;
using System.Text;
var result = Parser.Default.ParseArguments<Options>(args)
.WithNotParsed(errors =>
{
foreach (var error in errors)
{
Console.WriteLine(error);
}
Environment.Exit(1);
});
var gdb = new GitDatabase(".", result.Value.Token ?? throw new NullReferenceException(),
result.Value.Key ?? throw new NullReferenceException());
var semaphore = new SemaphoreSlim(result.Value.ConcurrentAccount);
var tasks = new ConcurrentBag<Task>();
var writeTasks = new ConcurrentBag<Task>();
switch (result.Value.Mode)
{
case "download":
var index = 0;
var total = gdb.GetAccounts().Count();
foreach (var accountInfo in gdb.GetAccounts(true))
{
await semaphore.WaitAsync();
Console.WriteLine($"[{index++}/{total}]Dispatching {accountInfo.AccountName}...");
tasks.Add(Task.Run(async () =>
{
var downloader = new ManifestDownloader(accountInfo);
try
{
await downloader.Connect().ConfigureAwait(false);
var info = await downloader.GetAccountInfo();
await gdb.WriteAccount(info);
await downloader.DownloadAllManifestsAsync(result.Value.ConcurrentManifest, gdb, writeTasks)
.ConfigureAwait(false);
}
catch (AuthenticationException e) when (e.Result is
EResult.AccessDenied
or EResult.AccountLogonDeniedVerifiedEmailRequired
or EResult.AccountLoginDeniedNeedTwoFactor
or EResult.AccountDisabled
or EResult.InvalidPassword)
{
await gdb.RemoveAccount(accountInfo);
Console.WriteLine($"{e.Result} for {downloader.Username}. Removed.");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
_ = downloader.Disconnect();
semaphore.Release();
}
}));
}
await Task.WhenAll(tasks);
Console.WriteLine("等待写入进程...");
await Task.WhenAll(writeTasks);
Console.WriteLine("开始写入Git Tag...");
await gdb.PruneExpiredTags();
Console.WriteLine("写入概括...");
var summaryPath = Environment.GetEnvironmentVariable("GITHUB_STEP_SUMMARY");
if (summaryPath != null)
{
var summaryFile = File.OpenWrite(summaryPath);
summaryFile.Write(Encoding.UTF8.GetBytes(gdb.ReportTrackingStatus()));
summaryFile.Close();
Console.WriteLine("概括已写入.");
}
else
{
Console.WriteLine("无法找到变量GITHUB_STEP_SUMMARY");
}
Console.WriteLine("完成.");
break;
case "account":
var raw = File.ReadAllText(result.Value.Account ?? throw new NullReferenceException());
try
{
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(raw);
var encryptedAccount = dictionary?["payload"];
var rsa = new RSACryptoServiceProvider();
var rsaPrivateKey = Environment.GetEnvironmentVariable("RSA_PRIVATE_KEY");
rsa.ImportFromPem(rsaPrivateKey);
var decryptedBytes = rsa.Decrypt(Convert.FromBase64String(encryptedAccount!), true);
raw = Encoding.UTF8.GetString(decryptedBytes);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
KeyValuePair<string, List<string?>>[] account;
try
{
var accountJson = JsonConvert.DeserializeObject<Dictionary<string, List<string?>>>(raw);
account = accountJson!.ToArray();
}
catch (Exception)
{
account = [];
Console.WriteLine("错误的账号文件.");
Environment.Exit(1);
}
for (var i = result.Value.Index; i < account.Length; i += result.Value.Number)
{
var infoPrev = gdb.GetAccount(account[i].Key);
ManifestDownloader downloader;
if (infoPrev != null)
{
infoPrev.AccountPassword = account[i].Value.FirstOrDefault();
downloader = new ManifestDownloader(infoPrev);
}
else
{
downloader = new ManifestDownloader(new AccountInfoCallback(
account[i].Key,
account[i].Value.FirstOrDefault()
));
}
await semaphore.WaitAsync();
Console.WriteLine($"[派遣] {account[i].Key}...");
tasks.Add(Task.Run(async () =>
{
try
{
await downloader.Connect().ConfigureAwait(false);
var info = await downloader.GetAccountInfo();
if (infoPrev == null || info.RefreshToken != infoPrev.RefreshToken)
await gdb.WriteAccount(info);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
await downloader.Disconnect();
semaphore.Release();
}
}));
}
await Task.WhenAll(tasks);
break;
default:
Console.WriteLine("未知模式.");
Environment.Exit(1);
break;
}
namespace ManifestHub
{
internal class Options
{
[Value(0, MetaName = "Mode", Default = "download", HelpText = "Mode of operation.")]
public string? Mode { get; set; }
[Option('a', "account", Required = false, HelpText = "Account file.")]
public string? Account { get; set; }
[Option('t', "token", Required = true, HelpText = "GitHub Access Token.")]
public string? Token { get; set; }
[Option('c', "concurrent-account", Required = false, HelpText = "Concurrent account.", Default = 4)]
public int ConcurrentAccount { get; set; }
[Option('p', "concurrent-manifest", Required = false, HelpText = "Concurrent manifest.", Default = 16)]
public int ConcurrentManifest { get; set; }
[Option('i', "index", Required = false, HelpText = "Index of instance.", Default = 0)]
public int Index { get; set; }
[Option('n', "number", Required = false, HelpText = "Number of instances.", Default = 1)]
public int Number { get; set; }
[Option('k', "key", Required = false, HelpText = "Encryption key.")]
public string? Key { get; set; }
}
}