Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using System.Security.Cryptography;
- using System.Threading;
- using System.Runtime.InteropServices;
- namespace Login
- {
- public class handleClinet
- {
- TcpClient clientSocket;
- string clNo;
- public void startClient(TcpClient inClientSocket, string clineNo)
- {
- this.clientSocket = inClientSocket;
- this.clNo = clineNo;
- Thread ctThread = new Thread(doChat);
- ctThread.Start();
- }
- private void doChat()
- {
- int requestCount = 0;
- byte[] bytesFrom = new byte[10025];
- string dataFromClient = null;
- Byte[] sendBytes = null;
- string serverResponse = null;
- string rCount = null;
- requestCount = 0;
- while ((true))
- {
- try
- {
- requestCount = requestCount + 1;
- NetworkStream networkStream = clientSocket.GetStream();
- networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
- dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
- dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
- Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient); //clNo wird mit Username des
- rCount = Convert.ToString(requestCount); // Clients ersetzt.
- serverResponse = "Server to clinet(" + clNo + ") " + rCount;
- sendBytes = Encoding.ASCII.GetBytes(serverResponse);
- networkStream.Write(sendBytes, 0, sendBytes.Length);
- networkStream.Flush();
- Console.WriteLine(" >> " + serverResponse);
- }
- catch (Exception ex)
- {
- Console.WriteLine(" >> " + ex.ToString());
- }
- }
- }
- }
- class Program
- {
- [STAThread]
- static void Main(string[] args)
- {
- Console.Title = "Login";
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("id: ");
- string id = Console.ReadLine();
- Console.Write("pw: ");
- string pw = ReadPassword();
- while (true)
- {
- if (string.Equals(pw, Password(), StringComparison.OrdinalIgnoreCase) && id == "shadowdriz")
- {
- Console.WriteLine("Successfully logged in.");
- System.Threading.Thread.Sleep(1500);
- Console.Clear();
- IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0]; //get the local ip
- TcpListener serverSocket = new TcpListener(ipAddress, 8181);
- TcpClient clientSocket = default(TcpClient);
- int counter = 0;
- serverSocket.Start();
- Console.WriteLine(">>Server Started. Waiting for Clients to connect.");
- counter = 0;
- while (true)
- {
- counter += 1;
- clientSocket = serverSocket.AcceptTcpClient();
- Console.WriteLine(">> A client just joined the Server. Client Number: " + counter.ToString());
- handleClinet client = new handleClinet();
- client.startClient(clientSocket, Convert.ToString(counter));
- }
- }
- else
- Console.WriteLine("Fuck off!");
- Form frm = new FuckYou();
- frm.ShowDialog();
- break;
- }
- }
- public static string Password()
- {
- using (MD5 md5 = MD5.Create())
- {
- string password = "";
- string time = DateTime.Now.ToString(), date = DateTime.Now.ToString();
- time = time.Substring(time.LastIndexOf(' ') + 1);
- time = time.Replace(':'.ToString(), "");
- time = time.Remove(time.Length - 2);
- date = date.Remove(date.LastIndexOf(' '));
- date = date.Replace('.'.ToString(), "");
- string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
- password = GetMD5Hash(md5, username + " " + time + date);
- return password;
- }
- }
- public static string GetMD5Hash(MD5 md5, string password)
- {
- byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < data.Length; i++)
- {
- sb.Append(data[i].ToString("x2")); //hexdecimal string
- }
- return sb.ToString();
- }
- public static string ReadPassword()
- {
- StringBuilder sb = new StringBuilder();
- while (true)
- {
- ConsoleKeyInfo cki = Console.ReadKey(true);
- if (cki.Key == ConsoleKey.Enter)
- {
- Console.WriteLine();
- break;
- }
- if (cki.Key == ConsoleKey.Backspace)
- {
- if (sb.Length > 0)
- {
- Console.Write("\b\0\b");
- sb.Length--;
- }
- continue;
- }
- Console.Write('*');
- sb.Append(cki.KeyChar);
- }
- return sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement