Guest User

Web/Utilities

a guest
Feb 7th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using Newtonsoft.Json;
  8. using RestSharp;
  9. using TarkovLibrary.Library.API;
  10.  
  11. namespace TarkovLibrary.Library
  12. {
  13.     public class Web
  14.     {
  15.         private Dictionary<Endpoint, RestClient> _clients = new Dictionary<Endpoint, RestClient>();
  16.  
  17.         public readonly string BSG_LAUNCHER_VERSION = "";
  18.         public readonly string EFT_CLIENT_VERSION = "";
  19.         public readonly string UNITY_VERSION = "";
  20.  
  21.         private string EFT_SESSION = "";
  22.         private const string EFT_HWID = "your hwid here";
  23.  
  24.         public enum Endpoint
  25.         {
  26.             Launcher,
  27.             Production,
  28.             Ragfair,
  29.             Trading
  30.         }
  31.  
  32.         public Web()
  33.         {
  34.             _clients.Add(Endpoint.Launcher, new RestClient("https://launcher.escapefromtarkov.com"));
  35.             _clients.Add(Endpoint.Production, new RestClient("https://prod.escapefromtarkov.com"));
  36.             _clients.Add(Endpoint.Ragfair, new RestClient("https://ragfair.escapefromtarkov.com"));
  37.             _clients.Add(Endpoint.Trading, new RestClient("https://trading.escapefromtarkov.com"));
  38.  
  39.             BSG_LAUNCHER_VERSION = "0.9.2.970";
  40.             EFT_CLIENT_VERSION = "0.12.3.5776";
  41.             UNITY_VERSION = "2018.4.13f1";
  42.         }
  43.  
  44.         public string GetHWID()
  45.         {
  46.             return EFT_HWID;
  47.         }
  48.  
  49.         public void SetSession(string sess)
  50.         {
  51.             EFT_SESSION = sess;
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Used for the first login request to eft
  56.         /// </summary>
  57.         /// <typeparam name="T"></typeparam>
  58.         /// <param name="p">The endpoint requested</param>
  59.         /// <param name="offset">The offset to the endpoint</param>
  60.         /// <param name="json">The content</param>
  61.         /// <returns></returns>
  62.         public async Task<T> PostLow<T>(Endpoint p, string offset, string json)
  63.         {
  64.             RestClient client = _clients[p];
  65.             RestRequest request = new RestRequest(offset, DataFormat.None); // We set the DataFormat to none to prevent RestSharp from throwing errors, since the data is still inflated
  66.  
  67.             request.Method = Method.POST; // Post it to bsg
  68.             request.AddJsonBody(json); // Add our body
  69.  
  70.             request.AddHeader("Content-Type", "application/json");
  71.             request.AddHeader("User-Agent", $"BSG Launcher {BSG_LAUNCHER_VERSION}");
  72.  
  73.             IRestResponse response = await client.ExecuteAsync(request);
  74.             BaseResponse<T> data = GetContentFromResponse<T>(response);
  75.  
  76.             return data.Get();
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Used to exchange tokens with EFT to get our session
  81.         /// </summary>
  82.         /// <typeparam name="T"></typeparam>
  83.         /// <param name="p"></param>
  84.         /// <param name="offset"></param>
  85.         /// <param name="json"></param>
  86.         /// <param name="token">The token from PostLow with the login offset</param>
  87.         /// <returns></returns>
  88.         public async Task<T> PostMedium<T>(Endpoint p, string offset, string json, string token)
  89.         {
  90.             RestClient client = _clients[p];
  91.             RestRequest request = new RestRequest(offset, DataFormat.None); // We set the DataFormat to none to prevent RestSharp from throwing errors, since the data is still inflated
  92.  
  93.             request.Method = Method.POST; // Post it to bsg
  94.             request.AddJsonBody(json); // Add our body
  95.  
  96.             request.AddHeader("Content-Type", "application/json");
  97.             request.AddHeader("User-Agent", $"BSG Launcher {BSG_LAUNCHER_VERSION}");
  98.             request.AddHeader("Authorization", token);
  99.  
  100.             IRestResponse response = await client.ExecuteAsync(request);
  101.             BaseResponse<T> data = GetContentFromResponse<T>(response);
  102.  
  103.             return data.Get();
  104.         }
  105.        
  106.         /// <summary>
  107.         /// Used to post to the general API
  108.         /// </summary>
  109.         /// <typeparam name="T"></typeparam>
  110.         /// <param name="p"></param>
  111.         /// <param name="offset"></param>
  112.         /// <param name="json"></param>
  113.         /// <returns></returns>
  114.         public async Task<T> PostHigh<T>(Endpoint p, string offset, string json)
  115.         {
  116.             RestClient client = _clients[p];
  117.             RestRequest request = new RestRequest(offset, DataFormat.None); // We set the DataFormat to none to prevent RestSharp from throwing errors, since the data is still inflated
  118.  
  119.             request.Method = Method.POST; // Post it to bsg
  120.             request.AddJsonBody(json); // Add our body
  121.  
  122.             request.AddHeader("Content-Type", "application/json");
  123.             request.AddHeader("User-Agent", $"UnityPlayer/{UNITY_VERSION} (UnityWebRequest/1.0, libcurl/7.52.0-DEV)");
  124.             request.AddHeader("App-Version", $"EFT Client {EFT_CLIENT_VERSION}");
  125.             request.AddHeader("X-Unity-Version", $"{UNITY_VERSION}");
  126.  
  127.             request.AddCookie("PHPSESSID", EFT_SESSION);
  128.  
  129.             IRestResponse response = await client.ExecuteAsync(request);
  130.             BaseResponse<T> data = GetContentFromResponse<T>(response);
  131.  
  132.             return data.Get();
  133.         }
  134.  
  135.         /// <summary>
  136.         /// Same as post high, but returns a string response of the raw json
  137.         /// </summary>
  138.         /// <param name="p"></param>
  139.         /// <param name="offset"></param>
  140.         /// <param name="json"></param>
  141.         /// <returns></returns>
  142.         public async Task<string> PostRaw(Endpoint p, string offset, string json)
  143.         {
  144.             RestClient client = _clients[p];
  145.             RestRequest request = new RestRequest(offset, DataFormat.None); // We set the DataFormat to none to prevent RestSharp from throwing errors, since the data is still inflated
  146.  
  147.             request.Method = Method.POST; // Post it to bsg
  148.             request.AddJsonBody(json); // Add our body
  149.  
  150.             request.AddHeader("Content-Type", "application/json");
  151.             request.AddHeader("User-Agent", $"UnityPlayer/{UNITY_VERSION} (UnityWebRequest/1.0, libcurl/7.52.0-DEV)");
  152.             request.AddHeader("App-Version", $"EFT Client {EFT_CLIENT_VERSION}");
  153.             request.AddHeader("X-Unity-Version", $"{UNITY_VERSION}");
  154.  
  155.             request.AddCookie("PHPSESSID", EFT_SESSION);
  156.  
  157.             IRestResponse response = await client.ExecuteAsync(request);
  158.             byte[] bytes = response.RawBytes;
  159.             string content = Encoding.UTF8.GetString(Utilities.Decompress(bytes));
  160.  
  161.             return content;
  162.         }
  163.  
  164.         private BaseResponse<T> GetContentFromResponse<T>(IRestResponse response)
  165.         {
  166.             byte[] bytes = response.RawBytes;
  167.             string content = Encoding.UTF8.GetString(Utilities.Decompress(bytes));
  168.  
  169.             return JsonConvert.DeserializeObject<BaseResponse<T>>(content);
  170.         }
  171.     }
  172. }
  173.  
  174.  
  175.  
  176. using System;
  177. using System.Collections.Generic;
  178. using System.Diagnostics;
  179. using System.IO;
  180. using System.IO.Compression;
  181. using System.Linq;
  182. using System.Security.Cryptography;
  183. using System.Text;
  184.  
  185. namespace TarkovLibrary.Library
  186. {
  187.     public class Utilities
  188.     {
  189.         public static string MD5Hash(string input)
  190.         {
  191.             using (MD5 md5Hash = MD5.Create())
  192.             {
  193.                 byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  194.                 StringBuilder sBuilder = new StringBuilder();
  195.                 for (int i = 0; i < data.Length; i++)
  196.                 {
  197.                     sBuilder.Append(data[i].ToString("x2"));
  198.                 }
  199.  
  200.                 return sBuilder.ToString();
  201.             }
  202.         }
  203.         public static byte[] Decompress(byte[] gzip)
  204.         {
  205.             using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
  206.             {
  207.                 const int size = 4096;
  208.                 byte[] buffer = new byte[size];
  209.                 using (MemoryStream memory = new MemoryStream())
  210.                 {
  211.                     int count = 0;
  212.                     do
  213.                     {
  214.                         count = stream.Read(buffer, 0, size);
  215.                         if (count > 0)
  216.                         {
  217.                             memory.Write(buffer, 0, count);
  218.                         }
  219.                     }
  220.                     while (count > 0);
  221.                     return memory.ToArray();
  222.                 }
  223.             }
  224.         }
  225.     }
  226. }
Add Comment
Please, Sign In to add comment