Skip to content
This repository was archived by the owner on Mar 8, 2025. It is now read-only.

Commit

Permalink
修改白名单验证机制
Browse files Browse the repository at this point in the history
  • Loading branch information
ACaiCat committed Oct 12, 2024
1 parent 7c6a96f commit e9439be
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 54 deletions.
40 changes: 15 additions & 25 deletions CaiBotMod/Common/Login.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Terraria;

namespace CaiBotMod.Common;

public static class Login
{
public static async Task<bool> CheckWhiteAsync(string name, int code, List<string> uuids)
public static async Task<bool> CheckWhiteAsync(string name, int code)
{
var playerList = TSPlayer.FindByNameOrID("tsn:" + name);
var number = Config.config.GroupNumber;
Expand All @@ -21,6 +22,7 @@ public static async Task<bool> CheckWhiteAsync(string name, int code, List<strin
{
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})版本可能过低...");
plr.Disconnect("你的游戏版本可能过低,请使用Terraria1.4.4+游玩");
Netplay.Clients[plr.Index].Socket.Close();
return false;
}

Expand All @@ -31,65 +33,53 @@ public static async Task<bool> CheckWhiteAsync(string name, int code, List<strin
case 200:
{
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})已通过白名单验证...");
//NetMessage.SendData(9, args.Who, -1, Terraria.Localization.NetworkText.FromLiteral($"[白名单]白名单校验成功!\n"), 1);
Packet.Login[plr.Index] = true;
break;
}
case 404:
{
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})没有添加白名单...");
plr.SilentKickInProgress = true;
plr.Disconnect($"没有添加白名单\n请在群{number}内发送'添加白名单 角色名字'");
Netplay.Clients[plr.Index].Socket.Close();
return false;
}
case 403:
{
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})白名单被冻结...");
plr.SilentKickInProgress = true;
plr.Disconnect("[白名单]你已被服务器屏蔽\n你在云黑名单内!");
Netplay.Clients[plr.Index].Socket.Close();
return false;
}
case 401:
{
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})不在本群内...");
plr.SilentKickInProgress = true;
plr.Disconnect($"[白名单]不在本服务器群内!\n请加入服务器群:{number}");
Netplay.Clients[plr.Index].Socket.Close();
return false;
}
default:
case 405:
{
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})触发了百年一遇的白名单bug...");
Console.WriteLine(($"[Cai白名单]玩家[{name}](IP: {plr.IP})使用未授权的设备..."));
plr.SilentKickInProgress = true;
plr.Disconnect("[白名单]这个情况可能只是一个摆设\n但是你触发了它?");
plr.Disconnect($"[Cai白名单]在群{number}内发送'登录',\n" +
$"以批准此设备登录");
Netplay.Clients[plr.Index].Socket.Close();

return false;
}
}

if (!uuids.Contains(plr.UUID))
{
if (string.IsNullOrEmpty(plr.UUID))
{
//Console.WriteLine(plr.Index);
plr.SilentKickInProgress = true;
plr.Disconnect("[白名单]UUID为空\n请尝试重新加入游戏或者联系服务器管理员");
return false;
}

Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})没有批准登录...");
plr.SilentKickInProgress = true;
plr.Disconnect($"[白名单]在群{number}内发送'登录',\n以批准此设备登录");

RestObject re = new () { { "type", "device" }, { "uuid", plr.UUID }, { "ip", plr.IP }, { "name", name } };
await MessageHandle.SendDateAsync(re.ToJson());

return false;
}

}
catch (Exception ex)
{
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})验证白名单时出现错误...");
Console.WriteLine("[XSB适配插件]:\n" + ex);
plr.SilentKickInProgress = true;
plr.Disconnect($"[白名单]服务器发生错误无法处理该请求!请尝试重新加入游戏或者联系服务器群{number}管理员");
Netplay.Clients[plr.Index].Socket.Close();
return false;
}

Expand Down
13 changes: 1 addition & 12 deletions CaiBotMod/Common/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,7 @@ public static async Task HandleMessageAsync(string receivedData)
case "whitelist":
var name = (string) jsonObject["name"]!;
var code = (int) jsonObject["code"]!;
var uuids = jsonObject["uuids"]!.ToObject<List<string>>()!;
if (await Login.CheckWhiteAsync(name, code, uuids))
{
var playerList = TSPlayer.FindByNameOrID("tsn:" + name);
if (playerList.Count == 0)
{
return;
}

Packet.Login[playerList[0].Index] = true;
}

await Login.CheckWhiteAsync(name, code);
break;
case "selfkick":
name = (string) jsonObject["name"]!;
Expand Down
61 changes: 44 additions & 17 deletions CaiBotMod/Common/Packet.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using Terraria;
using Terraria.Chat;
using Terraria.DataStructures;
Expand All @@ -14,8 +15,8 @@ namespace CaiBotMod.Common;

public class Packet : ModSystem
{
public static string[] UUIDs = new string[256];
public static bool[] Login = new bool[256];
public static readonly string[] UUIDs = new string[256];
public static readonly bool[] Login = new bool[256];

public override bool HijackSendData(int whoAmI, int msgType, int remoteClient, int ignoreClient, NetworkText text,
int number, float number2, float number3, float number4, int number5, int number6, int number7)
Expand All @@ -40,35 +41,61 @@ public override bool HijackGetData(ref byte messageType, ref BinaryReader Reader
{
case 4:
{

if (!Config.config.WhiteList)
{
return false;
}

if (Login[playerNumber])
{
return false;
}


reader.ReadByte();
reader.ReadByte();
reader.ReadByte();
var name = reader.ReadString().Trim().Trim();

var name = reader.ReadString().Trim();

if (name.Length == 17)
{
return false;
}

RestObject re = new () { { "type", "whitelist" }, { "name", name } };
if (!MessageHandle.IsWebsocketConnected)
Task.Run(async () =>
{
Console.WriteLine("[CaiBot]机器人处于未连接状态, 玩家无法加入。\n" +
"如果你不想使用Cai白名单,可以在tshock/CaiBot.json中将其关闭。");
return true;
}

MessageHandle.SendDateAsync(re.ToJson()).Wait();
break;
var timeout = Task.Delay(1000); //貌似不是很优雅捏
await Task.Run(async () =>
{
while ((Login[playerNumber] && UUIDs[playerNumber]!=null && timeout.IsCompleted == false) || (UUIDs[playerNumber]==null && timeout.IsCompleted == false))
{
await Task.Delay(10);
}

});
if (!Login[playerNumber] && timeout.IsCompleted)
{
NetMessage.SendData(MessageID.Kick, playerNumber, -1, NetworkText.FromLiteral("[CaiBot]UUID等待超时"));
Netplay.Clients[playerNumber].Socket.Close();
return true;
}
if (Login[playerNumber])
{
return false;
}
RestObject re = new ()
{
{ "type", "whitelistV2" }, { "name", name }, { "uuid", UUIDs[playerNumber] }, { "ip", Netplay.Clients[playerNumber].Socket.GetRemoteAddress().ToString()! },
};
if (!MessageHandle.IsWebsocketConnected)
{
Console.WriteLine("[CaiBot]机器人处于未连接状态, 玩家无法加入。\n" +
"如果你不想使用Cai白名单,可以在tshock/CaiBot.json中将其关闭。");
return true;
}

await MessageHandle.SendDateAsync(re.ToJson());

return false;
});
return false;
}
case 68:
{
Expand Down

0 comments on commit e9439be

Please sign in to comment.