From e12a10d467a088f54054822a26852fbf481cafdd Mon Sep 17 00:00:00 2001 From: Injector Date: Fri, 27 May 2022 20:27:31 +0300 Subject: [PATCH] Initial commit --- Server - C#/TcpServer/TcpServer.sln | 25 +++ Server - C#/TcpServer/TcpServer/App.config | 6 + Server - C#/TcpServer/TcpServer/Program.cs | 16 ++ .../TcpServer/Properties/AssemblyInfo.cs | 36 ++++ Server - C#/TcpServer/TcpServer/Server.cs | 171 ++++++++++++++++++ .../TcpServer/TcpServer/TcpServer.csproj | 55 ++++++ Server - C#/TcpServer/TcpServer/XmlParser.cs | 15 ++ Web - php/tcp_rcon.php | 40 ++++ 8 files changed, 364 insertions(+) create mode 100644 Server - C#/TcpServer/TcpServer.sln create mode 100644 Server - C#/TcpServer/TcpServer/App.config create mode 100644 Server - C#/TcpServer/TcpServer/Program.cs create mode 100644 Server - C#/TcpServer/TcpServer/Properties/AssemblyInfo.cs create mode 100644 Server - C#/TcpServer/TcpServer/Server.cs create mode 100644 Server - C#/TcpServer/TcpServer/TcpServer.csproj create mode 100644 Server - C#/TcpServer/TcpServer/XmlParser.cs create mode 100644 Web - php/tcp_rcon.php diff --git a/Server - C#/TcpServer/TcpServer.sln b/Server - C#/TcpServer/TcpServer.sln new file mode 100644 index 0000000..7cef0ef --- /dev/null +++ b/Server - C#/TcpServer/TcpServer.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32413.511 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TcpServer", "TcpServer\TcpServer.csproj", "{E82A6974-DF0B-4AD8-97DE-E561ADFF9F94}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E82A6974-DF0B-4AD8-97DE-E561ADFF9F94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E82A6974-DF0B-4AD8-97DE-E561ADFF9F94}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E82A6974-DF0B-4AD8-97DE-E561ADFF9F94}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E82A6974-DF0B-4AD8-97DE-E561ADFF9F94}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D557D5DD-8BC6-41C8-BC9D-AEDE37BB46F4} + EndGlobalSection +EndGlobal diff --git a/Server - C#/TcpServer/TcpServer/App.config b/Server - C#/TcpServer/TcpServer/App.config new file mode 100644 index 0000000..193aecc --- /dev/null +++ b/Server - C#/TcpServer/TcpServer/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Server - C#/TcpServer/TcpServer/Program.cs b/Server - C#/TcpServer/TcpServer/Program.cs new file mode 100644 index 0000000..1f92171 --- /dev/null +++ b/Server - C#/TcpServer/TcpServer/Program.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TcpServer +{ + public class Program + { + public static void Main() + { + Server.Initialize(); + } + } +} diff --git a/Server - C#/TcpServer/TcpServer/Properties/AssemblyInfo.cs b/Server - C#/TcpServer/TcpServer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..de4ca37 --- /dev/null +++ b/Server - C#/TcpServer/TcpServer/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Общие сведения об этой сборке предоставляются следующим набором +// набора атрибутов. Измените значения этих атрибутов для изменения сведений, +// связанные с этой сборкой. +[assembly: AssemblyTitle("TCP Server")] +[assembly: AssemblyDescription("TCP C# Server for listening php clients")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Xleb Factory")] +[assembly: AssemblyProduct("TCP Server")] +[assembly: AssemblyCopyright("© Bloomstorm, 2021-2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Установка значения False для параметра ComVisible делает типы в этой сборке невидимыми +// для компонентов COM. Если необходимо обратиться к типу в этой сборке через +// из модели COM задайте для атрибута ComVisible этого типа значение true. +[assembly: ComVisible(false)] + +// Следующий GUID представляет идентификатор typelib, если этот проект доступен из модели COM +[assembly: Guid("e82a6974-df0b-4ad8-97de-e561adff9f94")] + +// Сведения о версии сборки состоят из указанных ниже четырех значений: +// +// Основной номер версии +// Дополнительный номер версии +// Номер сборки +// Номер редакции +// +// Можно задать все значения или принять номера сборки и редакции по умолчанию +// используя "*", как показано ниже: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Server - C#/TcpServer/TcpServer/Server.cs b/Server - C#/TcpServer/TcpServer/Server.cs new file mode 100644 index 0000000..d7d005d --- /dev/null +++ b/Server - C#/TcpServer/TcpServer/Server.cs @@ -0,0 +1,171 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using System.Net; +using System.Net.Sockets; +using System.Xml; +using System.Xml.Serialization; + +namespace TcpServer +{ + public class Server + { + public const string SERVER_CONFIG_FILE = "ServerConfig.xml"; + public static string RconPassword { get; private set; } + public static int ListMode { get; private set; } + public static string[] IPList { get; private set; } + private static TcpListener _TcpListener; + private static bool _initialized; + + public static void Initialize() + { + if (_initialized) return; + _initialized = true; + var xmlConfig = new XmlServerConfig(); + if (File.Exists(SERVER_CONFIG_FILE)) + { + xmlConfig = XmlParser.Parse(File.ReadAllText(SERVER_CONFIG_FILE)); + } + TcpListener server = null; + var ipAddress = IPAddress.Parse(xmlConfig.Ip); + + RconPassword = xmlConfig.Rcon; + ListMode = xmlConfig.ListMode; + IPList = xmlConfig.Ips.Select(xmlIp => xmlIp.Ip).ToArray(); + + server = new TcpListener(ipAddress, xmlConfig.Port); + server.Start(); + _TcpListener = server; + Console.WriteLine($"Server is running with IP {xmlConfig.Ip} port {xmlConfig.Port}"); + + while (true) + { + var socket = server.AcceptSocket(); + Console.WriteLine($"Connection accepted from {socket.RemoteEndPoint}"); + if (ListMode == 1) + { + var clientAddress = socket.RemoteEndPoint.ToString().Split(':')[0]; + var found = false; + for (int i = 0; i < IPList.Length; i++) + { + if (IPList[i] == clientAddress) + found = true; + } + if (!found) + { + socket.Close(); + Console.WriteLine($"List Mode = 1, non white listed IP. Connection closed."); + return; + } + } + if (ListMode == 2) + { + var clientAddress = socket.RemoteEndPoint.ToString().Split(':')[0]; + var found = false; + for (int i = 0; i < IPList.Length; i++) + { + if (IPList[i] == clientAddress) + found = true; + } + if (found) + { + socket.Close(); + Console.WriteLine($"List Mode = 2, black listed IP. Connection closed."); + return; + } + } + var buffer = new byte[256]; + var receivedMessage = ReceiveString(socket, 1, buffer); + Console.WriteLine($"Received: {receivedMessage}"); + var args = receivedMessage.Split('|'); + if (args.Length > 0) + { + if (args[0] != RconPassword) + { + Console.Write("Invalid RCON password"); + SendString(socket, 1, "Invalid RCON Password"); + socket.Close(); + } + else + { + if (args.Length > 1) + { + switch (args[1]) + { + case "1": + Console.Write("Sending Pong"); + SendString(socket, 1, "Pong"); + break; + } + } + } + } + socket.Close(); + } + } + + public static void Stop() + { + _TcpListener.Stop(); + _initialized = false; + RconPassword = string.Empty; + ListMode = 0; + IPList = null; + } + + public static void SendString(Socket socket, int encodingType, string message) + { + switch (encodingType) + { + case 0: + var ascii = new ASCIIEncoding(); + socket.Send(ascii.GetBytes(message)); + break; + case 1: + var utf = new UTF8Encoding(); + socket.Send(utf.GetBytes(message)); + break; + } + } + + public static string ReceiveString(Socket socket, int encodingType, byte[] buffer) + { + switch (encodingType) + { + case 0: + var asciiK = socket.Receive(buffer); + return Encoding.ASCII.GetString(buffer.Take(asciiK).ToArray()); + case 1: + var utf8K = socket.Receive(buffer); + return Encoding.UTF8.GetString(buffer.Take(utf8K).ToArray()); + } + return string.Empty; + } + + [Serializable] + [XmlRoot(ElementName = "config")] + public class XmlServerConfig + { + [XmlElement("ip")] + public string Ip = "127.0.0.1"; + [XmlElement("port")] + public int Port = 27015; + [XmlElement("rcon")] + public string Rcon = string.Empty; + [XmlElement("listmode")] + public int ListMode = 0; + [XmlArray("ips"), XmlArrayItem("ip")] + public List Ips = new List(); + } + + [XmlRoot("ip")] + public class XmlIp + { + [XmlAttribute("ip")] + public string Ip = "127.0.0.1"; + } + } +} diff --git a/Server - C#/TcpServer/TcpServer/TcpServer.csproj b/Server - C#/TcpServer/TcpServer/TcpServer.csproj new file mode 100644 index 0000000..8981b2b --- /dev/null +++ b/Server - C#/TcpServer/TcpServer/TcpServer.csproj @@ -0,0 +1,55 @@ + + + + + Debug + AnyCPU + {E82A6974-DF0B-4AD8-97DE-E561ADFF9F94} + Exe + TcpServer + TcpServer + v4.8 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Server - C#/TcpServer/TcpServer/XmlParser.cs b/Server - C#/TcpServer/TcpServer/XmlParser.cs new file mode 100644 index 0000000..bdfdd41 --- /dev/null +++ b/Server - C#/TcpServer/TcpServer/XmlParser.cs @@ -0,0 +1,15 @@ +using System.Xml.Serialization; +using System.IO; + +namespace TcpServer +{ + public static class XmlParser + { + public static T Parse(string xml) + { + var xmlSerializer = new XmlSerializer(typeof(T)); + using (var stringReader = new StringReader(xml)) + return (T)xmlSerializer.Deserialize(stringReader); + } + } +} diff --git a/Web - php/tcp_rcon.php b/Web - php/tcp_rcon.php new file mode 100644 index 0000000..fc3a928 --- /dev/null +++ b/Web - php/tcp_rcon.php @@ -0,0 +1,40 @@ + 0 ? "$rcon|$cmdId|$cmdArguments" : "$rcon|$cmdId"; +$out = ""; + +echo "Sending cmd...\n"; +socket_write($socket, $in, strlen($in)); +echo "Sent cmd\n"; +echo "Reading response\n"; + +while ($out = socket_read($socket, 2048)) +{ + echo $out; +} +socket_close($socket); +?> \ No newline at end of file