Compare commits
18
Commits
e28be80856
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9afd362de6 | ||
|
|
5b9cf5a538 | ||
|
|
db203d01e1 | ||
|
|
8690824663 | ||
|
|
5ca8148c42 | ||
|
|
e54cafd16b | ||
|
|
c21504993f | ||
|
|
8e90c904ee | ||
|
|
85e60c01bc | ||
|
|
0ce1a57a53 | ||
|
|
c2451be4a2 | ||
|
|
8aa79a46e3 | ||
|
|
654f73aca8 | ||
|
|
a0eeb9c8c4 | ||
|
|
ebbb44b132 | ||
|
|
c94fe5c62f | ||
|
|
ffc9b3d425 | ||
|
|
d9d00cc9fa |
No files matched your search
Binary file not shown.
+109
-42
@@ -3,11 +3,14 @@ using SunlightAggregationManager.ViewModel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Reflection.PortableExecutable;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Sockets;
|
||||
using static SunlightAggregationManager.UserClass.AsyncTcpServer;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace SunlightAggregationManager.UserClass
|
||||
{
|
||||
@@ -105,19 +108,50 @@ namespace SunlightAggregationManager.UserClass
|
||||
//行为
|
||||
Dictionary<string, object> myDict = new Dictionary<string, object>();
|
||||
//myDict.Add("LinkID", client.Id);
|
||||
|
||||
//AES解密
|
||||
string dataAES = AES.AesDecrypt(deserializedPerson!.Terminal, deserializedPerson.Dat);
|
||||
Console.WriteLine("[Log on]User[" + deserializedPerson?.User + "]COMMAND[" +
|
||||
Regex.Replace(dataAES, @"\\u[0-9A-Fa-f]{4}", match =>
|
||||
{ return Regex.Unescape(match.Value); }) + "]");
|
||||
|
||||
//记录
|
||||
myDict.Add("LastLoginIP", deserializedPerson?.IP ?? client.IP);
|
||||
myDict.Add("LastActionTime", DateTime.Now);
|
||||
myDict.Add("Note", deserializedPerson?.Dat ?? "unknown");
|
||||
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" + deserializedPerson?.User + "'", myDict);
|
||||
myDict.Add("Note", dataAES ?? "unknown");
|
||||
|
||||
Console.WriteLine("[Log on]User[" + deserializedPerson?.User + "]Note[" + (deserializedPerson?.Dat ?? "unknown") + "]");
|
||||
CommandC(deserializedPerson?.Dat ?? "NOT", deserializedPerson!);
|
||||
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" +
|
||||
deserializedPerson?.User + "'", myDict);
|
||||
|
||||
CommandC(dataAES ?? "NOT", deserializedPerson!);
|
||||
}
|
||||
else
|
||||
{//停用账号关闭连接
|
||||
client.SendAsync("Account Deactivated");
|
||||
}
|
||||
}
|
||||
else if (password== MainWindowViewModel.SERRVERPASWORD && deserializedPerson?.User== MainWindowViewModel.SERVERUSER)
|
||||
{
|
||||
//行为
|
||||
Dictionary<string, object> myDict = new Dictionary<string, object>();
|
||||
//myDict.Add("LinkID", client.Id);
|
||||
|
||||
//AES解密
|
||||
string dataAES = AES.AesDecrypt(deserializedPerson!.Terminal, deserializedPerson.Dat);
|
||||
Console.WriteLine("[Log on]User[" + deserializedPerson?.User + "]COMMAND[" +
|
||||
Regex.Replace(dataAES, @"\\u[0-9A-Fa-f]{4}", match =>
|
||||
{ return Regex.Unescape(match.Value); }) + "]");
|
||||
|
||||
//记录
|
||||
myDict.Add("LastLoginIP", deserializedPerson?.IP ?? client.IP);
|
||||
myDict.Add("LastActionTime", DateTime.Now);
|
||||
myDict.Add("Note", dataAES ?? "unknown");
|
||||
|
||||
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData, "User='" +
|
||||
deserializedPerson?.User + "'", myDict);
|
||||
|
||||
CommandC(dataAES ?? "NOT", deserializedPerson!);
|
||||
}
|
||||
else
|
||||
{ //无效账号关闭连接
|
||||
client.SendAsync("Invalid Account");
|
||||
@@ -137,31 +171,38 @@ namespace SunlightAggregationManager.UserClass
|
||||
};
|
||||
#endregion
|
||||
|
||||
try
|
||||
// while (true)
|
||||
{
|
||||
await tcpClient.SetupAsync(new TouchSocketConfig()
|
||||
.SetSingleStreamDataHandlingAdapter(() => new PeriodPackageAdapter())//以超时周期和包
|
||||
.SetRemoteIPHost(ip)
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
a.UseReconnection<TouchSocket.Sockets.TcpClient>(options =>
|
||||
try
|
||||
{
|
||||
await tcpClient.SetupAsync(new TouchSocketConfig()
|
||||
.SetSingleStreamDataHandlingAdapter(() => new PeriodPackageAdapter())//以超时周期和包
|
||||
.SetRemoteIPHost(ip)
|
||||
.ConfigurePlugins(a =>
|
||||
{
|
||||
options.PollingInterval = TimeSpan.FromSeconds(5);
|
||||
});
|
||||
|
||||
})
|
||||
.ConfigureContainer(a =>
|
||||
{
|
||||
|
||||
}));
|
||||
a.UseReconnection<TouchSocket.Sockets.TcpClient>(options =>
|
||||
{
|
||||
options.PollingInterval = TimeSpan.FromSeconds(5);
|
||||
});
|
||||
|
||||
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
|
||||
})
|
||||
.ConfigureContainer(a =>
|
||||
{
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogGing.ERRDATA(ex);
|
||||
Console.WriteLine("Tcp:" + ex.ToString());
|
||||
}));
|
||||
|
||||
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
|
||||
// break;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// tcpClient.Dispose();//清
|
||||
LogGing.ERRDATA(ex);
|
||||
Console.WriteLine("Tcp:" + ex.ToString());
|
||||
// await Task.Delay(TimeSpan.FromMinutes(1));
|
||||
// Console.WriteLine("Tcp:重试连接...");
|
||||
}
|
||||
}
|
||||
}
|
||||
private static void TimerCallback(object? state)
|
||||
@@ -187,9 +228,13 @@ namespace SunlightAggregationManager.UserClass
|
||||
string Command = "", ret = "";
|
||||
|
||||
Dictionary<string, object> data_ = new Dictionary<string, object>();
|
||||
var deserialized = JsonSerializer.Deserialize<Dictionary<string, object>>( AES.AesDecrypt(person.Terminal, dat))!;
|
||||
var deserialized = JsonSerializer.Deserialize<Dictionary<string, object>>(dat)!;
|
||||
if (deserialized.TryGetValue("Command", out var val) && val != null) Command = val.ToString()!;
|
||||
if (Command == "Log")//回复请求的信息
|
||||
if (Command == "SysCom")//回复指令的信息
|
||||
{
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
else if (Command == "Log")//回复请求的信息
|
||||
{
|
||||
DataRow? _user = MainWindowViewModel.Selet_Row(MainWindowViewModel._userData, "User='" +
|
||||
person?.User + "'");
|
||||
@@ -200,17 +245,33 @@ namespace SunlightAggregationManager.UserClass
|
||||
data_.Add("UserId", _user?.Field<object>("UserID") ?? "");
|
||||
data_.Add("UserName", _user?.Field<object>("Name") ?? "");
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "SysData")//回复请求的信息
|
||||
}
|
||||
else if (Command == "SysData")//特殊指令数据库查询
|
||||
{
|
||||
string com_ = deserialized.GetValue("Comm").ToString()!;
|
||||
string da_ = deserialized.GetValue("Data").ToString()!;
|
||||
if (com_ == "SYSDB")//本机sqlite数据库
|
||||
{
|
||||
DataTable resultTable = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet(da_, null)?.Tables[0] ?? new DataTable();
|
||||
data_.Add("DB", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
||||
data_.Add("Comm", "SYSDB");
|
||||
data_.Add("Enterprise", MainWindowViewModel.Enterprise);
|
||||
}
|
||||
else if (com_ == "DBCDB")//系统dbc数据库
|
||||
{
|
||||
DataTable resultTable = await MainWindowViewModel.dataBase.ExecuteQueryAsync(da_);
|
||||
data_.Add("DB", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
||||
data_.Add("Comm", "DBCDB");
|
||||
data_.Add("Enterprise", MainWindowViewModel.Enterprise);
|
||||
}
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "ChemicalStatistics")//回复请求的信息
|
||||
else if (Command == "ChemicalStatistics")//回复请求的信息
|
||||
{
|
||||
data_.Add("ChemicalStatistics", Newtonsoft.Json.JsonConvert.SerializeObject(MainWindowViewModel.ChemicalStatistics));
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "Info")//回复info请求的信息
|
||||
else if (Command == "Info")//回复info请求的信息
|
||||
{
|
||||
var t = ToList(MainWindowViewModel._machines).ToJsonString();
|
||||
//格式化返回信息
|
||||
@@ -221,7 +282,7 @@ namespace SunlightAggregationManager.UserClass
|
||||
data_.Add("DyeDissolveNumber", MainWindowViewModel.DyeDissolveNumber.ToString());
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "Query")//回复Query请求的信息
|
||||
else if (Command == "Query")//回复Query请求的信息
|
||||
{
|
||||
string s_ = deserialized.GetValue("StartTime").ToString()!;
|
||||
string e_ = deserialized.GetValue("EndTime").ToString()!;
|
||||
@@ -233,7 +294,7 @@ namespace SunlightAggregationManager.UserClass
|
||||
data_.Add("Query", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "QueryDyelot")//回复QueryDyelot请求的信息
|
||||
else if (Command == "QueryDyelot")//回复QueryDyelot请求的信息
|
||||
{
|
||||
string d_ = deserialized.GetValue("DyelotName").ToString()!;
|
||||
string f_ = deserialized.GetValue("Field").ToString()!;
|
||||
@@ -244,7 +305,7 @@ namespace SunlightAggregationManager.UserClass
|
||||
data_.Add("Query", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "QueryDetail")//回复QueryDetail请求的信息
|
||||
else if (Command == "QueryDetail")//回复QueryDetail请求的信息
|
||||
{
|
||||
string d_ = deserialized.GetValue("Dyelot").ToString()!;
|
||||
string r_ = deserialized.GetValue("ReDye").ToString()!;
|
||||
@@ -256,24 +317,26 @@ namespace SunlightAggregationManager.UserClass
|
||||
data_.Add("QueryDetail", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "Notification")//回复Notification请求的信息
|
||||
else if (Command == "Notification")//回复Notification请求的信息
|
||||
{
|
||||
string s_; try { s_ = deserialized.GetValue("StartTime").ToString()!; } catch(Exception) { s_ = "0"; }
|
||||
string e_ = deserialized.GetValue("EndTime").ToString()!;
|
||||
|
||||
var groups = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData,
|
||||
"Groups", "User='" + person?.User + "'");
|
||||
var department = MainWindowViewModel.Selet_Memory(MainWindowViewModel._userData,
|
||||
"Department", "User='" + person?.User + "'");
|
||||
|
||||
DataTable resultTable = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("" +
|
||||
"select * from Notification WHERE Groups='" + groups + "' and Id>'" +
|
||||
s_ + "'", null)?.Tables[0] ?? new DataTable();
|
||||
DataTable resultTable = MainWindowViewModel.SQLiteHelpers.ExecuteDataSet("" +
|
||||
"select * from Notification WHERE Groups='" + groups + "'and Department='" +
|
||||
department + "' and Id>'" + s_ + "'", null)?.Tables[0] ?? new DataTable();
|
||||
if(resultTable.Rows.Count==0) resultTable.Rows.Add();
|
||||
|
||||
//格式化返回信息
|
||||
data_.Add("Notification", Newtonsoft.Json.JsonConvert.SerializeObject(resultTable));
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "InfoMachine")//回复InfoMachine请求的信息
|
||||
else if (Command == "InfoMachine")//回复InfoMachine请求的信息
|
||||
{
|
||||
string t_ = deserialized.GetValue("Type").ToString()!;
|
||||
string? m_ =null, f_ = "*";
|
||||
@@ -310,7 +373,7 @@ namespace SunlightAggregationManager.UserClass
|
||||
//格式化返回信息
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "InfoMachineHistory")//回复InfoMachineHistory请求的信息
|
||||
else if (Command == "InfoMachineHistory")//回复InfoMachineHistory请求的信息
|
||||
{
|
||||
string t_ = deserialized.GetValue("Type").ToString()!;
|
||||
string? m_ = null, f_ = "*";
|
||||
@@ -349,7 +412,7 @@ namespace SunlightAggregationManager.UserClass
|
||||
//格式化返回信息
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "InfoMachinePipes")//回复Notification请求的信息
|
||||
else if (Command == "InfoMachinePipes")//回复Notification请求的信息
|
||||
{
|
||||
string? m_ = null, f_ = "*";
|
||||
|
||||
@@ -363,7 +426,7 @@ namespace SunlightAggregationManager.UserClass
|
||||
//格式化返回信息
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
if (Command == "SETUSER")//回复请求的信息
|
||||
else if (Command == "SETUSER")//回复请求的信息
|
||||
{
|
||||
MainWindowViewModel.Updata_Memory(MainWindowViewModel._userData,
|
||||
"User='" + deserialized.GetValue("USER").ToString()! + "'" ,
|
||||
@@ -376,6 +439,10 @@ namespace SunlightAggregationManager.UserClass
|
||||
ret = JsonSerializer.Serialize(data_);
|
||||
}
|
||||
//加密回传
|
||||
/*Console.WriteLine("[Log on]User[" + person?.User + "]REPLY[" +
|
||||
Regex.Replace(ret, @"\\u[0-9A-Fa-f]{4}", match =>
|
||||
{ return Regex.Unescape(match.Value); }) + "]");*/
|
||||
|
||||
person!.Dat =AES.AesEncipher( person.Terminal, ret);
|
||||
var jsdat = JsonSerializer.Serialize(person);
|
||||
await tcpClient.SendAsync(jsdat);
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace SunlightAggregationManager.UserClass
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogGing.ERRDATA(ex);
|
||||
Console.WriteLine("DataBase Link timeout\n"+ex.ToString());
|
||||
Console.WriteLine("DataBase Link timeout\n"+ ex.Message.ToString());
|
||||
// 失败处理
|
||||
return; // 直接返回,不执行后续代码
|
||||
}
|
||||
@@ -111,9 +111,7 @@ namespace SunlightAggregationManager.UserClass
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogGing.ERRDATA(ex);
|
||||
Console.WriteLine("DataBase Query failed\n" + ex.ToString());
|
||||
// 将异常抛出给调用方,或者返回一个空的 DataTable
|
||||
throw;
|
||||
Console.WriteLine("DataBase Query failed\n" + ex.Message.ToString());
|
||||
}
|
||||
|
||||
return dataTable;
|
||||
@@ -158,9 +156,7 @@ namespace SunlightAggregationManager.UserClass
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogGing.ERRDATA(ex);
|
||||
Console.WriteLine("DataBase Query failed\n" + ex.ToString());
|
||||
// 将异常抛出给调用方,或者返回一个空的 DataTable
|
||||
throw;
|
||||
Console.WriteLine("DataBase Query failed\n" + ex.Message.ToString());
|
||||
}
|
||||
|
||||
return dataTable.Rows.Count;
|
||||
|
||||
+107
-59
@@ -1,85 +1,133 @@
|
||||
using System;
|
||||
using SunlightAggregationManager.View;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace SunlightAggregationManager.UserClass
|
||||
{
|
||||
public class LogGing
|
||||
{
|
||||
public class LOGinf
|
||||
{
|
||||
public required string Logpath { get; set; }
|
||||
public required string LogPath { get; set; }
|
||||
public required string Log_time { get; set; }
|
||||
public required object DAT { get; set; }
|
||||
}
|
||||
static Queue<LOGinf> LogGingQueue = new Queue<LOGinf>();
|
||||
static Queue<LOGinf> ERRQueue = new Queue<LOGinf>();
|
||||
static Queue<LOGinf> ExchangeQueue = new Queue<LOGinf>();
|
||||
public static void LogGingDATA(string dat)
|
||||
{
|
||||
string logpath = System.Environment.CurrentDirectory + "\\Log";//日志文件目录
|
||||
string logPath = "" + System.Environment.CurrentDirectory + "\\Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";//日志文件
|
||||
string Log_time = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]:";
|
||||
LOGinf ginf = new LOGinf
|
||||
{
|
||||
Logpath = System.Environment.CurrentDirectory + "\\Log",//日志文件目录
|
||||
LogPath = "" + System.Environment.CurrentDirectory + "\\Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt",//日志文件
|
||||
Log_time = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]:",
|
||||
DAT = dat
|
||||
};
|
||||
LogGingQueue.Enqueue(ginf);
|
||||
|
||||
if (Directory.Exists(logpath))//检查日志路径
|
||||
if(LogGingQueue.Count==1) LogGingQueueW();
|
||||
}
|
||||
public static async void LogGingQueueW()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (!File.Exists(logPath))//检查日志文件并写入启动日志
|
||||
try
|
||||
{
|
||||
FileStream fs = new FileStream(logPath, FileMode.CreateNew, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine(Log_time + dat);
|
||||
wr.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
LOGinf inf = LogGingQueue.Dequeue();
|
||||
|
||||
if (Directory.Exists(inf.Logpath))//检查日志路径
|
||||
{
|
||||
FileStream fs = new FileStream(logPath, FileMode.Append, FileAccess.Write);
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine(Log_time + dat);
|
||||
wr.Close();
|
||||
if (!File.Exists(inf.LogPath))//检查日志文件并写入启动日志
|
||||
{
|
||||
FileStream fs = new FileStream(inf.LogPath, FileMode.CreateNew, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine(inf.Log_time + inf.DAT);
|
||||
wr.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
FileStream fs = new FileStream(inf.LogPath, FileMode.Append, FileAccess.Write);
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine(inf.Log_time + inf.DAT);
|
||||
wr.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(inf.Logpath);
|
||||
directoryInfo.Create();//创建日志路径
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(logpath);
|
||||
directoryInfo.Create();//创建日志路径
|
||||
catch (Exception ) { }
|
||||
if (LogGingQueue.Count == 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ERRDATA(System.Exception dat)
|
||||
{
|
||||
string Log_time = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
string logpath = System.Environment.CurrentDirectory + "\\ERR";//日志文件目录
|
||||
// string logPathtxt = "" + System.Environment.CurrentDirectory + "\\Log\\"+ Log_time + "Log.txt";//日志文件
|
||||
// System.IO.DirectoryInfo log = new System.IO.DirectoryInfo();//生成日志文件目录
|
||||
string log_path = logpath + "\\ERR" + Log_time + ".txt";
|
||||
string Log_timehms = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
if (Directory.Exists(logpath))//检查日志路径
|
||||
{
|
||||
LOGinf ginf = new LOGinf
|
||||
{
|
||||
if (!File.Exists(log_path))//检查文件并写入
|
||||
{
|
||||
// FileStream fs = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建文件
|
||||
// StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
// wr.Close();
|
||||
FileStream fil = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wfil = new StreamWriter(fil);//创建文件
|
||||
wfil.WriteLine("[" + Log_timehms + "];[Error] ||" + Environment.NewLine.ToString());
|
||||
wfil.WriteLine("[" + Log_timehms + "];[Error source] ||" + dat.Source.ToString() + Environment.NewLine.ToString());
|
||||
wfil.WriteLine("[" + Log_timehms + "];[Error message] ||" + dat.Message.ToString() + Environment.NewLine.ToString());
|
||||
wfil.WriteLine("[" + Log_timehms + "];[Error area] ||" + dat.StackTrace.ToString());
|
||||
wfil.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
FileStream fs = new FileStream(log_path, FileMode.Append, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine("[" + Log_timehms + "];[Error] ||" + Environment.NewLine.ToString());
|
||||
wr.WriteLine("[" + Log_timehms + "];[Error source] ||" + dat.ToString() + Environment.NewLine.ToString());
|
||||
wr.WriteLine("[" + Log_timehms + "];[Error message] ||" + dat.Message.ToString() + Environment.NewLine.ToString());
|
||||
wr.WriteLine("[" + Log_timehms + "];[Error area] ||" + dat.ToString());
|
||||
wr.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
Logpath = System.Environment.CurrentDirectory + "\\ERR",//日志文件目录
|
||||
LogPath = "" + System.Environment.CurrentDirectory + "\\ERR\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt",//日志文件
|
||||
Log_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
|
||||
DAT = dat
|
||||
};
|
||||
ERRQueue.Enqueue(ginf);
|
||||
|
||||
if (ERRQueue.Count == 1) ERRQueueW();
|
||||
}
|
||||
public static async void ERRQueueW()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(logpath);
|
||||
directoryInfo.Create();
|
||||
try
|
||||
{
|
||||
LOGinf inf = ERRQueue.Dequeue();
|
||||
Exception dat = (Exception)inf.DAT;
|
||||
|
||||
if (Directory.Exists(inf.Logpath))//检查日志路径
|
||||
{
|
||||
if (!File.Exists(inf.LogPath))//检查文件并写入
|
||||
{
|
||||
// FileStream fs = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建文件
|
||||
// StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
// wr.Close();
|
||||
FileStream fil = new FileStream(inf.LogPath, FileMode.CreateNew, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wfil = new StreamWriter(fil);//创建文件
|
||||
wfil.WriteLine("[" + inf.Log_time + "];[Error] ||" + Environment.NewLine.ToString());
|
||||
wfil.WriteLine("[" + inf.Log_time + "];[Error source] ||" + dat.Source.ToString() + Environment.NewLine.ToString());
|
||||
wfil.WriteLine("[" + inf.Log_time + "];[Error message] ||" + dat.Message.ToString() + Environment.NewLine.ToString());
|
||||
wfil.WriteLine("[" + inf.Log_time + "];[Error area] ||" + dat.StackTrace.ToString());
|
||||
wfil.Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
FileStream fs = new FileStream(inf.LogPath, FileMode.Append, FileAccess.Write);//创建写入文件
|
||||
StreamWriter wr = new StreamWriter(fs);//创建文件
|
||||
wr.WriteLine("[" + inf.Log_time + "];[Error] ||" + Environment.NewLine.ToString());
|
||||
wr.WriteLine("[" + inf.Log_time + "];[Error source] ||" + dat.ToString() + Environment.NewLine.ToString());
|
||||
wr.WriteLine("[" + inf.Log_time + "];[Error message] ||" + dat.Message.ToString() + Environment.NewLine.ToString());
|
||||
wr.WriteLine("[" + inf.Log_time + "];[Error area] ||" + dat.ToString());
|
||||
wr.Close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(inf.Logpath);
|
||||
directoryInfo.Create();
|
||||
}
|
||||
}
|
||||
catch (Exception) { }
|
||||
if (ERRQueue.Count == 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ExchangeDATA(string dat)
|
||||
{
|
||||
string Log_time = DateTime.Now.ToString("yyyy-MM-dd");
|
||||
|
||||
+55
-42
@@ -15,62 +15,75 @@ namespace SunlightAggregationManager.UserClass
|
||||
|
||||
public static void AllowPort(int port, string protocol)
|
||||
{
|
||||
|
||||
DelPort(port, protocol);
|
||||
//创建一个INetFwRule对象
|
||||
Type type = Type.GetTypeFromProgID("HNetCfg.FwRule") ?? throw new
|
||||
InvalidOperationException("Failed to retrieve HNetCfg.FwRule type. Ensure the COM component is registered.");
|
||||
INetFwRule? rule = (INetFwRule?)Activator.CreateInstance(type);
|
||||
|
||||
//设置规则的属性
|
||||
rule?.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; //允许连接
|
||||
rule?.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN; //入站规则
|
||||
rule?.Enabled = true; //启用规则
|
||||
rule?.InterfaceTypes = "All"; //适用于所有网络接口
|
||||
rule?.Name = GetName(port, protocol); //规则名称
|
||||
if (protocol.ToLower() == "tcp")
|
||||
try
|
||||
{
|
||||
rule?.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; //TCP协议
|
||||
DelPort(port, protocol);
|
||||
//创建一个INetFwRule对象
|
||||
Type type = Type.GetTypeFromProgID("HNetCfg.FwRule") ?? throw new
|
||||
InvalidOperationException("Failed to retrieve HNetCfg.FwRule type. Ensure the COM component is registered.");
|
||||
INetFwRule? rule = (INetFwRule?)Activator.CreateInstance(type);
|
||||
|
||||
//设置规则的属性
|
||||
rule?.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; //允许连接
|
||||
rule?.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN; //入站规则
|
||||
rule?.Enabled = true; //启用规则
|
||||
rule?.InterfaceTypes = "All"; //适用于所有网络接口
|
||||
rule?.Name = GetName(port, protocol); //规则名称
|
||||
if (protocol.ToLower() == "tcp")
|
||||
{
|
||||
rule?.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; //TCP协议
|
||||
}
|
||||
else
|
||||
{
|
||||
rule?.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; //UDP协议
|
||||
}
|
||||
|
||||
rule?.LocalPorts = "" + port; //本地端口号
|
||||
|
||||
//获取FirewallPolicy对象
|
||||
Type policyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2") ?? throw new
|
||||
InvalidOperationException("Failed to retrieve HNetCfg.FwRule type. Ensure the COM component is registered.");
|
||||
INetFwPolicy2? policy = (INetFwPolicy2?)Activator.CreateInstance(policyType);
|
||||
|
||||
//将规则添加到防火墙策略中
|
||||
policy?.Rules.Add(rule);
|
||||
Console.WriteLine("Add firewall port:" + port);
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
rule?.Protocol = (int)NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; //UDP协议
|
||||
Console.WriteLine(ex.ToString());
|
||||
}
|
||||
|
||||
rule?.LocalPorts = "" + port; //本地端口号
|
||||
|
||||
//获取FirewallPolicy对象
|
||||
Type policyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2") ?? throw new
|
||||
InvalidOperationException("Failed to retrieve HNetCfg.FwRule type. Ensure the COM component is registered.");
|
||||
INetFwPolicy2? policy = (INetFwPolicy2?)Activator.CreateInstance(policyType);
|
||||
|
||||
//将规则添加到防火墙策略中
|
||||
policy?.Rules.Add(rule);
|
||||
Console.WriteLine("Add firewall port:"+port);
|
||||
}
|
||||
|
||||
public static void DelPort(int port, string protocol)
|
||||
{
|
||||
//获取FirewallPolicy对象
|
||||
Type policyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2") ?? throw new
|
||||
InvalidOperationException("Failed to retrieve HNetCfg.FwRule type. Ensure the COM component is registered.");
|
||||
INetFwPolicy2? policy = (INetFwPolicy2?)Activator.CreateInstance(policyType);
|
||||
|
||||
//获取现有的规则集合
|
||||
INetFwRules? rules = policy?.Rules;
|
||||
if (rules != null)
|
||||
try
|
||||
{
|
||||
//查找名称的规则并删除它
|
||||
foreach (INetFwRule rule in rules)
|
||||
//获取FirewallPolicy对象
|
||||
Type policyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2") ?? throw new
|
||||
InvalidOperationException("Failed to retrieve HNetCfg.FwRule type. Ensure the COM component is registered.");
|
||||
INetFwPolicy2? policy = (INetFwPolicy2?)Activator.CreateInstance(policyType);
|
||||
|
||||
//获取现有的规则集合
|
||||
INetFwRules? rules = policy?.Rules;
|
||||
if (rules != null)
|
||||
{
|
||||
if (rule.Name == GetName(port, protocol))
|
||||
//查找名称的规则并删除它
|
||||
foreach (INetFwRule rule in rules)
|
||||
{
|
||||
rules.Remove(rule.Name);
|
||||
//Console.WriteLine(@"Firewall rule deleted successfully.");
|
||||
break;
|
||||
if (rule.Name == GetName(port, protocol))
|
||||
{
|
||||
rules.Remove(rule.Name);
|
||||
//Console.WriteLine(@"Firewall rule deleted successfully.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -324,9 +324,14 @@ namespace SunlightAggregationManager.ViewModel
|
||||
{
|
||||
lock (DB)
|
||||
{
|
||||
DataRow drEmployee = DB.Select(key).First();
|
||||
object? index = drEmployee.Field<object>(name);
|
||||
return index;
|
||||
DataRow[] dataRows = DB.Select(key);
|
||||
if (dataRows.Count() > 0)
|
||||
{
|
||||
DataRow drEmployee = dataRows.First();
|
||||
object? index = drEmployee.Field<object>(name);
|
||||
return index;
|
||||
}
|
||||
else { return null; }
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in new issue
Block a user