Advertisement
tolikpunkoff

NetSettings example class

May 14th, 2018
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics;
  5. using System.Data;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Security.Cryptography;
  9.  
  10. namespace TestInternetConnect
  11. {
  12.     public enum NetConnectionType
  13.     {
  14.         NoProxy = 0,
  15.         SystemProxy = 1,
  16.         ManualProxy = 2
  17.     }
  18.     public enum NetConfigStatus
  19.     {
  20.         OK=0,
  21.         Error=1,
  22.         ProxyPassNotDecrypted=2
  23.     }
  24.    
  25.     public class NetSettings
  26.     {
  27.         private string configFile = "";
  28.         private string TableName = "";
  29.         private DataSet dsNetConfig = new DataSet();
  30.  
  31.         public NetConnectionType ConnectionType { get; set; }
  32.         public string ProxyAddress { get; set; }
  33.         public int ProxyPort { get; set; }
  34.         public string ProxyUser { get; set; }
  35.         public string ProxyPassword { get; set; }
  36.         public bool SavePassword { get; set; }
  37.         public int ConnectionTimeout { get; set; }
  38.  
  39.         public bool ShowChars { get; set; }
  40.         public string ConfigError { get; private set; }
  41.  
  42.  
  43.         public NetSettings(string filename)
  44.         {            
  45.             configFile = filename;
  46.             TableName = this.GetType().Name;
  47.             CreateDataSet();
  48.         }
  49.        
  50.         public NetConfigStatus LoadConfig()
  51.         {
  52.             //файла нет, все по-умолчанию, потом будет создан новый
  53.             if (!File.Exists(configFile)) return NetConfigStatus.OK;
  54.  
  55.             //файл есть, пробуем загрузить в DataSet
  56.             try
  57.             {
  58.                 dsNetConfig.ReadXml(configFile);
  59.             }
  60.             catch (Exception ex)
  61.             {
  62.                 ConfigError = ex.Message;
  63.                 return NetConfigStatus.Error;
  64.             }
  65.  
  66.             //загрузка полей класса из DataSet
  67.             if (dsNetConfig.Tables[TableName].Rows.Count > 0)
  68.             {
  69.                 PropertyInfo[] properties = this.GetType().GetProperties();
  70.                 foreach (PropertyInfo pr in properties)
  71.                 {
  72.                     string propName = pr.Name;
  73.                     object propValue = dsNetConfig.Tables[TableName].Rows[0][propName];
  74.                     if (propValue.GetType() != typeof(System.DBNull))
  75.                     {
  76.                         pr.SetValue(this, propValue, null);
  77.                     }
  78.                 }
  79.                 if (!DecryptPassword())
  80.                 {
  81.                     ProxyPassword = null;
  82.                     return NetConfigStatus.ProxyPassNotDecrypted;
  83.                 }
  84.             }
  85.  
  86.             return NetConfigStatus.OK;
  87.         }
  88.         public bool SaveConfig()
  89.         {
  90.             if (!SavePassword)
  91.             {
  92.                 ProxyPassword = null;
  93.             }
  94.             EncryptPassword();
  95.            
  96.             ConfigError = null;
  97.             dsNetConfig.Tables[TableName].Rows.Clear();
  98.             DataRow dr = dsNetConfig.Tables[TableName].NewRow();
  99.            
  100.            
  101.             PropertyInfo[] properties = this.GetType().GetProperties();
  102.             foreach (PropertyInfo pr in properties)
  103.             {
  104.                 string propName = pr.Name;
  105.                 object propValue = pr.GetValue(this,null);
  106.                 dr[propName] = propValue;
  107.             }
  108.  
  109.             dsNetConfig.Tables[TableName].Rows.Add(dr);
  110.  
  111.             try
  112.             {
  113.                 dsNetConfig.WriteXml(configFile);
  114.             }
  115.             catch (Exception ex)
  116.             {
  117.                 ConfigError = ex.Message;
  118.                 return false;
  119.             }
  120.            
  121.             return true;
  122.         }
  123.        
  124.         public static bool ShowSystemProxyWindow()
  125.         {
  126.             try
  127.             {                
  128.                 Process.Start("rundll32.exe", "inetcpl.cpl, LaunchConnectionDialog");
  129.             }
  130.             catch
  131.             {
  132.                 return false;
  133.             }
  134.  
  135.             return true;
  136.         }
  137.  
  138.         private void CreateDataSet()
  139.         {            
  140.             dsNetConfig.Tables.Add(TableName);
  141.            
  142.             PropertyInfo[] properties = this.GetType().GetProperties();
  143.  
  144.             foreach (PropertyInfo pr in properties)
  145.             {
  146.                 dsNetConfig.Tables[TableName].Columns.Add(pr.Name, pr.PropertyType);
  147.             }
  148.         }
  149.  
  150.         private byte[] GetEntropy(string EntropyString)
  151.         {
  152.  
  153.             MD5 md5 = MD5.Create();
  154.             return md5.ComputeHash(Encoding.UTF8.GetBytes(EntropyString));
  155.         }
  156.  
  157.         private bool EncryptPassword()
  158.         {
  159.             if (string.IsNullOrEmpty(ProxyPassword)) return true;
  160.             if (string.IsNullOrEmpty(ProxyAddress) || string.IsNullOrEmpty(ProxyUser))
  161.                 return false;
  162.  
  163.             byte[] entropy = GetEntropy(ProxyAddress + ProxyUser);
  164.             byte[] pass = Encoding.UTF8.GetBytes(ProxyPassword);
  165.             byte[] crypted=ProtectedData.Protect(pass,
  166.                 entropy, DataProtectionScope.LocalMachine);
  167.             ProxyPassword = Convert.ToBase64String(crypted);
  168.  
  169.             return true;
  170.         }
  171.  
  172.         private bool DecryptPassword()
  173.         {
  174.             if (string.IsNullOrEmpty(ProxyPassword)) return true;
  175.             if (string.IsNullOrEmpty(ProxyAddress) || string.IsNullOrEmpty(ProxyUser))
  176.                 return false;
  177.                            
  178.             byte[] pass = null;
  179.             try
  180.             {
  181.                 pass = Convert.FromBase64String(ProxyPassword);
  182.             }
  183.             catch (Exception ex)
  184.             {
  185.                 ConfigError = ex.Message;
  186.                 return false;
  187.             }
  188.             byte[] entropy = GetEntropy(ProxyAddress + ProxyUser);
  189.  
  190.             try
  191.             {
  192.                 pass = ProtectedData.Unprotect(pass, entropy, DataProtectionScope.LocalMachine);
  193.             }
  194.             catch (Exception ex)
  195.             {
  196.                 ConfigError = ex.Message;
  197.                 return false;
  198.             }
  199.             ProxyPassword = Encoding.UTF8.GetString(pass);                
  200.            
  201.             return true;
  202.         }
  203.  
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement