-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.cs
More file actions
307 lines (299 loc) · 11.6 KB
/
Copy pathCore.cs
File metadata and controls
307 lines (299 loc) · 11.6 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using dn42Bot.Chatting;
using dn42Bot.Chatting.Controllers;
using dn42Bot.Network;
using dn42Bot.TelegramBot;
using dn42Bot.Utils;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Text.Json;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace dn42Bot
{
internal class Core
{
public static BotInfo BotProfile
{
get
{
return new();
}
}
public static DateTime StartAt { get; } = DateTime.Now;
public static BotConfig Config
{
get
{
return _botConfig;
}
}
static long _botId = 0;
static string _botUsername = string.Empty;
static BotConfig _botConfig = new();
static Task _chatRoomCleanUpTask = Task.CompletedTask;
static TelegramBotClient _botClient = null!;
static HttpClient _botHttpClient = new HttpClient();
readonly static string WORKING_DIRECTORY_PATH = Environment.CurrentDirectory;
readonly static IReadOnlyDictionary<string, string> PRE_DEFINED_COMMAND_LIST = new Dictionary<string, string>()
{
{ "/ping", "Ping DN42 network" },
{ "/trace", "Traceroute on DN42 network" },
{ "/nslookup", "DNS query" },
{ "/dig", "DNS query" },
};
static async Task Main(string[] args)
{
TaskScheduler.UnobservedTaskException += (sender, eventArgs) =>
{
BotLogger.LogError($"Unobserved exception: {eventArgs}");
eventArgs.SetObserved();
};
BotLogger.LogInfo("Bot is starting...");
var configPath = Path.Combine(WORKING_DIRECTORY_PATH, "config.json");
var jsonOptions = new JsonSerializerOptions()
{
WriteIndented = true
};
if(File.Exists(configPath))
{
BotLogger.LogInfo($"Reading bot configuration from \"{configPath}\"");
try
{
var botConfig = await JsonSerializer.DeserializeAsync<BotConfig>(File.OpenRead(configPath), jsonOptions);
if(botConfig is null)
{
Fatal("Failed to parse the configuration file");
}
if(string.IsNullOrEmpty(botConfig.Token))
{
Fatal("API token not specified");
}
_botConfig = botConfig;
var useProxy = botConfig.Network.UseProxy;
var proxyUriStr = botConfig.Network.Proxy;
var proxy = default(IWebProxy);
if (string.IsNullOrEmpty(proxyUriStr))
{
proxy = WebRequest.GetSystemWebProxy();
}
else if (!Uri.TryCreate(proxyUriStr, UriKind.RelativeOrAbsolute, out var proxyUri))
{
Fatal("Proxy address format is incorrect");
}
else
{
if(proxyUri.Scheme is not ("http" or "https"))
{
Fatal("Unsupported proxy protocol");
}
proxy = new WebProxy(proxyUriStr);
}
_botHttpClient = new(new HttpClientHandler()
{
UseProxy = botConfig.Network.UseProxy,
Proxy = proxy
});
}
catch(Exception e)
{
BotLogger.LogError(e);
BotLogger.LogError("Failed to parse the configuration file");
Thread.Sleep(1000);
Environment.Exit(-1);
}
}
else
{
var json = JsonSerializer.Serialize(_botConfig, jsonOptions);
File.WriteAllText(configPath, json);
BotLogger.LogInfo("Configuration file has been generated");
Thread.Sleep(1000);
Environment.Exit(0);
}
_botClient = new TelegramBotClient(_botConfig.Token, _botHttpClient);
BotLogger.LogInfo("Logging into telegran.org...");
var isValid = await _botClient.TestApi();
if (isValid)
{
BotLogger.LogInfo("Logged in successfully");
}
else
{
Fatal("Failed to log in. Check the API token", -1);
}
var botDetails = await _botClient.GetMe();
BotLogger.LogInfo($"####################### Bot Profile #######################");
BotLogger.LogInfo($"Id : {botDetails.Id}");
BotLogger.LogInfo($"Name : {botDetails.FirstName} {botDetails.LastName}");
BotLogger.LogInfo($"Username: {botDetails.Username}");
BotLogger.LogInfo($"####################### Bot Profile End #######################");
_botId = botDetails.Id;
_botUsername = botDetails.Username ?? string.Empty;
BotLogger.LogInfo("Updating bot command list...");
await _botClient.SetMyCommands(PRE_DEFINED_COMMAND_LIST.Select(x =>
{
var (cmd, desc) = x;
return new BotCommand(cmd, desc);
}));
BotLogger.LogInfo("Starting receiving updates...");
_ = _botClient.ReceiveAsync(
updateHandler: UpdateHandler,
errorHandler: ErrorHandler
);
_chatRoomCleanUpTask = Task.Factory.StartNew(() =>
{
while(true)
{
try
{
ChatSession.CleanUpAsync().AsTask().Wait();
}
catch(Exception e)
{
BotLogger.LogError($"Failed to clean up the chat room: {e.Message}");
}
Thread.Sleep(30000);
}
}, TaskCreationOptions.LongRunning);
while(true)
{
Thread.Sleep(30000);
try
{
botDetails = await _botClient.GetMe();
if(botDetails.Username != _botUsername)
{
BotLogger.LogInfo($"New username: {botDetails.Username}");
}
_botUsername = botDetails.Username ?? string.Empty;
}
catch(Exception e)
{
BotLogger.LogError($"Error while getting bot info: {e}");
}
}
}
static void UpdateHandler(ITelegramBotClient client, Update update, CancellationToken token = default)
{
Task.Run(async () =>
{
try
{
var updateType = update.Type;
switch (updateType)
{
case UpdateType.Message:
if(update.Message!.Date.ToUniversalTime() < StartAt.ToUniversalTime())
{
return;
}
break;
case UpdateType.EditedMessage:
if (update.EditedMessage!.Date.ToUniversalTime() < StartAt.ToUniversalTime())
{
return;
}
break;
case UpdateType.CallbackQuery:
var query = update.CallbackQuery!;
var message = query.Message;
if (message is null)
{
BotLogger.LogWarning("Received an invalid callback query");
return;
}
if (message.Date.ToUniversalTime() < StartAt.ToUniversalTime())
{
return;
}
var chatRoom = await ChatSession.GetOrCreateChatRoomAsync(message.Chat.Id);
await chatRoom.OnCallbackAsync(query);
return;
default:
return;
}
var userRequest = UserRequest.Parse(client, update);
var sender = userRequest.Sender;
var chat = userRequest.Chat;
var username = string.Empty;
var chatName = string.Empty;
if (string.IsNullOrEmpty(sender.LastName))
{
username = sender.FirstName;
}
else
{
username = $"{sender.FirstName} {sender.LastName}";
}
if (string.IsNullOrEmpty(chat.LastName))
{
chatName = chat.FirstName;
}
else
{
chatName = $"{chat.FirstName} {chat.LastName}";
}
BotLogger.LogInfo($"[Recv][{updateType}] U:{sender.Id}({username})|C:{chat.Id}({chatName})|Said: \"{userRequest.Text}\"");
if (userRequest.IsBotSpecified && userRequest.SpecifiedBotUsername != _botUsername)
{
return;
}
await BotCommandHandleAsync(userRequest);
}
catch(Exception e)
{
BotLogger.LogError(e.ToString());
}
});
}
static void ErrorHandler(ITelegramBotClient client, Exception exception, CancellationToken token = default)
{
BotLogger.LogError($"Telegram Bot API Error: {exception}");
}
static async ValueTask BotCommandHandleAsync(UserRequest userRequest)
{
try
{
var cmdEntity = userRequest.Command;
var chatRoom = await ChatSession.GetOrCreateChatRoomAsync(userRequest.Chat.Id);
switch (cmdEntity.Prefix)
{
case "trace":
case "ping":
case "nslookup":
case "dig":
var controller = NetworkController.FromRequest(userRequest, chatRoom);
chatRoom.AddSession(controller);
await controller.ExecuteAsync();
break;
default:
return;
}
}
catch(Exception e)
{
BotLogger.LogError(e.ToString());
}
}
[DoesNotReturn]
static void Fatal(string message, int exitCode = -127)
{
BotLogger.LogError(message);
Thread.Sleep(1000);
Environment.Exit(exitCode);
}
[DoesNotReturn]
static void ExitWithMessage(string message, int exitCode = -127)
{
BotLogger.LogInfo(message);
Thread.Sleep(1000);
Environment.Exit(exitCode);
}
public readonly struct BotInfo
{
public long Id => _botId;
public string Username => _botUsername;
}
}
}