Skip to content

Commit

Permalink
更新二次验证功能
Browse files Browse the repository at this point in the history
  • Loading branch information
lintx committed Jul 14, 2023
1 parent 1099567 commit 832bd34
Show file tree
Hide file tree
Showing 9 changed files with 422 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Beanfun/Beanfun.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@
<Compile Include="Helper\PacketReader.cs" />
<Compile Include="Helper\TextBlockHelper.cs" />
<Compile Include="Helper\WindowAccentCompositor.cs" />
<Compile Include="Pages\LoginTotp.xaml.cs">
<DependentUpon>LoginTotp.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down Expand Up @@ -270,6 +273,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Pages\LoginTotp.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Windows\CaptchaWnd.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
1 change: 1 addition & 0 deletions Beanfun/Lang/zh-Hans.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,5 @@
<system:String x:Key="NoUpdatesDetected">未检测到有更新。</system:String>
<system:String x:Key="NewVersionDetected">检测到新版本 {0} 当前: {1}\r\n\r\n{2}\r\n\r\n是否打开下载页面?</system:String>
<system:String x:Key="Language">程序语言</system:String>
<system:String x:Key="InputTotp">请输入二次验证程序动态密码</system:String>
</ResourceDictionary>
1 change: 1 addition & 0 deletions Beanfun/Lang/zh.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,5 @@
<system:String x:Key="NoUpdatesDetected">未檢測到有更新。</system:String>
<system:String x:Key="NewVersionDetected">檢測到新版本 {0} 當前: {1}\r\n\r\n{2}\r\n\r\n是否打開下載頁面?</system:String>
<system:String x:Key="Language">程式語言</system:String>
<system:String x:Key="InputTotp">輸入應用程式內顯示的雙重驗證碼</system:String>
</ResourceDictionary>
163 changes: 162 additions & 1 deletion Beanfun/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ public partial class MainWindow : Window
public VerifyPage verifyPage;
public Settings settingPage;
public About aboutPage;
public LoginTotp loginTotp;

public System.ComponentModel.BackgroundWorker getOtpWorker;
public System.ComponentModel.BackgroundWorker loginWorker;
public System.ComponentModel.BackgroundWorker totpWorker;
public System.ComponentModel.BackgroundWorker pingWorker;
public System.ComponentModel.BackgroundWorker qrWorker;
public System.ComponentModel.BackgroundWorker verifyWorker;
Expand Down Expand Up @@ -98,6 +100,7 @@ public MainWindow()

this.getOtpWorker = new System.ComponentModel.BackgroundWorker();
this.loginWorker = new System.ComponentModel.BackgroundWorker();
this.totpWorker = new System.ComponentModel.BackgroundWorker();
this.pingWorker = new System.ComponentModel.BackgroundWorker();
this.qrWorker = new System.ComponentModel.BackgroundWorker();
this.verifyWorker = new System.ComponentModel.BackgroundWorker();
Expand All @@ -119,6 +122,13 @@ public MainWindow()
this.loginWorker.WorkerSupportsCancellation = true;
this.loginWorker.DoWork += this.loginWorker_DoWork;
this.loginWorker.RunWorkerCompleted += this.loginWorker_RunWorkerCompleted;
//
// totpWorker
//
this.totpWorker.WorkerReportsProgress = true;
this.totpWorker.WorkerSupportsCancellation = true;
this.totpWorker.DoWork += this.totpWorker_DoWork;
this.totpWorker.RunWorkerCompleted += this.totpWorker_RunWorkerCompleted;
//
// pingWorker
//
Expand Down Expand Up @@ -167,6 +177,7 @@ public MainWindow()
accountList = new AccountList();
settingPage = new Settings();
aboutPage = new About();
loginTotp = new LoginTotp();

Initialize();

Expand All @@ -190,6 +201,7 @@ public MainWindow()
protected override void OnContentRendered(EventArgs e)
{
frame.Content = loginPage;
//frame.Content = loginTotp;

if (App.LoginMethod == (int)LoginMethod.Regular && (bool)loginPage.id_pass.checkBox_AutoLogin.IsChecked)
{
Expand Down Expand Up @@ -943,6 +955,13 @@ public void do_Login()
frame.Content = loginWaitPage;
}

public void do_Totp()
{
btn_Region.Visibility = Visibility.Collapsed;
this.totpWorker.RunWorkerAsync();
frame.Content = loginWaitPage;
}

public bool errexit(string msg, int method, string title = null)
{
string originalMsg = msg;
Expand Down Expand Up @@ -1056,6 +1075,148 @@ private void loginWorker_DoWork(object sender, DoWorkEventArgs e)

// Login completed.
private void loginWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine("loginWorker end");
if (e != null && e.Error != null)
{

errexit(e.Error.Message, 1);
NavigateLoginPage();
return;
}
if (e != null && (string)e.Result != null)
{
if ((string)e.Result == "need_totp")
{
frame.Content = loginTotp;
loginTotp.otp1.Text = "";
loginTotp.otp2.Text = "";
loginTotp.otp3.Text = "";
loginTotp.otp4.Text = "";
loginTotp.otp5.Text = "";
loginTotp.otp6.Text = "";
loginTotp.otp1.Focus();
return;
}
else if ((string)e.Result == "LoginAdvanceCheck")
{
MessageBox.Show(TryFindResource("MsgNeedAuth") as string);

// Handle panel switching.
frame.Content = verifyPage;
if ((bool)verifyPage.checkBoxRememberVerify.IsChecked)
verifyPage.t_Code.Focus();
else
verifyPage.t_Verify.Focus();
verifyPage.t_Verify.Text = accountManager.getVerifyByAccount(App.LoginRegion, loginPage.id_pass.t_AccountID.Text);
verifyPage.checkBoxRememberVerify.IsChecked = verifyPage.t_Verify.Text != null && verifyPage.t_Verify.Text != "";
verifyPage.t_Code.Text = "";
string response = this.bfClient.getVerifyPageInfo();
if (response == null)
{ MessageBox.Show(I18n.ToSimplified(this.bfClient.errmsg)); NavigateLoginPage(); }
string errmsg = reLoadVerifyPage(response);
if (errmsg != null)
{ MessageBox.Show(I18n.ToSimplified(errmsg)); NavigateLoginPage(); }
}
else if (((string)e.Result).StartsWith("bfAPPAutoLogin.ashx"))
{
string[] args = Regex.Split((string)e.Result, "\",\"");
if (args.Length < 2)
{
errexit("LoginUnknown", 1);
return;
}
loginWaitPage.t_Info.Content = (TryFindResource("MsgNeedBeanfunAuth") as string).Replace("\\r\\n", "\r\n");
bfAPPAutoLogin.IsEnabled = true;
}
else
{
errexit((string)e.Result, 1);
}
return;
}

ConfigAppSettings.SetValue("loginMethod", App.LoginMethod.ToString());
if (App.LoginRegion != "TW" || App.LoginMethod != (int)LoginMethod.QRCode)
{
LastLoginAccountID = loginPage.id_pass.t_AccountID.Text;
ConfigAppSettings.SetValue("AccountID", LastLoginAccountID);
accountManager.addAccount(
App.LoginRegion,
loginPage.id_pass.t_AccountID.Text,
"",
loginPage.id_pass.checkBox_RememberPWD.IsEnabled && (bool)loginPage.id_pass.checkBox_RememberPWD.IsChecked ? loginPage.id_pass.t_Password.Password : "",
(bool)verifyPage.checkBoxRememberVerify.IsChecked ? verifyPage.t_Verify.Text : "",
App.LoginMethod,
(bool)loginPage.id_pass.checkBox_AutoLogin.IsChecked
);

loginMethodInit();
}
else ConfigAppSettings.SetValue("AccountID", null);

try
{
frame.Content = accountList;
btn_Region.Visibility = Visibility.Collapsed;

redrawSAccountList();

if (!this.pingWorker.IsBusy) this.pingWorker.RunWorkerAsync();

updateRemainPoint(this.bfClient.remainPoint);

accountList.list_Account.Focus();
if ((bool)settingPage.autoStartGame.IsChecked && this.bfClient.accountList.Count() > 0)
{
if (((bool)settingPage.tradLogin.IsChecked && login_action_type == 1) || login_action_type == 0)
runGame();
accountList.btnGetOtp_Click(null, null);
}
}
catch
{
errexit(TryFindResource("LoginNoAccountMatch") as string, 1);
}
}


// totp do work.
private void totpWorker_DoWork(object sender, DoWorkEventArgs e)
{
//if (this.pingWorker.IsBusy) this.pingWorker.CancelAsync();
// while (this.pingWorker.IsBusy)
// Thread.Sleep(137);
CancelWork();

Console.WriteLine("loginWorker starting");
Thread.CurrentThread.Name = "Totp Worker";
e.Result = "";
try
{
loginWaitPage.Dispatcher.Invoke(
new Action(
delegate
{
this.bfClient.TotpLogin(loginTotp.otp1.Text,loginTotp.otp2.Text,loginTotp.otp3.Text,loginTotp.otp4.Text,loginTotp.otp5.Text,loginTotp.otp6.Text,this.service_code, this.service_region);
}
)
);
if (this.bfClient.errmsg != null)
e.Result = this.bfClient.errmsg;
else
e.Result = null;
}
catch (Exception ex)
{
e.Result = TryFindResource("LoginErrorUnknown") as string + "\n\n" + ex.Message + "\n" + ex.StackTrace;
}

ResumeWork();
}

// Login completed.
private void totpWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine("loginWorker end");
if (e != null && e.Error != null)
Expand Down Expand Up @@ -1668,7 +1829,7 @@ private void pingWorker_DoWork(object sender, DoWorkEventArgs e)
break;
}

if (this.getOtpWorker.IsBusy || this.loginWorker.IsBusy)
if (this.getOtpWorker.IsBusy || this.loginWorker.IsBusy || this.totpWorker.IsBusy)
{
Console.WriteLine("ping.busy sleep 1s");
System.Threading.Thread.Sleep(1000 * 1);
Expand Down
88 changes: 88 additions & 0 deletions Beanfun/Pages/LoginTotp.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<Page x:Class="Beanfun.LoginTotp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Title="LoginTotp">

<Page.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#7FF5F5F5"/>
<Setter Property="Foreground" Value="Black"/>
</Style>

<SolidColorBrush x:Key="Button.Static.Default" Color="#848484"/>
<SolidColorBrush x:Key="Button.Static.MouseOver" Color="#484848"/>
<SolidColorBrush x:Key="Button.Static.Pressed" Color="#3AC3F7"/>
<Style x:Key="TextButton" TargetType="{x:Type Button}">
<Setter Property="Padding" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{x:Null}"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Foreground" Value="{StaticResource Button.Static.Default}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="{StaticResource Button.Static.MouseOver}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="{StaticResource Button.Static.Pressed}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Page.Resources>

<Grid Margin="0 20 0 0">
<StackPanel Margin="0 50 0 0" Background="#AAFFFFFF"/>
<DockPanel>
<Grid x:Name="SideBarLeft" MinWidth="90">
</Grid>
<Grid MinWidth="250" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Label Content="{DynamicResource InputTotp}" Margin="0 50 0 20" HorizontalAlignment="Center"></Label>
<Grid Grid.Row="1" Height="30">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox x:Name="otp1" GotFocus="otp_GotFocus" PreviewKeyUp="otp1_PreviewKeyUp" Grid.Column="0" TextAlignment="Center" VerticalContentAlignment="Center" FontSize="20" MaxLength="1"></TextBox>
<TextBox x:Name="otp2" GotFocus="otp_GotFocus" PreviewKeyUp="otp2_PreviewKeyUp" Grid.Column="2" TextAlignment="Center" VerticalContentAlignment="Center" FontSize="20" MaxLength="1"></TextBox>
<TextBox x:Name="otp3" GotFocus="otp_GotFocus" PreviewKeyUp="otp3_PreviewKeyUp" Grid.Column="4" TextAlignment="Center" VerticalContentAlignment="Center" FontSize="20" MaxLength="1"></TextBox>
<TextBox x:Name="otp4" GotFocus="otp_GotFocus" PreviewKeyUp="otp4_PreviewKeyUp" Grid.Column="6" TextAlignment="Center" VerticalContentAlignment="Center" FontSize="20" MaxLength="1"></TextBox>
<TextBox x:Name="otp5" GotFocus="otp_GotFocus" PreviewKeyUp="otp5_PreviewKeyUp" Grid.Column="8" TextAlignment="Center" VerticalContentAlignment="Center" FontSize="20" MaxLength="1"></TextBox>
<TextBox x:Name="otp6" GotFocus="otp_GotFocus" PreviewKeyUp="otp6_PreviewKeyUp" Grid.Column="10" TextAlignment="Center" VerticalContentAlignment="Center" FontSize="20" MaxLength="1"></TextBox>
</Grid>
<Button Grid.Row="2" x:Name="btn_login" Content="{DynamicResource Login}" Height="30" FontWeight="Bold" Margin="0 20 0 20" Click="btn_login_Click" IsDefault="True" Background="#7FF5F5F5" Foreground="Black"/>
</Grid>
<Grid MinWidth="{Binding MinWidth, ElementName=SideBarLeft}"></Grid>
</DockPanel>
</Grid>
</Page>
Loading

0 comments on commit 832bd34

Please sign in to comment.