-
Notifications
You must be signed in to change notification settings - Fork 12
Add full XI5Ticket support and improve ticket handling #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
score3229
wants to merge
14
commits into
GitHubProUser67:main
Choose a base branch
from
score3229:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bddcd96
Add files via upload
score3229 9e48d41
Add files via upload
score3229 13230ce
Add files via upload
score3229 ef6bd42
Delete BackendServices/CastleLibrary/XI5/obj directory
score3229 0db9adb
Ticket reader fix
score3229 87d03fb
Removed unnecessary usings
score3229 c0bb0b1
Removed unnecessary usings
score3229 d6e3604
Delete BackendServices/CastleLibrary/XI5/Verification/Keys/Games dire…
score3229 62d23eb
Removed unnecessary usings
score3229 f9bf324
Removed unnecessary usings
score3229 06ad1d8
Removed unnecessary usings
score3229 09c3b68
Delete BackendServices/CastleLibrary/XI5/StreamExtensions.cs
score3229 a2fd8d7
Update TicketParser30.cs
score3229 117f240
Updated XI5Ticket in login function
score3229 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
BackendServices/CastleLibrary/XI5/Reader/TicketReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
using System; | ||
using System.Buffers.Binary; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using XI5.Types; | ||
|
||
namespace XI5.Reader | ||
{ | ||
public class TicketReader : BinaryReader | ||
{ | ||
public TicketReader(Stream input) : base(input) | ||
{ } | ||
|
||
#region Big Endian Shenanigans | ||
|
||
// credit where credit is due: https://stackoverflow.com/a/71048495 | ||
|
||
public override short ReadInt16() => BinaryPrimitives.ReadInt16BigEndian(ReadBytes(2)); | ||
public override int ReadInt32() => BinaryPrimitives.ReadInt32BigEndian(ReadBytes(4)); | ||
public override long ReadInt64() => BinaryPrimitives.ReadInt64BigEndian(ReadBytes(8)); | ||
|
||
public override ushort ReadUInt16() => BinaryPrimitives.ReadUInt16BigEndian(ReadBytes(2)); | ||
public override uint ReadUInt32() => BinaryPrimitives.ReadUInt32BigEndian(ReadBytes(4)); | ||
public override ulong ReadUInt64() => BinaryPrimitives.ReadUInt64BigEndian(ReadBytes(8)); | ||
|
||
#endregion | ||
|
||
internal TicketVersion ReadTicketVersion() => new TicketVersion((byte)(ReadByte() >> 4), ReadByte()); | ||
|
||
internal ushort ReadTicketHeader() | ||
{ | ||
// Ticket header | ||
ReadBytes(4); // Header | ||
return ReadUInt16(); // Ticket length | ||
} | ||
|
||
public void DetermineTicketFormat() | ||
{ | ||
TicketVersion version = ReadTicketVersion(); | ||
Console.Write($"Ticket version {version.Major}.{version.Minor}"); | ||
score3229 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ushort length = ReadTicketHeader(); | ||
Console.WriteLine($", length is {length} bytes"); | ||
score3229 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
while (this.BaseStream.Position <= length) | ||
{ | ||
DetermineSectionFormat(); | ||
} | ||
|
||
long unreadBytes = this.BaseStream.Length - this.BaseStream.Position; | ||
if (unreadBytes != 0) | ||
{ | ||
Console.WriteLine($"!!! Unread bytes: {unreadBytes} !!!"); | ||
score3229 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
private void DetermineSectionFormat() | ||
{ | ||
TicketDataSection section = ReadTicketSectionHeader(); | ||
Console.WriteLine($" {section.Type} Section, length is {section.Length} @ offset {section.Position}"); | ||
score3229 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (section.Type == TicketDataSectionType.DateOfBirth) | ||
{ | ||
this.BaseStream.Position += section.Length; | ||
} | ||
|
||
int endSpot = section.Length + section.Position; | ||
while (this.BaseStream.Position <= endSpot) | ||
{ | ||
DetermineData(); | ||
} | ||
} | ||
|
||
private void DetermineData() | ||
{ | ||
TicketData data = ReadTicketData(TicketDataType.Empty); | ||
if ((ushort)data.Type >> 8 == 0x30) // if data type starts with section header | ||
{ | ||
this.BaseStream.Position -= sizeof(ushort) * 2; // roll back read from data | ||
DetermineSectionFormat(); // read the section | ||
return; | ||
} | ||
this.ReadBytes(data.Length); | ||
Console.WriteLine($" {data.Type}, length is {data.Length}"); | ||
score3229 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
internal TicketDataSection ReadTicketSectionHeader() | ||
{ | ||
long position = this.BaseStream.Position; | ||
|
||
byte sectionHeader = this.ReadByte(); | ||
if (sectionHeader != 0x30) | ||
{ | ||
throw new FormatException($"Expected 0x30 for section header, was {sectionHeader}. Offset is {this.BaseStream.Position}"); | ||
} | ||
|
||
TicketDataSectionType type = (TicketDataSectionType)this.ReadByte(); | ||
ushort length = this.ReadUInt16(); | ||
|
||
return new TicketDataSection(type, length, position); | ||
} | ||
|
||
private TicketData ReadTicketData(TicketDataType expectedType) | ||
{ | ||
TicketData data = new TicketData((TicketDataType)ReadUInt16(), ReadUInt16()); | ||
if (data.Type != expectedType && expectedType != TicketDataType.Empty) | ||
throw new FormatException($"Expected data type to be {expectedType}, was really {data.Type} ({(int)data.Type})"); | ||
score3229 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return data; | ||
} | ||
|
||
internal byte[] ReadTicketBinaryData(TicketDataType type = TicketDataType.Binary) | ||
=> ReadBytes(ReadTicketData(type).Length); | ||
internal string ReadTicketStringData(TicketDataType type = TicketDataType.String) | ||
=> Encoding.Default.GetString(ReadTicketBinaryData(type)).TrimEnd('\0'); | ||
|
||
internal uint ReadTicketUInt32Data() | ||
{ | ||
ReadTicketData(TicketDataType.UInt32); | ||
return ReadUInt32(); | ||
} | ||
|
||
internal ulong ReadTicketUInt64Data() | ||
{ | ||
ReadTicketData(TicketDataType.UInt64); | ||
return ReadUInt64(); | ||
} | ||
|
||
internal DateTimeOffset ReadTicketTimestampData() | ||
{ | ||
ReadTicketData(TicketDataType.Timestamp); | ||
return DateTimeOffset.FromUnixTimeMilliseconds((long)ReadUInt64()); | ||
} | ||
|
||
internal void SkipTicketEmptyData(int sections = 1) | ||
{ | ||
for (int i = 0; i < sections; i++) ReadTicketData(TicketDataType.Empty); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,55 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace XI5 | ||
{ | ||
internal static class StreamExtensions | ||
{ | ||
public static uint ReadUInt(this Stream stream) | ||
{ | ||
byte[] buffer = new byte[4]; | ||
stream.Read(buffer, 0, 4); | ||
if (BitConverter.IsLittleEndian) | ||
Array.Reverse(buffer); | ||
return BitConverter.ToUInt32(buffer, 0); | ||
} | ||
|
||
public static ulong ReadULong(this Stream stream) | ||
{ | ||
byte[] buffer = new byte[8]; | ||
stream.Read(buffer, 0, 8); | ||
if (BitConverter.IsLittleEndian) | ||
Array.Reverse(buffer); | ||
return BitConverter.ToUInt64(buffer, 0); | ||
} | ||
|
||
public static ushort ReadUShort(this Stream stream) | ||
{ | ||
byte[] buffer = new byte[2]; | ||
stream.Read(buffer, 0, 2); | ||
if (BitConverter.IsLittleEndian) | ||
Array.Reverse(buffer); | ||
return BitConverter.ToUInt16(buffer, 0); | ||
} | ||
|
||
public static bool ReadAll(this Stream stream, byte[] buffer, int startIndex, int count) | ||
{ | ||
if (stream == null) | ||
return false; | ||
|
||
int offset = 0; | ||
while (offset < count) | ||
{ | ||
int readCount = stream.Read(buffer, startIndex + offset, count - offset); | ||
if (readCount == 0) | ||
return false; | ||
offset += readCount; | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
score3229 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace XI5 | ||
{ | ||
internal static class StreamExtensions | ||
{ | ||
public static uint ReadUInt(this Stream stream) | ||
{ | ||
byte[] buffer = new byte[4]; | ||
stream.Read(buffer, 0, 4); | ||
if (BitConverter.IsLittleEndian) | ||
Array.Reverse(buffer); | ||
return BitConverter.ToUInt32(buffer, 0); | ||
} | ||
|
||
public static ulong ReadULong(this Stream stream) | ||
{ | ||
byte[] buffer = new byte[8]; | ||
stream.Read(buffer, 0, 8); | ||
if (BitConverter.IsLittleEndian) | ||
Array.Reverse(buffer); | ||
return BitConverter.ToUInt64(buffer, 0); | ||
} | ||
|
||
public static ushort ReadUShort(this Stream stream) | ||
{ | ||
byte[] buffer = new byte[2]; | ||
stream.Read(buffer, 0, 2); | ||
if (BitConverter.IsLittleEndian) | ||
Array.Reverse(buffer); | ||
return BitConverter.ToUInt16(buffer, 0); | ||
} | ||
|
||
public static bool ReadAll(this Stream stream, byte[] buffer, int startIndex, int count) | ||
{ | ||
if (stream == null) | ||
return false; | ||
|
||
int offset = 0; | ||
while (offset < count) | ||
{ | ||
int readCount = stream.Read(buffer, startIndex + offset, count - offset); | ||
if (readCount == 0) | ||
return false; | ||
offset += readCount; | ||
} | ||
return true; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
BackendServices/CastleLibrary/XI5/Types/Parsers/TicketParser21.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using XI5.Reader; | ||
|
||
namespace XI5.Types.Parsers | ||
{ | ||
internal static class TicketParser21 | ||
{ | ||
internal static void ParseTicket(XI5Ticket ticket, TicketReader reader) | ||
{ | ||
ticket.SerialId = reader.ReadTicketStringData(TicketDataType.Binary); | ||
|
||
ticket.IssuerId = reader.ReadTicketUInt32Data(); | ||
|
||
ticket.IssuedDate = reader.ReadTicketTimestampData(); | ||
ticket.ExpiryDate = reader.ReadTicketTimestampData(); | ||
|
||
ticket.UserId = reader.ReadTicketUInt64Data(); | ||
ticket.Username = reader.ReadTicketStringData(); | ||
|
||
ticket.Country = reader.ReadTicketStringData(TicketDataType.Binary); // No I am not going to brazil | ||
ticket.Domain = reader.ReadTicketStringData(); | ||
|
||
ticket.ServiceId = reader.ReadTicketStringData(TicketDataType.Binary); | ||
ticket.TitleId = XI5Ticket.ServiceIdRegex.Matches(ticket.ServiceId)[0].ToString(); | ||
|
||
ticket.Status = reader.ReadUInt32(); | ||
|
||
// Skip padding section in ticket | ||
reader.SkipTicketEmptyData(3); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
BackendServices/CastleLibrary/XI5/Types/Parsers/TicketParser30.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using XI5.Reader; | ||
|
||
namespace XI5.Types.Parsers | ||
{ | ||
internal static class TicketParser30 | ||
{ | ||
internal static void ParseTicket(XI5Ticket ticket, TicketReader reader) | ||
{ | ||
ticket.SerialId = reader.ReadTicketStringData(TicketDataType.Binary); | ||
|
||
ticket.IssuerId = reader.ReadTicketUInt32Data(); | ||
|
||
ticket.IssuedDate = reader.ReadTicketTimestampData(); | ||
ticket.ExpiryDate = reader.ReadTicketTimestampData(); | ||
|
||
ticket.UserId = reader.ReadTicketUInt64Data(); | ||
ticket.Username = reader.ReadTicketStringData(); | ||
|
||
ticket.Country = reader.ReadTicketStringData(TicketDataType.Binary); | ||
ticket.Domain = reader.ReadTicketStringData(); | ||
|
||
ticket.ServiceId = reader.ReadTicketStringData(TicketDataType.Binary); | ||
ticket.TitleId = XI5Ticket.ServiceIdRegex.Matches(ticket.ServiceId)[0].ToString(); | ||
|
||
TicketDataSection header = reader.ReadTicketSectionHeader(); | ||
if (header.Type != TicketDataSectionType.DateOfBirth) | ||
{ | ||
throw new FormatException($"Expected section to be {nameof(TicketDataSectionType.DateOfBirth)}, " + | ||
$"was really {header.Type} ({(int)header.Type})"); | ||
} | ||
|
||
reader.ReadUInt32(); // Birthdate | ||
reader.SkipTicketEmptyData(2); // Padding? | ||
|
||
Console.WriteLine(reader.BaseStream.Position); | ||
score3229 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
header = reader.ReadTicketSectionHeader(); | ||
if (header.Type != TicketDataSectionType.Age) | ||
{ | ||
throw new FormatException($"Expected section to be {nameof(TicketDataSectionType.Age)}, " + | ||
$"was really {header.Type} ({(int)header.Type})"); | ||
} | ||
|
||
reader.SkipTicketEmptyData(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace XI5.Types | ||
{ | ||
public readonly struct TicketData | ||
{ | ||
public readonly TicketDataType Type; | ||
public readonly ushort Length; | ||
|
||
public TicketData(TicketDataType type, ushort length) | ||
{ | ||
Type = type; | ||
Length = length; | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
BackendServices/CastleLibrary/XI5/Types/TicketDataSection.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace XI5.Types | ||
{ | ||
public readonly struct TicketDataSection | ||
{ | ||
public TicketDataSectionType Type { get; } | ||
public ushort Length { get; } | ||
public int Position { get; } | ||
|
||
public TicketDataSection(TicketDataSectionType type, ushort length, long position) | ||
{ | ||
Type = type; | ||
Length = length; | ||
Position = (int)position; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
BackendServices/CastleLibrary/XI5/Types/TicketDataSectionType.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace XI5.Types | ||
{ | ||
public enum TicketDataSectionType : byte | ||
{ | ||
Body = 0, | ||
Footer = 2, | ||
Age = 16, | ||
DateOfBirth = 17, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.