-
Notifications
You must be signed in to change notification settings - Fork 10
/
JsonSender.cs
80 lines (72 loc) · 2.64 KB
/
JsonSender.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace Trapdoor
{
public class JsonSender : SenderBase
{
private readonly string send_link;
private readonly HttpClient _client;
private readonly Storage<SessionLog> _storage;
private readonly IMemoryCache memoryCache;
private readonly Dictionary<string, string> paths;
public JsonSender(Storage<SessionLog> storage, Config config, IMemoryCache cache) : base(storage, config, cache)
{
_storage = storage;
send_link = config.PostUrl;
_client = new HttpClient();
paths = config.Paths;
memoryCache = cache;
}
public override async Task<string> SendAlert((string, Dictionary<string, dynamic>) res, string sourceIp, string path, string guid)
{
try
{
path = path.Split("/")[1];
var _path = paths.ContainsKey(path)? paths[path] : path;
var message = $"Trapdoor triggered in: {_path}";
var temp = await GenerateAlert(res, sourceIp, message);
var content = new StringContent(temp, Encoding.UTF8, "application/json");
await _client.PostAsync(send_link, content);
return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds().ToString();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
private async Task<string> GenerateAlert((string, Dictionary<string, dynamic>) res, string sourceIp, string message)
{
var ipLinks = new List<string>();
var sessionLinks = new List<string>();
res.Item2["Friendly Reminder"] = message;
try
{
if (!string.IsNullOrEmpty(res.Item1))
{
var sessionLogs = await GetLogs(res.Item1);
if (sessionLogs.Any())
{
res.Item2["Session ID Hits"] = sessionLogs;
}
}
var ipLogs = await GetLogs(sourceIp);
if (ipLogs.Any())
{
res.Item2["IP Hits"] = ipLogs;
}
}
catch (Exception e)
{
Console.WriteLine($"Error getting logs : {e.Message}");
}
return JsonSerializer.Serialize(res.Item2);
}
}
}