2018-11-25 21:00:36 -05:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using SharedLibraryCore.Exceptions;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using SharedLibraryCore.RCon;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2018-04-11 18:24:21 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-02-05 19:02:45 -05:00
|
|
|
|
using static SharedLibraryCore.Server;
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2018-08-07 14:43:09 -04:00
|
|
|
|
namespace IW4MAdmin.Application.RconParsers
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2019-06-24 12:01:34 -04:00
|
|
|
|
#if DEBUG
|
|
|
|
|
public class BaseRConParser : IRConParser
|
|
|
|
|
#else
|
2019-02-02 20:40:37 -05:00
|
|
|
|
class BaseRConParser : IRConParser
|
2019-06-24 12:01:34 -04:00
|
|
|
|
#endif
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2019-02-02 20:40:37 -05:00
|
|
|
|
public BaseRConParser()
|
2018-04-13 02:32:30 -04:00
|
|
|
|
{
|
2019-01-26 21:33:37 -05:00
|
|
|
|
Configuration = new DynamicRConParserConfiguration()
|
|
|
|
|
{
|
|
|
|
|
CommandPrefixes = new CommandPrefix()
|
|
|
|
|
{
|
2019-02-05 12:14:43 -05:00
|
|
|
|
Tell = "tell {0} {1}",
|
|
|
|
|
Say = "say {0}",
|
2019-01-26 21:33:37 -05:00
|
|
|
|
Kick = "clientkick {0} \"{1}\"",
|
|
|
|
|
Ban = "clientkick {0} \"{1}\"",
|
2019-02-01 20:49:25 -05:00
|
|
|
|
TempBan = "tempbanclient {0} \"{1}\"",
|
2019-02-03 21:47:05 -05:00
|
|
|
|
RConCommand = "ÿÿÿÿrcon {0} {1}",
|
|
|
|
|
RConGetDvar = "ÿÿÿÿrcon {0} {1}",
|
|
|
|
|
RConSetDvar = "ÿÿÿÿrcon {0} set {1}",
|
2019-02-01 20:49:25 -05:00
|
|
|
|
RConGetStatus = "ÿÿÿÿgetstatus",
|
|
|
|
|
RConGetInfo = "ÿÿÿÿgetinfo",
|
|
|
|
|
RConResponse = "ÿÿÿÿprint",
|
2019-01-26 21:33:37 -05:00
|
|
|
|
},
|
|
|
|
|
};
|
2019-01-27 19:41:54 -05:00
|
|
|
|
|
2019-02-06 21:12:35 -05:00
|
|
|
|
Configuration.Status.Pattern = @"^ *([0-9]+) +-?([0-9]+) +((?:[A-Z]+|[0-9]+)) +((?:[a-z]|[0-9]){8,32}|(?:[a-z]|[0-9]){8,32}|bot[0-9]+|(?:[0-9]+)) +(.{0,32}) +([0-9]+) +(\d+\.\d+\.\d+.\d+\:-*\d{1,5}|0+.0+:-*\d{1,5}|loopback) +(-*[0-9]+) +([0-9]+) *$";
|
2019-02-02 20:40:37 -05:00
|
|
|
|
Configuration.Status.AddMapping(ParserRegex.GroupType.RConClientNumber, 1);
|
|
|
|
|
Configuration.Status.AddMapping(ParserRegex.GroupType.RConScore, 2);
|
|
|
|
|
Configuration.Status.AddMapping(ParserRegex.GroupType.RConPing, 3);
|
|
|
|
|
Configuration.Status.AddMapping(ParserRegex.GroupType.RConNetworkId, 4);
|
|
|
|
|
Configuration.Status.AddMapping(ParserRegex.GroupType.RConName, 5);
|
|
|
|
|
Configuration.Status.AddMapping(ParserRegex.GroupType.RConIpAddress, 7);
|
2019-02-01 20:49:25 -05:00
|
|
|
|
|
2019-02-05 12:14:43 -05:00
|
|
|
|
Configuration.Dvar.Pattern = "^\"(.+)\" is: \"(.+)?\" default: \"(.+)?\"\n(?:latched: \"(.+)?\"\n)? *(.+)$";
|
2019-02-02 20:40:37 -05:00
|
|
|
|
Configuration.Dvar.AddMapping(ParserRegex.GroupType.RConDvarName, 1);
|
|
|
|
|
Configuration.Dvar.AddMapping(ParserRegex.GroupType.RConDvarValue, 2);
|
|
|
|
|
Configuration.Dvar.AddMapping(ParserRegex.GroupType.RConDvarDefaultValue, 3);
|
|
|
|
|
Configuration.Dvar.AddMapping(ParserRegex.GroupType.RConDvarLatchedValue, 4);
|
|
|
|
|
Configuration.Dvar.AddMapping(ParserRegex.GroupType.RConDvarDomain, 5);
|
2019-01-26 21:33:37 -05:00
|
|
|
|
}
|
2018-04-29 16:44:04 -04:00
|
|
|
|
|
2019-01-26 21:33:37 -05:00
|
|
|
|
public IRConParserConfiguration Configuration { get; set; }
|
2018-04-29 16:44:04 -04:00
|
|
|
|
|
2019-06-24 12:01:34 -04:00
|
|
|
|
public virtual string Version { get; set; } = "CoD";
|
2019-02-05 19:02:45 -05:00
|
|
|
|
public Game GameName { get; set; } = Game.COD;
|
|
|
|
|
public bool CanGenerateLogPath { get; set; } = true;
|
2019-02-02 19:54:30 -05:00
|
|
|
|
|
2018-04-11 18:24:21 -04:00
|
|
|
|
public async Task<string[]> ExecuteCommandAsync(Connection connection, string command)
|
|
|
|
|
{
|
2018-08-28 17:32:59 -04:00
|
|
|
|
var response = await connection.SendQueryAsync(StaticHelpers.QueryType.COMMAND, command);
|
2018-05-10 01:34:29 -04:00
|
|
|
|
return response.Skip(1).ToArray();
|
2018-04-11 18:24:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Dvar<T>> GetDvarAsync<T>(Connection connection, string dvarName)
|
|
|
|
|
{
|
2019-02-03 21:47:05 -05:00
|
|
|
|
string[] lineSplit = await connection.SendQueryAsync(StaticHelpers.QueryType.GET_DVAR, dvarName);
|
2019-02-01 20:49:25 -05:00
|
|
|
|
string response = string.Join('\n', lineSplit.Skip(1));
|
|
|
|
|
var match = Regex.Match(response, Configuration.Dvar.Pattern);
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2019-05-03 21:13:51 -04:00
|
|
|
|
if (!lineSplit[0].Contains(Configuration.CommandPrefixes.RConResponse) ||
|
|
|
|
|
response.Contains("Unknown command") ||
|
|
|
|
|
!match.Success)
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2019-05-03 21:13:51 -04:00
|
|
|
|
throw new DvarException(Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_DVAR"].FormatExt(dvarName));
|
2018-04-11 18:24:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 20:49:25 -05:00
|
|
|
|
string value = match.Groups[Configuration.Dvar.GroupMapping[ParserRegex.GroupType.RConDvarValue]].Value.StripColors();
|
|
|
|
|
string defaultValue = match.Groups[Configuration.Dvar.GroupMapping[ParserRegex.GroupType.RConDvarDefaultValue]].Value.StripColors();
|
|
|
|
|
string latchedValue = match.Groups[Configuration.Dvar.GroupMapping[ParserRegex.GroupType.RConDvarLatchedValue]].Value.StripColors();
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2019-02-01 20:49:25 -05:00
|
|
|
|
return new Dvar<T>()
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2019-02-01 20:49:25 -05:00
|
|
|
|
Name = match.Groups[Configuration.Dvar.GroupMapping[ParserRegex.GroupType.RConDvarName]].Value.StripColors(),
|
2019-05-03 21:13:51 -04:00
|
|
|
|
Value = string.IsNullOrEmpty(value) ? default : (T)Convert.ChangeType(value, typeof(T)),
|
|
|
|
|
DefaultValue = string.IsNullOrEmpty(defaultValue) ? default : (T)Convert.ChangeType(defaultValue, typeof(T)),
|
|
|
|
|
LatchedValue = string.IsNullOrEmpty(latchedValue) ? default : (T)Convert.ChangeType(latchedValue, typeof(T)),
|
2019-02-01 20:49:25 -05:00
|
|
|
|
Domain = match.Groups[Configuration.Dvar.GroupMapping[ParserRegex.GroupType.RConDvarDomain]].Value.StripColors()
|
2018-04-11 18:24:21 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 12:01:34 -04:00
|
|
|
|
public virtual async Task<List<EFClient>> GetStatusAsync(Connection connection)
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2019-02-09 16:35:13 -05:00
|
|
|
|
string[] response = await connection.SendQueryAsync(StaticHelpers.QueryType.COMMAND_STATUS);
|
2018-04-11 18:24:21 -04:00
|
|
|
|
return ClientsFromStatus(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> SetDvarAsync(Connection connection, string dvarName, object dvarValue)
|
|
|
|
|
{
|
2019-02-03 21:47:05 -05:00
|
|
|
|
return (await connection.SendQueryAsync(StaticHelpers.QueryType.SET_DVAR, $"{dvarName} {dvarValue}")).Length > 0;
|
2018-04-11 18:24:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
private List<EFClient> ClientsFromStatus(string[] Status)
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2018-11-05 22:01:29 -05:00
|
|
|
|
List<EFClient> StatusPlayers = new List<EFClient>();
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2018-04-21 18:18:20 -04:00
|
|
|
|
if (Status.Length < 4)
|
2018-11-25 21:00:36 -05:00
|
|
|
|
{
|
2018-04-21 18:18:20 -04:00
|
|
|
|
throw new ServerException("Unexpected status response received");
|
2018-11-25 21:00:36 -05:00
|
|
|
|
}
|
2018-04-21 18:18:20 -04:00
|
|
|
|
|
2018-05-03 01:25:49 -04:00
|
|
|
|
int validMatches = 0;
|
2019-04-02 21:20:37 -04:00
|
|
|
|
foreach (string statusLine in Status)
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2019-04-02 21:20:37 -04:00
|
|
|
|
string responseLine = statusLine.Trim();
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2019-01-27 19:41:54 -05:00
|
|
|
|
var regex = Regex.Match(responseLine, Configuration.Status.Pattern, RegexOptions.IgnoreCase);
|
2018-05-03 01:25:49 -04:00
|
|
|
|
|
2018-04-29 16:44:04 -04:00
|
|
|
|
if (regex.Success)
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2018-05-03 01:25:49 -04:00
|
|
|
|
validMatches++;
|
2019-01-27 19:41:54 -05:00
|
|
|
|
int clientNumber = int.Parse(regex.Groups[Configuration.Status.GroupMapping[ParserRegex.GroupType.RConClientNumber]].Value);
|
|
|
|
|
int score = int.Parse(regex.Groups[Configuration.Status.GroupMapping[ParserRegex.GroupType.RConScore]].Value);
|
2018-04-29 16:44:04 -04:00
|
|
|
|
|
|
|
|
|
int ping = 999;
|
|
|
|
|
|
|
|
|
|
// their state can be CNCT, ZMBI etc
|
2019-01-27 19:41:54 -05:00
|
|
|
|
if (regex.Groups[Configuration.Status.GroupMapping[ParserRegex.GroupType.RConPing]].Value.Length <= 3)
|
2018-04-29 16:44:04 -04:00
|
|
|
|
{
|
2019-01-27 19:41:54 -05:00
|
|
|
|
ping = int.Parse(regex.Groups[Configuration.Status.GroupMapping[ParserRegex.GroupType.RConPing]].Value);
|
2018-04-29 16:44:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 23:33:38 -04:00
|
|
|
|
long networkId;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
networkId = regex.Groups[Configuration.Status.GroupMapping[ParserRegex.GroupType.RConNetworkId]].Value.ConvertGuidToLong();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (FormatException)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-27 19:41:54 -05:00
|
|
|
|
string name = regex.Groups[Configuration.Status.GroupMapping[ParserRegex.GroupType.RConName]].Value.StripColors().Trim();
|
|
|
|
|
int? ip = regex.Groups[Configuration.Status.GroupMapping[ParserRegex.GroupType.RConIpAddress]].Value.Split(':')[0].ConvertToIP();
|
2018-04-29 16:44:04 -04:00
|
|
|
|
|
2018-12-16 22:16:56 -05:00
|
|
|
|
var client = new EFClient()
|
2018-04-11 18:24:21 -04:00
|
|
|
|
{
|
2018-11-25 21:00:36 -05:00
|
|
|
|
CurrentAlias = new EFAlias()
|
|
|
|
|
{
|
2019-05-29 17:55:35 -04:00
|
|
|
|
Name = name,
|
|
|
|
|
IPAddress = ip
|
2018-11-25 21:00:36 -05:00
|
|
|
|
},
|
2018-04-29 16:44:04 -04:00
|
|
|
|
NetworkId = networkId,
|
|
|
|
|
ClientNumber = clientNumber,
|
|
|
|
|
Ping = ping,
|
2018-04-21 18:18:20 -04:00
|
|
|
|
Score = score,
|
2018-11-05 22:01:29 -05:00
|
|
|
|
State = EFClient.ClientState.Connecting
|
2018-04-11 18:24:21 -04:00
|
|
|
|
};
|
2018-12-16 22:16:56 -05:00
|
|
|
|
|
2018-12-17 14:45:16 -05:00
|
|
|
|
StatusPlayers.Add(client);
|
2018-04-11 18:24:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 16:36:26 -04:00
|
|
|
|
// this happens if status is requested while map is rotating
|
2018-05-03 01:25:49 -04:00
|
|
|
|
if (Status.Length > 5 && validMatches == 0)
|
|
|
|
|
{
|
2018-05-24 15:48:57 -04:00
|
|
|
|
throw new ServerException("Server is rotating map");
|
2018-05-03 01:25:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 18:24:21 -04:00
|
|
|
|
return StatusPlayers;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|