CssSolution/CSS_Solution/Forms/Welcome_Form.cs

186 lines
6.9 KiB
C#
Raw Permalink Normal View History

using CSS_Solution.Request;
using Microsoft.Extensions.Logging;
2024-03-12 08:15:15 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
2024-03-12 08:15:15 +00:00
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
2024-03-12 08:15:15 +00:00
namespace CSS_Solution.Forms
{
public partial class Welcome_Form : Form
{
public Welcome_Form()
{
InitializeComponent();
if (Program.exceptionHandler == null)
Initialize.default_logger.LogError($"[{DateTime.Now}] exceptionHandler为空どうやって");
else
Application.ThreadException += Program.exceptionHandler.ApplicationThreadExceptionHandler;
2024-03-12 08:15:15 +00:00
}
private Font normal_font = new Font("Microsoft YaHei UI", 15F, FontStyle.Regular, GraphicsUnit.Point, 134);
private Font italic_font = new Font("Microsoft YaHei UI", 15F, FontStyle.Italic, GraphicsUnit.Point, 134);
private MD5 MD5 = MD5.Create();
private SHA1 SHA1 = SHA1.Create();
2024-03-12 08:15:15 +00:00
private void ID_Enter(object sender, EventArgs e)
{
if (ID.Font != normal_font)
{
ID.Text = string.Empty;
ID.ForeColor = Color.White;
ID.Font = normal_font;
}
}
private void ID_Leave(object sender, EventArgs e)
{
if (ID.Text == string.Empty)
{
ID.ForeColor = Color.Silver;
ID.Font = italic_font;
ID.Text = "学号...";
}
}
private void password_Enter(object sender, EventArgs e)
{
if (password.Font != normal_font)
{
password.Text = string.Empty;
password.ForeColor = Color.White;
password.Font = normal_font;
password.PasswordChar = '*';
}
}
private void password_Leave(object sender, EventArgs e)
{
if (password.Text == string.Empty)
{
password.ForeColor = Color.Silver;
password.Font = italic_font;
password.Text = "密码...";
password.PasswordChar = '\0';
}
}
bool save_disk_info_showed = false;
private void SavePassword_CheckedChanged(object sender, EventArgs e)
{
if (sender == null || save_disk_info_showed)
2024-03-12 08:15:15 +00:00
return;
CheckBox? checkBox = sender as CheckBox;
if (checkBox == null) return;
if (checkBox.Checked)
2024-03-12 08:15:15 +00:00
{
save_disk_info_showed = true;
MessageBox.Show("注意,密码确实会保存为密文,但是该密文可直接用于登录网站(教务管理系统),理想状态下,该密码应当仅保存在内存中。");
}
}
private void record_Click(object sender, EventArgs e)
{
try
{
var (username, password) = Code_Preprocess();
Code_Save(username, password);
if (MessageBox.Show("是否进行一次登录测试,以确保账号密码正确?", "测试输入?", MessageBoxButtons.YesNo) == DialogResult.Yes)
Task.Run(() => Code_TestAsync());
else
Show_info("信息已记录");
}
catch (Exception ex)
{
Show_info(ex.Message);
}
}
private void test_Click(object sender, EventArgs e)
{
try
{
var (username, password) = Code_Preprocess();
if(username!=Settings.etc.Username||password!=Settings.etc.Password)
Code_Save(username ,password);
Task.Run(()=>Code_TestAsync());
}
catch (Exception ex)
{
Show_info(ex.Message);
}
}
private void Code_Save(string username, string password)
{
if (SavePassword.CheckState == CheckState.Checked)
Settings.etc.Write_Code_To_File(username, password);
Settings.etc.Username = username;
Settings.etc.Password = password;
}
private async Task Code_TestAsync()
{
Invoke(() => info.Text = "测试中,可能需要一点时间");
bool status = false;
string error_info = "";
await Task.Run(() =>
{
bool not_done = true;
try
{
Task_handler.index_Task_Handler.Make_request((result) =>
{
Task_handler.login_Task_Handler.Make_request((fresult) =>
{
not_done = false;
status = !fresult.isError();
error_info = fresult.Error_info;
}, result);
});
}
catch (Exception ex)
{
status = false;
error_info = ex.Message;
}
while (not_done) ;
});
Invoke(() => { if (status) Show_info("测试成功"); else Show_info("测试失败" + error_info); });
}
private (string,string) Code_Preprocess()
{
string username = ID.Text, password = this.password.Text;
if (username == null || username.Length == 0)
throw new InvalidOperationException("账号不能为空");
if (password == null || password.Length == 0)
throw new InvalidOperationException("怕密码泄露可以不用");
password = username + password;
username = Convert.ToHexString(MD5.ComputeHash(Encoding.UTF8.GetBytes(username))).ToLower();
password = Convert.ToHexString(SHA1.ComputeHash(Encoding.UTF8.GetBytes(password))).ToLower();
if (!Settings.etc.Agree_use)
if (MessageBox.Show("你需要为自己造成的后果负责,同一个账号的大量请求可不是什么正常的事,作者不会也无法对任何后果负责", "Are you sure about that?", MessageBoxButtons.YesNo) == DialogResult.No)
throw new Exception("Wise choice");
else
Settings.etc.Do_Agree_use();
return (username, password);
}
private void errorInfo_CountDown_Tick(object sender, EventArgs e)
{
errorInfo_CountDown.Enabled = false;
info.Visible = false;
}
private void Show_info(string s)
{
info.Text = s;
info.Visible = true;
errorInfo_CountDown.Enabled = true;
}
2024-03-12 08:15:15 +00:00
}
}