Skip to content

Commit

Permalink
v5.11.2
Browse files Browse the repository at this point in the history
  • Loading branch information
trueai-org committed Nov 8, 2024
1 parent 9864b11 commit e123f89
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
86 changes: 85 additions & 1 deletion src/Midjourney.API/DiscordAccountInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
using Midjourney.Infrastructure.Services;
using Midjourney.Infrastructure.Util;
using MongoDB.Driver;
using Newtonsoft.Json.Linq;
using RestSharp;
using Serilog;
using System.Diagnostics;
using System.Text;
Expand All @@ -45,6 +47,7 @@ public class DiscordAccountInitializer : IHostedService
private readonly DiscordLoadBalancer _discordLoadBalancer;
private readonly DiscordAccountHelper _discordAccountHelper;
private readonly ProxyProperties _properties;
private readonly DiscordHelper _discordHelper;

private readonly ILogger _logger;
private readonly IConfiguration _configuration;
Expand All @@ -58,7 +61,8 @@ public DiscordAccountInitializer(
IConfiguration configuration,
IOptions<ProxyProperties> options,
ITaskService taskService,
IMemoryCache memoryCache)
IMemoryCache memoryCache,
DiscordHelper discordHelper)
{
// 配置全局缓存
GlobalConfiguration.MemoryCache = memoryCache;
Expand All @@ -70,6 +74,7 @@ public DiscordAccountInitializer(
_configuration = configuration;
_logger = Log.Logger;
_memoryCache = memoryCache;
_discordHelper = discordHelper;
}

/// <summary>
Expand Down Expand Up @@ -618,6 +623,12 @@ public async Task StartCheckAccount(DiscordAccount account, bool isValidateLock
info.AppendLine($"{account.Id}初始化中... 获取任务数耗时: {sw.ElapsedMilliseconds}ms");
sw.Restart();

// 随机延期token
await RandomSyncToken(account);
sw.Stop();
info.AppendLine($"{account.Id}初始化中... 随机延期token耗时: {sw.ElapsedMilliseconds}ms");
sw.Restart();

// 只要在工作时间内,就创建实例
if (DateTime.Now.IsInWorkTime(account.WorkTime))
{
Expand Down Expand Up @@ -826,6 +837,79 @@ public async Task StartCheckAccount(DiscordAccount account, bool isValidateLock
await Task.CompletedTask;
}

/// <summary>
/// 随机 60~600s 延期 token
/// </summary>
/// <returns></returns>
public async Task RandomSyncToken(DiscordAccount account)
{
var key = $"random_token_{account.ChannelId}";
await _memoryCache.GetOrCreateAsync(key, async c =>
{
try
{
_logger.Information("随机对 token 进行延期 {@0}", account.ChannelId);

// 随机 60~600s
var random = new Random();
var sec = random.Next(60, 600);
c.SetAbsoluteExpiration(TimeSpan.FromSeconds(sec));

var options = new RestClientOptions(_discordHelper.GetServer())
{
MaxTimeout = -1,
UserAgent = account.UserAgent ?? "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
};
var client = new RestClient(options);
var request = new RestRequest("/api/v9/content-inventory/users/@me", Method.Get);
request.AddHeader("authorization", account.UserToken);

// base64 编码
// "eyJvcyI6IldpbmRvd3MiLCJicm93c2VyIjoiQ2hyb21lIiwiZGV2aWNlIjoiIiwic3lzdGVtX2xvY2FsZSI6InpoLUNOIiwiYnJvd3Nlcl91c2VyX2FnZW50IjoiTW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzEyOS4wLjAuMCBTYWZhcmkvNTM3LjM2IiwiYnJvd3Nlcl92ZXJzaW9uIjoiMTI5LjAuMC4wIiwib3NfdmVyc2lvbiI6IjEwIiwicmVmZXJyZXIiOiJodHRwczovL2Rpc2NvcmQuY29tLz9kaXNjb3JkdG9rZW49TVRJM056TXhOVEEyT1RFMU1UQXlNekUzTlEuR1k2U2RpLm9zdl81cVpOcl9xeVdxVDBtTW0tYkJ4RVRXQzgwQzVPbzU4WlJvIiwicmVmZXJyaW5nX2RvbWFpbiI6ImRpc2NvcmQuY29tIiwicmVmZXJyZXJfY3VycmVudCI6IiIsInJlZmVycmluZ19kb21haW5fY3VycmVudCI6IiIsInJlbGVhc2VfY2hhbm5lbCI6InN0YWJsZSIsImNsaWVudF9idWlsZF9udW1iZXIiOjM0Mjk2OCwiY2xpZW50X2V2ZW50X3NvdXJjZSI6bnVsbH0="

var str = "{\"os\":\"Windows\",\"browser\":\"Chrome\",\"device\":\"\",\"system_locale\":\"zh-CN\",\"browser_user_agent\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36\",\"browser_version\":\"129.0.0.0\",\"os_version\":\"10\",\"referrer\":\"https://discord.com/?discordtoken={@token}\",\"referring_domain\":\"discord.com\",\"referrer_current\":\"\",\"referring_domain_current\":\"\",\"release_channel\":\"stable\",\"client_build_number\":342968,\"client_event_source\":null}";
str = str.Replace("{@token}", account.UserToken);

// str 转 base64
var bs64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(str));

request.AddHeader("x-super-properties", bs64);
var response = await client.ExecuteAsync(request);

//{
// "request_id": "62a56587a8964dfa9cbb81c234a9a962",
// "entries": [],
// "entries_hash": 0,
// "expired_at": "2024-11-08T02:50:21.323000+00:00",
// "refresh_stale_inbox_after_ms": 30000,
// "refresh_token": "eyJjcmVhdGVkX2F0IjogIjIwMjQtMTEtMDhUMDI6Mzk6MjguNDY4MzcyKzAwOjAwIiwgImNvbnRlbnRfaGFzaCI6ICI0N0RFUXBqOEhCU2ErL1RJbVcrNUpDZXVRZVJrbTVOTXBKV1pHM2hTdUZVPSJ9",
// "wait_ms_until_next_fetch": 652856
//}

var obj = JObject.Parse(response.Content);
if (obj.ContainsKey("refresh_token"))
{
var refreshToken = obj["refresh_token"].ToString();
if (!string.IsNullOrWhiteSpace(refreshToken))
{
_logger.Information("随机对 token 进行延期成功 {@0}", account.ChannelId);
return true;
}
}

_logger.Information("随机对 token 进行延期失败 {@0}, {@1}", account.ChannelId, response.Content);

return false;
}
catch (Exception ex)
{
_logger.Error(ex, "随机对 token 进行延期异常 {@0}", account.ChannelId);
}

return false;
});
}

/// <summary>
/// 更新账号信息
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Midjourney.Infrastructure/GlobalConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class GlobalConfiguration
/// <summary>
/// 版本号
/// </summary>
public static string Version { get; set; } = "v5.11.1";
public static string Version { get; set; } = "v5.11.2";

/// <summary>
/// 全局配置项
Expand Down
1 change: 1 addition & 0 deletions src/Midjourney.Infrastructure/Services/DiscordInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Midjourney.Infrastructure.Services;
using Midjourney.Infrastructure.Util;
using Newtonsoft.Json.Linq;
using RestSharp;
using Serilog;
using System.Collections.Concurrent;
using System.Diagnostics;
Expand Down

0 comments on commit e123f89

Please sign in to comment.