Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.32 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using LSPD_First_Response.Engine.Scripting.Entities;
  8. using LSPD_First_Response.Mod.API;
  9. using Rage;
  10. using Rage.Native;
  11. using Unosquare.Labs.EmbedIO;
  12. using Unosquare.Labs.EmbedIO.Modules;
  13. using HttpListenerContext = Unosquare.Net.HttpListenerContext;
  14.  
  15. namespace sardas_plugin
  16. {
  17.  
  18.     public class APIVehicle
  19.     {
  20.         public string name;
  21.  
  22.         // ReSharper disable once InconsistentNaming
  23.         private string OwnerName;
  24.         public string ownerName
  25.         {
  26.             get { return OwnerName; }
  27.  
  28.             set
  29.             {
  30.                 OwnerName = value;
  31.                 if(owner!=null)
  32.                     owner = Util.getPersonaForName(value);
  33.             }
  34.         }
  35.         public Persona owner { get; private set; }
  36.         public string regStatus;
  37.         public string insuranceStatus;
  38.         public bool stolen;
  39.     }
  40.     public class ResponseMessage
  41.     {
  42.         public object responseBody { get; set; }
  43.         public bool success;
  44.         public string message;
  45.  
  46.     }
  47.     public class WebAPI : WebApiController
  48.     {
  49.  
  50.         /// <summary>
  51.         /// In this method, we load up the .ini file so other methods can use it.
  52.         /// </summary>
  53.         /// <returns></returns>
  54.         public static InitializationFile initialiseFile()
  55.         {
  56.             //InitializationFile is a Rage class.
  57.             var ini = new InitializationFile("Plugins/LSPDFR/sardas.ini");
  58.             ini.Create();
  59.             return ini;
  60.         }
  61.  
  62.  
  63.         [WebApiHandler(HttpVerbs.Post, "/api/login/")]
  64.         public bool login(WebServer server, HttpListenerContext context)
  65.         {
  66.             try
  67.             {
  68.                 var initializationFile = initialiseFile();
  69.                 var username = initializationFile.ReadString("Login Data", "username", "ExampleOfficer");
  70.                 var password = initializationFile.ReadString("Login Data", "password", "ExamplePassword");
  71.                 var postData = context.RequestFormDataDictionary();
  72.                 Rage.Game.Console.Print($"Set Username: {username}");
  73.                 Rage.Game.Console.Print($"Set Password: {password}");
  74.                 if (postData!=null && postData.ContainsKey("username") && postData.ContainsKey("password"))
  75.                 {
  76.                     Rage.Game.Console.Print($"Username: {postData["username"]}");
  77.                     Rage.Game.Console.Print($"Password: {postData["password"]}");
  78.                     if ((string)postData["username"] == username && (string)postData["password"] == password)
  79.                     {
  80.                         return true;
  81.                     }
  82.                 }
  83.                 context.Response.StatusCode = 403;
  84.                 return true;
  85.             }
  86.             catch (Exception ex)
  87.             {
  88.                 return HandleError(context, ex);
  89.             }
  90.         }
  91.  
  92.         [WebApiHandler(HttpVerbs.Post, "/api/settings/login/")]
  93.         public bool changeLogin(WebServer server, HttpListenerContext context)
  94.         {
  95.             try
  96.             {
  97.                 var initializationFile = initialiseFile();
  98.                 var postData = context.RequestFormDataDictionary();
  99.                 if (postData != null && postData.ContainsKey("username"))
  100.                 {
  101.                     initializationFile.Write("Login Data", "username", postData["username"] as string);
  102.                     if (postData.ContainsKey("password"))
  103.                     {
  104.                         initializationFile.Write("Login Data", "password", postData["password"] as string);
  105.                     }
  106.                 }
  107.                 return true;
  108.             }
  109.             catch (Exception ex)
  110.             {
  111.                 return HandleError(context, ex);
  112.             }
  113.         }
  114.  
  115.         [WebApiHandler(HttpVerbs.Get, "/api/test/")]
  116.         public bool testAPI(WebServer server, HttpListenerContext context)
  117.         {
  118.             try
  119.             {
  120.                 foreach (var b in Encoding.ASCII.GetBytes("Hello World"))
  121.                 {
  122.                     context.Response.OutputStream.WriteByte(b);
  123.                 }
  124.                 context.Response.OutputStream.Close();
  125.                 context.Response.Close();
  126.                 return true;
  127.             }
  128.             catch (Exception ex)
  129.             {
  130.                 Game.LogVerbose("Error while accessing test API");
  131.                 Game.LogVerbose(ex.StackTrace);
  132.                 return HandleError(context, ex);
  133.             }
  134.         }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.         [WebApiHandler(HttpVerbs.Get, "/api/veh/{reg}")]
  141.         public bool getVehicle(WebServer server, HttpListenerContext context, string reg)
  142.         {
  143.             try
  144.             {
  145.  
  146.                 APIVehicle veh;
  147.                 if((veh=Util.getVehicleForPlate(reg))!=null)
  148.                 {
  149.                     return context.JsonResponse(new ResponseMessage()
  150.                     {
  151.                         success = true,
  152.                         message = "Vehicle Found",
  153.                         responseBody = veh
  154.                     });
  155.                 }
  156.                 context.Response.StatusCode = (int)HttpStatusCode.NotFound;
  157.                 return context.JsonResponse(new ResponseMessage()
  158.                 {
  159.                     success = false,
  160.                     message = "Vehicle not found",
  161.                     responseBody = null
  162.                 });
  163.  
  164.             }
  165.             catch (Exception ex)
  166.             {
  167.                 Game.LogVerbose("Error while accessing vehicle API");
  168.                 Game.LogVerbose(ex.StackTrace);
  169.                 return HandleError(context, ex);
  170.             }
  171.         }
  172.  
  173.         [WebApiHandler(HttpVerbs.Get, "/api/ped/{name}")]
  174.         public bool getPed(WebServer server, HttpListenerContext context, string name)
  175.         {
  176.             try
  177.             {
  178.                 var persona = Util.getPersonaForName(name);
  179.                 if (persona != null)
  180.                 {
  181.                     return context.JsonResponse(new ResponseMessage()
  182.                     {
  183.                         success = true,
  184.                         message = "Ped found",
  185.                         responseBody = persona
  186.                     });
  187.                 }
  188.                 context.Response.StatusCode = (int)HttpStatusCode.NotFound;
  189.                 return context.JsonResponse(new ResponseMessage()
  190.                 {
  191.                     success = false,
  192.                     message = "Ped not found",
  193.                     responseBody = null
  194.                 });
  195.  
  196.             }
  197.             catch (Exception ex)
  198.             {
  199.                 Game.LogVerbose("Error while accessing ped API");
  200.                 Game.LogVerbose(ex.StackTrace);
  201.                 return HandleError(context, ex);
  202.             }
  203.         }
  204.  
  205.         protected bool HandleError(HttpListenerContext context, Exception ex, int statusCode = 500)
  206.         {
  207.             var errorResponse = new
  208.             {
  209.                 Title = "Unexpected Error",
  210.                 ErrorCode = ex.GetType().Name,
  211.                 Description = ex.ExceptionMessage(),
  212.             };
  213.  
  214.             context.Response.StatusCode = statusCode;
  215.             return context.JsonResponse(errorResponse);
  216.         }
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement