mirror of https://github.com/icedream/npsharp.git
Sanitizing merged code.
parent
27fbe3e7e3
commit
ab2f918a5a
|
@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
||||||
using log4net;
|
using log4net;
|
||||||
using NPSharp.RPC;
|
using NPSharp.RPC;
|
||||||
using NPSharp.RPC.Messages.Client;
|
using NPSharp.RPC.Messages.Client;
|
||||||
|
using NPSharp.RPC.Messages.Data;
|
||||||
using NPSharp.RPC.Messages.Server;
|
using NPSharp.RPC.Messages.Server;
|
||||||
|
|
||||||
namespace NPSharp.NP
|
namespace NPSharp.NP
|
||||||
|
@ -38,7 +39,7 @@ namespace NPSharp.NP
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The internal RPC client.
|
/// The internal RPC client.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public RPCClientStream RPCClient { get { return _rpc; } }
|
public RPCClientStream RPCClient { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The assigned NP user ID. Will be set on successful authentication.
|
/// The assigned NP user ID. Will be set on successful authentication.
|
||||||
|
@ -155,7 +156,7 @@ namespace NPSharp.NP
|
||||||
{
|
{
|
||||||
var tcs = new TaskCompletionSource<bool>();
|
var tcs = new TaskCompletionSource<bool>();
|
||||||
|
|
||||||
_rpc.AttachHandlerForNextMessage(packet =>
|
RPC.AttachHandlerForNextMessage(packet =>
|
||||||
{
|
{
|
||||||
var result = packet as AuthenticateResultMessage;
|
var result = packet as AuthenticateResultMessage;
|
||||||
if (result == null)
|
if (result == null)
|
||||||
|
@ -172,31 +173,34 @@ namespace NPSharp.NP
|
||||||
SessionToken = result.SessionToken;
|
SessionToken = result.SessionToken;
|
||||||
tcs.SetResult(true);
|
tcs.SetResult(true);
|
||||||
});
|
});
|
||||||
_rpc.Send(new AuthenticateWithKeyMessage { LicenseKey = key });
|
RPC.Send(new AuthenticateWithKeyMessage { LicenseKey = key });
|
||||||
|
|
||||||
return await tcs.Task;
|
return await tcs.Task;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> ValidateTicket(uint clientIP, ulong npID, byte[] ticket)
|
/// <summary>
|
||||||
|
/// Authenticates a server ticket.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>True if the ticket validation succeeded, otherwise false.</returns>
|
||||||
|
public async Task<bool> ValidateTicket(IPAddress clientIP, Ticket ticket)
|
||||||
{
|
{
|
||||||
var tcs = new TaskCompletionSource<bool>();
|
var tcs = new TaskCompletionSource<bool>();
|
||||||
|
|
||||||
_rpc.AttachHandlerForNextMessage(packet =>
|
RPC.AttachHandlerForNextMessage(packet =>
|
||||||
{
|
{
|
||||||
var result = packet as AuthenticateValidateTicketResultMessage;
|
var result = packet as AuthenticateValidateTicketResultMessage;
|
||||||
if (result == null)
|
if (result == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (result.Result != 0)
|
tcs.SetResult(result.Result == 0);
|
||||||
{
|
});
|
||||||
tcs.SetResult(false);
|
|
||||||
}
|
RPC.Send(new AuthenticateValidateTicketMessage
|
||||||
else
|
{
|
||||||
{
|
ClientIP = (uint)IPAddress.HostToNetworkOrder((int)BitConverter.ToUInt32(clientIP.GetAddressBytes(), 0)),
|
||||||
tcs.SetResult(true);
|
Ticket = ticket.Serialize(),
|
||||||
}
|
NPID = ticket.ClientID
|
||||||
});
|
});
|
||||||
_rpc.Send(new AuthenticateValidateTicketMessage { ClientIP = clientIP, Ticket = ticket, NPID = npID });
|
|
||||||
|
|
||||||
return await tcs.Task;
|
return await tcs.Task;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,15 @@ using System.IO;
|
||||||
|
|
||||||
namespace NPSharp.RPC.Messages.Data
|
namespace NPSharp.RPC.Messages.Data
|
||||||
{
|
{
|
||||||
internal class Ticket
|
/// <summary>
|
||||||
|
/// Represents a ticket which is used to validate client-to-gameserver connections.
|
||||||
|
/// </summary>
|
||||||
|
public class Ticket
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Reconstructs the ticket from raw byte data.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data">The ticket's raw data</param>
|
||||||
public Ticket(byte[] data)
|
public Ticket(byte[] data)
|
||||||
{
|
{
|
||||||
if (data.Length < sizeof (uint) + (sizeof (ulong)*2) + sizeof (uint))
|
if (data.Length < sizeof (uint) + (sizeof (ulong)*2) + sizeof (uint))
|
||||||
|
@ -22,27 +29,45 @@ namespace NPSharp.RPC.Messages.Data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Maybe leave out arguments which are supposed to be autofilled
|
/// <summary>
|
||||||
public Ticket(uint version, ulong clientID, ulong serverID, uint? time = null)
|
/// Constructs a ticket from given parameters.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="clientID">The client NPID</param>
|
||||||
|
/// <param name="serverID">The server NPID</param>
|
||||||
|
/// <param name="version">The ticket's structure version</param>
|
||||||
|
/// <param name="time">The ticket time</param>
|
||||||
|
public Ticket(ulong clientID, ulong serverID, uint version = 1, uint? time = null)
|
||||||
{
|
{
|
||||||
Version = version;
|
|
||||||
ClientID = clientID;
|
ClientID = clientID;
|
||||||
ServerID = serverID;
|
ServerID = serverID;
|
||||||
|
Version = version;
|
||||||
if (time.HasValue)
|
if (time.HasValue)
|
||||||
Time = time.Value;
|
Time = time.Value;
|
||||||
else
|
else
|
||||||
Time = (uint) DateTime.Now.ToUniversalTime().ToBinary();
|
Time = (uint) DateTime.Now.ToUniversalTime().ToBinary();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ticket structure version.
|
||||||
|
/// </summary>
|
||||||
public uint Version { get; private set; }
|
public uint Version { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The client's ID on the NP server.
|
||||||
|
/// </summary>
|
||||||
public ulong ClientID { get; private set; }
|
public ulong ClientID { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The gameserver's ID on the NP server.
|
||||||
|
/// </summary>
|
||||||
public ulong ServerID { get; private set; }
|
public ulong ServerID { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The creation time of the ticket.
|
||||||
|
/// </summary>
|
||||||
public uint Time { get; private set; }
|
public uint Time { get; private set; }
|
||||||
|
|
||||||
public byte[] Serialize()
|
internal byte[] Serialize()
|
||||||
{
|
{
|
||||||
using (var ms = new MemoryStream(sizeof (uint) + (sizeof (ulong)*2) + sizeof (uint)))
|
using (var ms = new MemoryStream(sizeof (uint) + (sizeof (ulong)*2) + sizeof (uint)))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue