-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
333 lines (256 loc) · 12.2 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
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
using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading;
using SteamKit2;
namespace CloudKit.Cli
{
class Program
{
static SteamClient steamClient;
static CallbackManager manager;
static SteamUser steamUser;
static SteamCloud steamCloud;
static bool isRunning;
static string user, pass;
static string authCode, twoFactorAuth;
// Directory to write downloaded files to
static string saveDataDir = "Saves";
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Client: No username and password specified!");
return;
}
// save our logon details
user = args[0];
pass = args[1];
// create our steamclient instance
steamClient = new SteamClient();
// create the callback manager which will route callbacks to function calls
manager = new CallbackManager(steamClient);
// get the steamuser handler, which is used for logging on after successfully connecting
steamUser = steamClient.GetHandler<SteamUser>();
steamCloud = steamClient.GetHandler<SteamCloud>();
// register a few callbacks we're interested in
// these are registered upon creation to a callback manager, which will then route the callbacks
// to the functions specified
manager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
manager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
manager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
manager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff);
// this callback is triggered when the steam servers wish for the client to store the sentry file
manager.Subscribe<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth);
isRunning = true;
Console.WriteLine("Connecting to Steam...");
// initiate the connection
steamClient.Connect();
// create our callback handling loop
while (isRunning)
{
// in order for the callbacks to get routed, they need to be handled by the manager
manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
}
}
static void OnConnected(SteamClient.ConnectedCallback callback)
{
Console.WriteLine("Connected to Steam! Logging in '{0}'...", user);
byte[] sentryHash = null;
if (File.Exists("sentry.bin"))
{
// if we have a saved sentry file, read and sha-1 hash it
byte[] sentryFile = File.ReadAllBytes("sentry.bin");
sentryHash = CryptoHelper.SHAHash(sentryFile);
}
steamUser.LogOn(new SteamUser.LogOnDetails
{
Username = user,
Password = pass,
// in this sample, we pass in an additional authcode
// this value will be null (which is the default) for our first logon attempt
AuthCode = authCode,
// if the account is using 2-factor auth, we'll provide the two factor code instead
// this will also be null on our first logon attempt
TwoFactorCode = twoFactorAuth,
// our subsequent logons use the hash of the sentry file as proof of ownership of the file
// this will also be null for our first (no authcode) and second (authcode only) logon attempts
SentryFileHash = sentryHash,
});
}
static void OnDisconnected(SteamClient.DisconnectedCallback callback)
{
// after recieving an AccountLogonDenied, we'll be disconnected from steam
// so after we read an authcode from the user, we need to reconnect to begin the logon flow again
Console.WriteLine("Disconnected from Steam, reconnecting in 5...");
Thread.Sleep(TimeSpan.FromSeconds(5));
steamClient.Connect();
}
static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
{
bool isSteamGuard = callback.Result == EResult.AccountLogonDenied;
bool is2FA = callback.Result == EResult.AccountLoginDeniedNeedTwoFactor;
if (isSteamGuard || is2FA)
{
Console.WriteLine("This account is SteamGuard protected!");
if (is2FA)
{
Console.Write("Please enter your 2 factor auth code from your authenticator app: ");
twoFactorAuth = Console.ReadLine();
}
else
{
Console.Write("Please enter the auth code sent to the email at {0}: ", callback.EmailDomain);
authCode = Console.ReadLine();
}
return;
}
if (callback.Result != EResult.OK)
{
Console.WriteLine("Unable to logon to Steam: {0} / {1}", callback.Result, callback.ExtendedResult);
isRunning = false;
return;
}
Console.WriteLine("Successfully logged on!");
// at this point, we'd be able to perform actions on Steam
}
static void OnLoggedOff(SteamUser.LoggedOffCallback callback)
{
Console.WriteLine("Logged off of Steam: {0}", callback.Result);
}
static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback)
{
Console.WriteLine("Updating sentryfile...");
// write out our sentry file
// ideally we'd want to write to the filename specified in the callback
// but then this sample would require more code to find the correct sentry file to read during logon
// for the sake of simplicity, we'll just use "sentry.bin"
int fileSize;
byte[] sentryHash;
using (var fs = File.Open("sentry.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
fs.Seek(callback.Offset, SeekOrigin.Begin);
fs.Write(callback.Data, 0, callback.BytesToWrite);
fileSize = (int)fs.Length;
fs.Seek(0, SeekOrigin.Begin);
using (var sha = SHA1.Create())
{
sentryHash = sha.ComputeHash(fs);
}
}
// inform the steam servers that we're accepting this sentry file
steamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
{
JobID = callback.JobID,
FileName = callback.FileName,
BytesWritten = callback.BytesToWrite,
FileSize = fileSize,
Offset = callback.Offset,
Result = EResult.OK,
LastError = 0,
OneTimePassword = callback.OneTimePassword,
SentryFileHash = sentryHash,
});
Console.WriteLine("Done!");
}
static string FormatUrl(string host, string path)
{
return $"https://{host}{path}";
}
static async void DownloadFiles(uint appID)
{
if (steamCloud == null)
{
return;
}
var fileListInfo = await steamCloud.GetFileListForApp(appID);
foreach (var file in fileListInfo.Files)
{
var fileInfo = await steamCloud.GetSingleFileInfo(file.AppID, file.FileName);
Console.WriteLine("Filename is: " + fileInfo.FileName);
var downloadFileInfo = await steamCloud.ClientFileDownload(fileInfo.AppID, fileInfo.FileName);
using (var client = new HttpClient())
{
foreach (var header in downloadFileInfo.RequestHeaders)
{
client.DefaultRequestHeaders.Add(header.Name, header.Value);
}
var fileData = await client.GetByteArrayAsync(FormatUrl(downloadFileInfo.UrlHost, downloadFileInfo.UrlPath));
var fileName = Path.Combine(saveDataDir, file.AppID.ToString(), fileInfo.FileName);
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
File.WriteAllBytes(fileName,
downloadFileInfo.RawFileSize != downloadFileInfo.FileSize ? ZipUtil.Decompress(fileData) : fileData);
}
}
}
static async void UploadFileTest(uint appId)
{
string testFileName = "savedata_test.bin";
var testFileData = new byte[] { 0x1, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
var beginFileUploadResp = await steamCloud.RequestFileUpload(appId, testFileName, testFileData);
if (beginFileUploadResp.BlockRequests.Count == 0)
{
CommitFile(appId, testFileName, testFileData, false);
Console.WriteLine("File already exists in cloud");
return;
}
using (var uploadClient = new HttpClient())
{
try
{
using (HttpContent reqBody = new ByteArrayContent(testFileData))
{
foreach (var blockRequest in beginFileUploadResp.BlockRequests)
{
foreach (var header in blockRequest.RequestHeaders)
{
var headerName = header.Name;
var headerValue = header.Value;
Console.WriteLine("Adding header: {0}", headerName);
switch (headerName)
{
case "Content-Type":
case "Content-Length":
reqBody.Headers.Add(headerName, headerValue);
break;
case "Content-Disposition":
var disposition = headerValue;
reqBody.Headers.Add(headerName, disposition.EndsWith(';') ? disposition.TrimEnd(';') : disposition);
break;
default:
uploadClient.DefaultRequestHeaders.Add(headerName, headerValue);
break;
}
}
var uri = FormatUrl(blockRequest.UrlHost, blockRequest.UrlPath);
var uploadResult = await uploadClient.PutAsync(uri, reqBody);
if (uploadResult.StatusCode == System.Net.HttpStatusCode.Created)
{
Console.WriteLine("File uploaded to cloud. Now committing...");
}
else
{
CommitFile(appId, testFileName, testFileData, false);
Console.WriteLine("File failed uploaded to cloud");
return;
}
}
}
}
catch (Exception exception)
{
CommitFile(appId, testFileName, testFileData, false);
Console.WriteLine("File uploaded to cloud failed with {0}", exception.ToString());
return;
}
CommitFile(appId, testFileName, testFileData, true);
Console.WriteLine("Successfully uploaded and committed file!");
}
}
static async void CommitFile(uint appID, string fileName, byte[] fileData, bool commit)
{
await steamCloud.CommitFileUpload(appID, fileName, fileData, commit);
}
}
}