Advertisement
Guest User

Untitled

a guest
Jul 30th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using SilkroadSecurityApi;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ArionClientless
  10. {
  11.     public class Agent
  12.     {
  13.         public static Security ag_security = new Security();
  14.         static TransferBuffer ag_recv_buffer = new TransferBuffer(4096, 0, 0);
  15.         static List<Packet> ag_packets = new List<Packet>();
  16.         static Socket ag_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  17.        
  18.         static uint loginID;
  19.         static string username;
  20.         static string password;
  21.         object recv_obj = new object();
  22.         private static bool EventStarting = false;
  23.  
  24.         //Async Connecting & Starting threads.
  25.         #region AsyncConnect
  26.         public void Start(string IP, string Port, uint _loginID, string _username, string _password)
  27.         {
  28.             loginID = _loginID;
  29.             username = _username;
  30.             password = _password;
  31.            
  32.             ag_socket.BeginConnect(IP, Int32.Parse(Port), new AsyncCallback(AcceptedCallBack), null);
  33.         }
  34.  
  35.         void AcceptedCallBack(IAsyncResult result)
  36.         {
  37.             ag_socket.EndConnect(result);
  38.             //Globals.MainWindow.LogMain("Agent socket successfully connected.");
  39.             Thread Send = new Thread(SendToServer);
  40.             Thread Recv = new Thread(AsyncRecv);
  41.            
  42.             Send.Start();
  43.             Recv.Start();
  44.         }
  45.         #endregion
  46.  
  47.         #region AsyncReceiving
  48.         void AsyncRecv()
  49.         {
  50.                 ag_socket.BeginReceive(ag_recv_buffer.Buffer, 0, 0x1000, SocketFlags.None, new AsyncCallback(AsyncRecvFromServer), null);
  51.  
  52.         }
  53.  
  54.         void AsyncRecvFromServer(IAsyncResult iar)
  55.         {
  56.             lock (recv_obj)
  57.             {
  58.  
  59.                 int nReceived = ag_socket.EndReceive(iar);
  60.                 if (nReceived != 0)
  61.                 {
  62.                     ag_recv_buffer.Offset = 0;
  63.                     ag_recv_buffer.Size = nReceived;
  64.  
  65.                     ag_security.Recv(ag_recv_buffer);
  66.  
  67.                     List<Packet> RemotePackets = ag_security.TransferIncoming();
  68.  
  69.                     if (RemotePackets != null)
  70.                     {
  71.                         foreach (Packet packet in RemotePackets)
  72.                         {
  73.  
  74.                             byte[] packet_bytes = packet.GetBytes();
  75.  
  76.                             //Globals.MainWindow.Log("[S->C][{0:X4}][{1} bytes]{2}{3}{4}{5}{6}", packet.Opcode, packet_bytes.Length, packet.Encrypted ? "[Encrypted]" : "", packet.Massive ? "[Massive]" : "", Environment.NewLine, Utility.HexDump(packet_bytes), Environment.NewLine);
  77.                             if (packet.Opcode == 0x5000 || packet.Opcode == 0x9000)
  78.                             {
  79.                                 continue;
  80.                             }
  81.                             if (packet.Opcode == 0x2001)
  82.                             {
  83.                                 if (packet.ReadAscii() == "AgentServer")
  84.                                 {
  85.                                     Globals.Server = Globals.ServerEnum.Agent;
  86.                                     Packet p = new Packet(0x6103);
  87.                                     p.WriteUInt32(loginID);
  88.                                     p.WriteAscii(username);
  89.                                     p.WriteAscii(password);
  90.                                     p.WriteUInt8(22);
  91.                                     p.WriteUInt32(0);
  92.                                     p.WriteUInt16(0);
  93.                                     ag_security.Send(p);
  94.                                 }
  95.  
  96.                             }
  97.                             else if (packet.Opcode == 0xA103)
  98.                             {
  99.                                 if (packet.ReadUInt8() == 1)
  100.                                 {
  101.                                     Packet response = new Packet(0x7007);
  102.                                     response.WriteUInt8(2);
  103.                                     ag_security.Send(response);
  104.  
  105.                                 }
  106.                             }
  107.  
  108.                             else if (packet.Opcode == 0xB007)
  109.                             {
  110.                                 Login.HandleCharList(packet);
  111.                             }
  112.                             else if (packet.Opcode == 0x3020)
  113.                             {
  114.                                 Packet p = new Packet(0x3012);
  115.                                 Send(p);
  116.                             }
  117.  
  118.                             else if (packet.Opcode == 0x34b5)
  119.                             {
  120.                                 Packet packet3 = new Packet(0x34b6);
  121.                                 Send(packet3);
  122.  
  123.                                 //LogMain("[NOTIFY] Spawn Confirmed !!! " + System.DateTime.Now);
  124.  
  125.                             }
  126.  
  127.                             else if (packet.Opcode == 0x3026)
  128.                             {
  129.                                 //LogMain("[GMCMD][NOTICE]: "); /*IN-COMPLETE*/
  130.                             }
  131.  
  132.                             else if (packet.Opcode == 0x3013)
  133.                             {
  134.                                 Timer lordTime = new Timer(lordEventTime, 0, 0, 10000);
  135.                             }
  136.  
  137.                             else if (packet.Opcode == 0x34A6)
  138.                             {
  139.                                 Logger.LogIt("Character spawned succesfully", LogType.Notify);
  140.                                 Main.LogWindow.button4.Enabled = true;
  141.                                 //Main.LogWindow.button7.Enabled = true;
  142.                             }
  143.  
  144.  
  145.  
  146.    
  147.                         }
  148.  
  149.  
  150.                         AsyncRecv();
  151.                         ag_packets.Clear();
  152.                     }
  153.  
  154.                 }
  155.             }
  156.         }
  157.         #endregion
  158.  
  159.         #region AsyncSending
  160.         void AsyncSend(IAsyncResult iar)
  161.         {
  162.             int nSent = ag_socket.EndSend(iar);
  163.  
  164.         }
  165.        
  166.         void SendToServer()
  167.         {
  168.             try
  169.             {
  170.                 while (true)
  171.                 {
  172.  
  173.                     List<KeyValuePair<TransferBuffer, Packet>> list = ag_security.TransferOutgoing();
  174.                     if (list != null)
  175.                     {
  176.                         foreach (var kvp in list)
  177.                         {
  178.  
  179.                             byte[] packet_bytes = kvp.Value.GetBytes();
  180.                             //Globals.MainWindow.Log("[C->S][{0:X4}][{1} bytes]{2}{3}{4}{5}{6}", kvp.Value.Opcode, packet_bytes.Length, kvp.Value.Encrypted ? "[Encrypted]" : "", kvp.Value.Massive ? "[Massive]" : "", Environment.NewLine, Utility.HexDump(packet_bytes), Environment.NewLine);
  181.                             ag_socket.BeginSend(kvp.Key.Buffer, 0, kvp.Key.Size, SocketFlags.None, new AsyncCallback(AsyncSend), null);
  182.  
  183.                             if (kvp.Value.Opcode == 0x2002)
  184.                             {
  185.                                 /*Globals.MainWindow.label6.ForeColor = Color.Green;
  186.                                 Globals.MainWindow.label6.Text = "Responsive(Agent) " + DateTime.Now.ToString("h:mm:ss"); ;*/
  187.                             }
  188.  
  189.                         }
  190.                     }
  191.                 }
  192.  
  193.             }
  194.             catch (Exception ex)
  195.             {
  196.                 Logger.LogIt("Cannot Send Data(Agent).", LogType.Notify);
  197.             }
  198.         }
  199.         #endregion
  200.        
  201.         #region AsyncDisconnecting
  202.         public static void DisconnectModuleSocket()
  203.         {
  204.             try
  205.             {
  206.                 if (ag_socket != null)
  207.                 {
  208.                     ag_socket.BeginDisconnect(true, new AsyncCallback(EndDC), null);
  209.                     Logger.LogIt("Succsefully disconnect Agent.", LogType.Success);
  210.                 }
  211.  
  212.                 //gw_socket = null;
  213.             }
  214.             catch (Exception ex)
  215.             {
  216.                 Logger.LogIt("Cannot disconnect Agent.", LogType.Notify);
  217.             }
  218.         }
  219.  
  220.         private static void lordEventTime(object state)
  221.         {
  222.             lordYarkan();
  223.         }
  224.         public static async void lordYarkan()
  225.         {
  226.             Random lordRandom = new Random();
  227.             int worldid = lordRandom.Next(98, 102);
  228.             await Task.Delay(3000);
  229.             Packet packet = new Packet(0x7010);
  230.             packet.WriteUInt8(0x10); //static
  231.             packet.WriteUInt8(0); //static
  232.             packet.WriteInt16(27006); //regionID
  233.             packet.WriteSingle(1678); //x
  234.             packet.WriteSingle(404.6); //Y
  235.             packet.WriteSingle(524); //Z
  236.             packet.WriteInt8(worldid.ToString()); //worldid
  237.             packet.WriteUInt8(0); //static
  238.             Agent.Send(packet);
  239.             Thread.Sleep(5000);
  240.             Packet packet1 = new Packet(0x7010);
  241.             packet1.WriteUInt8(6);
  242.             packet1.WriteUInt8(0);
  243.             packet1.WriteUInt32(0x0EE2);
  244.             packet1.WriteUInt8(1);
  245.             packet1.WriteUInt8(3);
  246.             Agent.Send(packet1);
  247.             Console.WriteLine("Lord Yarkan has appeared ! Word ID = " + worldid.ToString());
  248.         }
  249.  
  250.         static void EndDC(IAsyncResult iar)
  251.         {
  252.             ag_socket.EndDisconnect(iar);
  253.         }
  254.         #endregion
  255.  
  256.         public static void Send(Packet packet)
  257.         {
  258.             ag_security.Send(packet);
  259.         }
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement