Skip to content

Commit 70e53e7

Browse files
committed
Přidejte soubory projektu.
1 parent d143336 commit 70e53e7

File tree

7 files changed

+566
-0
lines changed

7 files changed

+566
-0
lines changed

Example/Example.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Discord.Net" Version="3.6.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\SinkingYachts\SinkingYachts.csproj" />
14+
</ItemGroup>
15+
16+
</Project>

Example/Logger.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Discord;
2+
using System;
3+
using System.Threading.Tasks;
4+
5+
namespace Example
6+
{
7+
public static class Logger
8+
{
9+
public static Task Log(LogMessage logMessage)
10+
{
11+
Console.ForegroundColor = SeverityToConsoleColor(logMessage.Severity);
12+
Console.WriteLine($"{DateTime.Now:dd/MM. H:mm:ss} [{logMessage.Source}] {logMessage.Message}");
13+
Console.ResetColor();
14+
15+
return Task.CompletedTask;
16+
}
17+
18+
public static Task Log(string source, string message, LogSeverity severity)
19+
{
20+
Console.ForegroundColor = SeverityToConsoleColor(severity);
21+
Console.WriteLine($"{DateTime.Now:dd/MM. H:mm:ss} [{source}] {message}");
22+
Console.ResetColor();
23+
24+
return Task.CompletedTask;
25+
}
26+
27+
private static ConsoleColor SeverityToConsoleColor(LogSeverity severity)
28+
{
29+
return severity switch
30+
{
31+
LogSeverity.Critical => ConsoleColor.Red,
32+
LogSeverity.Debug => ConsoleColor.Blue,
33+
LogSeverity.Error => ConsoleColor.Yellow,
34+
LogSeverity.Info => ConsoleColor.Cyan,
35+
LogSeverity.Verbose => ConsoleColor.Green,
36+
LogSeverity.Warning => ConsoleColor.Magenta,
37+
_ => ConsoleColor.White,
38+
};
39+
}
40+
}
41+
}

Example/Program.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Discord;
2+
using Discord.WebSocket;
3+
using System;
4+
using System.Threading.Tasks;
5+
using SinkingYachts;
6+
using System.Linq;
7+
8+
namespace Example
9+
{
10+
public static class Program
11+
{
12+
private static readonly DiscordSocketClient Client = new (new DiscordSocketConfig()
13+
{
14+
GatewayIntents = GatewayIntents.Guilds | GatewayIntents.GuildMessages
15+
});
16+
17+
public static readonly StorageMode Mode = StorageMode.LocalWS;
18+
19+
private static readonly YachtsClient Yachts = new(Mode, 3, "Example Bot");
20+
21+
private static readonly string Token = Environment.GetEnvironmentVariable("BOT_TOKEN");
22+
23+
public static async Task Main()
24+
{
25+
if (string.IsNullOrEmpty(Token))
26+
{
27+
await Logger.Log("Bot", $"No bot token has been configured. Please set one as the \"BOT_TOKEN\" environment variable.", LogSeverity.Critical);
28+
throw new("Empty bot token");
29+
}
30+
31+
Client.Log += Logger.Log;
32+
Client.Ready += Ready;
33+
Client.MessageReceived += OnMessageReceived;
34+
35+
try
36+
{
37+
await Client.LoginAsync(TokenType.Bot, Token);
38+
await Client.StartAsync();
39+
}
40+
catch (Exception ex)
41+
{
42+
await Logger.Log("Bot", $"Exception while trying to log in: {ex.GetType().Name} => {ex.Message}", LogSeverity.Critical);
43+
throw;
44+
}
45+
46+
if (Mode == StorageMode.LocalWS)
47+
{
48+
Yachts.DomainAdded += (o, domain) =>
49+
{
50+
Console.WriteLine($"New domain added: {domain}");
51+
};
52+
53+
Yachts.DomainDeleted += (o, domain) =>
54+
{
55+
Console.WriteLine($"New domain deleted: {domain}");
56+
};
57+
}
58+
59+
await Task.Delay(-1);
60+
}
61+
62+
public static async Task Ready()
63+
{
64+
await Logger.Log("Bot", $"Bot is ready to protect your server from {await Yachts.DatabaseSize()} phishing domains", LogSeverity.Info);
65+
66+
Change[] changes = await Yachts.Recent(TimeSpan.FromDays(1));
67+
68+
int added = changes.Count(x => x.Type == ChangeType.Add);
69+
int deleted = changes.Count(x => x.Type == ChangeType.Delete);
70+
71+
await Logger.Log("Bot", $"Domains added within the past day: {added}", LogSeverity.Info);
72+
await Logger.Log("Bot", $"Domains deleted within the past day: {deleted}", LogSeverity.Info);
73+
}
74+
75+
public static async Task OnMessageReceived(SocketMessage msg)
76+
{
77+
if (msg is not SocketUserMessage) return;
78+
if (msg.Channel is not SocketTextChannel) return;
79+
if (msg.Author.IsBot) return;
80+
81+
if (await Yachts.IsPhishing(msg.Content))
82+
{
83+
await msg.DeleteAsync();
84+
await msg.Channel.SendMessageAsync("Phishing links are not allowed.");
85+
}
86+
}
87+
}
88+
}

SinkingYachts.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32328.378
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SinkingYachts", "SinkingYachts\SinkingYachts.csproj", "{0325F475-6034-4D9A-BB9C-94F25FFAAB4D}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{676CCDDA-5528-464A-8FB9-4C8874FD5D1D}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{0325F475-6034-4D9A-BB9C-94F25FFAAB4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{0325F475-6034-4D9A-BB9C-94F25FFAAB4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{0325F475-6034-4D9A-BB9C-94F25FFAAB4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{0325F475-6034-4D9A-BB9C-94F25FFAAB4D}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{676CCDDA-5528-464A-8FB9-4C8874FD5D1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{676CCDDA-5528-464A-8FB9-4C8874FD5D1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{676CCDDA-5528-464A-8FB9-4C8874FD5D1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{676CCDDA-5528-464A-8FB9-4C8874FD5D1D}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {658F1720-F9D3-4F88-BE12-02AA51A43AC2}
30+
EndGlobalSection
31+
EndGlobal

SinkingYachts/Connection.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Net.WebSockets;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace SinkingYachts
9+
{
10+
public class Connection
11+
{
12+
public const string URL = "wss://phish.sinking.yachts/feed";
13+
public const int ReconnectionInterval = 10000;
14+
15+
private ClientWebSocket WS;
16+
private readonly CancellationTokenSource Source = new();
17+
18+
public bool Connected = false;
19+
20+
public EventHandler<string> DomainAdded;
21+
public EventHandler<string> DomainDeleted;
22+
23+
private readonly string _identity;
24+
25+
public Connection(string identity)
26+
{
27+
_identity = identity;
28+
29+
Connect();
30+
}
31+
32+
public async void Connect()
33+
{
34+
Connected = false;
35+
WS = new ClientWebSocket();
36+
WS.Options.SetRequestHeader("X-Identity", $"https://github.com/actually-akac/SinkingYachts | {_identity}");
37+
38+
try
39+
{
40+
await WS.ConnectAsync(new Uri(URL), Source.Token);
41+
}
42+
catch
43+
{
44+
Connected = false;
45+
await Task.Delay(ReconnectionInterval);
46+
Connect();
47+
return;
48+
}
49+
50+
while (WS.State == WebSocketState.Open)
51+
{
52+
Connected = true;
53+
var receiveBuffer = new byte[1024];
54+
var offset = 0;
55+
56+
while (true)
57+
{
58+
try
59+
{
60+
ArraySegment<byte> bytesReceived = new(receiveBuffer, offset, receiveBuffer.Length);
61+
62+
WebSocketReceiveResult result = await WS.ReceiveAsync(bytesReceived, Source.Token);
63+
offset += result.Count;
64+
if (result.EndOfMessage)
65+
break;
66+
}
67+
catch { break; };
68+
}
69+
70+
if (offset != 0) OnMessage(Encoding.UTF8.GetString(receiveBuffer, 0, offset));
71+
}
72+
73+
Connected = false;
74+
await Task.Delay(ReconnectionInterval);
75+
Connect();
76+
}
77+
78+
public void OnMessage(string msg)
79+
{
80+
Change data = JsonSerializer.Deserialize<Change>(msg);
81+
82+
foreach (string domain in data.Domains)
83+
{
84+
if (data.Type == ChangeType.Add) DomainAdded.Invoke(this, domain);
85+
else if (data.Type == ChangeType.Delete) DomainDeleted.Invoke(this, domain);
86+
}
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)