Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- namespace MySockTest
- {
- class Program
- {
- // Variables
- public static string pServer = "87.106.48.157";
- public static int pPort = 6543;
- public static string dServer = "92.61.32.19";
- public static int dPort = 6667;
- public static string userName = "anonymous";
- public static string userPass = "anonymous";
- static void Main(string[] args)
- {
- // Variable
- IPEndPoint pIpPort = new IPEndPoint(IPAddress.Parse(pServer), pPort);
- ushort nIndex;
- byte[] pRequest = new byte[257];
- byte[] pResponse = new byte[257];
- // Connection
- Console.WriteLine("Trying to connect...");
- Socket pClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- pClient.Connect(pIpPort);
- Console.WriteLine(" - Info set up:\n");
- nIndex = 0;
- pRequest[nIndex++] = 0x05; // Version 5.
- Console.WriteLine("Index - {0}, Request: {1}", nIndex, pRequest[0]);
- pRequest[nIndex++] = 0x02; // 2 Authentication methods are in packet...
- Console.WriteLine("Index - {0}, Request: {1}", nIndex, pRequest[1]);
- pRequest[nIndex++] = 0x00; // NO AUTHENTICATION REQUIRED
- Console.WriteLine("Index - {0}, Request: {1}", nIndex, pRequest[2]);
- pRequest[nIndex++] = 0x02; // USERNAME/PASSWOR
- Console.WriteLine("Index - {0}, Request: {1}", nIndex, pRequest[3]);
- pClient.Send(pRequest, nIndex, SocketFlags.None);
- Console.WriteLine("\n[S] Index: {0}, Request[1]: {1}, Response[0]: {2}", nIndex, pRequest[1], pResponse[0]);
- int nGot = pClient.Receive(pResponse, 2, SocketFlags.None);
- Console.WriteLine("[R] Got: {0} <-- If 2 it's ok! ----| Response[1]: {1}\n", nGot, pResponse[1]);
- byte[] rawBytes;
- if (/*pResponse[1] == 0x02*/true)
- {
- Console.WriteLine(" - We need username/password! 0x02!");
- nIndex = 0;
- pRequest[nIndex++] = 0x05; // Version 5.
- // Add user name...
- pRequest[nIndex++] = (byte)userName.Length;
- rawBytes = System.Text.Encoding.Default.GetBytes(userName);
- rawBytes.CopyTo(pRequest, nIndex);
- nIndex += (ushort)rawBytes.Length;
- // Add user password...
- pRequest[nIndex++] = (byte)userPass.Length;
- rawBytes = System.Text.Encoding.Default.GetBytes(userPass);
- rawBytes.CopyTo(pRequest, nIndex);
- nIndex += (ushort)rawBytes.Length;
- // Send the Username/Password request
- pClient.Send(pRequest, nIndex, SocketFlags.None);
- // Receive 2 byte response...
- nGot = pClient.Receive(pResponse, 2, SocketFlags.None);
- if (nGot != 2)
- Console.WriteLine("Bad response received from proxy server.");
- if (pResponse[1] != 0x00)
- Console.WriteLine("Bad Usernaem/Password. {0} <-- not 0x00, Response[0]: {1}, Response[2]: {2}", pResponse[0], pResponse[1], pResponse[2]);
- }
- // Connection request...
- Console.WriteLine("\n - Sending connection request...");
- nIndex = 0;
- pRequest[nIndex++] = 0x05; // version 5.
- pRequest[nIndex++] = 0x01; // command = connect.
- pRequest[nIndex++] = 0x00; // Reserve = must be 0x00
- IPEndPoint dIpPort = new IPEndPoint(IPAddress.Parse(dServer), dPort);
- IPAddress destIP = IPAddress.Parse(dServer);
- // Address is IPV4 format
- pRequest[nIndex++] = 0x01;
- rawBytes = destIP.GetAddressBytes();
- rawBytes.CopyTo(pRequest, nIndex);
- Console.WriteLine("Index: {2}, IP Converted[First two numbers]: {0} Ip/Port: {1}", pRequest[nIndex], dIpPort, nIndex);
- nIndex += (ushort)rawBytes.Length;
- Console.WriteLine("Index: {0}", nIndex);
- // using big-edian byte order
- byte[] portBytes = BitConverter.GetBytes(dPort);
- for (int i = portBytes.Length - 1; i >= 0; i--)
- pRequest[nIndex++] = portBytes[i];
- // send connect request.
- pClient.Send(pRequest, nIndex, SocketFlags.None);
- pClient.Receive(pResponse); // Get variable length response...
- Console.WriteLine(" - Finishing...{0} {1}", pResponse[0], pResponse.Length);
- if (pResponse[1] != 0x00)
- {
- Console.WriteLine("\nError: {0}", pResponse[1]);
- }
- else
- {
- Console.WriteLine("\n * Connected. [{0}]", pResponse[1]);
- }
- /* Success Connected...
- Console.WriteLine("-!- * Giving information to server..");
- pClient.WriteLine("USER C-Sharp 8 * :I'm C# Experiment!");
- pClient.WriteLine("NICK Lovely[M]");
- pClient.WriteLine("JOIN #C-Sharp");
- Console.WriteLine("-!- * Completed.\n"); */
- //return pClient;
- Console.WriteLine("\nRead line begins... {0}", nGot);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement