Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.33 KB | None | 0 0
  1. using UnityEngine;
  2. using BitStrap;
  3. using System.Diagnostics;
  4. using JsonFx.Json;
  5.  
  6. public sealed class DebugServerSession : MonoBehaviour
  7. {
  8.     public TextAsset rubyCodeAsset;
  9.     public int repeatCount = 50;
  10.  
  11.     private string requestJson = "[[\"device_identifier\",\"n/a\"],[\"email\",\"carles@trail.gg\"],[\"password\",\"Khae3953\"]]";
  12.  
  13.     [Button]
  14.     public void Run()
  15.     {
  16.         for( var i = 0; i < repeatCount; i++ )
  17.         {
  18.             var createSessionJson = RunRubyFunction( "create_session" );
  19.             var session = JsonReader.Deserialize<SessionModel>( createSessionJson );
  20.             var serverTempDigest = JsonReader.Deserialize<string[]>( session.digest );
  21.             var publicKey = SecurityUtil.CreateSessionHandShake( session );
  22.             var serverDigest = RunRubyFunction( "hand_shake", "\"{0}\",\"{1}\",\"{2}\",\"{3}\"", serverTempDigest[0], serverTempDigest[1], session.pub_key, publicKey ).TrimEnd();
  23.  
  24.             var serverSignatue = RunRubyFunction( "get_signature", "\"{0}\",\"{1}\"", requestJson.Replace( "\"", "\\\"" ), serverDigest ).TrimEnd();
  25.             SecurityUtil.CreateNewMd5();
  26.             var clientSignature = SecurityUtil.Digest( requestJson + session.digest );
  27.  
  28.             if( !string.Equals( serverDigest, session.digest ) || !string.Equals( serverSignatue, clientSignature ) )
  29.             {
  30.                 UnityEngine.Debug.LogFormat(
  31.                     "OOOHH WE GOT SOMETHING\npublicKey: {0}\nclientDigest: '{1}'\nserverDigest: '{2}'\np: {3}\ng: {4}\npub_key: {5}\nserverSignature: '{6}'\nclientSignature: '{7}'",
  32.                     publicKey,
  33.                     session.digest,
  34.                     serverDigest,
  35.                     session.p,
  36.                     session.g,
  37.                     session.pub_key,
  38.                     serverSignatue,
  39.                     clientSignature
  40.                 );
  41.                 return;
  42.             };
  43.  
  44.             // var serverSignatue = RunRubyFunction( "get_signature", "\"{0}\",\"{1}\"", requestJson, serverDigest );
  45.             // var clientSignature = SecurityUtil.Digest( requestJson + session.digest );
  46.  
  47.             // if( serverSignatue != clientSignature )
  48.             // {
  49.             //  UnityEngine.Debug.LogFormat(
  50.             //      "OOOHH WE GOT SOMETHING\njson: {0}\npublicKey: {1}\nserverDigest: {2}\nserverSignature: {3}\nclientSignature: {4}",
  51.             //      JsonWriter.Serialize( session ),
  52.             //      publicKey,
  53.             //      serverDigest,
  54.             //      serverSignatue,
  55.             //      clientSignature
  56.             //  );
  57.  
  58.             //  return;
  59.             // }
  60.         }
  61.  
  62.         UnityEngine.Debug.Log( "NOT THIS TIME :((" );
  63.     }
  64.  
  65.     private string RunRubyFunction( string functionName, string argFormat = "", params object[] args )
  66.     {
  67.         var runLine = string.Format( "\nputs {0}({1})", functionName, string.Format( argFormat, args ) );
  68.         // UnityEngine.Debug.Log( runLine );
  69.         return RunRuby( rubyCodeAsset.text + runLine );
  70.     }
  71.  
  72.     private static string RunRuby( string code )
  73.     {
  74.         using( var process = new Process() )
  75.         {
  76.             process.StartInfo.FileName = "ruby.exe";
  77.             process.StartInfo.UseShellExecute = false;
  78.             process.StartInfo.RedirectStandardInput = true;
  79.             process.StartInfo.RedirectStandardOutput = true;
  80.             process.StartInfo.RedirectStandardError = true;
  81.             process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  82.             process.Start();
  83.             process.StandardInput.WriteLine( code );
  84.             process.StandardInput.Flush();
  85.             process.StandardInput.Close();
  86.             var output = process.StandardOutput.ReadToEnd();
  87.             var error = process.StandardError.ReadToEnd();
  88.             process.WaitForExit();
  89.  
  90.             if( !string.IsNullOrEmpty( error ) )
  91.                 throw new System.Exception( error );
  92.  
  93.             return output;
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement