CssSolution/CSS_Solution/Settings.cs

379 lines
14 KiB
C#

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mime;
using System.Reflection;
using System.Reflection.Metadata;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
namespace CSS_Solution
{
public class Settings
{
public static Get_Index_header get_Index_Header = new Get_Index_header();
public static GetCode_header getCode_Header = new GetCode_header();
public static Webfinger webfinger = new Webfinger();
public static Login_header login_Header = new Login_header();
public static Login_body login_Body = new Login_body();
public static To_Course_header to_Course_Header = new To_Course_header();
public static Etc etc = new Etc();
}
public class Get_Index_header
{
List<(string, string)> header = new List<(string, string)>();
public Get_Index_header()
{
User_Agent = "";
Accept = "";
Host = "";
Accept_Encoding = "";
Connection = "";
Initialize.configuration.Bind("getIndex_headers", this);
}
public List<(string, string)> Get_header()
{
if (header.Count != 0)
return header;
Type type = typeof(Get_Index_header);
PropertyInfo[] propertyInfo = type.GetProperties();
foreach (PropertyInfo property in propertyInfo)
{
var val = (property.Name, property.GetValue(this) as string);
if (val.Item2 == null)
throw new ConfigurationErrorsException("Default header get error");
header.Add((val.Item1.Replace('_', '-'), val.Item2));
}
return header;
}
public string User_Agent { get; set; }
public string Accept { get; set; }
public string Host { get; set; }
public string Accept_Encoding { get; set; }
public string Connection { get; set; }
}
public class GetCode_header
{
List<(string, string)> header = new List<(string, string)>();
public GetCode_header()
{
User_Agent = "";
Accept = "";
Host = "";
Accept_Encoding = "";
Connection = "";
Content_Type = "";
Content_Length = "";
Initialize.configuration.Bind("getCode_headers", this);
}
public List<(string, string)> Get_Request_header()
{
if (header.Count != 0)
return header;
Type type = typeof(GetCode_header);
PropertyInfo[] propertyInfo = type.GetProperties();
foreach (PropertyInfo property in propertyInfo)
{
var val = (property.Name, property.GetValue(this) as string);
if (val.Name == "Content_Type" || val.Name == "Content_Length")
continue;
if (val.Item2 == null)
throw new ConfigurationErrorsException("Default header get error");
header.Add((val.Item1.Replace('_', '-'), val.Item2));
}
return header;
}
public string User_Agent { get; set; }
public string Accept { get; set; }
public string Host { get; set; }
public string Accept_Encoding { get; set; }
public string Connection { get; set; }
public string Content_Type { get; set; }
public string Content_Length { get; set; }
}
public class Webfinger
{
public string webfinger { get; set; }
public Webfinger()
{
webfinger = "";
Initialize.configuration.Bind("getcode_body", this);
}
public (string, string) Get_webfinger() => ("webfinger", webfinger);
}
public class Login_header
{
List<KeyValuePair<string, string>> header = new List<KeyValuePair<string, string>>();
public Login_header()
{
Accept = "";
Accept_Encoding = "";
Accept_Language = "";
Cache_Control = "";
Content_Length = "";
Content_Type = "";
Host = "";
Origin = "";
Referer = "";
Upgrade_Insecure_Requests = "";
Keep_Alive = "";
User_Agent = "";
Connection = "";
Initialize.configuration.Bind("login_header", this);
}
public List<KeyValuePair<string, string>> Get_Request_header()
{
if (header.Count != 0)
return header;
Type type = typeof(Login_header);
PropertyInfo[] propertyInfo = type.GetProperties();
foreach (PropertyInfo property in propertyInfo)
{
var val = (property.Name, property.GetValue(this) as string);
if (val.Name == "Content_Type" || val.Name == "Content_Length")
continue;
if (val.Item2 == null)
throw new ConfigurationErrorsException("Default header get error");
header.Add(new KeyValuePair<string, string>(val.Item1.Replace('_', '-'), val.Item2));
}
return header;
}
public string Accept { get; set; }
public string Accept_Encoding { get; set; }
public string Accept_Language { get; set; }
public string Cache_Control { get; set; }
public string Content_Length { get; set; }
public string Content_Type { get; set; }
public string Host { get; set; }
public string Origin { get; set; }
public string Referer { get; set; }
public string Upgrade_Insecure_Requests { get; set; }
public string Keep_Alive { get; set; }
public string User_Agent { get; set; }
public string Connection { get; set; }
}
public class Login_body
{
public Login_body()
{
MsgID = "";
KeyID = "";
UserName = "";
Password = "";
rnd = "";
return_EncData = "";
code = "";
userName1 = "";
password1 = "";
webfinger = "";
falg = "";
type = "";
userName_ = "";
password_ = "";
Initialize.configuration.Bind("login_body", this);
}
public List<KeyValuePair<string, string>> Get_Request_body(string rnd, string code)
{
List<KeyValuePair<string, string>> body = new List<KeyValuePair<string, string>>();
Type type = typeof(Login_body);
PropertyInfo[] propertyInfo = type.GetProperties();
foreach (PropertyInfo property in propertyInfo)
{
var val = (property.Name, property.GetValue(this) as string);
if (val.Item2 == null)
throw new ConfigurationErrorsException("Default header get error");
switch(val.Name)
{
case "rnd":
body.Add(new KeyValuePair<string, string>(val.Name, rnd));
break;
case "code":
body.Add(new KeyValuePair<string, string>(val.Name, code));
break;
case "userName1":
body.Add(new KeyValuePair<string, string>(val.Name, Settings.etc.Username?.Length == 0 ? val.Item2 : Settings.etc.Username ?? val.Item2));
break;
case "password1":
body.Add(new KeyValuePair<string, string>(val.Name, Settings.etc.Password?.Length == 0 ? val.Item2 : Settings.etc.Password ?? val.Item2));
break;
default:
body.Add(new KeyValuePair<string, string>(val.Name.Last() == '_' ? val.Name.Remove(val.Name.Length - 1) : val.Name, val.Item2));
break;
}
}
return body;
}
public string MsgID { get; set; }
public string KeyID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string rnd { get; set; }
public string return_EncData { get; set; }
public string code { get; set; }
public string userName1 { get; set; }
public string password1 { get; set; }
public string webfinger { get; set; }
public string falg { get; set; }
public string type { get; set; }
public string userName_ { get; set; }
public string password_ { get; set; }
}
public class To_Course_header
{
List<(string, string)> header = new List<(string, string)>();
public To_Course_header()
{
User_Agent = "";
Accept = "";
Accept_Encoding = "";
Accept_Language = "";
Referer = "";
Upgrade_Insecure_Requests = "";
Connection = "";
Initialize.configuration.Bind("to_course_header", this);
}
public List<(string, string)> Get_header()
{
if (header.Count != 0)
return header;
Type type = typeof(To_Course_header);
PropertyInfo[] propertyInfo = type.GetProperties();
foreach (PropertyInfo property in propertyInfo)
{
var val = (property.Name, property.GetValue(this) as string);
if (val.Item2 == null)
throw new ConfigurationErrorsException("Default header get error");
header.Add((val.Item1.Replace('_', '-'), val.Item2));
}
return header;
}
public string User_Agent { get; set; }
public string Accept { get; set; }
public string Accept_Encoding { get; set; }
public string Accept_Language { get; set; }
public string Referer { get; set; }
public string Upgrade_Insecure_Requests { get; set; }
public string Connection { get; set; }
}
public class Course_Request_Url
{
public string? jxjhh { get; set; }//教学计划号
public string? xnxq { get; set; }//学年学期
public string? kcdm { get; set; }//课程代码
public string? flag { get; set; }//可选参数 flag
public string? temp { get; set; }//可选参数temp
public string? addid { get; set; }//选课时addid
public string? keyinfo { get; set; }//选课时keyinfo
public string? _ { get; set; }//时间戳
public Course_Request_Url(string? jxjhh, string? xnxq, string? kcdm, string? flag, string? temp, string? addid, string? keyinfo)
{
this.jxjhh = jxjhh;
this.xnxq = xnxq;
this.kcdm = kcdm;
this.flag = flag;
this.temp = temp;
this.addid = addid;
this.keyinfo = keyinfo;
_ = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond).ToString();
}
}
public class Etc
{
public Etc()
{
StartTime_Str = "";
Username = "";
Password = "";
Initialize.configuration.Bind("Etc", this);
if (StartTime_Str.Length == 0)
StartTime = DateTime.Now;
try
{
string[] strs = StartTime_Str.Split('-');
int[] arguments = strs.Select(x => int.Parse(x)).ToArray();
if (arguments.Length != 6)
throw new InvalidOperationException("No Enough Argument");
StartTime = new DateTime(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
}
catch (Exception)
{
StartTime = DateTime.Now;
}
}
public string StartTime_Str { get; set; }
public DateTime StartTime { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public bool Agree_use { get; set; }
public void Set_StartTime(int year, int month, int day, int hour, int minute, int second)
{
StartTime = new DateTime(year, month, day, hour, minute, second);
StartTime_Str = year.ToString() + "-" + month.ToString() + "-" + day.ToString() + "-" + hour.ToString() + "-" + minute.ToString() + "-" + second.ToString();
}
private JObject Json_Read()
{
JObject jsonObject;
string filePath = Initialize.settings_File_Path;
using (StreamReader fileReader = File.OpenText(filePath))
{
using (JsonTextReader reader = new JsonTextReader(fileReader))
{
jsonObject = (JObject)JToken.ReadFrom(reader);
}
}
return jsonObject;
}
private void Json_Write(JObject jsonObject)
{
string filePath = Initialize.settings_File_Path;
using (StreamWriter fileWriter = new StreamWriter(filePath))
{
fileWriter.Write(jsonObject.ToString());
}
}
public void Write_StartTime_To_File()
{
JObject jsonObject = Json_Read();
#pragma warning disable CS8602 // 解引用可能出现空引用。
jsonObject["Etc"]["StartTime_Str"] = StartTime_Str;
#pragma warning restore CS8602 // 解引用可能出现空引用。
Json_Write(jsonObject);
}
//userName md5 password1 username+password sha1
public void Write_Code_To_File(string username, string password)
{
JObject jsonObject = Json_Read();
#pragma warning disable CS8602 // 解引用可能出现空引用。
jsonObject["login_body"]["userName1"] = username;
jsonObject["login_body"]["password1"] = password;
#pragma warning restore CS8602 // 解引用可能出现空引用。
Json_Write(jsonObject);
}
internal void Do_Agree_use()
{
JObject jsonObject = Json_Read();
Agree_use = true;
#pragma warning disable CS8602 // 解引用可能出现空引用。
jsonObject["Etc"]["Agree_use"] = true;
#pragma warning restore CS8602 // 解引用可能出现空引用。
Json_Write(jsonObject);
}
}
}