| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Hosting;
- using Newtonsoft.Json;
- using SteamAuth;
- using SteamKit2;
- using SteamKit2.Authentication;
- var builder = WebApplication.CreateBuilder(args);
- var app = builder.Build();
- app.MapPost("/login", async (HttpContext http) =>
- {
- var req = await GetRequestBodyAsync(http);
- if (req == null)
- {
- http.Response.StatusCode = 400;
- await http.Response.WriteAsync("invalid request");
- return;
- }
- if (!File.Exists(req.MaFilePath))
- {
- http.Response.StatusCode = 400;
- await http.Response.WriteAsync("maFile not found");
- return;
- }
- SteamGuardAccount? account;
- try
- {
- var contents = await File.ReadAllTextAsync(req.MaFilePath);
- account = JsonConvert.DeserializeObject<SteamGuardAccount>(contents);
- }
- catch (Exception ex)
- {
- http.Response.StatusCode = 500;
- await http.Response.WriteAsync("failed to read maFile: " + ex.Message);
- return;
- }
- try
- {
- // Start SteamClient and authenticate using the maFile-provided SteamGuardAccount
- SteamClient steamClient = new SteamClient();
- steamClient.Connect();
- var timeoutMs = 15000;
- var waited = 0;
- while (!steamClient.IsConnected && waited < timeoutMs)
- {
- await Task.Delay(200);
- waited += 200;
- }
- if (!steamClient.IsConnected)
- {
- http.Response.StatusCode = 500;
- await http.Response.WriteAsync("failed to connect to steam network");
- return;
- }
- CredentialsAuthSession authSession = await steamClient.Authentication.BeginAuthSessionViaCredentialsAsync(new AuthSessionDetails
- {
- Username = req.Username,
- Password = req.Password,
- IsPersistentSession = false,
- // PlatformType and ClientOSType are optional; omit to avoid enum symbol differences across SteamKit2 versions.
- Authenticator = new FileAuthenticator(account),
- });
- var pollResponse = await authSession.PollingWaitForResultAsync();
- var sessionData = new SessionData()
- {
- SteamID = authSession.SteamID.ConvertToUInt64(),
- AccessToken = pollResponse.AccessToken,
- RefreshToken = pollResponse.RefreshToken,
- };
- // Serialize cookies
- var uri = new Uri("https://steamcommunity.com");
- var cookies = sessionData.GetCookies().GetCookies(uri).Cast<Cookie>().Select(c => new
- {
- name = c.Name,
- value = c.Value,
- domain = c.Domain,
- path = c.Path,
- expires = c.Expires == DateTime.MinValue ? null : (DateTime?)c.Expires
- }).ToArray();
- var resp = new
- {
- session = new
- {
- steamId = sessionData.SteamID,
- accessToken = sessionData.AccessToken,
- refreshToken = sessionData.RefreshToken,
- sessionId = sessionData.SessionID
- },
- cookies = cookies
- };
- http.Response.ContentType = "application/json";
- await http.Response.WriteAsync(JsonConvert.SerializeObject(resp, Formatting.Indented));
- return;
- }
- catch (Exception ex)
- {
- http.Response.StatusCode = 500;
- await http.Response.WriteAsync("login failed: " + ex.Message);
- return;
- }
- });
- app.Run();
- async Task<LoginRequest?> GetRequestBodyAsync(HttpContext http)
- {
- try
- {
- using var sr = new StreamReader(http.Request.Body);
- var body = await sr.ReadToEndAsync();
- if (string.IsNullOrWhiteSpace(body)) return null;
- return JsonConvert.DeserializeObject<LoginRequest>(body);
- }
- catch
- {
- return null;
- }
- }
- record LoginRequest(string Username, string Password, string MaFilePath);
- internal class FileAuthenticator : IAuthenticator
- {
- private readonly SteamGuardAccount? account;
- private int deviceCodesGenerated = 0;
- public FileAuthenticator(SteamGuardAccount? account)
- {
- this.account = account;
- }
- public Task<bool> AcceptDeviceConfirmationAsync()
- {
- return Task.FromResult(false);
- }
- public async Task<string> GetDeviceCodeAsync(bool previousCodeWasIncorrect)
- {
- if (previousCodeWasIncorrect)
- {
- if (deviceCodesGenerated > 2)
- throw new Exception("failed to generate device code from maFile");
- await Task.Delay(30000);
- }
- if (account == null) return null;
- deviceCodesGenerated++;
- var code = await account.GenerateSteamGuardCodeAsync();
- return code ?? "";
- }
- public Task<string> GetEmailCodeAsync(string email, bool previousCodeWasIncorrect)
- {
- // This implementation does not support interactive email codes.
- return Task.FromResult(string.Empty);
- }
- }
|