Skip to content

Commit

Permalink
Merge branch 'master' into l10n_master
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 authored Dec 17, 2023
2 parents b1d81b6 + 93da302 commit cb22dda
Show file tree
Hide file tree
Showing 16 changed files with 386 additions and 2,626 deletions.
16 changes: 10 additions & 6 deletions ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ public Task OnLoaded()
bot.Commands.Response(access, "CART ASF", steamId),

//Account
"CHECKAPIKEY" when access >= EAccess.Operator =>
Account.Command.ResponseCheckApiKey(bot),
"REVOKEAPIKEY" when access >= EAccess.Master =>
Account.Command.ResponseRevokeApiKey(bot),

"PURCHASEHISTORY" or
"PH" when access >= EAccess.Operator =>
Account.Command.ResponseAccountHistory(bot),
Expand Down Expand Up @@ -423,8 +428,6 @@ public Task OnLoaded()
//DevFuture
"COOKIES" when Config.DevFeature && access >= EAccess.Owner =>
Task.FromResult(DevFeature.Command.ResponseGetCookies(bot)),
"APIKEY" when Config.DevFeature && access >= EAccess.Owner =>
DevFeature.Command.ResponseGetAPIKey(bot),
"ACCESSTOKEN" when Config.DevFeature && access >= EAccess.Owner =>
DevFeature.Command.ResponseGetAccessToken(bot),

Expand All @@ -436,7 +439,6 @@ public Task OnLoaded()
Task.FromResult(Other.Command.ResponseEulaCmdUnavilable()),

"COOKIES" or
"APIKEY" or
"ACCESSTOKEN" when access >= EAccess.Owner =>
Task.FromResult(Other.Command.ResponseDevFeatureUnavilable()),

Expand Down Expand Up @@ -500,6 +502,11 @@ public Task OnLoaded()
bot.Commands.Response(access, "TRANSFER " + Utilities.GetArgsAsText(message, 1), steamId),

//Account
"CHECKAPIKEY" when access >= EAccess.Operator =>
Account.Command.ResponseCheckApiKey(Utilities.GetArgsAsText(args, 1, ",")),
"REVOKEAPIKEY" when access >= EAccess.Master =>
Account.Command.ResponseRevokeApiKey(Utilities.GetArgsAsText(args, 1, ",")),

"PURCHASEHISTORY" or
"PH" when access > EAccess.Operator =>
Account.Command.ResponseAccountHistory(Utilities.GetArgsAsText(args, 1, ",")),
Expand Down Expand Up @@ -898,8 +905,6 @@ public Task OnLoaded()
//DevFuture
"COOKIES" when Config.DevFeature && access >= EAccess.Owner =>
DevFeature.Command.ResponseGetCookies(Utilities.GetArgsAsText(args, 1, ",")),
"APIKEY" when Config.DevFeature && access >= EAccess.Owner =>
DevFeature.Command.ResponseGetAPIKey(Utilities.GetArgsAsText(args, 1, ",")),
"ACCESSTOKEN" when Config.DevFeature && access >= EAccess.Owner =>
DevFeature.Command.ResponseGetAccessToken(Utilities.GetArgsAsText(args, 1, ",")),

Expand All @@ -923,7 +928,6 @@ public Task OnLoaded()
Task.FromResult(Other.Command.ResponseEulaCmdUnavilable()),

"COOKIES" or
"APIKEY" or
"ACCESSTOKEN" when Config.DevFeature && access >= EAccess.Owner =>
Task.FromResult(Other.Command.ResponseDevFeatureUnavilable()),

Expand Down
106 changes: 98 additions & 8 deletions ASFEnhance/Account/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -725,15 +725,13 @@ private static string NotificationTargetToString(NotificationTarget target)
return bot.FormatBotResponse(Strings.BotNotConnected);
}

(_, string? apiKey) = await bot.ArchiWebHandler.CachedApiKey.GetValue().ConfigureAwait(false);

if (string.IsNullOrEmpty(apiKey))
var token = Config.ApiKey;
if (string.IsNullOrEmpty(token))
{
return await WebRequest.GetAccountBans(bot).ConfigureAwait(false);
//return bot.FormatBotResponse(Langs.NetworkError);
}

var result = await WebRequest.GetPlayerBans(bot, apiKey, steamId ?? bot.SteamID).ConfigureAwait(false);
var result = await WebRequest.GetPlayerBans(bot, token, steamId ?? bot.SteamID).ConfigureAwait(false);

if (result == null)
{
Expand Down Expand Up @@ -937,14 +935,14 @@ private static string NotificationTargetToString(NotificationTarget target)
return bot.FormatBotResponse(Strings.BotNotConnected);
}

(_, string? apiKey) = await bot.ArchiWebHandler.CachedApiKey.GetValue().ConfigureAwait(false);
(_, string? token) = await bot.ArchiWebHandler.CachedAccessToken.GetValue().ConfigureAwait(false);

if (string.IsNullOrEmpty(apiKey))
if (string.IsNullOrEmpty(token))
{
return bot.FormatBotResponse(Langs.NetworkError);
}

var result = await WebRequest.GetGamePlayTime(bot, apiKey).ConfigureAwait(false);
var result = await WebRequest.GetGamePlayTime(bot, token).ConfigureAwait(false);

if (result == null)
{
Expand Down Expand Up @@ -1081,4 +1079,96 @@ private static string NotificationTargetToString(NotificationTarget target)

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}

/// <summary>
/// 检查ApiKey
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<string?> ResponseCheckApiKey(Bot bot)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

var result = await WebRequest.CheckApiKey(bot).ConfigureAwait(false);
if (result == null)
{
return Langs.NetworkError;
}

return result.Value ? Langs.ExistsApiKey : Langs.NoExistsApiKey;
}

/// <summary>
/// 检查ApiKey (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <param name="query"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseCheckApiKey(string botNames)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

var bots = Bot.GetBots(botNames);

if (bots == null || bots.Count == 0)
{
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => ResponseCheckApiKey(bot))).ConfigureAwait(false);
var responses = new List<string?>(results.Where(result => !string.IsNullOrEmpty(result)));

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}

/// <summary>
/// 吊销ApiKey
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
internal static async Task<string?> ResponseRevokeApiKey(Bot bot)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

await WebRequest.RevokeApiKey(bot).ConfigureAwait(false);

return Langs.Success;
}

/// <summary>
/// 吊销ApiKey (多个Bot)
/// </summary>
/// <param name="botNames"></param>
/// <param name="query"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseRevokeApiKey(string botNames)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

var bots = Bot.GetBots(botNames);

if (bots == null || bots.Count == 0)
{
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => ResponseRevokeApiKey(bot))).ConfigureAwait(false);
var responses = new List<string?>(results.Where(result => !string.IsNullOrEmpty(result)));

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
}
33 changes: 29 additions & 4 deletions ASFEnhance/Account/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ internal static async Task<bool> RemoveLicense(Bot bot, uint subId)
/// <returns></returns>
internal static async Task<GetPlayerBansResponse?> GetPlayerBans(Bot bot, string token, ulong steamids)
{
var request = new Uri(SteamApiURL, $"/ISteamUser/GetPlayerBans/v1/?key={token}&steamids={steamids}");
var request = new Uri(SteamApiURL, $"/ISteamUser/GetPlayerBans/v1/?access_token={token}&steamids={steamids}");
var response = await bot.ArchiWebHandler.UrlGetToJsonObjectWithSession<GetPlayerBansResponse>(request, referer: SteamStoreURL).ConfigureAwait(false);
return response?.Content;
}
Expand Down Expand Up @@ -364,11 +364,11 @@ internal static async Task<bool> RemoveLicense(Bot bot, uint subId)
/// 获取游戏游玩时间
/// </summary>
/// <param name="bot"></param>
/// <param name="apiKey"></param>
/// <param name="token"></param>
/// <returns></returns>
internal static async Task<Dictionary<uint, GetOwnedGamesResponse.GameData>?> GetGamePlayTime(Bot bot, string apiKey)
internal static async Task<Dictionary<uint, GetOwnedGamesResponse.GameData>?> GetGamePlayTime(Bot bot, string token)
{
var request = new Uri(SteamApiURL, $"/IPlayerService/GetOwnedGames/v1/?key={apiKey}&steamid={bot.SteamID}&include_appinfo=true&include_played_free_games=true&include_free_sub=true&skip_unvetted_apps=true&language={Langs.Language}&include_extended_appinfo=true");
var request = new Uri(SteamApiURL, $"/IPlayerService/GetOwnedGames/v1/?access_token={token}&steamid={bot.SteamID}&include_appinfo=true&include_played_free_games=true&include_free_sub=true&skip_unvetted_apps=true&language={Langs.Language}&include_extended_appinfo=true");
var response = await bot.ArchiWebHandler.UrlGetToJsonObjectWithSession<GetOwnedGamesResponse>(request, referer: SteamStoreURL).ConfigureAwait(false);

if (response?.Content?.Response?.Games != null)
Expand Down Expand Up @@ -399,4 +399,29 @@ internal static async Task<bool> RemoveLicense(Bot bot, uint subId)
var response = await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(request, referer: SteamStoreURL).ConfigureAwait(false);
return HtmlParser.ParseAccountEmail(response?.Content);
}

internal static async Task<bool?> CheckApiKey(Bot bot)
{
var request = new Uri(SteamCommunityURL, "/dev/apikey");
var response = await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(request, referer: SteamStoreURL).ConfigureAwait(false);

if (response?.Content == null)
{
return null;
}

return response.Content.QuerySelector("#BG_bottom form") != null;
}

internal static async Task RevokeApiKey(Bot bot)
{
var request = new Uri(SteamCommunityURL, "/dev/revokekey");

var data = new Dictionary<string, string>(2)
{
{ "Revoke", "Revoke+My+Steam+Web+API+Key" }
};

await bot.ArchiWebHandler.UrlPostWithSession(request, data: data).ConfigureAwait(false);
}
}
5 changes: 5 additions & 0 deletions ASFEnhance/Data/PluginConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public sealed record PluginConfig
[JsonProperty(Required = Required.Default)]
public List<AddressConfig>? Addresses { get; set; }

/// <summary>
/// Api Key
/// </summary>
public string ApiKey { get; set; } = "";

/// <summary>
/// 地址信息
/// </summary>
Expand Down
47 changes: 0 additions & 47 deletions ASFEnhance/DevFeature/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,53 +61,6 @@ internal static class Command
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}

/// <summary>
/// 获取Bot APIKey
/// </summary>
/// <param name="bot"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseGetAPIKey(Bot bot)
{
if (!bot.IsConnectedAndLoggedOn)
{
return bot.FormatBotResponse(Strings.BotNotConnected);
}

(_, string? apiKey) = await bot.ArchiWebHandler.CachedApiKey.GetValue().ConfigureAwait(false);

bool success = !string.IsNullOrEmpty(apiKey);

return bot.FormatBotResponse(success ? apiKey! : string.Format(Langs.FetchDataFailed, nameof(apiKey)));
}

/// <summary>
/// 获取Bot APIKey (多个bot)
/// </summary>
/// <param name="botNames"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
internal static async Task<string?> ResponseGetAPIKey(string botNames)
{
if (string.IsNullOrEmpty(botNames))
{
throw new ArgumentNullException(nameof(botNames));
}

var bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0))
{
return FormatStaticResponse(Strings.BotNotFound, botNames);
}

var results = await Utilities.InParallel(bots.Select(bot => Task.Run(() => ResponseGetAPIKey(bot)))).ConfigureAwait(false);

var responses = new List<string?>(results.Where(result => !string.IsNullOrEmpty(result)));

return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}

/// <summary>
/// 获取Bot AccessToken
/// </summary>
Expand Down
Loading

0 comments on commit cb22dda

Please sign in to comment.