zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Hotfix/Game/Helper/AccountHelper.cs

108 lines
3.5 KiB
C#
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace ET
{
public static class AccountHelper
{
private static Dictionary<string, int> accountRegisterCount = new();
public static async ETTask<int> GetFirbiddenIpCount(string ip)
{
List<ForbiddenIP> list = await DBComponent.Instance.QueryFilter(Builders<ForbiddenIP>.Filter.Eq(t => t.Ip, ip));
if (list == null || list.Count == 0)
{
return 0;
}
ForbiddenIP forbiddenIP = list[0];
return forbiddenIP.count;
}
public static string GetPlayerFormatName(this long id)
{
return $"【{UserComponent.Instance.Get(id)?.NickName}({id})】";
}
public static async ETTask CheckIdentify(Account account,string identify)
{
account.identifyList ??= new();
byte[] bytes = Encoding.UTF8.GetBytes(identify);
Utility.Encryption.GetSelfXorBytes(bytes, CryptionHelper.GetXorKey());
string _key = Encoding.UTF8.GetString(bytes);
account.identifyList.Add(_key);
await DBComponent.Instance.Save(account);
}
public static bool CanRegister(string identify)
{
byte[] bytes = Encoding.UTF8.GetBytes(identify);
Utility.Encryption.GetSelfXorBytes(bytes, CryptionHelper.GetXorKey());
string _key = Encoding.UTF8.GetString(bytes);
var arr = _key.Split('+');
string strCount1 = arr[0];
string strCount2 = arr[4];
string strCount3 = arr[6];
string strValue1 = arr[1];
string strValue2 = arr[5];
string strValue3 = arr[7];
if (!int.TryParse(strCount1, out int count1) ||!int.TryParse(strCount2, out int count2)||!int.TryParse(strCount3, out int count3))
return false;
if (count1 != strValue1.Length || count2 != strValue2.Length || count3 != strValue3.Length)
return false;
if (accountRegisterCount.TryGetValue(_key, out var count))
{
if (count >= 10) return false;
}
return true;
}
public static void Refresh()
{
accountRegisterCount.Clear();
}
}
public sealed class SecurityUtil
{
public class SecurityData
{
public byte[] xorScale;
}
private static readonly byte[] xorScale;
static SecurityUtil()
{
if (xorScale == null)
{
string path = Path.Combine(Environment.CurrentDirectory, "../Release/key");
string str = File.ReadAllText(path);
SecurityData data = MongoHelper.FromJson<SecurityData>(str);
xorScale = data.xorScale;
}
}
/// <summary>
/// 对数组进行异或
/// </summary>
/// <param name="buffer"></param>
/// <returns></returns>
public static byte[] Xor(byte[] buffer)
{
//------------------
//第3步xor解密
//------------------
int iScaleLen = xorScale.Length;
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte)(buffer[i] ^ xorScale[i % iScaleLen]);
}
return buffer;
}
}
}