Skip to content

Commit

Permalink
gameserver authentication: protocolversion+key packets
Browse files Browse the repository at this point in the history
  • Loading branch information
shnok committed Jun 30, 2024
1 parent bf5dbbf commit 7aaafa6
Show file tree
Hide file tree
Showing 20 changed files with 244 additions and 140 deletions.
21 changes: 15 additions & 6 deletions l2-unity/Assets/Scripts/Game/Manager/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
using UnityEngine;
using static ServerListPacket;

public class GameManager : MonoBehaviour
{
public class GameManager : MonoBehaviour {
[SerializeField] private int _protocolVersion = 1;
[SerializeField] private GameState _gameState = GameState.LOGIN_SCREEN;
private bool _gameReady = false;

public GameState GameState {
get { return _gameState; }
public GameState GameState {
get { return _gameState; }
set {
_gameState = value;
Debug.Log($"Game state is now {_gameState}.");
}
}
public bool GameReady { get { return _gameReady; } set { _gameReady = value; } }
public int ProtocolVersion { get { return _protocolVersion; } }

private static GameManager _instance;
public static GameManager Instance { get { return _instance; } }
Expand Down Expand Up @@ -74,6 +75,14 @@ public void OnLoginServerAuthAllowed() {
L2LoginUI.Instance.ShowLicenseWindow();
}

public void OnLoginServerPlayOk() {
GameState = GameState.READY_TO_CONNECT;
}

public void OnConnectingToGameServer() {
GameState = GameState.CONNECTING_TO_GAMESERVER;
}

public void OnReceivedServerList(byte lastServer, List<ServerData> serverData, Dictionary<int, int> charsOnServers) {
GameState = GameState.SERVER_LIST;

Expand Down Expand Up @@ -119,10 +128,10 @@ public void OnRelogin() {
}

public void OnDisconnect() {
if(GameState > GameState.CHAR_CREATION) {
if (GameState > GameState.CHAR_CREATION) {
MusicManager.Instance.Clear();
SceneLoader.Instance.LoadMenu();
} else if(GameState > GameState.LOGIN_SCREEN) {
} else if(GameState > GameState.LOGIN_SCREEN && GameState != GameState.READY_TO_CONNECT) {
OnRelogin();
}
}
Expand Down
12 changes: 8 additions & 4 deletions l2-unity/Assets/Scripts/Game/Manager/GameState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ public enum GameState : byte {
LOGIN_SCREEN = 0,
LOGIN_CONNECTED = 1,
READING_LICENSE = 2,
SERVER_LIST = 3,
CHAR_SELECT = 4,
CHAR_CREATION = 5,
IN_GAME = 6
SERVER_LIST = 3,
READY_TO_CONNECT = 4,
CONNECTING_TO_GAMESERVER = 5,
GAMESERVER_CONNECTED = 6,
GAMESERVER_AUTHED = 7,
CHAR_SELECT = 8,
CHAR_CREATION = 9,
IN_GAME = 10
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,31 @@ public class AsynchronousClient {
private ServerPacketHandler _serverPacketHandler;
private DefaultClient _client;

// Crypt
public static byte[] STATIC_BLOWFISH_KEY = {
(byte) 0x6b,
(byte) 0x60,
(byte) 0xcb,
(byte) 0x5b,
(byte) 0x82,
(byte) 0xce,
(byte) 0x90,
(byte) 0xb1,
(byte) 0xcc,
(byte) 0x2b,
(byte) 0x6c,
(byte) 0x55,
(byte) 0x6c,
(byte) 0x6c,
(byte) 0x6c,
(byte) 0x6c
};

private bool _initPacket = true;
private RSACrypt _rsa;
private byte[] _blowfishKey;
private BlowfishEngine _decryptBlowfish;
private BlowfishEngine _encryptBlowfish;
private int _sessionKey1;
private int _sessionKey2;

public bool InitPacket { get { return _initPacket; } set { _initPacket = value; } }

public RSACrypt RSACrypt { get { return _rsa; } }
public BlowfishEngine DecryptBlowFish { get { return _decryptBlowfish; } }
public BlowfishEngine EncryptBlowFish { get { return _encryptBlowfish; } }
public int SessionKey1 { get { return _sessionKey1; } set { _sessionKey1 = value; } }
public int SessionKey2 { get { return _sessionKey2; } set { _sessionKey2 = value; } }
public byte[] BlowfishKey { get { return _blowfishKey; } }

public int Ping { get; set; }

public AsynchronousClient(string ip, int port, DefaultClient client, ClientPacketHandler clientPacketHandler, ServerPacketHandler serverPacketHandler) {
SetBlowFishKey(STATIC_BLOWFISH_KEY);
public AsynchronousClient(string ip, int port, DefaultClient client, ClientPacketHandler clientPacketHandler,
ServerPacketHandler serverPacketHandler, bool enableInitPacket) {
_ipAddress = ip;
_port = port;
_clientPacketHandler = clientPacketHandler;
_serverPacketHandler = serverPacketHandler;
_clientPacketHandler.SetClient(this);
_serverPacketHandler.SetClient(this, _clientPacketHandler);
_client = client;

_initPacket = enableInitPacket;
}

public void SetBlowFishKey(byte[] blowfishKey) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
public class PingPacket : ClientPacket {
public PingPacket() : base((byte)GameClientPacketType.Ping) {
SetData(new byte[] { (byte)GameClientPacketType.Ping, 0x02});
BuildPacket();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProtocolVersionPacket : ClientPacket
{
public ProtocolVersionPacket(int version) : base((byte)GameClientPacketType.ProtocolVersion) {
WriteI(version);

BuildPacket();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
public enum GameClientPacketType : byte
{
Ping = 0x00,
AuthRequest = 0x01,
SendMessage = 0x02,
RequestMove = 0x03,
LoadWorld = 0x04,
RequestRotate = 0x05,
RequestAnim = 0x06,
RequestAttack = 0x07,
RequestMoveDirection = 0x08,
RequestSetTarget = 0x09,
RequestAutoAttack = 0x0A
ProtocolVersion = 0x01,
AuthRequest = 0x02,
SendMessage = 0x03,
RequestMove = 0x04,
LoadWorld = 0x05,
RequestRotate = 0x06,
RequestAnim = 0x07,
RequestAttack = 0x08,
RequestMoveDirection = 0x09,
RequestSetTarget = 0x0A,
RequestAutoAttack = 0x0B
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
public enum GameServerPacketType : byte
{
Ping = 0,
AuthResponse = 1,
MessagePacket = 2,
SystemMessage = 3,
PlayerInfo = 4,
ObjectPosition = 5,
RemoveObject = 6,
ObjectRotation = 7,
ObjectAnimation = 8,
ApplyDamage = 9,
NpcInfo = 0x0A,
ObjectMoveTo = 0x0B,
UserInfo = 0x0C,
ObjectMoveDirection = 0x0D,
GameTime = 0x0E,
EntitySetTarget = 0x0F,
AutoAttackStart = 0x10,
AutoAttackStop = 0x11,
ActionFailed = 0x12
Ping = 0x00,
Key = 0x01,
CharSelectionInfo = 0x02,
MessagePacket = 0x03,
SystemMessage = 0x04,
PlayerInfo = 0x05,
ObjectPosition = 0x06,
RemoveObject = 0x07,
ObjectRotation = 0x08,
ObjectAnimation = 0x09,
ApplyDamage = 0x0A,
NpcInfo = 0x0B,
ObjectMoveTo = 0x0C,
UserInfo = 0x0D,
ObjectMoveDirection = 0x0E,
GameTime = 0x0F,
EntitySetTarget = 0x10,
AutoAttackStart = 0x11,
AutoAttackStop = 0x12,
ActionFailed = 0x13
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KeyPacket : ServerPacket {
public byte[] BlowFishKey { get; private set; }
public bool AuthAllowed { get; private set; }

public KeyPacket(byte[] d) : base(d) {
Parse();
}

public override void Parse() {
byte blowFishKeyLength = ReadB();
BlowFishKey = ReadB(blowFishKeyLength);
AuthAllowed = ReadB() == 1;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public void SendPing() {
SendPacket(packet);
}

public void SendAuth(string username) {
//AuthRequestPacket packet = new AuthRequestPacket(username);
//SendPacket(packet);
public void SendProtocolVersion() {
ProtocolVersionPacket packet = new ProtocolVersionPacket(GameManager.Instance.ProtocolVersion);
SendPacket(packet);
}

public void SendMessage(string message) {
Expand Down Expand Up @@ -66,6 +66,10 @@ public override void SendPacket(ClientPacket packet) {
}
}

if (_client.BlowfishKey != null) {
EncryptPacket(packet);
}

_client.SendPacket(packet);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ public override void HandlePacket(byte[] data) {
case GameServerPacketType.Ping:
OnPingReceive();
break;
case GameServerPacketType.AuthResponse:
OnAuthReceive(data);
case GameServerPacketType.Key:
OnKeyReceive(data);
break;
case GameServerPacketType.CharSelectionInfo:
OnCharSelectionInfoReceive(data);
break;
case GameServerPacketType.MessagePacket:
OnMessageReceive(data);
Expand Down Expand Up @@ -95,23 +98,19 @@ private void OnPingReceive() {
}, _tokenSource.Token);
}

private void OnAuthReceive(byte[] data) {
AuthResponsePacket packet = new AuthResponsePacket(data);
AuthResponse response = packet.Response;
private void OnKeyReceive(byte[] data) {
KeyPacket packet = new KeyPacket(data);

switch(response) {
case AuthResponse.ALLOW:
_eventProcessor.QueueEvent(() => GameClient.Instance.OnAuthAllowed());
break;
case AuthResponse.ALREADY_CONNECTED:
Debug.Log("User already connected.");
_client.Disconnect();
break;
case AuthResponse.INVALID_USERNAME:
Debug.Log("Incorrect user name.");
_client.Disconnect();
break;
if(packet.AuthAllowed) {
_client.Disconnect();
return;
}

GameClient.Instance.SetBlowFishKey(packet.BlowFishKey);
}

private void OnCharSelectionInfoReceive(byte[] data) {

}

private void OnMessageReceive(byte[] data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public void SendAuth() {
}

public void SendRequestServerList() {
RequestServerListPacket packet = new RequestServerListPacket(_client.SessionKey1, _client.SessionKey2);
RequestServerListPacket packet = new RequestServerListPacket(LoginClient.Instance.SessionKey1, LoginClient.Instance.SessionKey2);

SendPacket(packet);
}

public void SendRequestServerLogin(int serverId) {
RequestServerLoginPacket packet = new RequestServerLoginPacket(serverId, _client.SessionKey1, _client.SessionKey2);
RequestServerLoginPacket packet = new RequestServerLoginPacket(serverId, LoginClient.Instance.SessionKey1, LoginClient.Instance.SessionKey2);

SendPacket(packet);
}
Expand Down
Loading

0 comments on commit 7aaafa6

Please sign in to comment.