Skip to content

Commit

Permalink
Merge pull request #284 from ACaiCat/master
Browse files Browse the repository at this point in the history
更新: CaiBot代码格式化,白名单验证提示优化
  • Loading branch information
ACaiCat authored Jul 10, 2024
2 parents 36258d2 + faaf9db commit f759ec3
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 273 deletions.
8 changes: 5 additions & 3 deletions CaiBot/CaiBotPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public CaiBotPlayer()
{
Name = "CaiBot"
};

}

public override void SendMessage(string msg, Color color)
Expand All @@ -34,7 +33,9 @@ public override void SendMessage(string msg, byte red, byte green, byte blue)
{
result1 += item.Text;
}
Regex regex = new Regex(@"\[i(tem)?(?:\/s(?<Stack>\d{1,4}))?(?:\/p(?<Prefix>\d{1,3}))?:(?<NetID>-?\d{1,4})\]");

Regex regex =
new Regex(@"\[i(tem)?(?:\/s(?<Stack>\d{1,4}))?(?:\/p(?<Prefix>\d{1,3}))?:(?<NetID>-?\d{1,4})\]");

string result = regex.Replace(result1, m =>
{
Expand All @@ -47,7 +48,8 @@ public override void SendMessage(string msg, byte red, byte green, byte blue)
if (prefix == "0")
return $"[{Lang.GetItemName(int.Parse(netID))}]";
else
return $"[{Lang.prefix[int.Parse(prefix)]} {Lang.GetItemName(int.Parse(netID))}]"; //return $"[{Terraria.Lang.prefix[int.Parse(netID)]}]";
return
$"[{Lang.prefix[int.Parse(prefix)]} {Lang.GetItemName(int.Parse(netID))}]"; //return $"[{Terraria.Lang.prefix[int.Parse(netID)]}]";
return $"[{Lang.prefix[int.Parse(prefix)]} {Lang.GetItemName(int.Parse(netID))} ({stack})]";
});
CommandOutput.Add(result);
Expand Down
15 changes: 8 additions & 7 deletions CaiBot/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Config
public const string Path = "tshock/CaiBot.json";

public static Config config = new Config();

public void Write(string path = Path)
{
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
Expand Down Expand Up @@ -40,6 +41,7 @@ public static Config Read(string path = Path)
result = Config.Read(fileStream);
}
}

config = result;
return result;
}
Expand All @@ -51,13 +53,12 @@ public static Config Read(Stream stream)
{
result = JsonConvert.DeserializeObject<Config>(streamReader.ReadToEnd());
}

return result;
}
[JsonProperty("密钥")]
public string Token = "";
[JsonProperty("白名单开关")]
public bool WhiteList = true;
[JsonProperty("白名单拦截提示的群号")]
public long GroupNumber = 0;

[JsonProperty("密钥")] public string Token = "";
[JsonProperty("白名单开关")] public bool WhiteList = true;
[JsonProperty("白名单拦截提示的群号")] public long GroupNumber = 0;
}
}
}
141 changes: 69 additions & 72 deletions CaiBot/Login.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ public async static void OnGetData(GetDataEventArgs args)
return;
}

if ((player.State < 10 || player.Dead) && (int)type > 12 && (int)type != 16 && (int)type != 42 && (int)type != 50 &&
if ((player.State < 10 || player.Dead) && (int)type > 12 && (int)type != 16 && (int)type != 42 &&
(int)type != 50 &&
(int)type != 38 && (int)type != 21 && (int)type != 22)
{
args.Handled = true;
return;
}

try
{
using (var data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length - 1))
Expand All @@ -56,35 +58,35 @@ public async static void OnGetData(GetDataEventArgs args)
{
return;
}

data.ReadByte();
data.ReadByte();
data.ReadByte();
var name = data.ReadString().Trim().Trim();
var re = new RestObject
{
{ "type","whitelist" },
{ "name",name }
{ "type", "whitelist" },
{ "name", name }
};
if (!MessageHandle.isWebsocketConnected)
{
TShock.Log.ConsoleError("[CaiBot]机器人处于未连接状态, 玩家无法加入。\n" +
"如果你不想使用Cai白名单,可以在tshock/CaiBot.json中将其关闭。");
"如果你不想使用Cai白名单,可以在tshock/CaiBot.json中将其关闭。");

return;
}

await MessageHandle.SendDateAsync(re.ToJson());
await MessageHandle.SendDateAsync(re.ToJson());
//CheckWhite(name, player);
//{
// HandleLogin(player, Guid.NewGuid().ToString());
//}
}
}

}
catch (Exception e)
{
TShock.Log.ConsoleError(e.ToString());
}
}

public static async Task<bool> CheckWhiteAsync(string name, int code, List<string> uuids)
{
var playerList = TSPlayer.FindByNameOrID("tsn:" + name);
Expand All @@ -93,106 +95,95 @@ public static async Task<bool> CheckWhiteAsync(string name, int code, List<strin
{
return false;
}

TSPlayer plr = playerList[0];
//NetMessage.SendData(9, args.Who, -1, Terraria.Localization.NetworkText.FromLiteral($"[白名单]正在校验白名单..."), 1);
//NetMessage.SendData(9, args.Who, -1, Terraria.Localization.NetworkText.FromLiteral($"[Cai白名单]正在校验白名单..."), 1);
if (string.IsNullOrEmpty(name))
{
TShock.Log.Info($"[白名单]玩家[{name}](IP: {plr.IP};UUID: {plr.UUID})版本可能过低...");
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})版本可能过低...");
plr.Disconnect("你的游戏版本可能过低,请使用Terraria1.4.4+游玩");
TShock.Log.ConsoleInfo($"[Cai白名单]玩家[{name}](IP: {plr.IP})版本可能过低...");
plr.Disconnect("你的游戏版本可能过低,\n请使用Terraria1.4.4+游玩");
return false;
}

try
{
switch (code)
{
case 200:
{
TShock.Log.Info($"[白名单]玩家[{name}](IP: {plr.IP};UUID: {plr.UUID})已通过白名单验证...");
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})已通过白名单验证...");
//NetMessage.SendData(9, args.Who, -1, Terraria.Localization.NetworkText.FromLiteral($"[白名单]白名单校验成功!\n"), 1);
break;
}
{
TShock.Log.ConsoleInfo($"[Cai白名单]玩家[{name}](IP: {plr.IP})已通过白名单验证...");
//NetMessage.SendData(9, args.Who, -1, Terraria.Localization.NetworkText.FromLiteral($"[Cai白名单]白名单校验成功!\n"), 1);
break;
}
case 404:
{
TShock.Log.Info($"[白名单]玩家[{name}](IP: {plr.IP};UUID: {plr.UUID})没有添加白名单...");
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})没有添加白名单...");
plr.SilentKickInProgress = true;
plr.Disconnect($"没有添加白名单\n请在群{number}内发送'添加白名单 角色名字'");
return false;
}
{
TShock.Log.ConsoleInfo($"[Cai白名单]玩家[{name}](IP: {plr.IP})没有添加白名单...");
plr.SilentKickInProgress = true;
plr.Disconnect($"没有添加白名单!\n" +
$"请在群{number}内发送'添加白名单 角色名字'");
return false;
}
case 403:
{
TShock.Log.Info($"[白名单]玩家[{name}](IP: {plr.IP};UUID: {plr.UUID})白名单被冻结...");
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})白名单被冻结...");
plr.SilentKickInProgress = true;
plr.Disconnect("[白名单]你已被服务器屏蔽\n你在云黑名单内!");
return false;
}
{
TShock.Log.ConsoleInfo($"[Cai白名单]玩家[{name}](IP: {plr.IP})被屏蔽,处于CaiBot云黑名单中...");
plr.SilentKickInProgress = true;
plr.Disconnect("[Cai白名单]你已被服务器屏蔽,\n" +
"你处于CaiBot云黑名单中!");
return false;
}
case 401:
{
TShock.Log.Info($"[白名单]玩家[{name}](IP: {plr.IP};UUID: {plr.UUID})不在本群内...");
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})不在本群内...");
plr.SilentKickInProgress = true;
plr.Disconnect($"[白名单]不在本服务器群内!\n请加入服务器群:{number}");
return false;
}
default:
{
TShock.Log.Info($"[白名单]玩家[{name}](IP: {plr.IP};UUID: {plr.UUID})触发了百年一遇的白名单bug...");
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})触发了百年一遇的白名单bug...");
plr.SilentKickInProgress = true;
plr.Disconnect("[白名单]这个情况可能只是一个摆设\n但是你触发了它?");
return false;
}
{
TShock.Log.ConsoleInfo($"[Cai白名单]玩家[{name}](IP: {plr.IP})不在本群内...");
plr.SilentKickInProgress = true;
plr.Disconnect($"[Cai白名单]你不在服务器群内!\n" +
$"请加入服务器群: {number}");
return false;
}
}

if (!uuids.Contains(plr.UUID))
{
if (string.IsNullOrEmpty(plr.UUID))

{
plr.SilentKickInProgress = true;
plr.Disconnect("[白名单]UUID为空\n请尝试重新加入游戏或者联系服务器管理员");
plr.Disconnect("[Cai白名单]UUID获取失败!\n" +
"请尝试重新加入游戏或者联系服务器管理员");
return false;
}
TShock.Log.Info($"[白名单]玩家[{name}](IP: {plr.IP};UUID: {plr.UUID})没有批准登录...");
Console.WriteLine($"[白名单]玩家[{name}](IP: {plr.IP})没有批准登录...");

TShock.Log.ConsoleInfo($"[Cai白名单]玩家[{name}](IP: {plr.IP})使用未授权的设备...");
plr.SilentKickInProgress = true;
plr.Disconnect($"[白名单]在群{number}内发送'登录',以批准此设备登录");
plr.Disconnect($"[Cai白名单]在群{number}内发送'登录',\n" +
$"以批准此设备登录");

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

return false;
}

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



return true;
//NetMessage.SendData(9, plr.Index, -1, NetworkText.FromLiteral("正在检查白名单..."), 1);

}

public static bool HandleLogin(TSPlayer player, string password)
{

var account = TShock.UserAccounts.GetUserAccountByName(player.Name);
if (account != null)
{
Expand All @@ -218,8 +209,10 @@ public static bool HandleLogin(TSPlayer player, string password)
player.PlayerData.CopyCharacter(player);
TShock.CharacterDB.InsertPlayerData(player);
}

player.PlayerData.RestoreCharacter(player);
}

player.LoginFailsBySsi = false;

if (player.HasPermission(Permissions.ignorestackhackdetection))
Expand All @@ -229,12 +222,13 @@ public static bool HandleLogin(TSPlayer player, string password)
player.IsDisabledForBannedWearable = false;


player.SendSuccessMessage($"[账号管理]已经验证{account.Name}登录完毕。");
player.SendSuccessMessage($"[CaiBot]已经验证{account.Name}登录完毕。");
TShock.Log.ConsoleInfo(player.Name + "成功验证登录。");
TShock.UserAccounts.SetUserAccountUUID(account, player.UUID);
PlayerHooks.OnPlayerPostLogin(player);
return true;
}

if (player.Name != TSServerPlayer.AccountName)
{
account = new UserAccount
Expand All @@ -252,7 +246,8 @@ public static bool HandleLogin(TSPlayer player, string password)
//Kick(player, "注册失败!\n密码位数不能少于" + TShock.Config.Settings.MinimumPasswordLength + "个字符.");
return true;
}
player.SendSuccessMessage("[i:50]账户{0}注册成功。", account.Name);

player.SendSuccessMessage("[CaiBot]账户{0}注册成功。", account.Name);
TShock.UserAccounts.AddUserAccount(account);
TShock.Log.ConsoleInfo("玩家{0}注册了新账户:{1}", player.Name, account.Name);

Expand All @@ -279,8 +274,10 @@ public static bool HandleLogin(TSPlayer player, string password)
player.PlayerData.CopyCharacter(player);
TShock.CharacterDB.InsertPlayerData(player);
}

player.PlayerData.RestoreCharacter(player);
}

player.LoginFailsBySsi = false;

if (player.HasPermission(Permissions.ignorestackhackdetection))
Expand All @@ -290,7 +287,7 @@ public static bool HandleLogin(TSPlayer player, string password)
player.IsDisabledForBannedWearable = false;


player.SendSuccessMessage($"[账号管理]已经验证{account.Name}登录完毕.");
player.SendSuccessMessage($"[CaiBot]已经验证{account.Name}登录完毕.");
TShock.Log.ConsoleInfo(player.Name + "成功验证登录.");
TShock.UserAccounts.SetUserAccountUUID(account, player.UUID);
PlayerHooks.OnPlayerPostLogin(player);
Expand All @@ -302,4 +299,4 @@ public static bool HandleLogin(TSPlayer player, string password)
return true;
}
}
}
}
1 change: 1 addition & 0 deletions CaiBot/MapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Terraria;
using Terraria.Map;
using Image = SixLabors.ImageSharp.Image;

//using System.Drawing;

namespace CaiBotPlugin
Expand Down
Loading

0 comments on commit f759ec3

Please sign in to comment.