-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbjson.cs
More file actions
165 lines (156 loc) · 4.89 KB
/
dbjson.cs
File metadata and controls
165 lines (156 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
namespace Schema
{
public class Network : IIdentifiable
{
public int id { get; set; }
public string ProgramID { get; set; }
public string IPDNS { get; set; }
public bool Allow { get; set; }
}
public interface IIdentifiable
{
int id { get; set; }
}
}
public class DataBase
{
private static bool Encrypted = false;
private static string DbFile = "test.db";
private static Dictionary<string, object> db = new Dictionary<string, object>();
static DataBase()
{
var jsonData = LoadDataBase();
LoadOrCreateTable<Schema.Detection>(jsonData);
LoadOrCreateTable<Schema.Quarantine>(jsonData);
LoadOrCreateTable<Schema.Program>(jsonData);
LoadOrCreateTable<Schema.Network>(jsonData);
LoadOrCreateTable<Schema.Exclusion>(jsonData);
}
public static bool Insert<T>(T add) where T : class {
((List<T>)db[typeof(T).Name]).Add(add);
WriteDataBase();
return true;
}
public static bool Delete<T>(T Params) where T : class
{
var list = db[typeof(T).Name] as List<T>;
if (list != null)
{
var itemToRemove = list.OfType<T>().FirstOrDefault(item =>
{
foreach (var property in typeof(T).GetProperties())
{
var searchValue = property.GetValue(Params);
var itemValue = property.GetValue(item);
if (searchValue == null)
continue;
if (itemValue == null || !itemValue.Equals(searchValue))
return false;
}
return true;
});
if (itemToRemove != null)
{
list.Remove(itemToRemove as T);
WriteDataBase();
return true;
}
}
return false;
}
public static T Read<T>(T searchParams) where T : class {
var list = db[typeof(T).Name] as List<T>;
if (list != null)
return list.OfType<T>().FirstOrDefault(item =>
{
foreach (var property in typeof(T).GetProperties())
{
var searchValue = property.GetValue(searchParams);
var itemValue = property.GetValue(item);
if (searchValue == null)
continue;
if (itemValue == null || !itemValue.Equals(searchValue))
return false;
}
return true;
});
return null;
}
public static List<T> ReadAll<T>() where T : class {
var list = db[typeof(T).Name] as List<T>;
if (list != null)
return list;
return null;
}
public static int Count<T>() where T : class {
var list = db[typeof(T).Name] as List<T>;
if (list != null)
return -1;
return list.Count;
}
static bool LoadOrCreateTable<T>(Dictionary<string, object> Json = null) where T : class
{
try
{
if (!db.ContainsKey(typeof(T).Name) && Json.ContainsKey(typeof(T).Name))
{
db[typeof(T).Name] = new JavaScriptSerializer().ConvertToType<List<T>>(Json[typeof(T).Name]);
}
else
{
db.Add(typeof(T).Name, new List<T>());
WriteDataBase();
}
return true;
}
catch (Exception)
{
return false;
}
}
static bool WriteDataBase()
{
try
{
if (Encrypted)
{
Encryption.CryptFile(DbFile, UTF8Encoding.UTF8.GetBytes(new JavaScriptSerializer().Serialize(db)), Encryption.GenKey("Database"));
}
else
{
File.WriteAllBytes(DbFile, UTF8Encoding.UTF8.GetBytes(new JavaScriptSerializer().Serialize(db)));
}
return true;
}
catch
{
return false;
}
}
static Dictionary<string, object> LoadDataBase()
{
try
{
if (File.Exists(DbFile))
{
if (Encrypted)
{
byte[] jsondata = Encryption.DecryptFile(DbFile, Encryption.GenKey("Database"));
return new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(UTF8Encoding.UTF8.GetString(jsondata));
}
else
{
return new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(UTF8Encoding.UTF8.GetString(File.ReadAllBytes(DbFile)));
}
}
else
{
return new JavaScriptSerializer().Deserialize<Dictionary<string, object>>("{}");
}
}
catch
{
return null;
}
}
}