Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace teamwork_api.WinAuth
  11. {
  12.     class Auth
  13.     {
  14.         public List<string> Users { get; set; }
  15.         public Dictionary<string, string> Admins { get; set; }
  16.  
  17.         public Auth(List<string> users, Dictionary<string, string> admins)
  18.         {
  19.             Users = users;
  20.             Admins = admins;
  21.         }
  22.  
  23.         private string GetStringWOBlankLines(byte[] buffer)
  24.         {
  25.             for (int i = 0; i < buffer.Length; i++)
  26.             {
  27.                 if (buffer[i] == 0x00)
  28.                 {
  29.                     buffer[i] = 0x90;
  30.                 }
  31.             }
  32.             return ASCIIEncoding.ASCII.GetString(buffer).Replace("?", String.Empty);
  33.         }
  34.  
  35.         private void TransferClient([Out()] WinClient cl, WinClient cl2)
  36.         {
  37.             cl = cl2;
  38.         }
  39.  
  40.         private WinClient GetWinClient(TcpListener lis)
  41.         {
  42.             if (lis.Pending())
  43.             {
  44.                 return new WinClient(lis.AcceptTcpClient(), "locahost");
  45.             }
  46.             return null;
  47.         }
  48.  
  49.         private void xListen()
  50.         {
  51.             TcpListener lis = new TcpListener(6005);
  52.             lis.Start();
  53.             while (true)
  54.             {
  55.                 if (lis.Pending())
  56.                 {
  57.                     TcpClient client = lis.AcceptTcpClient();
  58.                     WinClient winCl = new WinClient(client, "localhost");
  59.                     if (client.Connected)
  60.                     {
  61.                         string arguments = "";
  62.                         using (Stream s = client.GetStream())
  63.                         {
  64.                             byte[] b = new byte[300];
  65.                             s.Read(b, 0, b.Length);
  66.                             arguments = GetStringWOBlankLines(b);
  67.                         }
  68.                         if (arguments.StartsWith("/login")) //unless but we have to make sure that we aren't recieving any other commands.
  69.                         {
  70.                             string user = arguments.Replace("/login ", "").Split(',')[0];
  71.                             string password = arguments.Replace("/login ", "").Split(',')[1];
  72.                             if (Admins.ContainsKey(user) && Admins.ContainsValue(password))
  73.                             {
  74.                                 //Transfer the Client to the server
  75.                                 //client.Connect("localhost", 10225);
  76.                                 winCl.Transfer(10225);
  77.                                 TransferClient(GetWinClient(lis), winCl);
  78.                             }
  79.                             else
  80.                             {
  81.                                 if (Users.Contains(user))
  82.                                 {
  83.                                     //Transfer the Client to the server
  84.                                     //client.Connect("localhost", 10225);
  85.                                     winCl.Transfer(10225);
  86.                                     TransferClient(GetWinClient(lis), winCl);
  87.                                 }
  88.                                 else
  89.                                 {
  90.                                     //Refuse any type of connections from this client
  91.                                     //client.Close();
  92.                                     winCl.Client.Close();
  93.                                     TransferClient(GetWinClient(lis), winCl);
  94.                                 }
  95.                             }
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.  
  102.         public void Run()
  103.         {
  104.             Thread thr = new Thread(new ThreadStart(xListen));
  105.             thr.Start();
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement