Skip to content

Commit 5865ff3

Browse files
committed
Added client for netframework
1 parent 254499a commit 5865ff3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2814
-5
lines changed

Client/Models/API/Protections/ProtectionLogDTO.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace Shield.API.Models.Protections {
3+
namespace Shield.Client.Models.API.Protections {
44
public class ProtectionLogDTO {
55
public int User { get; set; }
66
public int Prepare { get; set; } //Es el id de prepare

Client/Models/API/Protections/ProtectionParamsDTO.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
2-
using Shield.Client.Models.API.Protections;
32

4-
namespace Shield.API.Models.Protections {
3+
namespace Shield.Client.Models.API.Protections {
54
public class ProtectionParamsDto {
65
public string PrepareKey { get; set; }
76
public List<ProtectionDto> SelectedProtections { get; set; }

Client/Models/API/Protections/ProtectionResultDTO.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace Shield.API.Models.Protections {
3+
namespace Shield.Client.Models.API.Protections {
44
public class ProtectionResultDto {
55
public DateTime Date { get; set; }
66
public string OutKey { get; set; }

Client/Shield.Client.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Client", "Client", "{207AD3
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{AFB18044-235C-4127-B936-04DA8E10773C}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCore.Console.Example", "..\Examples\NetCore.Console.Example\NetCore.Console.Example.csproj", "{68535106-AB2D-448C-A7FC-2BBFEEA7DEE5}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCore.Console.Example", "..\Examples\NetCore.Console.Example\NetCore.Console.Example.csproj", "{68535106-AB2D-448C-A7FC-2BBFEEA7DEE5}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shield.Client.Fr", "..\Shield.Client.Fr\Shield.Client.Fr.csproj", "{9C8C99C5-7E90-4E7E-95AE-6654EDFB3EB0}"
1315
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -25,13 +27,18 @@ Global
2527
{68535106-AB2D-448C-A7FC-2BBFEEA7DEE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
2628
{68535106-AB2D-448C-A7FC-2BBFEEA7DEE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
2729
{68535106-AB2D-448C-A7FC-2BBFEEA7DEE5}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{9C8C99C5-7E90-4E7E-95AE-6654EDFB3EB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{9C8C99C5-7E90-4E7E-95AE-6654EDFB3EB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{9C8C99C5-7E90-4E7E-95AE-6654EDFB3EB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{9C8C99C5-7E90-4E7E-95AE-6654EDFB3EB0}.Release|Any CPU.Build.0 = Release|Any CPU
2834
EndGlobalSection
2935
GlobalSection(SolutionProperties) = preSolution
3036
HideSolutionNode = FALSE
3137
EndGlobalSection
3238
GlobalSection(NestedProjects) = preSolution
3339
{3D23F395-320B-44D6-BF7A-1EF970C236AA} = {207AD39A-FDE0-4DFD-B975-A3C93753D23F}
3440
{68535106-AB2D-448C-A7FC-2BBFEEA7DEE5} = {AFB18044-235C-4127-B936-04DA8E10773C}
41+
{9C8C99C5-7E90-4E7E-95AE-6654EDFB3EB0} = {207AD39A-FDE0-4DFD-B975-A3C93753D23F}
3542
EndGlobalSection
3643
GlobalSection(ExtensibilityGlobals) = postSolution
3744
SolutionGuid = {E91157BC-877C-4084-9E37-1B81E3D274B7}

Shield.Client.Fr/AppSettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"url": "https://api.dotnetsafer.com/api/services/shield",
3+
"version": "1.1"
4+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.IO;
2+
using System.Text.Json;
3+
using Shield.Client.Fr.Models.API.Application;
4+
using Shield.Client.Fr.Models.API.Project;
5+
6+
namespace Shield.Client.Fr.Extensions
7+
{
8+
public static class ConfigurationExtensions
9+
{
10+
//public static async Task SaveToFileAsync(this ApplicationConfigurationDto config, string path, string applicationName)
11+
// => await File.WriteAllTextAsync(Path.Combine(path, $"shield.application.${applicationName}.json"), JsonConvert.SerializeObject(config));
12+
13+
public static void SaveToFile(this ApplicationConfigurationDto config, string path, string applicationName)
14+
=> File.WriteAllText(Path.Combine(path, $"shield.application.{applicationName}.json"), JsonSerializer.Serialize(config));
15+
16+
//public static async Task SaveToFileAsync(this ProjectConfigurationDto config, string path, string projectName)
17+
// => await File.WriteAllTextAsync(Path.Combine(path,$"shield.project.${projectName}.json"), JsonConvert.SerializeObject(config));
18+
19+
public static void SaveToFile(this ProjectConfigurationDto config, string path, string projectName)
20+
=> File.WriteAllText(Path.Combine(path, $"shield.project.{projectName}.json"), JsonSerializer.Serialize(config));
21+
22+
}
23+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.IO;
2+
using System.Threading.Tasks;
3+
4+
namespace Shield.Client.Fr.Extensions
5+
{
6+
public static class FileExtensions
7+
{
8+
/// <summary>
9+
/// Save downloaded application as stream to path.
10+
/// </summary>
11+
/// <param name="downloadedApplication"></param>
12+
/// <param name="path"></param>
13+
/// <param name="replaceIfExist">Replace if the path contains an existing file.</param>
14+
/// <returns></returns>
15+
public static async Task SaveOnAsync(this Stream downloadedApplication, string path, bool replaceIfExist = false)
16+
{
17+
if(File.Exists(path))
18+
if (replaceIfExist)
19+
File.Delete(path);
20+
else return;
21+
22+
using var fileStream = File.Create(path);
23+
downloadedApplication.Seek(0, SeekOrigin.Begin);
24+
await downloadedApplication.CopyToAsync(fileStream);
25+
}
26+
27+
/// <summary>
28+
/// Save downloaded application as stream to path.
29+
/// </summary>
30+
/// <param name="downloadedApplication"></param>
31+
/// <param name="path"></param>
32+
/// <param name="replaceIfExist">Replace if the path contains an existing file.</param>
33+
/// <returns></returns>
34+
public static void SaveOn(this Stream downloadedApplication, string path, bool replaceIfExist = false)
35+
{
36+
if(File.Exists(path))
37+
if (replaceIfExist)
38+
File.Delete(path);
39+
else return;
40+
41+
using var fileStream = File.Create(path);
42+
downloadedApplication.Seek(0, SeekOrigin.Begin);
43+
downloadedApplication.CopyTo(fileStream);
44+
}
45+
46+
47+
/// <summary>
48+
/// Save downloaded application as array to path.
49+
/// </summary>
50+
/// <param name="downloadedApplication"></param>
51+
/// <param name="path"></param>
52+
/// <param name="replaceIfExist">Replace if the path contains an existing file.</param>
53+
/// <returns></returns>
54+
public static void SaveOn(this byte[] downloadedApplication, string path, bool replaceIfExist = false)
55+
{
56+
if (File.Exists(path))
57+
if (replaceIfExist)
58+
File.Delete(path);
59+
else return;
60+
61+
File.WriteAllBytes(path, downloadedApplication);
62+
63+
}
64+
/// <summary>
65+
/// Save downloaded application as array to path.
66+
/// </summary>
67+
/// <param name="downloadedApplication"></param>
68+
/// <param name="path"></param>
69+
/// <param name="replaceIfExist">Replace if the path contains an existing file.</param>
70+
/// <returns></returns>
71+
public static async Task SaveOnAsync(this byte[] downloadedApplication, string path, bool replaceIfExist = false)
72+
{
73+
await Task.Run(() =>
74+
{
75+
if (File.Exists(path))
76+
if (replaceIfExist)
77+
File.Delete(path);
78+
else return;
79+
80+
// Netstandar 2.2
81+
//File.WriteAllBytesAsync(path, downloadedApplication);
82+
83+
File.WriteAllBytes(path, downloadedApplication);
84+
});
85+
}
86+
}
87+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text.Json;
4+
using System.Threading.Tasks;
5+
using Azure.Messaging.ServiceBus;
6+
using Shield.Client.Fr.Models;
7+
8+
namespace Shield.Client.Fr.Extensions
9+
{
10+
public class QueueConnection
11+
{
12+
private ServiceBusClient BusClient { get; }
13+
private ServiceBusProcessor BusProcessor { get; }
14+
private Dictionary<string, Action<string, string, DateTime>> Methods { get; } = new Dictionary<string, Action<string, string, DateTime>>();
15+
16+
public void On(string method, Action<string, string, DateTime> action) =>
17+
Methods.Add(method, action);
18+
19+
public void Destroy(string method) => Methods.Remove(method);
20+
21+
public async Task StartAsync()
22+
{
23+
await BusProcessor.StartProcessingAsync();
24+
}
25+
public async Task StopAsync()
26+
{
27+
await BusProcessor.StartProcessingAsync();
28+
}
29+
public QueueConnection(string queueName)
30+
{
31+
//TODO: Change
32+
BusClient = new ServiceBusClient("Endpoint=sb://dotnetsafer.servicebus.windows.net/;SharedAccessKeyName=CLI;SharedAccessKey=koL+6ZQLUUVo7du5dtaFuo8cYrzDAIhXpHDWBQ1vHnU=");
33+
34+
BusProcessor = BusClient.CreateProcessor(queueName);
35+
BusProcessor.ProcessMessageAsync += ReceiverOnProcessMessageAsync;
36+
BusProcessor.ProcessErrorAsync += ReceiverOnProcessErrorAsync;
37+
}
38+
39+
private async Task ReceiverOnProcessMessageAsync(ProcessMessageEventArgs arg)
40+
{
41+
var message = JsonSerializer.Deserialize<QueueLogMessageModel>(arg.Message.Body.ToString());
42+
43+
await arg.CompleteMessageAsync(arg.Message).ConfigureAwait(false);
44+
45+
if (message is null)
46+
return;
47+
48+
if (!Methods.TryGetValue(message.Method, out var action))
49+
return;
50+
51+
action(message.Message,message.LogLevelString,message.Time);
52+
53+
}
54+
private static Task ReceiverOnProcessErrorAsync(ProcessErrorEventArgs arg)
55+
{
56+
return null;
57+
}
58+
}
59+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Text.Json;
4+
using Shield.Client.Fr.Models;
5+
using Shield.Client.Fr.Models.API.Application;
6+
7+
namespace Shield.Client.Fr.Extensions
8+
{
9+
public static class ShieldLoggerExtensions
10+
{
11+
public static void OnSuccess(this ProtectionResult result, QueueConnection connection, Action<ProtectedApplicationDto> action)
12+
=> connection.On(result.OnSuccess, (message, level, time) => action(JsonSerializer.Deserialize<ProtectedApplicationDto>(message)));
13+
public static void OnError(this ProtectionResult result, QueueConnection connection, Action<string> action)
14+
=> connection.On(result.OnError, (message, level, time) => action(message));
15+
public static void OnClose(this ProtectionResult result, QueueConnection connection, Action<string> action)
16+
=> connection.On(result.OnClose, (message, level, time) => action(message));
17+
18+
public static void OnLog(this QueueConnection connection, string id, Action<string, string, string> action)
19+
=> connection.On(id, (message, level, time) => action(time.ToString(CultureInfo.InvariantCulture), message, level));
20+
}
21+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.IO.Compression;
4+
5+
namespace Shield.Client.Fr.Helpers
6+
{
7+
public static class DependenciesHelper
8+
{
9+
/// <summary>
10+
/// Convert to zip a list of path files
11+
/// </summary>
12+
/// <returns></returns>
13+
public static (byte[] zipContent,string name, string contentType) PackDependenciesToZip(List<string> filesPath)
14+
{
15+
byte[] zipFile;
16+
17+
using (var memoryStream = new MemoryStream())
18+
{
19+
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
20+
{
21+
filesPath.ForEach(file =>
22+
{
23+
zip.CreateEntryFromFile(file, Path.GetFileName(file));
24+
});
25+
}
26+
zipFile = memoryStream.ToArray();
27+
}
28+
29+
return (zipFile, "dependencies.zip", MimeTypeMap.GetMimeType(".zip"));
30+
}
31+
/// <summary>
32+
/// Convert to zip a list of files
33+
/// </summary>
34+
/// <returns></returns>
35+
public static (byte[] zipContent, string name, string contentType) PackDependenciesToZip(List<(byte[] content,string name)> files)
36+
{
37+
byte[] zipFile;
38+
39+
using (var memoryStream = new MemoryStream())
40+
{
41+
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
42+
{
43+
files.ForEach(file =>
44+
{
45+
var (content, name) = file;
46+
var zipItem = zip.CreateEntry(name);
47+
48+
using (var fileStream = new MemoryStream(content))
49+
{
50+
using (var entry = zipItem.Open())
51+
{
52+
fileStream.CopyTo(entry);
53+
}
54+
}
55+
56+
});
57+
}
58+
zipFile = memoryStream.ToArray();
59+
}
60+
return (zipFile, "dependencies.zip", MimeTypeMap.GetMimeType(".zip"));
61+
}
62+
}
63+
64+
}

0 commit comments

Comments
 (0)