-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilityIO.cs
344 lines (328 loc) · 13.7 KB
/
UtilityIO.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Xml.Serialization;
using Xceed.Wpf.AvalonDock;
using Xceed.Wpf.AvalonDock.Layout.Serialization;
namespace LungMorphApp
{
public static class UtilityIO
{
public static string[] getFiles(this string SourceFolder, string Filter, SearchOption searchOption)
{
var alFiles = new System.Collections.ArrayList();
string[] MultipleFilters = Filter.Split('|');
foreach (string FileFilter in MultipleFilters) {
alFiles.AddRange(Directory.GetFiles(SourceFolder, FileFilter, searchOption));
}
return (string[])alFiles.ToArray(typeof(string));
}
//public static T DeepCopy<T>(T other)
//{
// using (var ms = new MemoryStream()) {
// var formatter = new BinaryFormatter();
// formatter.Serialize(ms, other);
// ms.Position=0;
// return (T)formatter.Deserialize(ms);
// }
//}
//public static async Task<bool> Save(this UISettings ui, string filename = "LastSaved.xsetting")
//{
// ui.ShowBusySign("Saving Settings...");
// bool result = await Task<bool>.Factory.StartNew(() => {
// try {
// using (FileStream fsUserSetting = File.Create(filename)) {
// var formatter = new XmlSerializer(ui.GetType());
// formatter.Serialize(fsUserSetting, ui);
// }
// return true;
// } catch { MessageBox.Show("Failed to save the current settings!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return false; }
// });
// ui.StopBusySign(); return result;
//}
public static async Task<bool> SaveCrypt(this UISettings ui, string filename = "LastSaved.xsetting")
{
ui.ShowBusySign("Saving Settings...");
try {
await Task.Factory.StartNew(() => {
var aUE = new UnicodeEncoding();
byte[] key = aUE.GetBytes("password");
var RMCrypto = new RijndaelManaged();
using (var fs = File.Open(filename, FileMode.Create)) {
using (var cs = new CryptoStream(fs, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write)) {
var xml = new XmlSerializer(ui.GetType());
xml.Serialize(cs, ui);
}
}
});
return true;
} catch {
MessageBox.Show("Failed to save the current settings!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
} finally { ui.StopBusySign(); }
}
//public static async Task<bool> Load(this UISettings ui, string filename = "LastSaved.xsetting")
//{
// ui.ShowBusySign("Loading Settings...");
// bool result = await Task<bool>.Factory.StartNew(() => {
// try {
// var deserializer = new XmlSerializer(typeof(UISettings));
// using (var reader = new StreamReader(filename)) {
// object obj = deserializer.Deserialize(reader);
// ui.UpdateAll((UISettings)obj);
// }
// if (MessageBox.Show($"Yes: Update the project name to the current date. => {DateTime.Now:yyyy-MM-dd}\nNo: Use the previouly saved. => {ui.ProjectName}", "Question", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No)==MessageBoxResult.Yes) {
// ui.ProjectName=$"{DateTime.Now:yyyy-MM-dd}";
// }
// return true;
// } catch { MessageBox.Show("Failed to load the selected settings!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return false; }
// });
// ui.StopBusySign(); return result;
//}
public static async Task<bool> LoadCrypt(this UISettings ui, string filename = "LastSaved.xsetting")
{
ui.ShowBusySign("Loading Settings...");
try {
await Task.Factory.StartNew(() => {
var deserializer = new XmlSerializer(typeof(UISettings));
using (var fs = new FileStream(filename, FileMode.Open)) {
using (var sr = new StreamReader(fs)) {
var aUE = new UnicodeEncoding();
byte[] key = aUE.GetBytes("password");
using (var RMCrypto = new RijndaelManaged()) {
using (var cs = new CryptoStream(fs, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read)) {
object obj = deserializer.Deserialize(cs);
ui.UpdateAll((UISettings)obj);
}
}
}
}
});
if (Path.GetFileNameWithoutExtension(filename).Substring(0, 1)=="~") {
ui.WorkDirectory=Directory.GetCurrentDirectory()+"\\"+Path.GetFileNameWithoutExtension(filename).Substring(1);
ui.ProjectName=$"{DateTime.Now:yyyy-MM-dd}";
} else if ((ui.ProjectName!=$"{DateTime.Now:yyyy-MM-dd}")&&(MessageBox.Show($"Yes: Update the project name to the current date. => {DateTime.Now:yyyy-MM-dd}\nNo: Use the previouly saved. => {ui.ProjectName}", "Would you like to update project name to the current date?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No)==MessageBoxResult.Yes))
ui.ProjectName=$"{DateTime.Now:yyyy-MM-dd}";
return true;
} catch {
MessageBox.Show("Failed to load the selected settings!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
} finally { ui.StopBusySign(); }
}
//public static async Task<bool> Save(this DockingManager dockingManager, UISettings ui, string filename = "LastSaved.xlayout")
//{
// ui.ShowBusySign("Saving Settings...");
// try {
// var serializer = new XmlLayoutSerializer(dockingManager);
// using (var stream = new StreamWriter(filename)) { serializer.Serialize(stream); }
// ui.StopBusySign(); return true;
// } catch { ui.StopBusySign(); MessageBox.Show("Failed to save the current layout!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return false; }
//}
public static async Task<bool> SaveCrypt(this DockingManager dockingManager, UISettings ui, string filename = "LastSaved.xlayout")
{
ui.ShowBusySign("Saving Settings...");
try {
var aUE = new UnicodeEncoding();
byte[] key = aUE.GetBytes("password");
var RMCrypto = new RijndaelManaged();
using (var fs = File.Open(filename, FileMode.Create)) {
using (var cs = new CryptoStream(fs, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write)) {
var serializer = new XmlLayoutSerializer(dockingManager);
serializer.Serialize(cs);
}
}
return true;
} catch {
MessageBox.Show("Failed to save the current layout!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
} finally { ui.StopBusySign(); }
}
//public static async Task<bool> Load(this DockingManager dockingManager, UISettings ui, string filename = "LastSaved.xlayout")
//{
// ui.ShowBusySign("Saving Settings...");
// try {
// var serializer = new XmlLayoutSerializer(dockingManager);
// using (var stream = new StreamReader(filename)) { serializer.Deserialize(stream); }
// ui.StopBusySign(); return true;
// } catch { ui.StopBusySign(); MessageBox.Show("Failed to load the selected layout!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return false; }
//}
public static async Task<bool> LoadCrypt(this DockingManager dockingManager, UISettings ui, string filename = "LastSaved.xlayout")
{
ui.ShowBusySign("Saving Settings...");
try {
var deserializer = new XmlLayoutSerializer(dockingManager);
using (var fs = new FileStream(filename, FileMode.Open)) {
using (var sr = new StreamReader(fs)) {
var aUE = new UnicodeEncoding();
byte[] key = aUE.GetBytes("password");
using (var RMCrypto = new RijndaelManaged()) {
using (var cs = new CryptoStream(fs, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read)) {
deserializer.Deserialize(cs);
}
}
}
}
return true;
} catch {
MessageBox.Show("Failed to load the selected layout!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
} finally { ui.StopBusySign(); }
}
public static bool ValidateSystemTime(string ExpirationDate)
{
try {
return string.Compare($"{DateTime.Now:yy-MM}", ExpirationDate, StringComparison.Ordinal)<=0;
} catch {
//throw new Exception("Could not acccess current system time.");
return false;
}
}
public static bool ValidateInternetTimeNtp(string ExpirationDate)
{
string date = "50-12"; //2050-12
foreach (string ntpServer in new string[] { "time.nist.gov", "time.windows.com" }) {
try {
var ntpData = new byte[48];// NTP message size - 16 bytes of the digest (RFC 2030)
//Setting the Leap Indicator, Version Number and Mode values
ntpData[0]=0x1B; //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
var addresses = Dns.GetHostEntry(ntpServer).AddressList;
var ipEndPoint = new IPEndPoint(addresses[0], 123); //The UDP port number assigned to NTP is 123
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//NTP uses UDP
socket.Connect(ipEndPoint);
socket.ReceiveTimeout=2000; //Stops code hang if NTP is blocked
socket.Send(ntpData);
socket.Receive(ntpData);
socket.Close();
//Offset to get to the "Transmit Timestamp" field (time at which the reply
//departed the server for the client, in 64-bit timestamp format."
const byte serverReplyTime = 40;
ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime); //Get the seconds part
ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime+4); //Get the seconds fraction
//Convert From big-endian to little-endian
intPart=SwapEndianness(intPart);
fractPart=SwapEndianness(fractPart);
var milliseconds = (intPart*1000)+((fractPart*1000)/0x100000000L);
//**UTC** time
var networkDateTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);
date=networkDateTime.ToLocalTime().ToString("yy-MM");
return (string.Compare(date, ExpirationDate, StringComparison.Ordinal)<=0);
} catch { }
}
return false;
}
static uint SwapEndianness(ulong x)
{
return (uint) (((x & 0x000000ff) << 24) +
((x & 0x0000ff00) << 8) +
((x & 0x00ff0000) >> 8) +
((x & 0xff000000) >> 24));
}
public static bool ValidateInternetTimeHttp(string ExpirationDate)
{
string yyMM = "50-12"; // 2050-12
try {
//DateTime dateTime = DateTime.MinValue;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi?lzbc=siqm9b");
request.Method="GET";
request.Accept="text/html, application/xhtml+xml, */*";
request.UserAgent="Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
request.ContentType="application/x-www-form-urlencoded";
request.CachePolicy=new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore); //No caching
request.ReadWriteTimeout=2000; //milliseconds
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode==HttpStatusCode.OK) {
StreamReader stream = new StreamReader(response.GetResponseStream());
string html = stream.ReadToEnd();//<timestamp time=\"1395772696469995\" delay=\"1395772696469995\"/>
string time = System.Text.RegularExpressions.Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
double milliseconds = Convert.ToInt64(time)/1000.0;
yyMM=new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToString("yy-MM");
}
return string.Compare(yyMM, ExpirationDate, StringComparison.Ordinal)<=0;
} catch { return false; }
}
}
/// <summary>
/// TcpClientWithTimeout is used to open a TcpClient connection, with a
/// user definable connection timeout in milliseconds (1000=1second)
/// Use it like this:
/// TcpClient connection = new TcpClientWithTimeout('127.0.0.1',80,1000).Connect();
/// </summary>
public class TcpClientWithTimeout
{
protected string _hostname;
protected int _port;
protected int _timeout_milliseconds;
protected TcpClient connection;
protected bool connected;
protected Exception exception;
public TcpClientWithTimeout(string hostname, int port, int timeout_milliseconds)
{
_hostname=hostname;
_port=port;
_timeout_milliseconds=timeout_milliseconds;
}
public TcpClient Connect()
{
// kick off the thread that tries to connect
connected=false;
exception=null;
Thread thread = new Thread(new ThreadStart(BeginConnect));
thread.IsBackground=true; // So that a failed connection attempt
// wont prevent the process from terminating while it does the long timeout
thread.Start();
// wait for either the timeout or the thread to finish
thread.Join(_timeout_milliseconds);
if (connected==true) {
// it succeeded, so return the connection
thread.Abort();
return connection;
}
if (exception!=null) {
// it crashed, so return the exception to the caller
thread.Abort();
throw exception;
} else {
// if it gets here, it timed out, so abort the thread and throw an exception
thread.Abort();
string message = string.Format("TcpClient connection to {0}:{1} timed out",
_hostname, _port);
throw new TimeoutException(message);
}
}
protected void BeginConnect()
{
try {
connection=new TcpClient(_hostname, _port);
// record that it succeeded, for the main thread to return to the caller
connected=true;
} catch (Exception ex) {
// record the exception for the main thread to re-throw back to the calling code
exception=ex;
}
}
}
[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(bool)value;
}
}
}