Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11.  
  12. namespace Retel.FactoryLayer.Backend.PLCFileService
  13. {
  14. class Panel : HTTPServer
  15. {
  16. public Panel(String ip, String username, String password, List<Job> jobs, int timeoutAttempts, bool statistics, double statisticsIntervalInHours, List<String> mailList)
  17. {
  18. base.ip = ip;
  19. base.username = username;
  20. base.password = password;
  21. base.jobs = jobs;
  22. base.timeoutAttempts = timeoutAttempts;
  23. base.statistics = statistics;
  24. base.statisticsIntervalInHours = statisticsIntervalInHours;
  25. base.mailList = mailList;
  26. }
  27.  
  28. public override void Schedule()
  29. {
  30. if (statistics && nextStatistic < DateTime.Now)
  31. {
  32. SendStatistic();
  33. nextStatistic = DateTime.Now.AddHours(statisticsIntervalInHours);
  34. }
  35. int threadId = Thread.CurrentThread.ManagedThreadId;
  36. //if the next execution time of a job is before the current time, execute the job
  37. foreach (Job job in this.jobs)
  38. {
  39. if (PLCFileService.watchdog != null)
  40. {
  41. PLCFileService.watchdog.NotifyThreadHeartbeat(threadId);
  42. }
  43.  
  44. if (job.GetNextExecutionDate() < DateTime.Now)
  45. {
  46. PLCFileService.msgLog.LogMessage("Executing job: " + job.GetName(), Common.MessageLogger.LogLevel.DEBUG);
  47. job.AddExecute();
  48. if (this.sessionkey == null)
  49. {
  50. this.sessionkey = GetSessionKey();
  51. if (this.sessionkey == null)
  52. {
  53. job.SetNextExecutionDate();
  54. continue;
  55. }
  56. }
  57. Dictionary<String, DateTime> data;
  58. try
  59. {
  60. data = GetData(arguments: job.GetArguments());
  61. }
  62. catch (Exception)
  63. {
  64. data = null;
  65. }
  66.  
  67.  
  68. if (PLCFileService.watchdog != null)
  69. {
  70. PLCFileService.watchdog.NotifyThreadHeartbeat(threadId);
  71. }
  72.  
  73.  
  74. if (data == null)
  75. {
  76. PLCFileService.msgLog.LogMessage("Failed to get data for: " + this.ip + ", skipping job.", Common.MessageLogger.LogLevel.WARN);
  77. job.SetNextExecutionDate();
  78. continue;
  79. }
  80.  
  81. job.Execute(this.ip, data, this.sessionkey);
  82.  
  83. }
  84. }
  85. }
  86.  
  87. protected override Dictionary<string, DateTime> GetData(int tries = 1, Dictionary<string, string> arguments = null)
  88. {
  89. //try as many times at timeout attempts are configured, if it failed send a timeout mail
  90. if (tries > timeoutAttempts)
  91. {
  92. SendTimeoutMail();
  93. return null;
  94. }
  95.  
  96. int threadId = Thread.CurrentThread.ManagedThreadId;
  97. if (PLCFileService.watchdog != null)
  98. {
  99. PLCFileService.watchdog.NotifyThreadHeartbeat(threadId);
  100. }
  101.  
  102.  
  103. Dictionary<String, DateTime> result = new Dictionary<String, DateTime>();
  104. string html = String.Empty;
  105. try
  106. {
  107. //send request to userfiles site to grab the userfiles of the html file
  108. WebRequest request = WebRequest.Create("http://" + this.ip + "/Browse.html");
  109. request.Headers.Add("Cookie", "coming_from_login=false; " + this.sessionkey);
  110. request.Credentials = CredentialCache.DefaultCredentials;
  111. ((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";
  112. request.Timeout = 5000;
  113. request.Method = "GET";
  114. WebResponse response = request.GetResponse();
  115.  
  116. //convert response to html
  117. Stream data = response.GetResponseStream();
  118.  
  119. using (StreamReader sr = new StreamReader(data))
  120. {
  121. html = sr.ReadToEnd();
  122. }
  123. }
  124. catch (Exception)
  125. {
  126. PLCFileService.msgLog.LogMessage("Timeout while retrieving data. (" + tries + " out of " + timeoutAttempts + ")", Common.MessageLogger.LogLevel.WARN);
  127. this.sessionkey = GetSessionKey();
  128. return GetData(tries + 1);
  129.  
  130. }
  131.  
  132. //get all folders on the webserver
  133. Dictionary<string, string> folders = ListFolders(html);
  134.  
  135. //check which of the folders will be downloaded
  136. foreach(string folder in arguments["folders"].Split(';'))
  137. {
  138. if(folders.Keys.Contains(folder))
  139. {
  140. //get all files of that folder
  141. Dictionary<string, DateTime> files = GetFiles(folders[folder]);
  142. foreach (string file in files.Keys)
  143. result.Add(file, files[file]);
  144. }
  145. }
  146.  
  147. if (result.Count() == 0)
  148. {
  149. PLCFileService.msgLog.LogMessage("Timeout while retrieving data, requesting new sessionkey. (" + tries + " out of " + timeoutAttempts + ")", Common.MessageLogger.LogLevel.WARN);
  150. this.sessionkey = GetSessionKey();
  151. return GetData(tries + 1);
  152. }
  153.  
  154.  
  155. return result;
  156. }
  157.  
  158. private Dictionary<string, DateTime> GetFiles(string link)
  159. {
  160. //get all files from the link to the folder
  161. Dictionary<string, DateTime> result = new Dictionary<string, DateTime>();
  162.  
  163. String html;
  164.  
  165. WebRequest request = WebRequest.Create("http://" + this.ip + link);
  166. request.Headers.Add("Cookie", "coming_from_login=false; " + this.sessionkey);
  167. request.Credentials = CredentialCache.DefaultCredentials;
  168. ((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";
  169. request.Timeout = 5000;
  170. request.Method = "GET";
  171. WebResponse response = request.GetResponse();
  172.  
  173. //convert response to html
  174. Stream data = response.GetResponseStream();
  175.  
  176. using (StreamReader sr = new StreamReader(data))
  177. {
  178. html = sr.ReadToEnd();
  179. }
  180.  
  181. String pattern = @"<TR>\r\n<TD><IMG(.+?)TR>";
  182. MatchCollection matches = Regex.Matches(html, pattern, RegexOptions.Singleline);
  183.  
  184. foreach(Match m in matches)
  185. {
  186. //check if the link is a file, if yes get the date and name and return it
  187. bool isFile = Regex.IsMatch(m.Value, @"\/Images\/File.gif", RegexOptions.Singleline);
  188.  
  189. if (isFile)
  190. {
  191. String name = Regex.Match(m.Value, @"<A href='(.+?)\?UP", RegexOptions.Singleline).Groups[1].Value;
  192. string dateString = Regex.Match(m.Value, "... ... .. ..:..:.. ....", RegexOptions.Singleline).Groups[0].Value;
  193.  
  194. DateTime date = DateTime.ParseExact(dateString, "ddd MMM dd HH:mm:ss yyyy", CultureInfo.InvariantCulture);
  195. result.Add(name, date);
  196.  
  197. }
  198.  
  199. }
  200.  
  201.  
  202. return result;
  203. }
  204.  
  205. private Dictionary<string, string> ListFolders(string data)
  206. {
  207. Dictionary<string, string> result = new Dictionary<string, string>();
  208. Dictionary<string, string> folderQueue = new Dictionary<string, string>();
  209.  
  210. //search the html for hyperlinks directed to the root folders
  211. string pattern = @"<TR class='Storage'(.*?)TR>";
  212. MatchCollection matches = Regex.Matches(data, pattern, RegexOptions.Singleline);
  213. Dictionary<string, string> folders = new Dictionary<string, string>();
  214.  
  215. foreach (Match m in matches)
  216. {
  217. //Get the Filename and links of the root folders
  218. String name = Regex.Match(m.Value, @"<font face=""Tahoma"" color=""#000000"" size=""2"">(.+?)</font>").Groups[1].Value;
  219. String link = Regex.Match(m.Value, "<A HREF=\"(.+?)\"><b>").Groups[1].Value;
  220. folderQueue.Add(name, link);
  221. }
  222.  
  223.  
  224. String folderData;
  225. //search all folders for new folders, if new folders are found they are placed in the folderQueue and after a folder is searched it will move to the result dictionary
  226. while (folderQueue.Count() > 0)
  227. {
  228. KeyValuePair<string, string> folder = folderQueue.First();
  229.  
  230. //send request to folder site to grab the folders of the html file
  231. WebRequest request = WebRequest.Create("http://" + this.ip + folder.Value);
  232. request.Headers.Add("Cookie", "coming_from_login=false; " + this.sessionkey);
  233. request.Credentials = CredentialCache.DefaultCredentials;
  234. ((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";
  235. request.Timeout = 5000;
  236. request.Method = "GET";
  237. WebResponse response;
  238. try
  239. {
  240. response = request.GetResponse();
  241. }
  242. catch(Exception)
  243. {
  244. folderQueue.Remove(folder.Key);
  245. continue;
  246. }
  247.  
  248.  
  249. //convert response to html
  250. Stream stream = response.GetResponseStream();
  251.  
  252. using (StreamReader sr = new StreamReader(stream))
  253. {
  254. folderData = sr.ReadToEnd();
  255. }
  256.  
  257. pattern = @"<TR>\r\n<TD><IMG(.+?)TR>";
  258. matches = Regex.Matches(folderData, pattern, RegexOptions.Singleline);
  259.  
  260. //find all links with a folder picture to identify between folders and files
  261. foreach(Match m in matches)
  262. {
  263. bool isFolder = Regex.IsMatch(m.Value, @"\/Images\/Directory.gif", RegexOptions.Singleline);
  264. if(isFolder)
  265. {
  266. //filter name and link on webserver, remove the "go back" folder -> Folder with name ".."
  267. string name = Regex.Match(m.Value, @"BROWSE'>(.+?)<\/A>", RegexOptions.Singleline).Groups[1].Value;
  268. string link = Regex.Match(m.Value, @"<A href='(?!\/browse.html)(.+?)'>", RegexOptions.Singleline).Groups[1].Value;
  269. if(!name.Equals("") && !name.Equals("..") && !link.Equals(""))
  270. folderQueue.Add(name, link);
  271. }
  272. }
  273.  
  274.  
  275.  
  276. result.Add(folder.Key, folder.Value);
  277. folderQueue.Remove(folder.Key);
  278. }
  279.  
  280. return result;
  281. }
  282. }
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement