Skip to content

Commit

Permalink
v6.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
trueai-org committed Jan 22, 2025
1 parent 18e1e91 commit bd0c5d4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Midjourney.API/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ public ActionResult AccountLoginNotify([FromBody] AutoLoginRequest request)
if (!request.Success)
{
// 发送邮件
EmailJob.Instance.EmailSend(_properties.Smtp, $"自动登录失败-{item.ChannelId}", $"自动登录失败-{item.ChannelId}, 请手动登录");
EmailJob.Instance.EmailSend(_properties.Smtp, $"自动登录失败-{item.ChannelId}", $"自动登录失败-{item.ChannelId}, {request.Message}, 请手动登录");
}
}
else
Expand Down
50 changes: 41 additions & 9 deletions src/Midjourney.Captcha.API/SeleniumLoginHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public class SeleniumLoginHelper
/// <summary>
/// 登录
/// </summary>
/// <param name="contentRootPath"> </param>
/// <param name="contentRootPath"></param>
/// <param name="clientKey"></param>
/// <param name="loginAccount"></param>
/// <param name="loginPassword"></param>
/// <param name="twofa"></param>
public static string Login(string contentRootPath, string clientKey, string loginAccount, string loginPassword, string twofa)
/// <returns></returns>
public static (bool success, string data) Login(string contentRootPath, string clientKey, string loginAccount, string loginPassword, string twofa)
{
var errorMsg = "";
ChromeDriver driver = null;
try
{
Expand Down Expand Up @@ -97,18 +99,34 @@ public static string Login(string contentRootPath, string clientKey, string logi

Thread.Sleep(5000);

// 确保页面加载完成
var wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait1.Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").ToString() == "complete");

var sw = new Stopwatch();
sw.Start();

while (true)
{
// 判断是否是密码错误
// 查找元素 登录或密码无效
var errorPwd = driver.FindElements(By.XPath("//span[contains(text(),'登录或密码无效')]"))?.FirstOrDefault();
if (errorPwd != null)
{
Log.Error("登录或密码无效");

return (false, "登录或密码无效");
}

// placeholder="6位数字身份验证码"
var codeInput = driver.FindElements(By.CssSelector("input[placeholder='6位数字身份验证码']"))
?.FirstOrDefault();

// 如果找到了
if (codeInput != null)
{
var faRetry = 0;

while (true)
{
var num = TwoFAHelper.GenerateOtp(twofa);
Expand Down Expand Up @@ -171,23 +189,35 @@ public static string Login(string contentRootPath, string clientKey, string logi
var token = driver.FindElement(By.CssSelector("body")).GetAttribute("data-token");
if (!string.IsNullOrEmpty(token))
{
Log.Information($"token: {token}");
Log.Information($"登录成功 token: {token}");

return token;
return (true, token);
}
}
else
{
Log.Error("双重认证码无效");
faRetry++;

if (faRetry > 3)
{
return (false, "双重认证码无效");
}
}

if (sw.ElapsedMilliseconds > 120 * 1000)
if (sw.ElapsedMilliseconds > 30 * 1000)
{
break;
return (false, "2FA 执行登录超时");
}

Thread.Sleep(5000);
}
}
else
{
if (sw.ElapsedMilliseconds > 120 * 1000)
if (sw.ElapsedMilliseconds > 60 * 1000)
{
break;
return (false, "登录超时");
}

Thread.Sleep(2000);
Expand All @@ -197,14 +227,16 @@ public static string Login(string contentRootPath, string clientKey, string logi
catch (Exception ex)
{
Log.Error(ex, "自动登录执行异常");

errorMsg = "登录异常";
}
finally
{
// 关闭浏览器
driver?.Quit();
}

return null;
return (false, errorMsg);
}

/// <summary>
Expand Down
11 changes: 7 additions & 4 deletions src/Midjourney.Captcha.API/SeleniumLoginQueueHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ private async Task DoWork(AutoLoginRequest request)

try
{
var token = SeleniumLoginHelper.Login(root, _captchaOption.YesCaptchaKey,
var (success, data) = SeleniumLoginHelper.Login(root, _captchaOption.YesCaptchaKey,
request.LoginAccount, request.LoginPassword, request.Login2fa);

if (!string.IsNullOrWhiteSpace(token))
if (success == true && !string.IsNullOrWhiteSpace(data))
{
finSuccess = true;

request.Token = token;
request.Token = data;
request.Success = true;
request.Message = "自动登录成功";

Expand Down Expand Up @@ -239,6 +239,8 @@ private async Task DoWork(AutoLoginRequest request)
}
else
{
request.Message = data ?? "登录失败";

Log.Warning("自动登录失败 {@0}", request);
}
}
Expand All @@ -263,7 +265,8 @@ private async Task DoWork(AutoLoginRequest request)
if (!finSuccess)
{
request.Success = false;
request.Message = "自动登录失败,请手动登录";

request.Message ??= "自动登录失败,请手动登录";

// 验证失败计数 10 分钟内,最多 3 次
if (_memoryCache.TryGetValue<int>(failKey, out var fc))
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; } = "v6.1.1";
public static string Version { get; set; } = "v6.1.2";

/// <summary>
/// 全局配置项
Expand Down

0 comments on commit bd0c5d4

Please sign in to comment.