Sanitizing merged code.

feature-npv2
Icedream 2014-05-31 01:04:49 +02:00
parent 27fbe3e7e3
commit ab2f918a5a
2 changed files with 48 additions and 19 deletions

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using log4net;
using NPSharp.RPC;
using NPSharp.RPC.Messages.Client;
using NPSharp.RPC.Messages.Data;
using NPSharp.RPC.Messages.Server;
namespace NPSharp.NP
@ -38,7 +39,7 @@ namespace NPSharp.NP
/// <summary>
/// The internal RPC client.
/// </summary>
public RPCClientStream RPCClient { get { return _rpc; } }
public RPCClientStream RPCClient { get; private set; }
/// <summary>
/// The assigned NP user ID. Will be set on successful authentication.
@ -155,7 +156,7 @@ namespace NPSharp.NP
{
var tcs = new TaskCompletionSource<bool>();
_rpc.AttachHandlerForNextMessage(packet =>
RPC.AttachHandlerForNextMessage(packet =>
{
var result = packet as AuthenticateResultMessage;
if (result == null)
@ -172,31 +173,34 @@ namespace NPSharp.NP
SessionToken = result.SessionToken;
tcs.SetResult(true);
});
_rpc.Send(new AuthenticateWithKeyMessage { LicenseKey = key });
RPC.Send(new AuthenticateWithKeyMessage { LicenseKey = key });
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>();
_rpc.AttachHandlerForNextMessage(packet =>
RPC.AttachHandlerForNextMessage(packet =>
{
var result = packet as AuthenticateValidateTicketResultMessage;
if (result == null)
return;
if (result.Result != 0)
{
tcs.SetResult(false);
}
else
{
tcs.SetResult(true);
}
tcs.SetResult(result.Result == 0);
});
RPC.Send(new AuthenticateValidateTicketMessage
{
ClientIP = (uint)IPAddress.HostToNetworkOrder((int)BitConverter.ToUInt32(clientIP.GetAddressBytes(), 0)),
Ticket = ticket.Serialize(),
NPID = ticket.ClientID
});
_rpc.Send(new AuthenticateValidateTicketMessage { ClientIP = clientIP, Ticket = ticket, NPID = npID });
return await tcs.Task;
}

View File

@ -3,8 +3,15 @@ using System.IO;
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)
{
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
public Ticket(uint version, ulong clientID, ulong serverID, uint? time = null)
/// <summary>
/// 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;
ServerID = serverID;
Version = version;
if (time.HasValue)
Time = time.Value;
else
Time = (uint) DateTime.Now.ToUniversalTime().ToBinary();
}
/// <summary>
/// Ticket structure version.
/// </summary>
public uint Version { get; private set; }
/// <summary>
/// The client's ID on the NP server.
/// </summary>
public ulong ClientID { get; private set; }
/// <summary>
/// The gameserver's ID on the NP server.
/// </summary>
public ulong ServerID { get; private set; }
/// <summary>
/// The creation time of the ticket.
/// </summary>
public uint Time { get; private set; }
public byte[] Serialize()
internal byte[] Serialize()
{
using (var ms = new MemoryStream(sizeof (uint) + (sizeof (ulong)*2) + sizeof (uint)))
{