public void Serialize(MonitorInformation mi, string fileName) { // Create a hashtable of values that will eventually be serialized. // To serialize the hashtable and its key/value pairs, // you must first open a stream for writing. // In this case, use a file stream. FileStream fs = new FileStream(fileName, FileMode.Create); // Construct a BinaryFormatter and use it to serialize the data to the stream. BinaryFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(fs, mi); } catch (SerializationException e) { Console.WriteLine("Failed to serialize. Reason: " + e.Message); throw; } catch (IOException e) { Console.WriteLine("Failed to access file: " + e.Message); throw; } finally { fs.Close(); } }
public bool VerifyVPNIP(MonitorInformation mi) { if (IsValidIP(mi.VPNIP)) { if (IsSimilarTo(mi.VPNIP, mi.HomeIP, 9)) { mi.VPNIP = ""; return(false); } else if (IsSimilarTo(mi.CurrentIP, mi.VPNIP, 3)) { return(true); } else { return(false); } } else if (!mi.CurrentIP.Equals(mi.HomeIP) && IsValidIP(mi.CurrentIP)) { mi.VPNIP = mi.CurrentIP; SessionInformationStorage sis = new SessionInformationStorage(); try { sis.Serialize(mi, FILENAME); } catch (IOException e) { Console.WriteLine("Failed to access file: " + e.Message); } return(true); } else { if (!IsSimilarTo(mi.CurrentIP, mi.HomeIP, 9)) { mi.VPNIP = mi.CurrentIP; SessionInformationStorage sis = new SessionInformationStorage(); try { sis.Serialize(mi, FILENAME); } catch (IOException e) { Console.WriteLine("Failed to access file: " + e.Message); } return(true); } else { return(false); } } }
public VPN_Stability_Monitor(IPMonitor ipMon) { SessionInformationStorage sis = new SessionInformationStorage(); try { mi = sis.Deserialize(FILENAME, this); ipMonitor = ipMon; } catch { mi = new MonitorInformation(); } }
void IncrementThresholdIfUnstable(MonitorInformation mi) { if (mi.CurrentIP != null) { if (IsValidIP(mi.CurrentIP) && IsSimilarTo(mi.CurrentIP, mi.VPNIP, 3)) { SetShutdownThresholdCounter(0); } else if (mi.CurrentIP.Equals(mi.HomeIP) || !IsSimilarTo(mi.CurrentIP, mi.VPNIP, 3)) { SetShutdownThresholdCounter(totalWarnings + 1); } Stability = thresholdReached ? $"Connection unstable after {totalWarnings} tries." : $"VPN active {mi.VPNIP}, connection stable. Home IP: {mi.HomeIP}"; } }