Skip to content

Commit

Permalink
eascache added
Browse files Browse the repository at this point in the history
  • Loading branch information
bberka committed Jan 12, 2023
1 parent cfe95b2 commit 6de2ba9
Show file tree
Hide file tree
Showing 39 changed files with 109 additions and 29 deletions.
Binary file modified .vs/EasMe/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified .vs/EasMe/v17/.futdcache.v2
Binary file not shown.
Binary file modified .vs/EasMe/v17/.suo
Binary file not shown.
Binary file modified .vs/ProjectEvaluation/easme.metadata.v5.2
Binary file not shown.
Binary file modified .vs/ProjectEvaluation/easme.projects.v5.2
Binary file not shown.
5 changes: 4 additions & 1 deletion EasMe.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
var stopwatch = new Stopwatch();
stopwatch.Start();

Console.WriteLine(EasProxy.GetNetworkInfo_Client().ToJsonString());
var enc = "MKHrRzfF9DZwPnnoGgy7lj/Pp8Nz2OGRM+JYUkTdSv/0aixC+vso9eU/k4zEivWSUQTdxap9TaVhv7MKKICHBg+nfr9Smt4pw0AXULZ0Ei6eNlJcsnn/1m37pzrcvg7yna1MZpCoeIkcfHP2SvXJTPm9q7nR0HKFxwmFu+g4+UkHMPHyrRR3p+/MdXybWYyW3T0WxIgqyzgXqQ4WOVnKwNPRp8xKExww1+kiPALJn9Xdu1dnO4vyzWQlMVy8ehNwgFWZG8kmZIDZyNfim3IdgvgqAb2p+kBzEsANN46R6wxEgvujojk0O/AtC8Kn9ULXDAqScxHGQ4yHCseC02A+RPTqKrAA/4d4pP+Clg1X3SCplSAZvKrZXKWHdTFVlX6nV4g+I0+SGRtfuasD9ZIj4QNkchTsn9lGQkbpNwXk9sDUSmjB9K2UW1LpRzdJ+EzMtg4Ui+ljYoaKeRXd2S4UBHV4Ify1lBWhgVZMk4xiKF4nQYoMLLIdalpypccevyGDVvfeJxsM37xzllopZOEgmGmQpaC1nyyHj0/2PMSDU1DC0EW/J8ufeHt+QF8B6uW/vJ9yI3virEwbPZkvHbnHpac/tCUBYHpYyoVPOVKzs/60eYnF1GdijXHLllzlFerJbFxtjUASPIDGz12F32lodAKh2Ls1xQz8n1hhI1/1kfemkQ9uhbJqYjJX7G7n5nlitFcfrf51jaFXXslpaC9PGEBEtQWQFBAvfFdqg7roxXlBXrmQenVFsEDwng8fyw49bZLz+NFwVDq3ei5CZ82jgU0CQZvrUSft8GRhcu5cLOHla2J4FboeqreQl9brxeUXDQo47PKXMsGhXwA2f+u5JjSNAvNfH3RMjki0Pw2wGj3hpq7JfZiWNdPatGrHSwSP";

var ex = new EasEncrypt("2S3EWJ4VSWU9EY3TJDEWVNP5TM6QYJHG");
Console.WriteLine(ex.DecryptString(enc));
stopwatch.Stop();
Console.WriteLine($"Elapsed time: {stopwatch.ElapsedMilliseconds} ms");

Binary file modified EasMe.Test/bin/Debug/net6.0/EasMe.Test.dll
Binary file not shown.
Binary file modified EasMe.Test/bin/Debug/net6.0/EasMe.Test.pdb
Binary file not shown.
Binary file modified EasMe.Test/bin/Debug/net6.0/EasMe.dll
Binary file not shown.
Binary file modified EasMe.Test/bin/Debug/net6.0/EasMe.pdb
Binary file not shown.
Binary file modified EasMe.Test/bin/Release/net6.0/EasMe.Test.dll
Binary file not shown.
Binary file modified EasMe.Test/bin/Release/net6.0/EasMe.Test.pdb
Binary file not shown.
Binary file modified EasMe.Test/bin/Release/net6.0/EasMe.dll
Binary file not shown.
Binary file modified EasMe.Test/bin/Release/net6.0/EasMe.pdb
Binary file not shown.
Binary file not shown.
Binary file modified EasMe.Test/obj/Debug/net6.0/EasMe.Test.dll
Binary file not shown.
Binary file modified EasMe.Test/obj/Debug/net6.0/EasMe.Test.pdb
Binary file not shown.
Binary file not shown.
Binary file modified EasMe.Test/obj/Release/net6.0/EasMe.Test.dll
Binary file not shown.
Binary file modified EasMe.Test/obj/Release/net6.0/EasMe.Test.pdb
Binary file not shown.
83 changes: 83 additions & 0 deletions EasMe/EasCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasMe
{
/// <summary>
/// Simple thread-safe variable cache helper.
/// </summary>
/// <typeparam name="TData"></typeparam>
public class EasCache<TData> where TData : class
{
public EasCache(Func<TData> action,int intervalMinutes)
{
INTERVAL = intervalMinutes;
_action = action;
_result = _action();
LAST_UPDATE = DateTime.Now;
}
private readonly int INTERVAL;
private static readonly object _locker = new object();
private DateTime LAST_UPDATE;
private readonly Func<TData> _action;
private TData _result;

public TData Get()
{
var isUpdateTime = LAST_UPDATE.AddMinutes(INTERVAL) < DateTime.Now;
if (!isUpdateTime) return _result;
lock (_locker)
{
if (!isUpdateTime) return _result;
Refresh();
}
return _result;
}
public void Refresh()
{
_result = _action();
LAST_UPDATE = DateTime.Now;
}
}
/// <summary>
/// Simple thread-safe variable cache helper. With In data is key of the caches.
/// </summary>
/// <typeparam name="TData"></typeparam>
public class EasCache<TIn, TData>
where TData : class
where TIn : class
{
public EasCache(Func<TIn,TData> action, int intervalMinutes)
{
INTERVAL = intervalMinutes;
_action = action;
LAST_UPDATE = DateTime.UnixEpoch;
}
private readonly int INTERVAL;
private static readonly object _locker = new object();
private DateTime LAST_UPDATE;
private readonly Func<TIn, TData> _action;
private Dictionary<TIn, TData> _result;

public TData? Get(TIn inVal)
{
var isUpdateTime = LAST_UPDATE.AddMinutes(INTERVAL) < DateTime.Now;
if (_result is not null && !isUpdateTime) return _result.GetValueOrDefault(inVal);
lock (_locker)
{
if (_result is not null && !isUpdateTime) return _result.GetValueOrDefault(inVal);
Refresh(inVal);
}
return _result.GetValueOrDefault(inVal); ;
}
public void Refresh(TIn inVal)
{
_result.Remove(inVal);
_result.Add(inVal,_action(inVal));
LAST_UPDATE = DateTime.Now;
}
}
}
40 changes: 16 additions & 24 deletions EasMe/EasProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,22 @@ public static class EasProxy

public static NetworkInfoModel GetNetworkInfo_Client()
{
try
{
var resp = EasAPI.SendGetRequest("https://cloudflare.com/cdn-cgi/trace", null, 3);
if (!resp.IsSuccessStatusCode) return new();
var bodyText = resp.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var bodyLines = bodyText.Split("\n");
var ip = bodyLines[2].Split("=")[1];
var loc = bodyLines[9].Split("=")[1];
var warp = bodyLines[12].Split("=")[1].StringConversion<bool>();
var gateway = bodyLines[13].Split("=")[1].StringConversion<bool>();
return new NetworkInfoModel
{
IpAddress = ip,
IsGatewayOn = gateway,
IsWarpOn = warp,
Location = loc,
};

}
catch(Exception)
{
return new();
}
}
var resp = EasAPI.SendGetRequest("https://cloudflare.com/cdn-cgi/trace", null, 3);
if (!resp.IsSuccessStatusCode) throw new Exception("Failed to get network info");
var bodyText = resp.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var bodyLines = bodyText.Split("\n");
var ip = bodyLines[2].Split("=")[1];
var loc = bodyLines[9].Split("=")[1];
var warp = bodyLines[12].Split("=")[1].StringConversion<bool>();
var gateway = bodyLines[13].Split("=")[1].StringConversion<bool>();
return new NetworkInfoModel
{
IpAddress = ip,
IsGatewayOn = gateway,
IsWarpOn = warp,
Location = loc,
};
}


public static string GetStatusCodeShortMessage(uint httpStatusCode)
Expand Down
8 changes: 5 additions & 3 deletions EasMe/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,7 @@ public static string TruncateString(this string value, int maxChars)
{
return value.Length <= maxChars ? value : value[..maxChars] + "...";
}





public static string RemoveLineEndings(this string str)
{
Expand Down Expand Up @@ -307,5 +305,9 @@ public static string FromBase64String(this string base64String)
var base64EncodedBytes = System.Convert.FromBase64String(base64String);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
public static string TruncateLongString(this string str, int maxLength)
{
return str[0..Math.Min(str.Length, maxLength)];
}
}
}
Binary file modified EasMe/bin/Debug/net6.0/EasMe.dll
Binary file not shown.
Binary file modified EasMe/bin/Debug/net6.0/EasMe.pdb
Binary file not shown.
Binary file modified EasMe/obj/Debug/net6.0/EasMe.dll
Binary file not shown.
Binary file modified EasMe/obj/Debug/net6.0/EasMe.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1e77e2890acb14c665edaf5a7b3e8a462f8af44c
0094e749579ba00a18ac4641b51a983698638b06
Binary file modified EasMe/obj/Release/net6.0/EasMe.dll
Binary file not shown.
Binary file modified EasMe/obj/Release/net6.0/EasMe.pdb
Binary file not shown.
Binary file modified EasMe/obj/Release/net6.0/ref/EasMe.dll
Binary file not shown.
Binary file modified EasMe/obj/Release/net6.0/refint/EasMe.dll
Binary file not shown.

0 comments on commit 6de2ba9

Please sign in to comment.