Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
 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);
         }
     }
 }
Exemplo n.º 3
0
        public VPN_Stability_Monitor(IPMonitor ipMon)
        {
            SessionInformationStorage sis = new SessionInformationStorage();

            try
            {
                mi        = sis.Deserialize(FILENAME, this);
                ipMonitor = ipMon;
            }
            catch
            {
                mi = new MonitorInformation();
            }
        }
Exemplo n.º 4
0
 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}";
     }
 }