Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Dictionary<string, UserInfo> users = new Dictionary<string, UserInfo>();
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- using System.Net.Security;
- using System.Security.Authentication;
- using System.IO;
- using System.Threading;
- using MySql.Data.MySqlClient;
- namespace GarkServer
- {
- public class Client
- {
- public Client(Program p, TcpClient c)
- {
- prog = p;
- client = c;
- // Handle client in another thread.
- (new Thread(new ThreadStart(SetupConn))).Start();
- }
- public static string Base64Encode(string plainText)
- {
- var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
- return System.Convert.ToBase64String(plainTextBytes);
- }
- public static string Base64Decode(string base64EncodedData)
- {
- var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
- return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
- }
- Program prog;
- public TcpClient client;
- public NetworkStream netStream; // raw-data stream of connection.
- public SslStream ssl; // encrypts connection using SSL.
- public BinaryReader br;
- public BinaryWriter bw;
- UserInfo userInfo; // info about current user.
- void SetupConn() // setup connection / login
- {
- var dbCon = DBConnection.Instance();
- dbCon.DatabaseName = "dbName";
- try
- {
- netStream = client.GetStream();
- ssl = new SslStream(netStream, false);
- ssl.AuthenticateAsServer(prog.cert, false, SslProtocols.Tls, true);
- br = new BinaryReader(ssl, Encoding.UTF8);
- bw = new BinaryWriter(ssl, Encoding.UTF8);
- bw.Write(IM_Hello);
- bw.Flush();
- int hello = br.ReadInt32();
- if (hello == IM_Hello)
- {
- byte logMode = br.ReadByte();
- string userName = br.ReadString();
- string password = br.ReadString();
- string ipadresa = br.ReadString();
- if (userName.Length < 32 || userName.Length > 4)
- {
- if (password.Length < 512 || password.Length > 4)
- {
- if (logMode == IM_Login)
- {
- if (dbCon.IsConnect())
- {
- string query = "SELECT COUNT(id) FROM users WHERE username = '" + userName.ToLower() + "'";
- using (MySqlCommand check_User_Name = new MySqlCommand(query, dbCon.Connection))
- {
- int UserExist = Convert.ToInt32(check_User_Name.ExecuteScalar());
- if (UserExist == 1)
- {
- string query1 = "SELECT password,value1,apponline FROM users WHERE username='" + userName.ToLower() + "'";
- using (MySqlCommand cmd = new MySqlCommand(query1, dbCon.Connection))
- {
- MySqlDataReader reader = cmd.ExecuteReader();
- reader.Read();
- string gm_password = (string)reader["password"];
- string gm_value1 = (string)reader["im_value1"];
- bool gm_LoggedIn = (bool)reader["apponline"];
- reader.Close();
- if (password == gm_password)
- {
- UserInfo ucheck;
- if (prog.users.TryGetValue(userName.ToLower(), out ucheck))
- {
- if (ucheck.LoggedIn)
- {
- // disconnect already connected client with same useranem
- ucheck.Connection.CloseConn();
- string query200 = "UPDATE users SET apponline='0' WHERE username='" + userName.ToLower() + "'";
- using (MySqlCommand cmd5 = new MySqlCommand(query200, dbCon.Connection))
- {
- MySqlDataReader reader1 = cmd5.ExecuteReader();
- reader1.Read();
- reader1.Close();
- }
- }
- }
- userInfo = new UserInfo(userName.ToLower(), Base64Encode(password), this);
- prog.users.Add(userName.ToLower(), userInfo);
- var dateValue = DateTime.Now;
- string dateMysql = dateValue.ToString("yyyy-MM-dd HH:mm:ss");
- string query201 = "UPDATE users SET ipadresav4='" + ipadresa + "',apponline='1',webdt='" + dateMysql + "' WHERE username='" + userName.ToLower() + "'";
- using (MySqlCommand cmd111 = new MySqlCommand(query201, dbCon.Connection))
- {
- MySqlDataReader reader2 = cmd111.ExecuteReader();
- reader2.Read();
- reader2.Close();
- userInfo.Connection = this;
- userInfo.LoggedIn = true;
- bw.Write(IM_OK);
- bw.Flush();
- Receiver();
- }
- }
- else
- bw.Write(IM_WrongPass);
- }
- }
- else
- bw.Write(IM_NoExists);
- }
- }
- else
- Console.WriteLine("[{0}] MySQL Disconnected!", DateTime.Now);
- }
- }
- else
- bw.Write(IM_TooPassword);
- }
- else
- bw.Write(IM_TooUsername);
- }
- CloseConn();
- }
- catch { CloseConn(); }
- }
- void CloseConn() // Close connection.
- {
- try
- {
- string userConnected = userInfo.UserName;
- var dbCon = DBConnection.Instance();
- dbCon.DatabaseName = "dbName";
- var dateValue = DateTime.Now;
- string dateMysql = dateValue.ToString("yyyy-MM-dd HH:mm:ss");
- string query7 = "UPDATE users SET apponline='0',webdt='" + dateMysql + "' WHERE username='" + userConnected.ToLower() + "'";
- using (MySqlCommand cmd = new MySqlCommand(query7, dbCon.Connection))
- {
- MySqlDataReader reader = cmd.ExecuteReader();
- reader.Read();
- reader.Close();
- }
- userInfo.LoggedIn = false;
- prog.users.Remove(userConnected.ToLower());
- br.Close();
- bw.Close();
- ssl.Close();
- netStream.Close();
- client.Close();
- }
- catch { }
- }
- void Receiver()
- {
- var dbCon = DBConnection.Instance();
- dbCon.DatabaseName = "dbName";
- userInfo.LoggedIn = true;
- try
- {
- while (client.Client.Connected) // while user is connected
- {
- byte type = br.ReadByte(); // incoming packet type.
- if (type == IM_IsAvailable)
- {
- // chat form packet to check if user is online
- string who = br.ReadString();
- bw.Write(IM_IsAvailable);
- bw.Write(who);
- UserInfo info;
- if (prog.users.TryGetValue(who.ToLower(), out info))
- {
- if (info.LoggedIn)
- {
- bw.Write(true);
- }
- else
- bw.Write(false);
- }
- else
- bw.Write(false);
- bw.Flush();
- }
- else if (type == IM_Send)
- {
- // chat form send message
- string to = br.ReadString();
- string msg = br.ReadString();
- UserInfo recipient;
- if (prog.users.TryGetValue(to.ToLower(), out recipient))
- {
- // recipient logged in?
- if (recipient.LoggedIn) // if yes
- {
- // send chat form opener packet
- recipient.Connection.bw.Write(IM_Newmsg);
- recipient.Connection.bw.Write(userInfo.UserName.ToLower());
- recipient.Connection.bw.Flush();
- // then send a message
- recipient.Connection.bw.Write(IM_Received);
- recipient.Connection.bw.Write(userInfo.UserName.ToLower()); // From
- recipient.Connection.bw.Write(msg);
- recipient.Connection.bw.Flush();
- }
- }
- }
- else if (type == IM_Online)
- {
- // timer every 10 sec to set "i'm online" checkin time and to fetch friends
- string u_id;
- string f_id;
- string f_username;
- string f_displayname;
- string f_webstatus;
- string f_checkin;
- bool f_apponline;
- string who = br.ReadString();
- var dateValue = DateTime.Now;
- string dateMysql = dateValue.ToString("yyyy-MM-dd HH:mm:ss");
- string query100 = "UPDATE users SET checkin='" + dateMysql + "' WHERE username='" + who.ToLower() + "'";
- using (MySqlCommand cmd100 = new MySqlCommand(query100, dbCon.Connection))
- {
- MySqlDataReader reader = cmd100.ExecuteReader();
- reader.Read();
- reader.Close();
- }
- string query1 = "SELECT id FROM users WHERE username='" + who.ToLower() + "' ";
- using (MySqlCommand cmd1 = new MySqlCommand(query1, dbCon.Connection))
- {
- using (var reader1 = cmd1.ExecuteReader())
- {
- reader1.Read();
- u_id = reader1.GetString("id");
- reader1.Close();
- string query2 = "SELECT a.friend,b.username,b.displayname,b.webstatus,b.apponline,b.checkin FROM friends a JOIN users b on b.id = a.friend WHERE a.user = " + u_id + "";
- using (MySqlCommand cmd0 = new MySqlCommand(query2, dbCon.Connection))
- {
- using (var reader = cmd0.ExecuteReader())
- {
- while (reader.Read())
- {
- f_id = reader.GetString("friend");
- f_username = reader.GetString("username");
- f_displayname = reader.GetString("displayname");
- f_webstatus = reader.GetString("webstatus");
- f_apponline = reader.GetBoolean("apponline");
- f_checkin = reader.GetString("checkin");
- DateTime now = DateTime.Now;
- DateTime myDate;
- DateTime.TryParse(f_checkin, out myDate);
- var diffInSeconds = (DateTime.Now - myDate).TotalSeconds;
- UserInfo info;
- if (prog.users.TryGetValue(f_username.ToLower(), out info) && diffInSeconds < 21)
- {
- if (info.LoggedIn)
- {
- // if friend is ONLINE
- bw.Write(IM_Here);
- bw.Write(true);
- bw.Write(f_username);
- bw.Write(f_displayname);
- bw.Write(f_webstatus);
- bw.Flush();
- }
- }
- else
- {
- // else friend is OFFLINE
- bw.Write(IM_Here);
- bw.Write(false);
- bw.Write(f_username);
- bw.Write(f_displayname);
- bw.Write(f_webstatus);
- bw.Flush();
- }
- }
- }
- }
- }
- }
- }
- }
- }
- catch (IOException) { }
- userInfo.LoggedIn = false;
- }
- public const int IM_Hello = 2016; // Check Connection
- public const byte IM_OK = 0; // Connection OK
- public const byte IM_Login = 1; // Login
- public const byte IM_Register = 2; // Register - disabled because i don't need it
- public const byte IM_TooUsername = 3; // Too long username
- public const byte IM_TooPassword = 4; // Too long password
- public const byte IM_Exists = 5; // Already exists
- public const byte IM_NoExists = 6; // Doesn't exists
- public const byte IM_WrongPass = 7; // Wrong password
- public const byte IM_IsAvailable = 8; // Is user available?
- public const byte IM_Send = 9; // Send message
- public const byte IM_Received = 10; // Message received
- public const byte IM_Online = 11; // Receive "I'm Online" packet and sends Friends status (offline/online) to clients
- public const byte IM_Here = 12; // Get "I'm Here" user info from server
- public const byte IM_Newmsg = 13; // Chat form opener packet
- }
- }
- [Serializable]
- public class UserInfo
- {
- public string UserName;
- public string Password;
- [NonSerialized] public bool LoggedIn; // is logged in and connected?
- [NonSerialized] public Client Connection; // connection info
- public UserInfo(string user, string pass)
- {
- this.UserName = user;
- this.Password = pass;
- this.LoggedIn = false;
- }
- public UserInfo(string user, string pass, Client conn)
- {
- this.UserName = user;
- this.Password = pass;
- this.LoggedIn = true;
- this.Connection = conn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement