Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Xml;
- namespace PrivateServerBot.Network
- {
- class SocketHandler
- {
- private readonly Socket Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- private string sBuffer = string.Empty;
- public int UserID;
- public int RoomID;
- public string Username;
- public string Password;
- public string FollowTarget = string.Empty;
- public string CopyTarget = string.Empty;
- public SocketHandler(string username, string password)
- {
- Username = username;
- Password = password;
- }
- public void ConnectToServer(string ip, int port)
- {
- Socket.Connect(ip, port);
- Send("<msg t='sys'><body action='verChk' r='0'><ver v='157' /></body></msg>");
- EventLoop();
- }
- private async void HandleData(string packet)
- {
- if (packet.Contains("apiOK"))
- {
- 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>");
- }
- else if (packet.StartsWith("<msg t='sys'><body action='joinOK' r='"))
- {
- RoomID = int.Parse(packet.Split('\'')[5]);
- }
- else if (packet.Contains("%xt%loginResponse%"))
- {
- UserID = int.Parse(packet.Split('%')[5]);
- Send("%xt%zm%firstJoin%1%");
- Send("%xt%zm%cmd%1%ignoreList%$clearAll%");
- }
- else if (packet.Contains("moveToArea"))
- {
- Send("%xt%zm%moveToCell%" + UserID + "%Enter%Spawn%");
- Send("%xt%zm%retrieveUserDatas%" + RoomID + "%" + UserID + "%");
- }
- else if (packet.Contains("initUserDatas"))
- {
- Send("%xt%zm%retrieveInventory%" + RoomID + "%" + UserID + "%");
- }
- else if (packet.Contains("bankCount"))
- {
- // user loaded
- }
- else if (packet.Contains("%uotls%"))
- {
- string[] parts = packet.Split('%');
- if (FollowTarget != string.Empty && FollowTarget == parts[4])
- {
- string[] positions = parts[5].Split(',');
- string sp = positions[0].Split(':')[1];
- string tx = positions[1].Split(':')[1];
- string ty = positions[2].Split(':')[1];
- Send(string.Format("%xt%zm%mv%{0}%{1}%{2}%{3}%", RoomID, tx, ty, sp));
- }
- }
- else if (packet.Contains("chatm"))
- {
- string[] parts = packet.Split('%');
- string message = parts[4].Split('~')[1];
- if (CopyTarget != string.Empty && CopyTarget == parts[5])
- {
- Send(string.Format("%xt%zm%message%{0}%{1}%zone%", RoomID, message));
- }
- if (parts[4].Split('~')[0] == "zone")
- {
- }
- if (parts[5] == "anon" && message.StartsWith("!"))
- {
- HandleCommands(message.Split(' '));
- }
- }
- }
- public void HandleCommands(string[] command)
- {
- switch (command[0])
- {
- case "!say":
- Send(string.Format("%xt%zm%message%{0}%{1}%zone%", RoomID, command[1]));
- break;
- case "!raw":
- Send(command[1]);
- break;
- case "!join":
- Send($"%xt%zm%cmd%1%tfer%{Username}%{command[1]}%");
- Send($"%xt%zm%moveToCell%{RoomID}%Enter%Spawn%");
- Send($"%xt%zm%setHomeTown%{RoomID}%");
- break;
- case "!emote":
- Send($"%xt%zm%emotea%1%{command[1]}%");
- break;
- case "!follow":
- FollowTarget = command[1];
- break;
- case "!copy":
- CopyTarget = command[1];
- break;
- case "!goto":
- Send($"%xt%zm%cmd%1%goto%{command[1]}%");
- break;
- }
- }
- private void EventLoop()
- {
- while (true)
- {
- byte[] bBuffer = new byte[1024];
- int bytesRec = Socket.Receive(bBuffer);
- sBuffer += Encoding.UTF8.GetString(bBuffer, 0, bytesRec);
- while (sBuffer.Contains("\0"))
- {
- string packet = sBuffer.Split('\0')[0];
- //DataReceived?.Invoke(packet);
- HandleData(packet);
- sBuffer = sBuffer.Substring(packet.Length + 1);
- }
- }
- }
- /*
- public async Task<string> GetUserToken()
- {
- using (var handler = new HttpClient())
- {
- XmlDocument x = new XmlDocument();
- x.LoadXml(await handler.PostAsync("http://fworlds.withgames.org/game/cf-userlogin.php", new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
- {
- new KeyValuePair<string, string>("unm", Username),
- new KeyValuePair<string, string>("pwd", Password)
- })).Result.Content.ReadAsStringAsync());
- return x["login"].Attributes["sToken"].Value;
- }
- }
- */
- public void Send(string packet)
- {
- Console.WriteLine(packet);
- byte[] bytes = Encoding.UTF8.GetBytes(packet + '\0');
- Socket.Send(bytes, 0, bytes.Length, SocketFlags.None);
- }
- public void Dispose()
- {
- Socket.Disconnect(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement