-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataService.svc.cs
328 lines (306 loc) · 15.8 KB
/
DataService.svc.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Net;
using System.Text;
using AngleSharp.Dom.Html;
using AngleSharp.Parser.Html;
using AngleSharp.Dom;
namespace MyService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "DataService" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select DataService.svc or DataService.svc.cs at the Solution Explorer and start debugging.
public class DataService : IDataService
{
static string diskLetter = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System));
Report report = new Report(diskLetter + @"\pctesting\");
UserReport Usreport = new UserReport(diskLetter + @"\pctesting\");
SQLiteConnection sql = new SQLiteConnection("DataSource = " + diskLetter + @"pctesting\mydb.sqlite;Version=3");
string adminLogin = "admin";
string adminPassword = "admin";
string fk_computer = "CONSTRAINT fk_computer " +
"FOREIGN KEY (computerID) " +
"REFERENCES COMPUTER(id)";
string fk_user = "CONSTRAINT fk_user " +
"FOREIGN KEY (userID) " +
"REFERENCES USER(id))";
public List<string> getUsers()
{
sql.Open();
SQLiteCommand sc = new SQLiteCommand("SELECT NAME FROM USER;", sql);
SQLiteDataReader reader = sc.ExecuteReader();
List<string> list = new List<string>();
while (reader.Read())
{
list.Add(reader[0].ToString());
}
sql.Close();
return list;
}
public List<List<string>> getActivity()
{
sql.Open();
SQLiteCommand sc = new SQLiteCommand("SELECT FROM USER;", sql);
SQLiteDataReader reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
reader.Close();
List<List<string>> list = new List<List<string>>();
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
list.Add(new List<string>());
list[list.Count-1].Add(dt.Rows[i][j].ToString());
}
}
sql.Close();
return list;
}
public bool addUser(string name, string password)
{
sql.Open();
SQLiteCommand sc = new SQLiteCommand("SELECT COUNT(*) FROM USER WHERE NAME = '" + name + "';", sql);
if (Convert.ToInt32(sc.ExecuteScalar()) > 0)
return false;
execute(String.Format("INSERT INTO USER VALUES(NULL, '{0}', '{1}', {2});", name, password, 0));
sql.Close();
return true;
}
public void makeReport()
{
report.makeReport();
}
public string login(string name, string password, string compMAC, string compName)
{
if (!Directory.Exists(diskLetter + @"pctesting"))
Directory.CreateDirectory(diskLetter + @"pctesting");
if (!File.Exists(diskLetter + @"pctesting\mydb.sqlite"))
SQLiteConnection.CreateFile(diskLetter + @"pctesting\mydb.sqlite");
//if (!File.Exists(@"mydb.sqlite"))
// SQLiteConnection.CreateFile(@"mydb.sqlite");//файл хранится в папке с IIS Express
string ansString = "denied";
sql.Open();
setConnection();
SQLiteCommand sc = new SQLiteCommand("SELECT COUNT(*) FROM USER WHERE name = '" + name + "' AND password = '" + password + "';", sql);
if (Convert.ToInt32(sc.ExecuteScalar()) > 0)
{
sc = new SQLiteCommand("SELECT COUNT(*) FROM COMPUTER WHERE MAC = '" + compMAC + "';", sql);
if (Convert.ToInt32(sc.ExecuteScalar()) == 0)
{
execute("INSERT INTO COMPUTER VALUES(NULL, '" + compMAC + "' , '" + compName + "');");
}
sc = new SQLiteCommand(String.Format("SELECT COUNT(*) FROM USER WHERE name = '{0}' AND password = '{1}' AND admin = 1;", name, password), sql);
if (Convert.ToInt32(sc.ExecuteScalar()) > 0)
ansString = "admin";
else
ansString = "user";
}
sql.Close();
return ansString;
}
public void execute(string query)
{
SQLiteCommand sc = new SQLiteCommand(query, sql);
sc.ExecuteNonQuery();
}
public void setConnection()
{
execute("CREATE TABLE IF NOT EXISTS COMPUTER(id INTEGER PRIMARY KEY AUTOINCREMENT, MAC TEXT, name TEXT);"
+ "CREATE TABLE IF NOT EXISTS USER(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, password TEXT, admin INTEGER);");
SQLiteCommand sc = new SQLiteCommand("SELECT COUNT(*) FROM USER WHERE admin = 1;", sql);
if (Convert.ToInt32(sc.ExecuteScalar()) == 0)
{
execute("INSERT INTO USER VALUES(NULL, '" + adminLogin + "', '" + adminPassword + "', " + 1 + ");");
}
execute("CREATE TABLE IF NOT EXISTS FILE(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, path TEXT, extension TEXT, time INTEGER, type TEXT, computerID INTEGER, userID INTEGER, " +
fk_computer + ", " +
fk_user + ";");
execute("CREATE TABLE IF NOT EXISTS TRAFFIC(id INTEGER PRIMARY KEY AUTOINCREMENT, URL TEXT, host TRXT, referer TEXT, time INTEGER, refID INTEGER, computerID INTEGER, userID INTEGER, theme TEXT, " +
fk_computer + ", " +
fk_user + ";");
//execute("CREATE TABLE IF NOT EXISTS TRAFFIC_THEME(value TEXT, trafficID INTEGER, themeID INTEGER, CONSTRAINT trafficFK FOREIGN KEY (trafficID) REFERENCES TRAFFIC(id), CONSTRAINT themeFK FOREIGN KEY (themeID) REFERENCES THEME(id));");
//execute("CREATE TABLE IF NOT EXISTS THEME(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, trafficID INTEGER, CONSTRAINT trafficFK FOREIGN KEY (trafficID) REFERENCES TRAFFIC(id));");
execute("CREATE TABLE IF NOT EXISTS CHARACTERISTIC(id INTEGER PRIMARY KEY AUTOINCREMENT, time TEXT, teapots INTEGER, RAM TEXT, freeRAM TEXT, CPU TEXT, VideoRAM TEXT, computerID INTEGER, " +
fk_computer + ");");
execute("CREATE TABLE IF NOT EXISTS PROCESS(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, startTime INTEGER, finishTime INTEGER, allTime INTEGER, computerID INTEGER, userID INTEGER, " +
fk_computer + ", " +
fk_user + ";");
execute("CREATE TABLE IF NOT EXISTS ACTIVITY(id INTEGER PRIMARY KEY AUTOINCREMENT, allTime INTEGER, activeTime INTEGER, passiveTime INTEGER, computerID INTEGER, userID INTEGER, " +
fk_computer + ", " +
fk_user + ";");
}
//public DataTable getThemeTable()
//{
// if (!Directory.Exists(diskLetter + @"pctesting"))
// Directory.CreateDirectory(diskLetter + @"pctesting");
// if (!File.Exists(diskLetter + @"pctesting\mydb.sqlite"))
// SQLiteConnection.CreateFile(diskLetter + @"pctesting\mydb.sqlite");
// sql.Open();
// setConnection();
// SQLiteCommand sc = new SQLiteCommand("SELECT NAME FROM THEME ORDER BY ID;", sql);
// DataTable dt = new DataTable();
// dt.Load(sc.ExecuteReader());
// sql.Close();
// return dt;
//}
public DataTable getTrafficTable()
{
if (!Directory.Exists(diskLetter + @"pctesting"))
Directory.CreateDirectory(diskLetter + @"pctesting");
if (!File.Exists(diskLetter + @"pctesting\mydb.sqlite"))
SQLiteConnection.CreateFile(diskLetter + @"pctesting\mydb.sqlite");
sql.Open();
setConnection();
SQLiteCommand sc = new SQLiteCommand("SELECT * FROM TRAFFIC ORDER BY ID;", sql);
DataTable dt = new DataTable();
dt.Load(sc.ExecuteReader());
sql.Close();
return dt;
}
//public DataTable getTTTable()
//{
// if (!Directory.Exists(diskLetter + @"pctesting"))
// Directory.CreateDirectory(diskLetter + @"pctesting");
// if (!File.Exists(diskLetter + @"pctesting\mydb.sqlite"))
// SQLiteConnection.CreateFile(diskLetter + @"pctesting\mydb.sqlite");
// sql.Open();
// setConnection();
// SQLiteCommand sc = new SQLiteCommand("SELECT * FROM TRAFFIC_THEME;", sql);
// DataTable dt = new DataTable();
// dt.Load(sc.ExecuteReader());
// sql.Close();
// return dt;
//}
private int[] selectIDs(string comp, string user)
{
using (SQLiteConnection sql = new SQLiteConnection("DataSource = " + diskLetter + @"pctesting\mydb.sqlite;Version=3"))
{
sql.Open();
SQLiteCommand scComp = new SQLiteCommand("SELECT ID FROM COMPUTER WHERE MAC = '" + comp + "';", sql);
SQLiteCommand scUser = new SQLiteCommand("SELECT ID FROM USER WHERE NAME = '" + user + "';", sql);
return new int[] { Convert.ToInt32(scComp.ExecuteScalar()), Convert.ToInt32(scUser.ExecuteScalar()) };
}
}
public void saveFileDataToDB(string name, string path, string ext, long time, string type, string comp, string user)
{
sql.Open();
int[] IDs = selectIDs(comp, user);
execute(String.Format("INSERT INTO FILE VALUES( NULL, '{0}', '{1}', '{2}', {3}, '{4}', {5}, {6});", name, path, ext, time, type, IDs[0], IDs[1]));
sql.Close();
}
public void saveTrafficDataToDB(string URL, string host, string referer, long time, string comp, string user)
{
SQLiteCommand sc;
sql.Open();
int[] IDs = selectIDs(comp, user);
int refId = 0;
if (referer.Length > 0)
{
sc = new SQLiteCommand("SELECT ID FROM TRAFFIC WHERE URL = '" + referer + "' AND TIME IN(SELECT MAX(TIME) FROM TRAFFIC WHERE URL = '" + referer + "');", sql);
refId = Convert.ToInt32(sc.ExecuteScalar());
sc = new SQLiteCommand("SELECT MAX(TIME) FROM TRAFFIC WHERE URL = '" + referer + "';", sql);
if (refId == 0)
return;
}
//string Parameters = "cat=ling-themurl&url=" + URL;
//byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
//string resp = GetResponse("http://www.keva.ru/?cat=ling-themurl", bytes, "POST");
//IHtmlDocument angle = new HtmlParser().Parse(resp);
//IHtmlCollection<IElement> themeCol = angle.QuerySelectorAll("table ul.tree > li a");
//IHtmlCollection<IElement> valCol = angle.QuerySelectorAll("table ul.tree > li a sup");
//string[] theme = new string[themeCol.Length], val = new string[themeCol.Length];
//string[] theme, val;
////sc = new SQLiteCommand("SELECT MAX(ID) FROM TRAFFIC;", sql);
////int traffId = Convert.ToInt32(sc.ExecuteScalar());
////for (int i = 0; i < themeCol.Length; i++)
////{
//int i = 0;
//try
//{
// val[i] = valCol[i].TextContent;
// theme[i] = themeCol[i].TextContent.Remove(themeCol[i].TextContent.IndexOf(val[i]));
//}
//catch(ArgumentNullException ex)
//{
// theme[i] = "";
//}
//sc = new SQLiteCommand("SELECT COUNT(*) FROM THEME WHERE NAME = '" + theme[i] + "';", sql);
//if (Convert.ToInt32(sc.ExecuteScalar()) > 0)
//{
// sc = new SQLiteCommand("SELECT ID FROM THEME WHERE NAME = '" + theme[i] + "';", sql);
//}
//else
//{
// execute(String.Format("INSERT INTO THEME VALUES( NULL, '{0}');", theme[i]));
// sc = new SQLiteCommand("SELECT MAX(ID) FROM THEME;", sql);
//}
//int themeId = Convert.ToInt32(sc.ExecuteScalar());
//execute(String.Format("INSERT INTO TRAFFIC_THEME VALUES('{0}', {1}, {2});", val[i], traffId, themeId));
//sql.Close();
execute(String.Format("INSERT INTO TRAFFIC VALUES( NULL, '{0}', '{1}', '{2}', {3}, {4}, {5}, {6}, '{7}');", URL, host, referer, time, refId, IDs[0], IDs[1], ""));
return;
//}
}
public void SaveActivityToDB(TimeSpan AllTime, TimeSpan ActivityTime, TimeSpan NotActivityTime, string comp, string user)
{
sql.Open();
int[] IDs = selectIDs(comp, user);
execute("INSERT INTO ACTIVITY VALUES(NULL, " + (long)AllTime.Ticks / 10000 + ", " + (long)ActivityTime.Ticks / 10000 + ", " + (long)NotActivityTime.Ticks / 10000 + ", " + IDs[0] + ", " + IDs[1] + ");");
sql.Close();
}
public void SaveProcessesToDB(string Name, DateTime StartTime, DateTime FinishTime, TimeSpan GeneralTime, string comp, string user)
{
sql.Open();
int[] IDs = selectIDs(comp, user);
execute("INSERT INTO PROCESS VALUES( NULL, '" + Name + "', " + (long)StartTime.Ticks / 10000 + ", " + (long)FinishTime.Ticks / 10000 + ", " + (long)GeneralTime.Ticks / 10000 + "," + IDs[0] + "," + IDs[1] + ");");
sql.Close();
}
public void SaveTestCharacteristic(DateTime time, int teapots, string RAM, string freeRAM, string CPU, string VideoRAM, string comp, string user)
{
sql.Open();
int[] IDs = selectIDs(comp, user);
execute("INSERT INTO CHARACTERISTIC VALUES( NULL, " + (long)time.Ticks / 10000 + ", " + teapots + ", '" + RAM + "', '" + freeRAM + "', '" + CPU + "', '" + VideoRAM + "', " + IDs[0] + ");");
sql.Close();
}
public List<List<string>> FindInternetActivity(string UserName)
{
return Usreport.FindInternetActivity(UserName);
}
public List<List<string>> FindFileActivity(string UserName)
{
return Usreport.FindFileActivity(UserName);
}
private string GetResponse(string url, byte[] byteData, string method)
{
if (url == null) return null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11";
request.Method = method;
if (byteData.Length > 0)
{
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteData.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(byteData, 0, byteData.Length);
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
Encoding responseEncoding = Encoding.Default;
if (!String.IsNullOrEmpty(response.CharacterSet))
{
responseEncoding = Encoding.GetEncoding(response.CharacterSet);
}
using (var reader = new StreamReader(responseStream, responseEncoding, true))
{
return reader.ReadToEnd();
}
}
}
}
}