-
Notifications
You must be signed in to change notification settings - Fork 5
/
RustApp.cs
685 lines (551 loc) · 22 KB
/
RustApp.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//AMP Rust Module - See LICENCE.txt
//©2017-2019 CubeCoders Limited - All rights reserved.
using Ionic.Zip;
using ModuleShared;
using RCONPlugin;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace RustModule
{
public class RustApp : AppServerBase, IApplicationWrapper, IHasReadableConsole, IHasWriteableConsole, IHasSimpleUserList
{
private ModuleMain module;
private bool IgnoreStatus = false;
private bool IssueUpdateEvent = true;
public RustApp(ModuleMain module)
{
this.module = module;
this.Users = new List<SimpleUser>();
}
[MessageHandler(@"^New version detected!$")]
private bool UpdateAvailableHandler(Match match)
{
if (IssueUpdateEvent)
{
UpdateAvailable.InvokeSafe(this, EventArgs.Empty);
IssueUpdateEvent = false;
if (module.settings.Rust.AutoUpdate)
{
Update();
}
}
return false;
}
Regex statusLineRegex = new Regex(@"^(\d+?)\s+""(.+?)""\s+(\d+?)\s+(.+?)\s+(.+?):(\d+?)\s+(.+?)$");
[MessageHandler(@"^hostname.+$", Options: RegexOptions.Singleline)]
private bool StatusResponse(Match match)
{
if (IgnoreStatus)
{
IgnoreStatus = false;
var lines = match.Value.Split('\n');
foreach (var l in lines)
{
var lineMatch = statusLineRegex.Match(l);
if (lineMatch.Success)
{
StatusUserLine(lineMatch);
}
}
return true;
}
return false;
}
private bool StatusUserLine(Match match)
{
string steamId = match.Groups[1].Value;
string playerName = match.Groups[2].Value.Replace(@"\""", "\"");
IPAddress.TryParse(match.Groups[5].Value, out IPAddress address);
int.TryParse(match.Groups[6].Value, out int port);
var user = Users.Where(u => u.Name == playerName).FirstOrDefault();
var endpoint = new IPEndPoint(address, port);
if (user != null)
{
if (!string.IsNullOrEmpty(user.Id))
{
return false;
}
user.RemoteEndpoint = endpoint;
user.Id = steamId;
}
else
{
var newUser = new SimpleUser()
{
Name = playerName,
JoinTime = DateTime.Now,
RemoteEndpoint = endpoint,
Id = steamId,
};
Users.Add(newUser);
UserJoins.InvokeSafe(this, new UserEventArgs(newUser));
}
return false;
}
[MessageHandler(@"^Unsupported encoding: 'utf8'$")]
[MessageHandler(@"^Reporting Performance Data system/server$")]
private bool IgnoreLine(Match match)
{
return true;
}
private void InternalQueryStatus()
{
IgnoreStatus = true;
PostMessage("status");
}
[MessageHandler(@"^(.+?) was killed by (.+?)$")]
private bool PlayerKilled(Match match)
{
var playerName = match.Groups[1].Value;
var deathMethod = match.Groups[2].Value;
var victim = Users.Where(u => u.Name == playerName).FirstOrDefault();
var attacker = Users.Where(u => u.Name == deathMethod).FirstOrDefault();
if (victim == null) { return false; } //How'd that happen?
if (attacker != null)
{
PlayerKilledByPlayer.InvokeSafe(this, new PlayerKilledEventArgs(victim, attacker));
}
else
{
PlayerKilledByEnvironment.InvokeSafe(this, new PlayerKilledByEnvironmentEventArgs(victim, deathMethod));
}
Users.Remove(victim);
return false;
}
[MessageHandler(@"^([\d\.]+):(\d+)\/(\d+)\/(.+?) disconnecting: (.+?)$")]
private bool PlayerLeave(Match match)
{
var playerId = match.Groups[3].Value;
var playerName = match.Groups[4].Value;
var reason = match.Groups[5].Value;
var user = Users.Where(u => u.Name == playerName).FirstOrDefault();
if (user == null) { return false; } //How'd that happen?
UserLeaves.InvokeSafe(this, new UserEventArgs(user));
Users.Remove(user);
return false;
}
[MessageHandler(@"^\[RCON\]\[(.+?)\] (.+)$")]
private bool RconCommand(Match match)
{
var address = match.Groups[1].Value;
var message = match.Groups[2].Value;
if (IgnoreStatus && message == "status")
{
return true;
}
var newEntry = new ConsoleEntry()
{
Contents = message,
Source = "RCON/" + address,
SourceId = "RCON/" + address,
Type = "Console",
Timestamp = DateTime.Now
};
AddConsoleEntry(newEntry);
module.log.ConsoleOutput(match.Value);
return true;
}
[MessageHandler(@"^\[CHAT\] (.+?): (.+)$")]
private bool PlayerChat(Match match)
{
var playerName = match.Groups[1].Value;
var message = match.Groups[2].Value;
var user = Users.Where(u => u.Name == playerName).FirstOrDefault();
if (user == null) { return false; } //How'd that happen?
var newEntry = new ConsoleEntry()
{
Contents = message,
Source = user.Name,
SourceId = user.Id,
Type = "Chat",
Timestamp = DateTime.Now
};
AddConsoleEntry(newEntry);
module.log.Chat(playerName, message);
PlayerChats.InvokeSafe(this, new UserChatEventArgs(user, message));
return true;
}
[MessageHandler(@"^([\d\.]+):(\d+)\/(\d+)\/(.+?) joined \[(.+?)\/(\d+)\]$")]
private bool PlayerJoined(Match match)
{
InternalQueryStatus();
return false;
}
internal void PostInit()
{
module.steamcmd.UpdateProgressChange += Steamcmd_UpdateProgressChange;
}
private steamcmdplugin.Steamcmdhelper.AppInfo CurrentApp => module.steamcmd.CurrentApp;
private readonly Dictionary<SupportedOS, string> SRCDSAppPath = new Dictionary<SupportedOS, string>()
{
{ SupportedOS.Windows, "RustDedicated.exe" },
{ SupportedOS.Linux, "RustDedicated" }
};
private string WorkingDir => Path.Combine(module.settings.Rust.SteamCMDPath, CurrentApp.ID.ToString());
private string ServerFile => Path.Combine(WorkingDir, SRCDSAppPath[this.module.os]);
public bool IsGameServerInstalled() => File.Exists(ServerFile);
/// <summary>
/// Generates a random password for RCON and creates a new ApplicationProcess StartInfo
/// </summary>
private void SetupProcess()
{
ApplicationProcess = new AMPProcess()
{
Win32RequiresConsoleAssistant = false,
};
var id = CurrentApp.ID;
RandomRCONPassword = GenerateRandomPassword();
//GetTaggedValues() returns information about all of the settings according to their tag, which is the final parameter of the WebSetting attribute.
var settingArgs = string.Join(" ", module.settings.Rust.GetTaggedValues().Select(kvp => $"+{kvp.Key} \"{kvp.Value.ToString()}\""));
string args = $"-batchmode -nographics -logFile +rcon.password \"{RandomRCONPassword}\" {settingArgs} +rcon.web \"False\" {module.settings.Rust.CustomArgs}";
ApplicationProcess.StartInfo = new ProcessStartInfo()
{
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
ErrorDialog = false,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = WorkingDir,
FileName = ServerFile,
Arguments = args,
};
//Rust requires some extra environment variables on Linux to tell it where its own plugins are.
if (module.os == SupportedOS.Linux)
{
string LD_LIBRARY_PATH = Environment.GetEnvironmentVariable(nameof(LD_LIBRARY_PATH));
var AddPath = Path.Combine(WorkingDir, "RustDedicated_Data", "Plugins", "x86_64");
LD_LIBRARY_PATH = LD_LIBRARY_PATH + $":{AddPath}";
ApplicationProcess.StartInfo.AddEnvirionmentVariable(nameof(LD_LIBRARY_PATH),LD_LIBRARY_PATH);
}
ApplicationProcess.EnableRaisingEvents = true;
ApplicationProcess.Exited += ApplicationProcess_Exited;
ApplicationProcess.OutputDataReceived += ApplicationProcess_OutputDataReceived;
ApplicationProcess.ErrorDataReceived += ApplicationProcess_OutputDataReceived;
}
private void ApplicationProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
module.log.Debug(e.Data);
}
private void ApplicationProcess_Exited(object sender, EventArgs e)
{
var unexpectedStop = (this.State != ApplicationState.Stopping && this.State != ApplicationState.Restarting);
var needsRestart = (this.State == ApplicationState.Restarting);
this.State = ApplicationState.Stopped;
if (unexpectedStop)
{
this.module.log.Warning("The application stopped unexpectedly.");
UnexpectedStop.InvokeSafe(this, new ServerStoppedEventArgs(DateTime.Now));
}
else
{
NormalStop.InvokeSafe(this, new ServerStoppedEventArgs(DateTime.Now));
}
this.Users.Clear();
if (needsRestart)
{
Start();
}
}
public void StartGameServer()
{
if (!IsGameServerInstalled()) // If our Game Server directory does not exist with the srcds executable in
{
try
{
Update();
}
catch
{
this.State = ApplicationState.Failed;
return;
}
}
this.State = ApplicationState.PreStart;
IssueUpdateEvent = true;
if (!IsGameServerInstalled())
{
this.State = ApplicationState.Failed;
module.log.Warning("Rust is not installed!");
return;
}
SetupProcess();
ApplicationProcess.Start();
SetupRCON();
this.State = ApplicationState.Starting;
}
private SourceRconClient rcon;
private async void SetupRCON()
{
await Task.Delay(10000);
if (this.State != ApplicationState.Starting)
{
module.log.Warning("Tried to start RCON but server is not running!");
return;
}
try
{
rcon = new SourceRconClient();
rcon.DataRecieved += Rcon_DataRecieved;
bool connectResult = false;
int rconRetry = 0;
//Keep trying to connect to RCON over and over until we either succeed, get a permission denied, or the server stops.
do
{
string rconTargetIP = "";
if (IPAddress.TryParse(module.settings.Rust.IP, out var BindIP))
{
rconTargetIP = IPAddress.Equals(BindIP, IPAddress.Any) ? IPAddress.Loopback.ToString() : BindIP.ToString();
}
connectResult = await rcon.Connect(rconTargetIP, module.settings.Rust.RconPort);
if (connectResult == false)
{
await Task.Delay(10000);
rconRetry++;
}
} while (connectResult == false && this.State == ApplicationState.Starting);
if (!connectResult)
{
throw new Exception("Unable to connect.");
}
var authResult = await rcon.Login(RandomRCONPassword);
//Once we're connected, the application can transition to the Ready state - even if auth failed (but this stops the console being usable)
if (connectResult && authResult)
{
PostMessage("chat.serverlog true");
module.log.Info("RCON connection successful.");
State = ApplicationState.Ready;
}
else
{
module.log.Warning("RCON connection failed, console write unavailable.");
State = ApplicationState.Ready;
}
}
catch
{
module.log.Warning("RCON connection failed, console write unavailable.");
State = ApplicationState.Ready;
}
}
void Rcon_DataRecieved(object sender, SourceRconPacket e)
{
if (e.ID != 0) { return; }
ProcessMessage(e.Body);
}
void ProcessMessage(string message)
{
if (string.IsNullOrEmpty(message)) { return; }
var newEntry = new ConsoleEntry()
{
Contents = message,
Source = "Console",
Type = "Console",
Timestamp = DateTime.Now
};
if (!ProcessOutput(message))
{
AddConsoleEntry(newEntry);
this.module.log.ConsoleOutput(message);
}
}
internal SimpleUser GetPlayer(string Id)
{
return Users.Where(u => u.UID == Id).FirstOrDefault();
}
[ScheduleableTask("Kick a player")]
public void KickPlayer(SimpleUser Player) => WriteLine($"kick {Player.Name}");
[ScheduleableTask("Ban a player")]
public void BanPlayer(SimpleUser Player) => WriteLine($"ban {Player.Name}");
[ScheduleableTask("Save the map and player inventories")]
public void SaveMap() => WriteLine("save.all");
[ScheduleableTask("Send a chat message to everyone")]
public void ChatMessage(string Message) => WriteLine($"say {Message}");
[ScheduleableTask("Send a popup message to everyone")]
public void PopupMessage(string Message) => WriteLine($"notice.popupall {Message}");
[ScheduleableTask("Perform a console command")]
public void ConsoleCommand(string Command) => PostMessage(Command);
[ScheduleableTask("Start the Rust server")]
public ActionResult Start()
{
StartGameServer();
return ActionResult.Success;
}
[ScheduleableTask("Stop the Rust server")]
public void Stop()
{
StopApplication(false);
}
public ActionResult Sleep()
{
throw new NotSupportedException();
}
[ScheduleableTask("Restart the Rust server")]
public void Restart()
{
StopApplication(true);
}
public void StopApplication(bool andRestart = false)
{
if (this.State != ApplicationState.Stopped)
{
this.State = (andRestart) ? ApplicationState.Restarting : ApplicationState.Stopping;
WriteLine("quit");
}
}
public void Kill()
{
if (this.State != ApplicationState.Stopped)
{
ApplicationProcess.Kill();
}
}
/// <summary>
/// Called when the update progress changes
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Steamcmd_UpdateProgressChange(object sender, steamcmdplugin.Steamcmdhelper.UpdateProgressChangedEventArgs e)
{
if (e.Finished)
{
this.State = ApplicationState.Stopped;
}
else if (e.Error)
{
this.State = ApplicationState.Failed;
}
}
public ActionResult Update()
{
if (this.State != ApplicationState.Stopped)
{
NormalStop += RustApp_AutoUpdate;
this.Stop();
}
else
{
this.State = ApplicationState.Installing;
module.steamcmd.UpdateAndInstallInBackground(this.ApplicationName);
module.steamcmd.UpdateProgressChange += (s, e) => { if (e.Finished) { InstallOxide(); } };
}
return ActionResult.Success;
}
private async void InstallOxide()
{
if (!module.settings.Rust.InstallOxide) { return; }
var outFile = Path.Combine(WorkingDir, "Oxide-Rust.zip");
var task = module.taskmgr.CreateTask("Updating UMod");
if (await Utilities.DownloadFileWithProgressAsync(module.settings.Rust.OxideModDownloadURL, outFile, task) == false)
{
return;
}
if (module.os == SupportedOS.Windows)
{
using (var zip = new ZipFile(outFile))
{
await zip.ExtractAllAsync(WorkingDir, ExtractExistingFileAction.OverwriteSilently);
}
}
else
{
Utilities.UnzipArchive_Linux(outFile, WorkingDir);
}
}
void RustApp_AutoUpdate(object sender, ServerStoppedEventArgs e)
{
NormalStop -= RustApp_AutoUpdate;
Update();
}
public int MaxRAMUsage => 0;
public int MaxUsers => module.settings.Rust.MaxPlayers;
public bool SupportsSleep => false;
public string ApplicationName => "Rust Dedicated Server";
public string ModuleName => "RustModule";
public string ModuleAuthor => "CubeCoders Limited";
public SupportedOS SupportedOperatingSystems => SupportedOS.Windows | SupportedOS.Linux;
public bool CanRunVirtualized => true;
public bool CanUpdateApplication => true;
private string RandomRCONPassword = "";
#pragma warning disable 0162 //Ignore 'cannot be reached' because it's conditional on DEBUG
private string GenerateRandomPassword()
{
#if DEBUG
return "testingpassword123";
#endif
return Guid.NewGuid().ToString("N");
}
#pragma warning restore 0162
public void WriteLine(string message)
{
if (State != ApplicationState.Stopped && State != ApplicationState.Sleeping)
{
PostMessage(message);
}
}
private void PostMessage(string Message)
{
if (rcon.Connected)
{
var result = rcon.SendMessage(new SourceRconPacket() { Body = Message, Type = SourceRconPacket.PacketType.ExecCommandOrAuthResponse }, true).GetAwaiter().GetResult();
}
}
public List<SimpleUser> Users { get; private set; }
[ScheduleableEvent("An update for the Rust server is available")]
public event EventHandler<EventArgs> UpdateAvailable;
[ScheduleableEvent("A player joins the Rust server")]
public event EventHandler<UserEventArgs> UserJoins;
[ScheduleableEvent("A player leaves the Rust server")]
public event EventHandler<UserEventArgs> UserLeaves;
[ScheduleableEvent("A player sends a chat message")]
public event EventHandler<UserChatEventArgs> PlayerChats;
public class PlayerKilledEventArgs : EventArgs
{
public SimpleUser Victim { get; private set; }
public SimpleUser Attacker { get; private set; }
public PlayerKilledEventArgs(SimpleUser Victim, SimpleUser Attacker)
{
this.Victim = Victim;
this.Attacker = Attacker;
}
}
public class PlayerKilledByEnvironmentEventArgs : EventArgs
{
public SimpleUser Victim { get; private set; }
public string Method { get; private set; }
public PlayerKilledByEnvironmentEventArgs(SimpleUser Victim, string Method)
{
this.Victim = Victim;
this.Method = Method;
}
}
public class ServerStoppedEventArgs : EventArgs
{
public DateTime Time { get; private set; }
public ServerStoppedEventArgs(DateTime time)
{
this.Time = time;
}
}
[ScheduleableEvent("The Rust Server stops unexpectedly")]
public event EventHandler<ServerStoppedEventArgs> UnexpectedStop;
[ScheduleableEvent("The Rust Server stops normally")]
public event EventHandler<ServerStoppedEventArgs> NormalStop;
[ScheduleableEvent("A player is killed by another player")]
public event EventHandler<PlayerKilledEventArgs> PlayerKilledByPlayer;
[ScheduleableEvent("A player is killed by the environment")]
public event EventHandler<PlayerKilledByEnvironmentEventArgs> PlayerKilledByEnvironment;
public string BaseDirectory
{
get => module.settings.Rust.SteamCMDPath;
set => module.settings.Rust.SteamCMDPath = value;
}
}
}