Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10.  
  11. namespace PrivateServerBot.Network
  12. {
  13.     class SocketHandler
  14.     {
  15.         private readonly Socket Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  16.         private string sBuffer = string.Empty;
  17.  
  18.         public int UserID;
  19.         public int RoomID;
  20.  
  21.         public string Username;
  22.         public string Password;
  23.  
  24.         public string FollowTarget = string.Empty;
  25.         public string CopyTarget = string.Empty;
  26.  
  27.         public SocketHandler(string username, string password)
  28.         {
  29.             Username = username;
  30.             Password = password;
  31.         }
  32.  
  33.         public void ConnectToServer(string ip, int port)
  34.         {
  35.             Socket.Connect(ip, port);
  36.  
  37.             Send("<msg t='sys'><body action='verChk' r='0'><ver v='157' /></body></msg>");
  38.             EventLoop();
  39.         }
  40.  
  41.         private async void HandleData(string packet)
  42.         {
  43.             if (packet.Contains("apiOK"))
  44.             {
  45.                 Send("<msg t='sys'><body action='login' r='0'><login z='zone_master'><nick><![CDATA[N7B5W8W1Y5B1R5VWVZ~" + Username + "]]></nick><pword><![CDATA[" +  Password + "]]></pword></login></body></msg>");
  46.             }
  47.             else if (packet.StartsWith("<msg t='sys'><body action='joinOK' r='"))
  48.             {
  49.                 RoomID = int.Parse(packet.Split('\'')[5]);
  50.             }
  51.             else if (packet.Contains("%xt%loginResponse%"))
  52.             {
  53.                 UserID = int.Parse(packet.Split('%')[5]);
  54.  
  55.                 Send("%xt%zm%firstJoin%1%");
  56.                 Send("%xt%zm%cmd%1%ignoreList%$clearAll%");
  57.             }
  58.             else if (packet.Contains("moveToArea"))
  59.             {
  60.                 Send("%xt%zm%moveToCell%" + UserID + "%Enter%Spawn%");
  61.                 Send("%xt%zm%retrieveUserDatas%" + RoomID + "%" + UserID + "%");
  62.             }
  63.             else if (packet.Contains("initUserDatas"))
  64.             {
  65.                 Send("%xt%zm%retrieveInventory%" + RoomID + "%" + UserID + "%");
  66.             }
  67.             else if (packet.Contains("bankCount"))
  68.             {
  69.                 // user loaded
  70.             }
  71.             else if (packet.Contains("%uotls%"))
  72.             {
  73.                 string[] parts = packet.Split('%');
  74.  
  75.                 if (FollowTarget != string.Empty && FollowTarget == parts[4])
  76.                 {
  77.                     string[] positions = parts[5].Split(',');
  78.  
  79.                     string sp = positions[0].Split(':')[1];
  80.                     string tx = positions[1].Split(':')[1];
  81.                     string ty = positions[2].Split(':')[1];
  82.  
  83.                     Send(string.Format("%xt%zm%mv%{0}%{1}%{2}%{3}%", RoomID, tx, ty, sp));
  84.                 }
  85.  
  86.             }
  87.             else if (packet.Contains("chatm"))
  88.             {
  89.                 string[] parts = packet.Split('%');
  90.                 string message = parts[4].Split('~')[1];
  91.  
  92.                 if (CopyTarget != string.Empty && CopyTarget == parts[5])
  93.                 {
  94.                     Send(string.Format("%xt%zm%message%{0}%{1}%zone%", RoomID, message));
  95.                 }
  96.  
  97.                 if (parts[4].Split('~')[0] == "zone")
  98.                 {
  99.  
  100.                 }
  101.  
  102.                 if (parts[5] == "anon" && message.StartsWith("!"))
  103.                 {
  104.                     HandleCommands(message.Split(' '));
  105.                 }
  106.             }
  107.         }
  108.  
  109.         public void HandleCommands(string[] command)
  110.         {
  111.             switch (command[0])
  112.             {
  113.                 case "!say":
  114.                     Send(string.Format("%xt%zm%message%{0}%{1}%zone%", RoomID, command[1]));
  115.                     break;
  116.                 case "!raw":
  117.                     Send(command[1]);
  118.                     break;
  119.                 case "!join":
  120.                     Send($"%xt%zm%cmd%1%tfer%{Username}%{command[1]}%");
  121.                     Send($"%xt%zm%moveToCell%{RoomID}%Enter%Spawn%");
  122.                     Send($"%xt%zm%setHomeTown%{RoomID}%");
  123.                     break;
  124.                 case "!emote":
  125.                     Send($"%xt%zm%emotea%1%{command[1]}%");
  126.                     break;
  127.                 case "!follow":
  128.                     FollowTarget = command[1];
  129.                     break;
  130.                 case "!copy":
  131.                     CopyTarget = command[1];
  132.                     break;
  133.                 case "!goto":
  134.                     Send($"%xt%zm%cmd%1%goto%{command[1]}%");
  135.                     break;
  136.             }
  137.         }
  138.  
  139.         private void EventLoop()
  140.         {
  141.             while (true)
  142.             {
  143.                 byte[] bBuffer = new byte[1024];
  144.                 int bytesRec = Socket.Receive(bBuffer);
  145.  
  146.                 sBuffer += Encoding.UTF8.GetString(bBuffer, 0, bytesRec);
  147.  
  148.                 while (sBuffer.Contains("\0"))
  149.                 {
  150.                     string packet = sBuffer.Split('\0')[0];
  151.                     //DataReceived?.Invoke(packet);
  152.  
  153.                     HandleData(packet);
  154.  
  155.                     sBuffer = sBuffer.Substring(packet.Length + 1);
  156.                 }
  157.             }
  158.         }
  159.         /*
  160.         public async Task<string> GetUserToken()
  161.         {
  162.             using (var handler = new HttpClient())
  163.             {
  164.                 XmlDocument x = new XmlDocument();
  165.  
  166.                 x.LoadXml(await handler.PostAsync("http://fworlds.withgames.org/game/cf-userlogin.php", new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
  167.                 {
  168.                     new KeyValuePair<string, string>("unm", Username),
  169.                     new KeyValuePair<string, string>("pwd", Password)
  170.                 })).Result.Content.ReadAsStringAsync());
  171.  
  172.                 return x["login"].Attributes["sToken"].Value;
  173.             }
  174.         }
  175.         */
  176.  
  177.         public void Send(string packet)
  178.         {
  179.             Console.WriteLine(packet);
  180.  
  181.             byte[] bytes = Encoding.UTF8.GetBytes(packet + '\0');
  182.             Socket.Send(bytes, 0, bytes.Length, SocketFlags.None);
  183.         }
  184.  
  185.         public void Dispose()
  186.         {
  187.             Socket.Disconnect(false);
  188.         }
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement