Изменения в TraningControlPlugin и LightPT.pas
This commit is contained in:
parent
e38921c9e8
commit
e71d14dab2
|
|
@ -220,7 +220,6 @@ namespace DBAccessPluginNamespace
|
|||
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
|
||||
panelUnAuthorized.BringToFront();
|
||||
labelUserName.Text = "";
|
||||
|
||||
}
|
||||
|
||||
private async void enterButton_Click(object sender, EventArgs e)
|
||||
|
|
|
|||
59
VisualPlugins/TraningControlPlugin/LoginUtils.cs
Normal file
59
VisualPlugins/TraningControlPlugin/LoginUtils.cs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Management;
|
||||
using System.Security.Cryptography;
|
||||
using System.IO;
|
||||
|
||||
namespace VisualPascalABCPlugins
|
||||
{
|
||||
public static class LoginUtils
|
||||
{
|
||||
public static string ProcessorId()
|
||||
{
|
||||
var mbs = new ManagementObjectSearcher("Select ProcessorId From Win32_processor");
|
||||
var mbsList = mbs.Get();
|
||||
foreach (ManagementObject mo in mbsList)
|
||||
{
|
||||
return mo['ProcessorId'].ToString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static byte[] Encrypt(string src)
|
||||
{
|
||||
var ae = Aes.Create();
|
||||
var key = Encoding.UTF8.GetBytes(ProcessorId());
|
||||
var crypt = ae.CreateEncryptor(key, key);
|
||||
byte[] encrypted;
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
using (var cs = new CryptoStream(ms, crypt, CryptoStreamMode.Write))
|
||||
using (var sw = new StreamWriter(cs))
|
||||
{
|
||||
sw.Write(src);
|
||||
}
|
||||
encrypted = ms.ToArray();
|
||||
}
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
public static string Decrypt(byte[] data)
|
||||
{
|
||||
if (data == null)
|
||||
return "";
|
||||
var ae = Aes.Create();
|
||||
var key = Encoding.UTF8.GetBytes(ProcessorId());
|
||||
var crypt = ae.CreateDecryptor(key, key);
|
||||
string text;
|
||||
using (var ms = new MemoryStream(data))
|
||||
using (var cs = new CryptoStream(ms, crypt, CryptoStreamMode.Read))
|
||||
using (var sr = new StreamReader(cs))
|
||||
{
|
||||
text = sr.ReadToEnd();
|
||||
}
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,23 @@
|
|||
/// Модуль LightPT автоматической легковесной проверки заданий
|
||||
unit LightPT;
|
||||
|
||||
{$reference System.Net.Http.dll}
|
||||
{$reference System.Security.dll}
|
||||
{$reference System.Management.dll}
|
||||
uses __RedirectIOMode;
|
||||
|
||||
uses System.Management;
|
||||
uses System.Security.Cryptography;
|
||||
uses System.IO;
|
||||
|
||||
uses System;
|
||||
uses System.Net.Http;
|
||||
uses System.Threading.Tasks;
|
||||
|
||||
// Исключения - для вывода сообщения
|
||||
// TaskResult - для базы данных
|
||||
// Запоминать исключение в глобальной переменной TaskException - для доп. параметров для БД в формате
|
||||
// ПодтипИсключения(парам1,...,парамn)
|
||||
// Эту строку также можно писать в БД как доп параметры TaskResult
|
||||
// Эту строку также можно писать в БД как доп параметры TaskResultInfo
|
||||
// Чем хороши исключения - их можно делать разными с абсолютно разными параметрами
|
||||
|
||||
const lightptname = 'lightpt.dat';
|
||||
|
|
@ -32,7 +42,44 @@ var
|
|||
LessonName: string := '';
|
||||
TaskNamesMap := new Dictionary<string,string>;
|
||||
|
||||
type
|
||||
ServerAddr := 'https://air.mmcs.sfedu.ru/pascalabc';
|
||||
|
||||
type
|
||||
UserTypeEnum = (None, Student, Teacher, Admin);
|
||||
ServerAccessProvider = class
|
||||
private
|
||||
client: HTTPClient;
|
||||
public
|
||||
ServerAddr: string;
|
||||
auto property ShortFIO: string;
|
||||
auto property FullFIO: string;
|
||||
auto property Password: string;
|
||||
auto property Group: string;
|
||||
auto property UserType: UserTypeEnum;
|
||||
constructor (ServerAddr: string);
|
||||
begin
|
||||
Self.ServerAddr := ServerAddr;
|
||||
client := new HttpClient();
|
||||
client.Timeout := TimeSpan.FromSeconds(10);
|
||||
end;
|
||||
|
||||
function SendPostRequest(FullFIO, Password, LessonName, TaskName, TaskStatus: string): Task<string>;
|
||||
begin
|
||||
var values := Dict(
|
||||
( 'shortFIO', '' ),
|
||||
( 'FIO', FullFIO ),
|
||||
( 'taskName', TaskName ),
|
||||
( 'lessonName', LessonName ),
|
||||
( 'type', TaskStatus ),
|
||||
( 'content', '' ),
|
||||
( 'password', Password )
|
||||
);
|
||||
var content := new FormUrlEncodedContent(values);
|
||||
var response := client.PostAsync(ServerAddr + '/add.php', content);
|
||||
Result := response.Result.Content.ReadAsStringAsync();
|
||||
end;
|
||||
end;
|
||||
|
||||
InputCountException = class(PTException) // Ровно Count
|
||||
Count: integer; // Count - сколько введено
|
||||
n: integer; // n - сколько требуется ввести
|
||||
|
|
@ -144,6 +191,81 @@ function cStr := typeof(string);
|
|||
function cBool := typeof(boolean);
|
||||
function cChar := typeof(char);
|
||||
|
||||
function FindAuthDat: string;
|
||||
begin
|
||||
var auth := 'auth.dat';
|
||||
Result := '';
|
||||
// В текущем каталоге
|
||||
if FileExists(auth) then
|
||||
Result := ExpandFileName(auth)
|
||||
// Если нет, в каталоге уровня выше
|
||||
else if FileExists(System.IO.Path.Combine('..',auth)) then
|
||||
begin
|
||||
Result := ExpandFileName(System.IO.Path.Combine('..',auth))
|
||||
end
|
||||
// Если нет, в корневом каталоге сетевого диска
|
||||
else begin
|
||||
var fullname := ExpandFileName(auth);
|
||||
var drive := ExtractFileDrive(fullname);
|
||||
var di := new System.IO.DriveInfo(drive);
|
||||
if di.DriveType = System.IO.DriveType.Network then
|
||||
begin
|
||||
var af := System.IO.Path.Combine(di.RootDirectory.FullName,auth);
|
||||
if FileExists(af) then
|
||||
Result := af;
|
||||
end
|
||||
end;
|
||||
end;
|
||||
|
||||
function ProcessorId: string;
|
||||
begin
|
||||
var mbs := new ManagementObjectSearcher('Select ProcessorId From Win32_processor');
|
||||
var mbsList := mbs.Get();
|
||||
Result := '';
|
||||
foreach var mo: ManagementObject in mbsList do
|
||||
begin
|
||||
Result := mo['ProcessorId'].ToString();
|
||||
break;
|
||||
end;
|
||||
end;
|
||||
|
||||
function Encrypt(src: string): array of byte; // записать в файл
|
||||
begin
|
||||
var ae := Aes.Create();
|
||||
var key := Encoding.UTF8.GetBytes(ProcessorId);
|
||||
var encrypted: array of byte;
|
||||
var crypt := ae.CreateEncryptor(key, key);
|
||||
var ms := new MemoryStream();
|
||||
var cs := new CryptoStream(ms, crypt, CryptoStreamMode.Write);
|
||||
var sw := new StreamWriter(cs);
|
||||
sw.Write(src);
|
||||
sw.Close;
|
||||
cs.Close;
|
||||
ms.Close;
|
||||
Result := ms.ToArray;
|
||||
end;
|
||||
|
||||
function Decrypt(data: array of byte): string;
|
||||
begin
|
||||
if data = nil then
|
||||
begin
|
||||
Result := '';
|
||||
exit
|
||||
end;
|
||||
var ae := Aes.Create();
|
||||
var key := Encoding.UTF8.GetBytes(ProcessorId);
|
||||
var encrypted: array of byte;
|
||||
var crypt := ae.CreateDecryptor(key, key);
|
||||
var ms := new MemoryStream(data);
|
||||
var cs := new CryptoStream(ms, crypt, CryptoStreamMode.Read);
|
||||
var sr := new StreamReader(cs);
|
||||
var text := sr.ReadToEnd;
|
||||
sr.Close;
|
||||
cs.Close;
|
||||
ms.Close;
|
||||
Result := text;
|
||||
end;
|
||||
|
||||
procedure CheckInitialIO;
|
||||
begin
|
||||
if (OutputList.Count = InitialOutputList.Count) and (InputList.Count = InitialInputList.Count) then
|
||||
|
|
@ -218,16 +340,6 @@ procedure CheckInitialOutputSeq(a: sequence of System.Type) := CheckInitialOutpu
|
|||
|
||||
procedure CheckInitialInputSeq(a: sequence of System.Type) := CheckInitialInput(a.Select(x->object(x)).ToArray);
|
||||
|
||||
procedure WriteInfoToLocalDatabase(LessonName,TaskName: string; result: TaskStatus; AdditionalInfo: string := '');
|
||||
begin
|
||||
try
|
||||
System.IO.File.AppendAllText('db.txt', $'{LessonName} {TaskName} {dateTime.Now.ToString(''u'')} {Result.ToString} {AdditionalInfo}' + #10);
|
||||
except
|
||||
on e: Exception do
|
||||
Print(e.Message);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure CheckInputCount(n: integer);
|
||||
begin
|
||||
if InputList.Count < n then
|
||||
|
|
@ -990,6 +1102,18 @@ begin
|
|||
WriteInfoCallBack(LessonName,TName, TaskResult, TaskResultInfo);
|
||||
end;
|
||||
|
||||
procedure WriteInfoToLocalDatabase(LessonName,TaskName: string; result: TaskStatus; AdditionalInfo: string := '');
|
||||
begin
|
||||
try
|
||||
System.IO.File.AppendAllText('db.txt', $'{LessonName} {TaskName} {dateTime.Now.ToString(''u'')} {Result.ToString} {AdditionalInfo}' + #10);
|
||||
var auth := FindAuthDat;
|
||||
// Console.WriteLine(auth);
|
||||
except
|
||||
on e: Exception do
|
||||
Console.WriteLine(e.Message);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure LoadLightPTInfo;
|
||||
begin
|
||||
try
|
||||
|
|
|
|||
Loading…
Reference in a new issue