Implemented Gen4 Global Terminal conversation logic.

This commit is contained in:
Greg Edwards 2014-06-05 17:14:40 -04:00
parent 41b3bf95ea
commit 6d98210d97
14 changed files with 154 additions and 48 deletions

View File

@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using PkmnFoundations.Support;
namespace PkmnFoundations.GlobalTerminalService
{
public class GTServer4 : GTServerBase
{
public GTServer4()
: base(12400, false)
{
Initialize();
}
public GTServer4(int threads)
: base(12400, false, threads)
{
Initialize();
}
private void Initialize()
{
m_pad = new byte[256];
using (FileStream s = File.Open("pad.bin", FileMode.Open))
{
s.Read(m_pad, 0, m_pad.Length);
s.Close();
}
}
protected override byte[] ProcessRequest(byte[] data)
{
int length = BitConverter.ToInt32(data, 0);
AssertHelper.Equals(length, data.Length);
RequestTypes4 requestType = (RequestTypes4)data[4];
MemoryStream response = new MemoryStream();
response.Write(new byte[] { 0x00, 0x00, 0x00, 0x00 }, 0, 4); // placeholder for length
response.WriteByte((byte)requestType);
response.WriteByte(Byte6(requestType));
// todo: implement each of the request types here
switch (requestType)
{
}
response.Flush();
byte[] responseData = response.ToArray();
WriteLength(responseData);
CryptMessage(responseData);
return responseData;
}
private void WriteLength(byte[] message)
{
byte[] data = BitConverter.GetBytes(message.Length);
Array.Copy(data, 0, message, 0, 4);
}
private void CryptMessage(byte[] message)
{
if (message.Length < 5) return;
byte padOffset = (byte)(message[0] + message[4]);
// encrypt and decrypt are the same operation...
for (int x = 5; x < message.Length; x++)
message[x] ^= m_pad[(x + padOffset) & 0xff];
}
private byte Byte6(RequestTypes4 type)
{
switch (type)
{
case RequestTypes4.BoxUpload:
case RequestTypes4.BoxSearch:
return 0x52;
case RequestTypes4.DressupUpload:
case RequestTypes4.DressupSearch:
return 0x4e;
case RequestTypes4.BattleVideoUpload:
case RequestTypes4.BattleVideoSearch:
case RequestTypes4.BattleVideoWatch:
return 0x59;
default:
return 0x00;
}
}
private byte[] m_pad;
}
internal enum RequestTypes4 : byte
{
BoxUpload = 0x08,
BoxSearch = 0x09,
DressupUpload = 0x20,
DressupSearch = 0x21,
BattleVideoUpload = 0xd8,
BattleVideoSearch = 0xd9,
BattleVideoWatch = 0xda
}
}

View File

@ -8,7 +8,7 @@ using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace GlobalTerminalService
namespace PkmnFoundations.GlobalTerminalService
{
public abstract class GTServerBase
{
@ -30,7 +30,7 @@ namespace GlobalTerminalService
m_listener = new TcpListener(port);
if (UseSsl)
{
m_cert = new X509Certificate2("cert.pfx", "letmein");
Certificate = new X509Certificate2("cert.pfx", "letmein");
}
}
@ -46,11 +46,12 @@ namespace GlobalTerminalService
set;
}
protected X509Certificate Certificate { get; set; }
private List<Thread> m_workers;
private object m_lock = new object();
private bool m_closing;
private TcpListener m_listener;
private X509Certificate m_cert;
public void BeginPolling()
{
@ -104,7 +105,8 @@ namespace GlobalTerminalService
BitConverter.GetBytes(length).CopyTo(data, 0);
s.Read(data, 4, length - 4); // todo: stop DoS by timing out blocking requests
ProcessRequest(data, s);
byte[] response = ProcessRequest(data);
s.Write(response, 0, response.Length);
}
m_workers.Remove(Thread.CurrentThread);
}
@ -116,12 +118,12 @@ namespace GlobalTerminalService
if (UseSsl)
{
SslStream sslClient = new SslStream(c.GetStream());
sslClient.AuthenticateAsServer(m_cert);
sslClient.AuthenticateAsServer(Certificate);
return sslClient;
}
else return c.GetStream();
}
protected abstract void ProcessRequest(byte[] data, Stream response);
protected abstract byte[] ProcessRequest(byte[] data);
}
}

View File

@ -8,10 +8,11 @@
<ProjectGuid>{7AB1A65F-3AD1-4356-94E7-F1A669BF4CE0}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GlobalTerminalService</RootNamespace>
<RootNamespace>PkmnFoundations.GlobalTerminalService</RootNamespace>
<AssemblyName>GlobalTerminalService</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
@ -44,6 +45,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="GTServer4.cs" />
<Compile Include="GTServerBase.cs" />
<Compile Include="Service1.cs">
<SubType>Component</SubType>
@ -54,6 +56,18 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\library\Library.csproj">
<Project>{408EFC7E-C6B0-4160-8628-2679E34385CE}</Project>
<Name>Library</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="pad.bin">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace GlobalTerminalService
namespace PkmnFoundations.GlobalTerminalService
{
static class Program
{

View File

@ -1,4 +1,4 @@
namespace GlobalTerminalService
namespace PkmnFoundations.GlobalTerminalService
{
partial class Service1
{

View File

@ -7,7 +7,7 @@ using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace GlobalTerminalService
namespace PkmnFoundations.GlobalTerminalService
{
public partial class Service1 : ServiceBase
{

View File

@ -0,0 +1,3 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>

Binary file not shown.

Binary file not shown.

View File

@ -64,6 +64,9 @@
<Content Include="Default.aspx" />
<Content Include="Global.asax" />
<Content Include="test\RngTest.aspx" />
<None Include="pad.bin">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Web.config">
<SubType>Designer</SubType>
</None>
@ -151,8 +154,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
<Folder Include="images\sm-s\" />
<Folder Include="images\sm\" />
<Folder Include="images\" />
</ItemGroup>
<ItemGroup>
<Content Include="MasterPage.master" />

BIN
gts/pad.bin Normal file

Binary file not shown.

View File

@ -8,8 +8,6 @@
<div>
<p>Uplaod your Generation IV box upload, battle video, or dressup capture:</p>
<asp:FileUpload ID="fuBox" runat="server" />
&nbsp;Pad offset:
<asp:TextBox ID="txtOffset" runat="server" />
<asp:Button ID="btnSend" Text="Send" OnClick="btnSend_Click" runat="server" />
</div>
<asp:Literal ID="litMessage" runat="server" />

View File

@ -21,25 +21,17 @@ namespace PkmnFoundations.GTS.debug
}
private static byte[] PAD = new byte[256];
private static byte[] m_pad = new byte[256];
protected void btnSend_Click(object sender, EventArgs e)
{
phDecoded.Visible = false;
byte[] data = fuBox.FileBytes;
FileStream s = File.Open(Server.MapPath("~/Box Upload Xor Pad.bin"), FileMode.Open);
s.Read(PAD, 0, PAD.Length);
FileStream s = File.Open(Server.MapPath("~/pad.bin"), FileMode.Open);
s.Read(m_pad, 0, m_pad.Length);
s.Close();
if (txtOffset.Text.Length > 0)
{
int padOffset = Convert.ToInt32(txtOffset.Text);
Encrypt(data, padOffset);
}
else
{
Decrypt(data);
}
CryptMessage(data);
litDecoded.Text = RenderHex(data.ToHexStringLower());
phDecoded.Visible = true;
@ -62,19 +54,14 @@ namespace PkmnFoundations.GTS.debug
return builder.ToString();
}
public static void Encrypt(byte[] data, int padOffset)
private void CryptMessage(byte[] message)
{
// encrypt and decrypt are the same operation...
for (int x = 6; x < data.Length; x++)
{
data[x] ^= PAD[(x + padOffset) % 256];
}
}
if (message.Length < 5) return;
byte padOffset = (byte)(message[0] + message[4]);
public static void Decrypt(byte[] data)
{
int padOffset = (Array.IndexOf(PAD, data[6]) + 250) % 256;
Encrypt(data, padOffset);
// encrypt and decrypt are the same operation...
for (int x = 5; x < message.Length; x++)
message[x] ^= m_pad[(x + padOffset) & 0xff];
}
}
}

View File

@ -30,15 +30,6 @@ namespace PkmnFoundations.GTS.debug {
/// </remarks>
protected global::System.Web.UI.WebControls.FileUpload fuBox;
/// <summary>
/// txtOffset control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox txtOffset;
/// <summary>
/// btnSend control.
/// </summary>