Refactoring

pull/4/head
Ogoun 2 years ago
parent 99f11f87b6
commit b0a1f5ba54

@ -451,7 +451,7 @@ namespace PartitionFileStorageTest
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Error(ex, "FaultUncompressedReadTest");
} }
} }
} }

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using ZeroLevel.Services.Network.Utils;
namespace TestApp namespace TestApp
{ {
@ -7,9 +8,7 @@ namespace TestApp
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
var test = new ConcurrentDictionary<string, int>(); Console.WriteLine(Network.ExternalIP);
var v = test.GetOrAdd("sss", 1);
Console.ReadKey();
} }
} }
} }

@ -50,16 +50,6 @@ namespace ZeroLevel.SqlServer
private void Initialize() private void Initialize()
{ {
try
{
var perm = new SqlClientPermission(PermissionState.Unrestricted);
perm.Demand();
perm = null;
}
catch
{
throw new ApplicationException("No permission for access to SqlClient");
}
dbConnectionString = new SqlConnectionStringBuilder(_currentConnectionString); dbConnectionString = new SqlConnectionStringBuilder(_currentConnectionString);
dbConnectionString.Pooling = true; dbConnectionString.Pooling = true;
} }

@ -7,9 +7,9 @@ namespace ZeroLevel.Services.Cache
internal sealed class TimerCachee<T> internal sealed class TimerCachee<T>
: IDisposable : IDisposable
{ {
private sealed class CacheeItem<T> private sealed class CacheeItem<I>
{ {
public T Value { get; set; } public I Value { get; set; }
public DateTime LastAcessTime { get; set; } public DateTime LastAcessTime { get; set; }
} }

@ -4,7 +4,7 @@ using System.Security.Cryptography;
namespace ZeroLevel.Services.Encryption namespace ZeroLevel.Services.Encryption
{ {
public class RijndaelEncryptor public class AesEncryptor
: IDisposable : IDisposable
{ {
protected const int DEFAULT_STREAM_BUFFER_SIZE = 16384; protected const int DEFAULT_STREAM_BUFFER_SIZE = 16384;
@ -12,20 +12,20 @@ namespace ZeroLevel.Services.Encryption
#region Crypt fields #region Crypt fields
private Rijndael _rijndael; private Aes _aes;
private CryptoStream _stream; private CryptoStream _stream;
#endregion Crypt fields #endregion Crypt fields
public RijndaelEncryptor(Stream stream, string password, byte[] salt = null) public AesEncryptor(Stream stream, string password, byte[] salt = null)
{ {
_rijndael = Rijndael.Create(); _aes = Aes.Create();
using (var pdb = new Rfc2898DeriveBytes(password, SALT)) using (var pdb = new Rfc2898DeriveBytes(password, SALT))
{ {
_rijndael.Key = pdb.GetBytes(32); _aes.Key = pdb.GetBytes(32);
_rijndael.IV = pdb.GetBytes(16); _aes.IV = pdb.GetBytes(16);
} }
_stream = new CryptoStream(stream, _rijndael.CreateEncryptor(), CryptoStreamMode.Write); _stream = new CryptoStream(stream, _aes.CreateEncryptor(), CryptoStreamMode.Write);
} }
public void Write(byte[] data) public void Write(byte[] data)
@ -56,15 +56,15 @@ namespace ZeroLevel.Services.Encryption
public static byte[] Encrypt(byte[] plain, string password, byte[] salt = null) public static byte[] Encrypt(byte[] plain, string password, byte[] salt = null)
{ {
using (var rijndael = Rijndael.Create()) using (var aes = Aes.Create())
{ {
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT)) using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{ {
rijndael.Key = pdb.GetBytes(32); aes.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16); aes.IV = pdb.GetBytes(16);
using (var memoryStream = new MemoryStream()) using (var memoryStream = new MemoryStream())
{ {
using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write)) using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{ {
cryptoStream.Write(plain, 0, plain.Length); cryptoStream.Write(plain, 0, plain.Length);
cryptoStream.Close(); cryptoStream.Close();
@ -77,15 +77,15 @@ namespace ZeroLevel.Services.Encryption
public static byte[] Encrypt(Stream stream, string password, byte[] salt = null) public static byte[] Encrypt(Stream stream, string password, byte[] salt = null)
{ {
using (var rijndael = Rijndael.Create()) using (var aes = Aes.Create())
{ {
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT)) using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{ {
rijndael.Key = pdb.GetBytes(32); aes.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16); aes.IV = pdb.GetBytes(16);
using (var memoryStream = new MemoryStream()) using (var memoryStream = new MemoryStream())
{ {
using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write)) using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{ {
Transfer(stream, cryptoStream); Transfer(stream, cryptoStream);
cryptoStream.Close(); cryptoStream.Close();
@ -98,13 +98,13 @@ namespace ZeroLevel.Services.Encryption
public static void Encrypt(Stream inputStream, Stream outputStream, string password, byte[] salt = null) public static void Encrypt(Stream inputStream, Stream outputStream, string password, byte[] salt = null)
{ {
using (var rijndael = Rijndael.Create()) using (var aes = Aes.Create())
{ {
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT)) using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{ {
rijndael.Key = pdb.GetBytes(32); aes.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16); aes.IV = pdb.GetBytes(16);
using (var cryptoStream = new CryptoStream(outputStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write)) using (var cryptoStream = new CryptoStream(outputStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{ {
Transfer(inputStream, cryptoStream); Transfer(inputStream, cryptoStream);
cryptoStream.Close(); cryptoStream.Close();
@ -115,15 +115,15 @@ namespace ZeroLevel.Services.Encryption
public static byte[] Decrypt(byte[] cipher, string password, byte[] salt = null) public static byte[] Decrypt(byte[] cipher, string password, byte[] salt = null)
{ {
using (var rijndael = Rijndael.Create()) using (var aes = Aes.Create())
{ {
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT)) using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{ {
rijndael.Key = pdb.GetBytes(32); aes.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16); aes.IV = pdb.GetBytes(16);
using (var memoryStream = new MemoryStream()) using (var memoryStream = new MemoryStream())
{ {
using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write)) using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{ {
cryptoStream.Write(cipher, 0, cipher.Length); cryptoStream.Write(cipher, 0, cipher.Length);
cryptoStream.Close(); cryptoStream.Close();
@ -136,15 +136,15 @@ namespace ZeroLevel.Services.Encryption
public static byte[] Decrypt(Stream stream, string password, byte[] salt = null) public static byte[] Decrypt(Stream stream, string password, byte[] salt = null)
{ {
using (var rijndael = Rijndael.Create()) using (var aes = Aes.Create())
{ {
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT)) using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{ {
rijndael.Key = pdb.GetBytes(32); aes.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16); aes.IV = pdb.GetBytes(16);
using (var memoryStream = new MemoryStream()) using (var memoryStream = new MemoryStream())
{ {
using (var cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write)) using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{ {
Transfer(stream, cryptoStream); Transfer(stream, cryptoStream);
cryptoStream.Close(); cryptoStream.Close();
@ -157,13 +157,13 @@ namespace ZeroLevel.Services.Encryption
public static void Decrypt(Stream inputStream, Stream outputStream, string password, byte[] salt = null) public static void Decrypt(Stream inputStream, Stream outputStream, string password, byte[] salt = null)
{ {
using (var rijndael = Rijndael.Create()) using (var aes = Aes.Create())
{ {
using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT)) using (var pdb = new Rfc2898DeriveBytes(password, salt ?? SALT))
{ {
rijndael.Key = pdb.GetBytes(32); aes.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16); aes.IV = pdb.GetBytes(16);
using (var cryptoStream = new CryptoStream(outputStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write)) using (var cryptoStream = new CryptoStream(outputStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{ {
Transfer(inputStream, cryptoStream); Transfer(inputStream, cryptoStream);
cryptoStream.Close(); cryptoStream.Close();
@ -183,8 +183,8 @@ namespace ZeroLevel.Services.Encryption
_stream.Dispose(); _stream.Dispose();
} }
catch { } catch { }
_rijndael.Clear(); _aes.Clear();
_rijndael.Dispose(); _aes.Dispose();
} }
} }
} }

@ -126,7 +126,6 @@ namespace MemoryPools.Collections.Specialized
if (ReferenceEquals(_other, LengthIs2)) if (ReferenceEquals(_other, LengthIs2))
{ {
var done = false;
if (ItemComparer.Equals(_items.Item2, item)) if (ItemComparer.Equals(_items.Item2, item))
{ {
_items.Item2 = default; _items.Item2 = default;

@ -1,37 +1,46 @@
using System; using System;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Http;
namespace ZeroLevel.Services.Network.Utils namespace ZeroLevel.Services.Network.Utils
{ {
/// <summary> /// <summary>
/// Methods related to Network. /// Methods related to Network.
/// </summary> /// </summary>
public static class Network public static class Network
{ {
/// <summary> /// <summary>
/// Gets the external IP Address. /// Gets the external IP Address.
/// </summary> /// </summary>
/// <value>The external IP Address.</value> /// <value>The external IP Address.</value>
public static string ExternalIP public static string ExternalIP
{ {
get get
{ {
try try
{ {
WebRequest request = WebRequest.Create("http://ipv4.icanhazip.com"); using (var clientHandler = new HttpClientHandler())
using (var response = request.GetResponse()) {
using (var sr = new StreamReader(response.GetResponseStream())) clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
{ clientHandler.UseCookies = true;
return sr.ReadLine(); clientHandler.CookieContainer = new CookieContainer();
} using (var client = new HttpClient(clientHandler))
} {
catch (Exception e) var message = new HttpRequestMessage(HttpMethod.Get, "http://ipv4.icanhazip.com");
{ using (var response = client.Send(message))
Console.WriteLine("Error: " + e.Message); {
return ""; return response.Content.ReadAsStringAsync().Result;
} }
} }
} }
} }
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
return "";
}
}
}
}
} }

@ -29,6 +29,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Deterministic>False</Deterministic> <Deterministic>False</Deterministic>
<DebugType>portable</DebugType>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
@ -62,6 +63,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Deterministic>False</Deterministic> <Deterministic>False</Deterministic>
<DebugType>none</DebugType>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

Loading…
Cancel
Save

Powered by TurnKey Linux.