Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 50.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Threading;
  10.  
  11. namespace WhisperServer
  12. {
  13.     class Program
  14.     {
  15.         static Socket sv = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  16.         static IPEndPoint ep = new IPEndPoint(IPAddress.Any, 9647);
  17.         static List<Thread> accepters = new List<Thread>();
  18.         static Hashtable users = new Hashtable();
  19.         static List<string> queue = new List<string>();
  20.         static bool closing = false;
  21.         static Thread lst;
  22.         static Thread msg;
  23.         static byte[] cmsg = new byte[] { 0x00, 0x00, 0x00, 0x01, 0x05 };
  24.         static void Main(string[] args)
  25.         {            
  26.             Info("Starting server.");
  27.             sv.Bind(ep);
  28.             sv.Listen(5);
  29.             lst = new Thread(new ThreadStart(Listener));
  30.             lst.Start();
  31.             msg = new Thread(new ThreadStart(MsgHandler));
  32.             msg.Start();
  33.             while (true)
  34.             {
  35.                 ServerCommand com = new ServerCommand(Console.ReadLine());                
  36.                 if (com.CommandName.ToLower() == "stop")
  37.                 {
  38.                     closing = true;
  39.                 }
  40.             }
  41.         }
  42.         static void Info(string message)
  43.         {
  44.             Console.WriteLine("[INFO] " + message);
  45.         }
  46.         static void Error(string message)
  47.         {
  48.             Console.WriteLine("[ERROR] " + message);
  49.         }
  50.         static void Warning(string message)
  51.         {
  52.             Console.WriteLine("[WARNING] " + message);
  53.         }
  54.         static void Chat(string message)
  55.         {
  56.             Console.WriteLine("[CHAT] " + message);
  57.         }
  58.         static void Listener()
  59.         {
  60.             while (true)
  61.             {
  62.                 if (sv.Poll(-1, SelectMode.SelectRead))
  63.                 {
  64.                     Thread a = new Thread(new ParameterizedThreadStart(Accepter));
  65.                     a.Start(sv.Accept());
  66.                 }
  67.             }
  68.         }
  69.         static void MsgHandler()
  70.         {
  71.             while (true)
  72.             {
  73.                 while (!closing && queue.Count == 0)
  74.                 {
  75.                     Thread.Sleep(1);
  76.                 }
  77.                 if (closing)
  78.                 {
  79.                     Info("Shutting down server.");
  80.                     Thread.Sleep(1000);
  81.                     if (users.Count > 0)
  82.                     {
  83.                         foreach (Client c in users)
  84.                         {
  85.                             try
  86.                             {
  87.                                 NetworkStream net = new NetworkStream(c.sock);
  88.                                 net.Write(cmsg, 0, cmsg.Length);
  89.                                 net.Close();
  90.                                 DisconnectUser(c);
  91.                             }
  92.                             catch
  93.                             {
  94.                                 Error("Couldn't disconnect user " + c.name + ".");
  95.                             }
  96.                             finally
  97.                             {
  98.                             }
  99.                         }
  100.                         users.Clear();
  101.                     }
  102.                     Environment.Exit(0);
  103.                 }
  104.                 if (queue.Count > 0)
  105.                 {
  106.                     foreach (Client c in users)
  107.                     {
  108.                         try
  109.                         {
  110.                             foreach (string s in queue)
  111.                             {
  112.                                 NetworkStream net = new NetworkStream(c.sock);
  113.                                 byte[] m = CreatePacket(ServerPacketType.ChatPost, s);
  114.                                 net.Write(m, 0, m.Length);
  115.                             }
  116.                         }
  117.                         catch (Exception e)
  118.                         {
  119.                             Error("Server error occured with user " + c.name + ". Exception data: " + e);
  120.                             Info("Disconnecting user " + c.name + " from server.");
  121.                             queue.Add("[INFO] " + c.name + " has been disconnected due to an internal server error.");
  122.                             DisconnectUser(c);
  123.                             users.Remove(c.name);
  124.                         }
  125.                         finally
  126.                         {
  127.                         }
  128.                     }
  129.                     queue.Clear();
  130.                 }
  131.  
  132.             }
  133.         }
  134.         static void Accepter(object socket)
  135.         {
  136.             Socket sock = (Socket)socket;
  137.             Client c = new Client();
  138.             IPAddress sockip = ((IPEndPoint)sock.RemoteEndPoint).Address;
  139.             try
  140.             {
  141.                 bool invalid = false;                
  142.                 byte[] req1 = CreatePacket(ServerPacketType.RequestUsername, "");
  143.                 sock.Send(req1);
  144.                 if (sock.Poll(5000000, SelectMode.SelectRead))
  145.                 {
  146.                     if (sock.Available > 0)
  147.                     {
  148.                         int len = 0;
  149.                         NetworkStream ns = new NetworkStream(sock);
  150.                         if (ns.Length > 4)
  151.                         {
  152.                             byte[] l = new byte[4];
  153.                             ns.Read(l, 0, 4);
  154.                             len = BitConverter.ToInt32(l, 0);
  155.                             byte[] dat = new byte[len];
  156.                             ns.Read(dat, 0, len);
  157.                             ClientPacket cp = ClientPacket.Parse(dat);
  158.                             if (cp != null)
  159.                             {
  160.                                 if (cp.PacketType == ClientPacketType.Username && cp.Data.Length > 0)
  161.                                 {
  162.                                     c.name = cp.Data;
  163.                                 }
  164.                                 else
  165.                                 {
  166.                                     invalid = true;
  167.                                 }
  168.                             }
  169.                             else
  170.                             {
  171.                                 invalid = true;
  172.                             }
  173.                         }
  174.                         else
  175.                         {
  176.                             invalid = true;
  177.                         }
  178.                         ns.Close();
  179.                     }
  180.                     else
  181.                     {
  182.                         invalid = true;
  183.                     }
  184.                     // Check if this user is banned?
  185.                     if (!invalid)
  186.                     {
  187.                         List<Ban> banlist = GetBanlist();
  188.                         if (banlist != null)
  189.                         {
  190.                             foreach (Ban b in banlist)
  191.                             {
  192.                                 if (sockip == b.IP || c.name == b.UserName)
  193.                                 {
  194.                                     // The 7 lines of code sure to piss anybody off
  195.                                     NetworkStream ns = new NetworkStream(sock);
  196.                                     byte[] rejban = CreatePacket(ServerPacketType.Rejected, "[ERROR] Rejected: You are banned from this server!");
  197.                                     ns.Write(rejban, 0, rejban.Length);
  198.                                     ns.Close();
  199.                                     sock.Shutdown(SocketShutdown.Both);
  200.                                     sock.Close();
  201.                                     invalid = true;
  202.                                     break;
  203.                                 }
  204.                             }
  205.                         }
  206.                     }
  207.                     //Check user rank
  208.                     if (!invalid)
  209.                     {
  210.                         List<UserSpec> userlist = GetUserSpeclist();
  211.                         if (userlist != null)
  212.                         {
  213.                             foreach (UserSpec spec in userlist)
  214.                             {
  215.                                 if (sockip == IPAddress.Parse("127.0.0.1") || sockip == IPAddress.Parse("192.168.1.1"))
  216.                                 {
  217.                                     c.rank = Rank.Host;
  218.                                 }
  219.                                 else if (sockip == spec.IP)
  220.                                 {
  221.                                     c.rank = spec.r;
  222.                                     break;
  223.                                 }
  224.                             }
  225.                         }
  226.                         c.sock = sock;
  227.                         string nn = c.name;
  228.                         while (users.ContainsKey(nn))
  229.                         {
  230.                             nn += "_";
  231.                         }
  232.                         c.name = nn;
  233.                         users.Add(c.name, c);
  234.                     }
  235.                     if (invalid)
  236.                     {
  237.                         sock.Shutdown(SocketShutdown.Both);
  238.                         sock.Close();
  239.                     }
  240.                 }
  241.             }
  242.             catch
  243.             {
  244.                 Error(sockip.ToString() + " could not be connected to the server.");
  245.                 sock.Shutdown(SocketShutdown.Both);
  246.                 sock.Close();
  247.             }
  248.         }
  249.         static void ClientThread(object cl)
  250.         {
  251.             Client c = (Client)cl;
  252.             while (true)
  253.             {
  254.                 try
  255.                 {
  256.                     bool invalid = false;
  257.                     if (c.sock.Poll(-1, SelectMode.SelectRead))
  258.                     {
  259.                         if (c.sock.Available > 0)
  260.                         {
  261.                             int len = 0;
  262.                             NetworkStream ns = new NetworkStream(c.sock);
  263.                             if (ns.Length > 4)
  264.                             {
  265.                                 while (ns.Position < ns.Length)
  266.                                 {
  267.                                     if (invalid)
  268.                                     {
  269.                                         break;
  270.                                     }
  271.                                     byte[] l = new byte[4];
  272.                                     ns.Read(l, 0, 4);
  273.                                     len = BitConverter.ToInt32(l, 0);
  274.                                     byte[] dat = new byte[len];
  275.                                     ns.Read(dat, 0, len);
  276.                                     ClientPacket cp = ClientPacket.Parse(dat);
  277.                                     if (cp != null)
  278.                                     {
  279.                                         //Process packet here
  280.                                         switch (cp.PacketType)
  281.                                         {
  282.                                             case ClientPacketType.ChatPost:
  283.                                                 ChatCommand com = new ChatCommand(cp.Data);
  284.                                                 if (!com.Invalid)
  285.                                                 {
  286.                                                     switch (com.CommandName)
  287.                                                     {
  288.                                                         case "me":
  289.                                                             #region /me
  290.                                                             if (c.rank != Rank.Troll)
  291.                                                             {
  292.                                                                 ChatCommand c2 = new ChatCommand(cp.Data, 0);
  293.                                                                 queue.Add(c.name + " " + c2.Parameters[0]);
  294.                                                             }
  295.                                                             else
  296.                                                             {
  297.                                                                 SendMessage(c, "[ERROR] You are a troll!");
  298.                                                             }
  299.                                                             #endregion
  300.                                                             break;
  301.                                                         case "kick":
  302.                                                             #region Kick
  303.                                                             if ((byte)c.rank > 4)
  304.                                                             {
  305.                                                                 if (com.Parameters.Count == 1)
  306.                                                                 {
  307.                                                                     if (users.ContainsKey(com.Parameters[0]))
  308.                                                                     {
  309.                                                                         ((Client)(users[com.Parameters[0]])).state = PenaltyState.Kicked;
  310.                                                                         try
  311.                                                                         {
  312.                                                                             byte[] kickmsg = CreatePacket(ServerPacketType.Kicked, "");
  313.                                                                             ((Client)(users[com.Parameters[0]])).sock.Send(kickmsg);
  314.                                                                             DisconnectUser(((Client)(users[com.Parameters[0]])));
  315.                                                                             users.Remove(com.Parameters[0]);
  316.                                                                             queue.Add("[INFO] " + c.name + " has kicked " + com.Parameters[0] + " from the server.");
  317.                                                                         }
  318.                                                                         catch
  319.                                                                         {
  320.                                                                             ((Client)(users[com.Parameters[0]])).error = true;
  321.                                                                         }
  322.                                                                     }
  323.                                                                     else
  324.                                                                     {
  325.                                                                         SendMessage(c, "[ERROR] No user with the name \"" + com.Parameters[0] + "\" exists.");
  326.                                                                     }
  327.                                                                 }
  328.                                                                 else
  329.                                                                 {
  330.                                                                     SendMessage(c, "[ERROR] Invalid parameter count '" + com.Parameters.Count + "'. This command takes 1 parameter.");
  331.                                                                 }
  332.                                                             }
  333.                                                             else
  334.                                                             {
  335.                                                                 SendMessage(c, "[ERROR] Your current rank does not allow you to kick users.");
  336.                                                             }
  337.                                                             #endregion
  338.                                                             break;
  339.                                                         case "ban":
  340.                                                             #region Ban
  341.                                                             if ((byte)c.rank > (byte)Rank.VIP)
  342.                                                             {
  343.                                                                 if (com.Parameters.Count == 1)
  344.                                                                 {
  345.                                                                     if (users.ContainsKey(com.Parameters[0]))
  346.                                                                     {
  347.                                                                         ((Client)(users[com.Parameters[0]])).state = PenaltyState.Banned;
  348.                                                                         try
  349.                                                                         {
  350.                                                                             byte[] kickmsg = CreatePacket(ServerPacketType.Banned, "");
  351.                                                                             ((Client)(users[com.Parameters[0]])).sock.Send(kickmsg);
  352.                                                                             DisconnectUser(((Client)(users[com.Parameters[0]])));
  353.                                                                             Ban b = new Ban();
  354.                                                                             b.UserName = com.Parameters[0];
  355.                                                                             b.IP = ((IPEndPoint)(((Client)(users[com.Parameters[0]])).sock.RemoteEndPoint)).Address;
  356.                                                                             try
  357.                                                                             {
  358.                                                                                 List<Ban> current = GetBanlist();
  359.                                                                                 StreamWriter writer = new StreamWriter("banned.txt");
  360.                                                                                 if (current != null)
  361.                                                                                 {
  362.                                                                                     foreach (Ban ban in current)
  363.                                                                                     {
  364.                                                                                         writer.WriteLine(ban.UserName + "|" + ban.IP.ToString());
  365.                                                                                     }
  366.                                                                                     writer.WriteLine(b.UserName + "|" + b.IP.ToString());
  367.                                                                                 }
  368.                                                                                 writer.Close();
  369.                                                                             }
  370.                                                                             catch
  371.                                                                             {
  372.                                                                                 SendMessage(c, "[ERROR] A problem occured when trying to log the ban. The targeted user may still be able to reconnect.");
  373.                                                                                 Error("A problem occured when trying to log a ban issued by " + c.name + " for " + com.Parameters[0] + ". The targeted user may still be able to reconnect.");
  374.                                                                             }
  375.                                                                             users.Remove(com.Parameters[0]);
  376.                                                                             queue.Add("[INFO] " + c.name + " has banned " + com.Parameters[0] + " from the server.");
  377.                                                                         }
  378.                                                                         catch
  379.                                                                         {
  380.                                                                             ((Client)(users[com.Parameters[0]])).error = true;
  381.                                                                         }
  382.                                                                     }
  383.                                                                     else
  384.                                                                     {
  385.                                                                         SendMessage(c, "[ERROR] No user with the name \"" + com.Parameters[0] + "\" exists.");
  386.                                                                     }
  387.                                                                 }
  388.                                                                 else
  389.                                                                 {
  390.                                                                     SendMessage(c, "[ERROR] Invalid parameter count '" + com.Parameters.Count + "'. This command takes 1 parameter.");
  391.                                                                 }
  392.                                                             }
  393.                                                             else
  394.                                                             {
  395.                                                                 SendMessage(c, "[ERROR] Your current rank does not allow you to ban users.");
  396.                                                             }
  397.                                                             #endregion
  398.                                                             break;
  399.                                                         case "mute":
  400.                                                             #region Mute
  401.                                                             if ((byte)c.rank > (byte)Rank.VIP)
  402.                                                             {
  403.                                                                 if (com.Parameters.Count == 1)
  404.                                                                 {
  405.                                                                     if (users.ContainsKey(com.Parameters[0]))
  406.                                                                     {
  407.                                                                         if ((byte)((Client)(users[com.Parameters[0]])).rank < (byte)Rank.VIP)
  408.                                                                         {
  409.                                                                             ((Client)(users[com.Parameters[0]])).state = PenaltyState.Muted;
  410.                                                                             queue.Add("[INFO] " + c.name + " has muted " + com.Parameters[0] + ".");
  411.                                                                         }
  412.                                                                         else
  413.                                                                         {
  414.                                                                             SendMessage(c, "[ERROR] You may not mute moderators or the host.");
  415.                                                                         }
  416.                                                                                                                                              
  417.                                                                     }
  418.                                                                     else
  419.                                                                     {
  420.                                                                         SendMessage(c, "[ERROR] No user with the name \"" + com.Parameters[0] + "\" exists.");
  421.                                                                     }
  422.                                                                 }
  423.                                                                 else
  424.                                                                 {
  425.                                                                     SendMessage(c, "[ERROR] Invalid parameter count '" + com.Parameters.Count + "'. This command takes 1 parameter.");
  426.                                                                 }
  427.                                                             }
  428.                                                             else
  429.                                                             {
  430.                                                                 SendMessage(c, "[ERROR] Your current rank does not allow you to mute users.");
  431.                                                             }
  432.                                                             #endregion
  433.                                                             break;
  434.                                                         case "unmute":
  435.                                                             #region Unmute
  436.                                                             if ((byte)c.rank > (byte)Rank.VIP)
  437.                                                             {
  438.                                                                 if (com.Parameters.Count == 1)
  439.                                                                 {
  440.                                                                     if (users.ContainsKey(com.Parameters[0]))
  441.                                                                     {
  442.                                                                         if ((byte)((Client)(users[com.Parameters[0]])).rank < (byte)Rank.VIP)
  443.                                                                         {
  444.                                                                             ((Client)(users[com.Parameters[0]])).state = PenaltyState.None;
  445.                                                                             queue.Add("[INFO] " + c.name + " has unmuted " + com.Parameters[0] + ".");
  446.                                                                         }
  447.                                                                         else
  448.                                                                         {
  449.                                                                             SendMessage(c, "[ERROR] Moderators and the host cannot be muted anyway, so what are you playing at?!");
  450.                                                                         }
  451.                                                                     }
  452.                                                                     else
  453.                                                                     {
  454.                                                                         SendMessage(c, "[ERROR] No user with the name \"" + com.Parameters[0] + "\" exists.");
  455.                                                                     }
  456.                                                                 }
  457.                                                                 else
  458.                                                                 {
  459.                                                                     SendMessage(c, "[ERROR] Invalid parameter count '" + com.Parameters.Count + "'. This command takes 1 parameter.");
  460.                                                                 }
  461.                                                             }
  462.                                                             else
  463.                                                             {
  464.                                                                 SendMessage(c, "[ERROR] Your current rank does not allow you to unmute users.");
  465.                                                             }
  466.                                                             #endregion
  467.                                                             break;
  468.                                                         case "unban":
  469.                                                             #region Unban
  470.                                                             if ((byte)c.rank > (byte)Rank.VIP)
  471.                                                             {
  472.                                                                 if (com.Parameters.Count == 1)
  473.                                                                 {
  474.                                                                             try
  475.                                                                             {
  476.                                                                                 List<Ban> current = GetBanlist();
  477.                                                                                 List<string> names = new List<string>();
  478.                                                                                 int ind = 0;
  479.                                                                                 if (current != null)
  480.                                                                                 {
  481.                                                                                     for (int i = 0; i < current.Count; i++)
  482.                                                                                     {
  483.                                                                                         if (current[i].UserName == com.Parameters[0])
  484.                                                                                         {
  485.                                                                                             ind = i;
  486.                                                                                         }
  487.                                                                                         names.Add(current[i].UserName);
  488.                                                                                     }
  489.                                                                                     if (names.Contains(com.Parameters[0]))
  490.                                                                                     {
  491.                                                                                         current.RemoveAt(ind);
  492.                                                                                         StreamWriter writer = new StreamWriter("banned.txt");
  493.                                                                                         foreach (Ban b in current)
  494.                                                                                         {
  495.                                                                                             writer.WriteLine(b.UserName + "|" + b.IP.ToString());
  496.                                                                                         }
  497.                                                                                         writer.Close();
  498.                                                                                         queue.Add("[INFO] " + c.name + " has unbanned " + com.Parameters[0] + ".");
  499.                                                                                     }
  500.                                                                                     else
  501.                                                                                     {
  502.                                                                                         SendMessage(c, "[ERROR] There is no banned user named " + com.Parameters[0] + ".");
  503.                                                                                     }
  504.                                                                                 }
  505.                                                                                 else
  506.                                                                                 {
  507.                                                                                     SendMessage(c, "[ERROR] There is no existing ban list.");
  508.                                                                                 }
  509.                                                                                
  510.                                                                             }
  511.                                                                             catch
  512.                                                                             {
  513.                                                                                 SendMessage(c, "[ERROR] A problem occured when trying to remove the ban. The targeted user may still be banned.");
  514.                                                                                 Error("A problem occured when trying to remove a ban issued to" + com.Parameters[0] + ". The targeted user may still be banned.");
  515.                                                                             }    
  516.                                                                 }
  517.                                                                 else
  518.                                                                 {
  519.                                                                     SendMessage(c, "[ERROR] Invalid parameter count '" + com.Parameters.Count + "'. This command takes 1 parameter.");
  520.                                                                 }
  521.                                                             }
  522.                                                             else
  523.                                                             {
  524.                                                                 SendMessage(c, "[ERROR] Your current rank does not allow you to unban users.");
  525.                                                             }
  526.                                                             #endregion
  527.                                                             break;
  528.                                                         case "getrank":
  529.                                                             #region Getrank
  530.                                                             if (com.Parameters.Count == 1)
  531.                                                                 {
  532.                                                                     if (users.ContainsKey(com.Parameters[0]))
  533.                                                                     {
  534.                                                                         SendMessage(c, "[INFO] " + ((Client)(users[com.Parameters[0]])).name + " is a " + ((Client)(users[com.Parameters[0]])).rank.ToString() + ".");
  535.                                                                     }
  536.                                                                     else
  537.                                                                     {
  538.                                                                         SendMessage(c, "[ERROR] No user with the name \"" + com.Parameters[0] + "\" exists.");
  539.                                                                     }
  540.                                                                 }
  541.                                                                 else
  542.                                                                 {
  543.                                                                     SendMessage(c, "[ERROR] Invalid parameter count '" + com.Parameters.Count + "'. This command takes 1 parameter.");
  544.                                                                 }            
  545.                                                                 #endregion
  546.                                                             break;
  547.                                                         case "rank":
  548.                                                             #region Mute
  549.                                                             if ((byte)c.rank > (byte)Rank.VIP)
  550.                                                             {
  551.                                                                 if (com.Parameters.Count == 2)
  552.                                                                 {
  553.                                                                     if (users.ContainsKey(com.Parameters[0]))
  554.                                                                     {
  555.                                                                         if ((byte)((Client)(users[com.Parameters[0]])).rank < (byte)Rank.Mod)
  556.                                                                         {
  557.                                                                             byte r;
  558.                                                                             if (Byte.TryParse(com.Parameters[1], out r))
  559.                                                                             {
  560.                                                                                 if (r == (byte)Rank.Troll || r == (byte)Rank.Noob || r == (byte)Rank.User || r == (byte)Rank.VIP || r == (byte)Rank.Mod)
  561.                                                                                 {
  562.                                                                                     ((Client)(users[com.Parameters[0]])).rank = (Rank)r;
  563.                                                                                     queue.Add("[INFO] " + c.name + " has ranked " + com.Parameters[0] + " as a " + ((Client)(users[com.Parameters[0]])).rank.ToString() + ".");
  564.                                                                                 }
  565.                                                                                 else
  566.                                                                                 {
  567.                                                                                     SendMessage(c, "[ERROR] Invalid rank. Rank numbers are numbered by the power of 2 (i.e. 1, 2, 4, 8, 16, etc.)");
  568.                                                                                 }
  569.                                                                             }
  570.                                                                             else
  571.                                                                             {
  572.                                                                                 SendMessage(c, "[ERROR] Invalid rank. Rank numbers are numbered by the power of 2 (i.e. 1, 2, 4, 8, 16, etc.)");
  573.                                                                             }
  574.                                                                         }
  575.                                                                         else
  576.                                                                         {
  577.                                                                             SendMessage(c, "[ERROR] You may not change the ranks of moderators or the host.");
  578.                                                                         }
  579.  
  580.                                                                     }
  581.                                                                     else
  582.                                                                     {
  583.                                                                         SendMessage(c, "[ERROR] No user with the name \"" + com.Parameters[0] + "\" exists.");
  584.                                                                     }
  585.                                                                 }
  586.                                                                 else
  587.                                                                 {
  588.                                                                     SendMessage(c, "[ERROR] Invalid parameter count '" + com.Parameters.Count + "'. This command takes 2 parameters.");
  589.                                                                 }
  590.                                                             }
  591.                                                             else
  592.                                                             {
  593.                                                                 SendMessage(c, "[ERROR] Your current rank does not allow you to rank users.");
  594.                                                             }
  595.                                                             #endregion
  596.                                                             break;
  597.                                                     }
  598.                                                 }
  599.                                                 else
  600.                                                 {
  601.                                                     queue.Add(c.name + ": " + cp.Data);
  602.                                                 }
  603.                                                 break;
  604.                                             case ClientPacketType.Leaving:
  605.                                                 queue.Add(c.name + " left the server.");
  606.                                                 DisconnectUser(c);
  607.                                                 users.Remove(c.name);
  608.                                                 invalid = true;
  609.                                                 break;
  610.                                             default:
  611.                                                 break;
  612.                                         }
  613.                                     }
  614.                                     else
  615.                                     {
  616.                                         invalid = true;
  617.                                     }
  618.                                 }
  619.                             }
  620.                             else
  621.                             {
  622.                                 invalid = true;
  623.                             }
  624.                             ns.Close();
  625.                         }
  626.                     }
  627.                 }
  628.                 catch
  629.                 {
  630.  
  631.                 }
  632.             }
  633.         }
  634.         static void DisconnectUser(Client c)
  635.         {
  636.             c.sock.Shutdown(SocketShutdown.Both);
  637.             c.sock.Close();
  638.         }
  639.         static void SendMessage(Client c, string message)
  640.         {
  641.             byte[] m = CreatePacket(ServerPacketType.ChatPost, message);
  642.             c.sock.Send(m);            
  643.         }
  644.         static byte[] CreatePacket(ServerPacketType type, string info)
  645.         {
  646.             byte[] a = new byte[] { (byte)type };
  647.             byte[] b = Encoding.ASCII.GetBytes(info);
  648.             byte[] c = info.Length > 0 ? a.Concat(b).ToArray() : a;
  649.             MemoryStream ms = new MemoryStream();
  650.             ms.Write(BitConverter.GetBytes(c.Length), 0, 4);
  651.             ms.Write(c, 0, c.Length);
  652.             ms.Position = 0;
  653.             byte[] final = new byte[ms.Length];            
  654.             ms.Read(final, 0, (int)ms.Length);
  655.             return final;
  656.         }
  657.  
  658.         static List<Ban> GetBanlist()
  659.         {
  660.             if (File.Exists("banned.txt"))
  661.             {
  662.                 List<Ban> list = new List<Ban>();
  663.                 StreamReader sr = new StreamReader("banned.txt");
  664.                 string raw = sr.ReadToEnd();
  665.                 sr.Close();
  666.                 string[] sl = raw.Split('\n');
  667.                 foreach (string s in sl)
  668.                 {
  669.                     int ind = s.LastIndexOf("|");
  670.                     if (ind > -1)
  671.                     {
  672.                         string n = Split(s, 0, ind);
  673.                         IPAddress ip;
  674.                         if (!IPAddress.TryParse(Split(s, ind + 1, s.Length), out ip))
  675.                         {
  676.                             continue;
  677.                         }
  678.                         else
  679.                         {
  680.                             Ban b = new Ban();
  681.                             b.UserName = n;
  682.                             b.IP = ip;
  683.                             list.Add(b);
  684.                         }
  685.                     }
  686.                 }
  687.                 return list;
  688.             }
  689.             else
  690.             {
  691.                 return null;
  692.             }
  693.         }
  694.         static List<UserSpec> GetUserSpeclist()
  695.         {
  696.             if (File.Exists("users.txt"))
  697.             {
  698.                 List<UserSpec> list = new List<UserSpec>();
  699.                 StreamReader sr = new StreamReader("users.txt");
  700.                 string raw = sr.ReadToEnd();
  701.                 sr.Close();
  702.                 string[] sl = raw.Split('\n');
  703.                 foreach (string s in sl)
  704.                 {
  705.                     int ind = s.LastIndexOf("|");
  706.                     if (ind > -1)
  707.                     {
  708.                         byte rank;
  709.                         IPAddress ip;
  710.                         if (!IPAddress.TryParse(Split(s, 0, ind), out ip) || !Byte.TryParse(Split(s, ind + 1, s.Length), out rank))
  711.                         {
  712.                             continue;
  713.                         }
  714.                         else
  715.                         {
  716.                             UserSpec b = new UserSpec();
  717.                             if (rank > 0 && rank < 33)
  718.                             {
  719.                                 b.r = (Rank)rank;
  720.                             }
  721.                             else
  722.                             {
  723.                                 b.r = (Rank)4;
  724.                             }
  725.                             b.IP = ip;
  726.                             list.Add(b);
  727.                         }
  728.                     }
  729.                 }
  730.                 return list;
  731.             }
  732.             else
  733.             {
  734.                 return null;
  735.             }
  736.         }
  737.         public static string Split(string parent, int start, int end)
  738.         {
  739.             return parent.Substring(start, end - start);
  740.         }
  741.     }
  742.     public enum Rank
  743.     {
  744.         Troll = 1,
  745.         Noob = 2,
  746.         User = 4,
  747.         VIP = 8,
  748.         Mod = 16,
  749.         Host = 32
  750.     }
  751.     public enum ServerPacketType : byte
  752.     {
  753.         ChatPost = 0,
  754.         Accepted = 1,
  755.         Rejected = 2,
  756.         Kicked = 3,
  757.         Banned = 4,
  758.         Shutdown = 5,
  759.         MOTD = 6,
  760.         UserList = 7,
  761.         Muted = 8,
  762.         Unmuted = 9,
  763.         RequestPassword = 10,
  764.         RequestUsername = 11
  765.     }
  766.     public enum ClientPacketType : byte
  767.     {
  768.         ChatPost = 0,
  769.         Username = 1,
  770.         Password = 2,
  771.         Leaving = 3
  772.     }
  773.     public enum PenaltyState
  774.     {
  775.         None = 0,
  776.         Muted = 1,
  777.         Kicked = 2,
  778.         Banned = 3
  779.     }
  780.     public class Client
  781.     {
  782.         public Socket sock;
  783.         public PenaltyState state = PenaltyState.None;
  784.         public bool error = false;        
  785.         public string name;
  786.         public Rank rank;
  787.     }
  788.     public struct Ban
  789.     {
  790.         public string UserName;
  791.         public IPAddress IP;
  792.     }
  793.     public struct UserSpec
  794.     {
  795.         public IPAddress IP;
  796.         public Rank r;
  797.     }
  798.     public class ChatCommand
  799.     {
  800.         string cmd;
  801.         string name;
  802.         List<string> pmts;
  803.         public bool Invalid = false;
  804.         public ChatCommand(string command)
  805.         {
  806.             if (!command.StartsWith("/"))
  807.             {
  808.                 Invalid = true;
  809.             }
  810.             else
  811.             {
  812.                 cmd = command;
  813.                 List<string> pts = command.Split(' ').ToList();
  814.                 name = pts[0].Substring(1);
  815.                 pts.RemoveAt(0);
  816.                 pmts = pts;
  817.             }
  818.         }
  819.         public ChatCommand(string command, int combineStart)
  820.         {
  821.             if (!command.StartsWith("/"))
  822.             {
  823.                 Invalid = true;
  824.             }
  825.             else
  826.             {
  827.                 cmd = command;
  828.                 List<string> pts = command.Split(' ').ToList();
  829.                 name = pts[0].Substring(1);
  830.                 pts.RemoveAt(0);
  831.                 string last = "";
  832.                 for (int i = pts.Count - 1; i >= combineStart; i--)
  833.                 {
  834.                     last = pts[i] + " " + last;
  835.                 }
  836.                 last = last.Substring(0, last.Length - 1);
  837.                 pts.Add(last);
  838.                 pmts = pts;
  839.             }
  840.         }
  841.         public List<string> Parameters
  842.         {
  843.             get { return pmts; }
  844.         }
  845.         public string CommandName
  846.         {
  847.             get { return name; }
  848.         }
  849.         public string CommandString
  850.         {
  851.             get { return cmd; }
  852.         }
  853.     }
  854.  
  855.     public class ServerCommand
  856.     {
  857.         string cmd;
  858.         string name;
  859.         List<string> pmts;
  860.         public ServerCommand(string command)
  861.         {            
  862.             cmd = command;
  863.             List<string> pts = command.Split(' ').ToList();
  864.             name = pts[0];
  865.             pts.RemoveAt(0);
  866.             pmts = pts;
  867.         }
  868.         public ServerCommand(string command, int combineStart)
  869.         {
  870.             if (!command.StartsWith("/"))
  871.             {
  872.                 throw new InvalidDataException("The string you provided is not prefixed with a slash.");
  873.             }
  874.             cmd = command;
  875.             List<string> pts = command.Split(' ').ToList();
  876.             name = pts[0].Substring(1);
  877.             pts.RemoveAt(0);
  878.             string last = "";
  879.             for (int i = pts.Count - 1; i >= combineStart; i--)
  880.             {
  881.                 last = pts[i] + " " + last;
  882.             }
  883.             last = last.Substring(0, last.Length - 1);
  884.             pts.Add(last);
  885.             pmts = pts;
  886.         }
  887.         public List<string> Parameters
  888.         {
  889.             get { return pmts; }
  890.         }
  891.         public string CommandName
  892.         {
  893.             get { return name; }
  894.         }
  895.         public string CommandString
  896.         {
  897.             get { return cmd; }
  898.         }
  899.     }
  900.  
  901.     public class ClientPacket
  902.     {
  903.         static int ln = 0;
  904.         static ClientPacketType type = ClientPacketType.Leaving;
  905.         static string data = "";
  906.         public static ClientPacket Parse(byte[] buffer)
  907.         {
  908.             if (buffer.Length > 4)
  909.             {
  910.                 MemoryStream ms = new MemoryStream(buffer);
  911.                 byte[] a = new byte[4];
  912.                 ms.Read(a, 0, 4);
  913.                 ln = BitConverter.ToInt32(a, 0);
  914.                 byte[] b = new byte[1];
  915.                 ms.Read(b, 0, 1);
  916.                 byte bb = b[0];
  917.                 if (bb < 4)
  918.                 {
  919.                     type = (ClientPacketType)bb;
  920.                 }
  921.                 else
  922.                 {
  923.                     return null;
  924.                 }
  925.                 if (buffer.Length > 5)
  926.                 {
  927.                     byte[] c = new byte[buffer.Length - 5];
  928.                     ms.Read(c, 0, c.Length);
  929.                     data = Encoding.ASCII.GetString(c);
  930.                 }
  931.                 ClientPacket packet = new ClientPacket();
  932.                 packet.Length = ln;
  933.                 packet.PacketType = type;
  934.                 packet.Data = data;
  935.                 return packet;
  936.             }
  937.             else
  938.             {
  939.                 return null;
  940.             }            
  941.         }
  942.         public int Length
  943.         {
  944.             get { return ln; }
  945.             set { ln = value; }
  946.         }
  947.         public ClientPacketType PacketType
  948.         {
  949.             get { return type; }
  950.             set { type = value; }
  951.         }
  952.         public string Data
  953.         {
  954.             get { return data; }
  955.             set { data = value; }
  956.         }
  957.     }
  958.      
  959. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement