PSO2SERVER/Server/Packets/Handlers/11-ClientHandler/11-00-Login.cs

126 lines
4.4 KiB
C#
Raw Normal View History

2024-09-10 00:31:40 +08:00
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
2024-09-10 01:13:20 +08:00
using PSO2SERVER.Database;
using PSO2SERVER.Packets.PSOPackets;
using PSO2SERVER.Models;
2024-09-10 00:31:40 +08:00
2024-09-10 01:13:20 +08:00
namespace PSO2SERVER.Packets.Handlers
2024-09-10 00:31:40 +08:00
{
2024-09-16 02:56:02 +08:00
[PacketHandlerAttr(0x11, 0x00)]
2024-09-10 00:31:40 +08:00
public class Login : PacketHandler
{
#region implemented abstract members of PacketHandler
public override void HandlePacket(Client context, byte flags, byte[] data, uint position, uint size)
{
var reader = new PacketReader(data, position, size);
//var info = string.Format("[<--] 接收到的数据 (hex): ");
//Logger.WriteHex(info, data);
2024-09-12 02:14:42 +08:00
2024-09-10 00:31:40 +08:00
reader.BaseStream.Seek(0x2C, SeekOrigin.Current);
var macCount = reader.ReadMagic(0x5E6, 107);
reader.BaseStream.Seek(0x1C * macCount, SeekOrigin.Current);
reader.BaseStream.Seek(0x280, SeekOrigin.Current);
2024-09-10 00:31:40 +08:00
var username = reader.ReadFixedLengthAscii(0x60);
var password = reader.ReadFixedLengthAscii(0x60);
2024-09-12 02:14:42 +08:00
//var username = "sancaros";
//var password = "12345678";
2024-09-10 00:31:40 +08:00
//Logger.Write("用户名 {0} 密码 {1}", username, password);
2024-09-10 00:31:40 +08:00
// What am I doing here even
2024-09-10 01:13:20 +08:00
using (var db = new ServerEf())
2024-09-10 00:31:40 +08:00
{
2024-09-20 21:58:37 +08:00
var users = from u in db.Account
2024-09-10 00:31:40 +08:00
where u.Username.ToLower().Equals(username.ToLower())
select u;
var error = "";
2024-09-20 21:58:37 +08:00
Account user;
2024-09-10 00:31:40 +08:00
if (!users.Any())
{
// Check if there is an empty field
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
{
2024-09-11 00:44:21 +08:00
error = "用户名或密码为空.";
2024-09-10 00:31:40 +08:00
user = null;
}
// Check for special characters
else if (!Regex.IsMatch(username, "^[a-zA-Z0-9 ]*$", RegexOptions.IgnoreCase))
{
2024-09-11 00:55:22 +08:00
error = "用户名不能包含特殊字符\n请只使用字母和数字.";
2024-09-10 00:31:40 +08:00
user = null;
}
else // We're all good!
{
2024-09-15 17:17:05 +08:00
// 直接插入新账户至数据库
2024-09-20 21:58:37 +08:00
user = new Account
2024-09-10 00:31:40 +08:00
{
Username = username.ToLower(),
Password = BCrypt.Net.BCrypt.HashPassword(password),
Nickname = username.ToLower(),
// Since we can't display the nickname prompt yet, just default it to the username
2024-09-11 00:44:21 +08:00
SettingsIni = File.ReadAllText(ServerApp.ServerSettingsKey)
2024-09-10 00:31:40 +08:00
};
2024-09-20 21:58:37 +08:00
db.Account.Add(user);
2024-09-10 00:31:40 +08:00
db.SaveChanges();
context.SendPacket(0x11, 0x1e, 0x0, new byte[0x44]); // Request nickname
2024-09-10 00:31:40 +08:00
}
}
else
{
user = users.First();
if(password != user.Password)
{
2024-09-12 03:23:56 +08:00
if(password == "")
{
error = "密码为空.";
user = null;
}
else
if (!BCrypt.Net.BCrypt.Verify(password, user.Password))
{
error = "密码错误.";
user = null;
}
}
2024-09-10 00:31:40 +08:00
}
2024-09-20 21:58:37 +08:00
context.SendPacket(new LoginDataPacket("Server AuthList 1", error, (user == null) ? (uint)0 : (uint)user.AccountId));
2024-09-12 03:23:56 +08:00
// Mystery packet
2024-09-12 09:37:33 +08:00
//var mystery = new PacketWriter();
//mystery.Write((uint)100);
//context.SendPacket(0x11, 0x49, 0, mystery.ToArray());
2024-09-10 00:31:40 +08:00
// Login response packet
if (user == null)
2024-09-12 03:23:56 +08:00
{
2024-09-10 00:31:40 +08:00
return;
2024-09-12 03:23:56 +08:00
}
2024-09-10 00:31:40 +08:00
2024-09-20 21:58:37 +08:00
context._account = user;
2024-09-10 00:31:40 +08:00
}
2024-09-10 01:13:20 +08:00
if (ServerApp.Config.motd != "")
2024-09-10 00:31:40 +08:00
{
2024-09-10 01:13:20 +08:00
context.SendPacket(new SystemMessagePacket(ServerApp.Config.motd, SystemMessagePacket.MessageType.AdminMessageInstant));
2024-09-10 00:31:40 +08:00
}
}
#endregion
}
}