Skip to content

Commit 6720aed

Browse files
committed
功能复刻
1 parent 641cd22 commit 6720aed

File tree

57 files changed

+2553
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2553
-0
lines changed
Binary file not shown.
Binary file not shown.
49 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

Form1.Designer.cs

+632
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Form1.cs

+546
Large diffs are not rendered by default.

Form1.resx

+496
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net6.0-windows</TargetFramework>
6+
<RootNamespace>Palworld_server_protector_DotNet</RootNamespace>
7+
<Nullable>enable</Nullable>
8+
<UseWindowsForms>true</UseWindowsForms>
9+
<ImplicitUsings>enable</ImplicitUsings>
10+
<ApplicationIcon>icon.ico</ApplicationIcon>
11+
<AssemblyVersion>2.0.1</AssemblyVersion>
12+
<Copyright>Kiros @ONBETA</Copyright>
13+
<Version>$(VersionPrefix)</Version>
14+
<Company>$(Authors)@ONBETA</Company>
15+
</PropertyGroup>
16+
17+
<ItemGroup>
18+
<Content Include="icon.ico" />
19+
</ItemGroup>
20+
21+
</Project>
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net6.0-windows</TargetFramework>
6+
<RootNamespace>Palworld_server_protector_DotNet</RootNamespace>
7+
<Nullable>enable</Nullable>
8+
<UseWindowsForms>true</UseWindowsForms>
9+
<ImplicitUsings>enable</ImplicitUsings>
10+
<ApplicationIcon>icon.ico</ApplicationIcon>
11+
<AssemblyVersion>2.0.1</AssemblyVersion>
12+
<Copyright>Kiros @ONBETA</Copyright>
13+
<Version>$(VersionPrefix)2.0.1</Version>
14+
<Company>$(Authors)@ONBETA</Company>
15+
<FileVersion>2.0.1</FileVersion>
16+
</PropertyGroup>
17+
18+
<ItemGroup>
19+
<Content Include="icon.ico" />
20+
</ItemGroup>
21+
22+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Compile Update="Form1.cs">
5+
<SubType>Form</SubType>
6+
</Compile>
7+
</ItemGroup>
8+
</Project>

Palworld-server-protector-DotNet.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34511.84
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Palworld-server-protector-DotNet", "Palworld-server-protector-DotNet.csproj", "{7B4126A6-6075-4ADA-B5DD-D019B891B553}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{7B4126A6-6075-4ADA-B5DD-D019B891B553}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{7B4126A6-6075-4ADA-B5DD-D019B891B553}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{7B4126A6-6075-4ADA-B5DD-D019B891B553}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{7B4126A6-6075-4ADA-B5DD-D019B891B553}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {19D38278-77BA-4595-8523-33F17F5060B7}
24+
EndGlobalSection
25+
EndGlobal

Program.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Palworld_server_protector_DotNet
2+
{
3+
internal static class Program
4+
{
5+
/// <summary>
6+
/// The main entry point for the application.
7+
/// </summary>
8+
[STAThread]
9+
static void Main()
10+
{
11+
// To customize application configuration such as set high DPI settings or default font,
12+
// see https://aka.ms/applicationconfiguration.
13+
ApplicationConfiguration.Initialize();
14+
Application.Run(new Form1());
15+
}
16+
}
17+
}

Rcon.cs

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
using System;
2+
using System.Linq;
3+
using System.Net.Sockets;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows.Forms;
7+
using System.Collections.Generic;
8+
using System.Security.Permissions;
9+
namespace Palworld_server_protector_DotNet
10+
{
11+
12+
internal class Rcon
13+
{
14+
internal static TcpClient Client;
15+
private static NetworkStream networkStream;
16+
17+
static Form form;
18+
19+
[STAThread]
20+
static async Task Main(string[] args)
21+
{
22+
Application.EnableVisualStyles();
23+
Application.SetCompatibleTextRenderingDefault(false);
24+
form = new Form1();
25+
Application.Run(form);
26+
27+
AppDomain.CurrentDomain.ProcessExit += ProcessExit;
28+
}
29+
30+
public static async Task<string> Connect(string address, string port, string pass)
31+
{
32+
Client = new TcpClient();
33+
34+
try
35+
{
36+
await Client.ConnectAsync(address, int.Parse(port));
37+
}
38+
catch
39+
{
40+
return "Rcon连接失败";
41+
}
42+
43+
if (!Client.Connected)
44+
{
45+
Client.Dispose();
46+
Client = null;
47+
return "Rcon连接失败";
48+
}
49+
50+
networkStream = Client.GetStream();
51+
52+
Pck auth = new Pck(0xf5, PacketType.AUTH, Encoding.ASCII.GetBytes(pass));
53+
54+
networkStream.Write(auth.ToBytes(), 0, auth.Len);
55+
56+
int size = networkStream.ReadByte();
57+
byte[] data = new byte[size];
58+
59+
await networkStream.ReadAsync(data, 0, size);
60+
61+
if (data[3] != 0xf5)
62+
{
63+
if (Client != null)
64+
{
65+
if (Client.Connected)
66+
{
67+
Client.GetStream().Close();
68+
}
69+
Client.Dispose();
70+
}
71+
return "认证失败";
72+
}
73+
74+
var output = await SInfo();
75+
76+
return string.Format("连接成功" + Environment.NewLine + $"{output}");
77+
}
78+
79+
public static async Task<string> SInfo(bool keep = false)
80+
{
81+
if (Client != null)
82+
{
83+
while (networkStream.DataAvailable) { _ = networkStream.ReadByte(); }
84+
Pck pck = new Pck(0x1f, PacketType.EXECCOMMAND, Encoding.ASCII.GetBytes("info"));
85+
networkStream.Write(pck.ToBytes(), 0, pck.Len);
86+
87+
int size = networkStream.ReadByte();
88+
var data = new byte[size];
89+
await networkStream.ReadAsync(data, 0, size);
90+
91+
if (!keep)
92+
{
93+
return Encoding.UTF8.GetString(data.Skip(11).ToArray());
94+
}
95+
}
96+
97+
return "";
98+
}
99+
100+
public static async Task<string> GetPlayers()
101+
{
102+
if (Client != null)
103+
{
104+
try
105+
{
106+
if (networkStream != null)
107+
{
108+
while (networkStream.DataAvailable) { _ = networkStream.ReadByte(); }
109+
Pck pck = new Pck(0x1f, PacketType.EXECCOMMAND, Encoding.ASCII.GetBytes("showplayers"));
110+
networkStream.Write(pck.ToBytes(), 0, pck.Len);
111+
112+
int size = networkStream.ReadByte();
113+
byte[] data = new byte[size];
114+
115+
await networkStream.ReadAsync(data, 0, size);
116+
117+
string resstring = Encoding.UTF8.GetString(data.Skip(34).ToArray());
118+
string[] players = resstring.Split('\n');
119+
120+
string output = "";
121+
122+
for (int i = 1; i <= players.Length; i++)
123+
{
124+
output += $"{i}:" + players[i - 1] + Environment.NewLine;
125+
}
126+
127+
return resstring;
128+
}
129+
return "";
130+
}
131+
catch (NullReferenceException)
132+
{
133+
return "获取玩家失败";
134+
}
135+
}
136+
return "获取玩家失败";
137+
}
138+
139+
public static async Task<string> Broadcast(string text)
140+
{
141+
if (Client != null)
142+
{
143+
try
144+
{
145+
if (networkStream != null)
146+
{
147+
while (networkStream.DataAvailable) { _ = networkStream.ReadByte(); }
148+
Pck packet = new Pck(0x1f, PacketType.EXECCOMMAND, Encoding.UTF8.GetBytes("broadcast " + text));
149+
networkStream.Write(packet.ToBytes(), 0, packet.Len);
150+
151+
int size = networkStream.ReadByte();
152+
byte[] data = new byte[size];
153+
154+
await networkStream.ReadAsync(data, 0, size);
155+
156+
string resstring = Encoding.UTF8.GetString(data.Skip(11).ToArray());
157+
158+
return resstring;
159+
}
160+
return "";
161+
}
162+
catch (NullReferenceException)
163+
{
164+
return "广播失败";
165+
}
166+
}
167+
return "广播失败";
168+
}
169+
170+
public static async Task Reload()
171+
{
172+
if (Client.Connected)
173+
{
174+
await SInfo(true);
175+
}
176+
}
177+
178+
/*public static async Task<string> ShutDown(string time, string text)
179+
{
180+
181+
}*/
182+
183+
[Serializable]
184+
public class Pck
185+
{
186+
public int Size;
187+
public int ID;
188+
public PacketType Type;
189+
public byte[] Body;
190+
191+
public Pck(int id, PacketType type, byte[] body)
192+
{
193+
ID = id; Type = type; Body = body.Concat(new byte[] { 0x00, 0x00 }).ToArray(); Size = body.Length + 10;
194+
}
195+
196+
internal byte[] ToBytes()
197+
{
198+
byte[] res = new byte[Size + 4];
199+
BitConverter.GetBytes(Size).CopyTo(res, 0);
200+
BitConverter.GetBytes(ID).CopyTo(res, 4);
201+
BitConverter.GetBytes((int)Type).CopyTo(res, 8);
202+
Body.CopyTo(res, 12);
203+
return res;
204+
}
205+
206+
internal int Len => Body.Length + 12;
207+
}
208+
209+
public enum PacketType : int
210+
{
211+
RESPONSE_VALUE = 0,
212+
AUTH_RESPONSE = 2,
213+
EXECCOMMAND = 2,
214+
AUTH = 3
215+
}
216+
217+
public static void ProcessExit(object sender, EventArgs ea)
218+
{
219+
if (Client != null)
220+
{
221+
if (Client.Connected)
222+
{
223+
Client.GetStream().Close();
224+
}
225+
Client.Dispose();
226+
Client = null;
227+
}
228+
}
229+
}
230+
}

Rcon/Encoder.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Text;
2+
3+
namespace Palworld_server_protector_DotNet;
4+
5+
public class Encoder
6+
{
7+
public const int HeaderLength = 10; // Does not include 4-byte message length.
8+
9+
public static byte[] EncodeMessage(RconMessage msg)
10+
{
11+
List<byte> bytes = new List<byte>();
12+
13+
bytes.AddRange(BitConverter.GetBytes(msg.Length));
14+
bytes.AddRange(BitConverter.GetBytes(msg.ID));
15+
bytes.AddRange(BitConverter.GetBytes((int) msg.Type));
16+
bytes.AddRange(Encoding.ASCII.GetBytes(msg.Body));
17+
bytes.AddRange(new byte[] {0, 0});
18+
19+
return bytes.ToArray();
20+
}
21+
22+
public static RconMessage DecodeMessage(byte[] bytes)
23+
{
24+
int len = BitConverter.ToInt32(bytes, 0);
25+
int id = BitConverter.ToInt32(bytes, 4);
26+
int type = BitConverter.ToInt32(bytes, 8);
27+
28+
int bodyLen = bytes.Length - (HeaderLength + 4);
29+
if(bodyLen > 0)
30+
{
31+
byte[] bodyBytes = new byte[bodyLen];
32+
Array.Copy(bytes, 12, bodyBytes, 0, bodyLen);
33+
Array.Resize(ref bodyBytes, bodyLen);
34+
return new RconMessage(len, id, (RconMessageType) type, Encoding.ASCII.GetString(bodyBytes));
35+
}
36+
else
37+
{
38+
return new RconMessage(len, id, (RconMessageType) type, "");
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)