Advertisement
Guest User

Untitled

a guest
May 16th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.39 KB | None | 0 0
  1. public Dictionary<string, UserInfo> users = new Dictionary<string, UserInfo>();
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Net.Sockets;
  8. using System.Net.Security;
  9. using System.Security.Authentication;
  10. using System.IO;
  11. using System.Threading;
  12. using MySql.Data.MySqlClient;
  13.  
  14. namespace GarkServer
  15. {
  16. public class Client
  17. {
  18. public Client(Program p, TcpClient c)
  19. {
  20. prog = p;
  21. client = c;
  22.  
  23. // Handle client in another thread.
  24. (new Thread(new ThreadStart(SetupConn))).Start();
  25. }
  26. public static string Base64Encode(string plainText)
  27. {
  28. var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
  29. return System.Convert.ToBase64String(plainTextBytes);
  30. }
  31. public static string Base64Decode(string base64EncodedData)
  32. {
  33. var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
  34. return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
  35. }
  36.  
  37. Program prog;
  38. public TcpClient client;
  39. public NetworkStream netStream; // raw-data stream of connection.
  40. public SslStream ssl; // encrypts connection using SSL.
  41. public BinaryReader br;
  42. public BinaryWriter bw;
  43.  
  44. UserInfo userInfo; // info about current user.
  45.  
  46. void SetupConn() // setup connection / login
  47. {
  48. var dbCon = DBConnection.Instance();
  49. dbCon.DatabaseName = "dbName";
  50. try
  51. {
  52.  
  53. netStream = client.GetStream();
  54. ssl = new SslStream(netStream, false);
  55. ssl.AuthenticateAsServer(prog.cert, false, SslProtocols.Tls, true);
  56.  
  57. br = new BinaryReader(ssl, Encoding.UTF8);
  58. bw = new BinaryWriter(ssl, Encoding.UTF8);
  59.  
  60. bw.Write(IM_Hello);
  61. bw.Flush();
  62. int hello = br.ReadInt32();
  63. if (hello == IM_Hello)
  64. {
  65. byte logMode = br.ReadByte();
  66. string userName = br.ReadString();
  67. string password = br.ReadString();
  68. string ipadresa = br.ReadString();
  69. if (userName.Length < 32 || userName.Length > 4)
  70. {
  71. if (password.Length < 512 || password.Length > 4)
  72. {
  73. if (logMode == IM_Login)
  74. {
  75. if (dbCon.IsConnect())
  76. {
  77.  
  78. string query = "SELECT COUNT(id) FROM users WHERE username = '" + userName.ToLower() + "'";
  79. using (MySqlCommand check_User_Name = new MySqlCommand(query, dbCon.Connection))
  80. {
  81. int UserExist = Convert.ToInt32(check_User_Name.ExecuteScalar());
  82. if (UserExist == 1)
  83. {
  84. string query1 = "SELECT password,value1,apponline FROM users WHERE username='" + userName.ToLower() + "'";
  85. using (MySqlCommand cmd = new MySqlCommand(query1, dbCon.Connection))
  86. {
  87. MySqlDataReader reader = cmd.ExecuteReader();
  88. reader.Read();
  89. string gm_password = (string)reader["password"];
  90. string gm_value1 = (string)reader["im_value1"];
  91. bool gm_LoggedIn = (bool)reader["apponline"];
  92. reader.Close();
  93.  
  94. if (password == gm_password)
  95. {
  96. UserInfo ucheck;
  97. if (prog.users.TryGetValue(userName.ToLower(), out ucheck))
  98. {
  99. if (ucheck.LoggedIn)
  100. {
  101. // disconnect already connected client with same useranem
  102. ucheck.Connection.CloseConn();
  103. string query200 = "UPDATE users SET apponline='0' WHERE username='" + userName.ToLower() + "'";
  104. using (MySqlCommand cmd5 = new MySqlCommand(query200, dbCon.Connection))
  105. {
  106. MySqlDataReader reader1 = cmd5.ExecuteReader();
  107. reader1.Read();
  108. reader1.Close();
  109. }
  110. }
  111. }
  112.  
  113. userInfo = new UserInfo(userName.ToLower(), Base64Encode(password), this);
  114. prog.users.Add(userName.ToLower(), userInfo);
  115.  
  116. var dateValue = DateTime.Now;
  117. string dateMysql = dateValue.ToString("yyyy-MM-dd HH:mm:ss");
  118. string query201 = "UPDATE users SET ipadresav4='" + ipadresa + "',apponline='1',webdt='" + dateMysql + "' WHERE username='" + userName.ToLower() + "'";
  119. using (MySqlCommand cmd111 = new MySqlCommand(query201, dbCon.Connection))
  120. {
  121. MySqlDataReader reader2 = cmd111.ExecuteReader();
  122. reader2.Read();
  123. reader2.Close();
  124.  
  125. userInfo.Connection = this;
  126. userInfo.LoggedIn = true;
  127. bw.Write(IM_OK);
  128. bw.Flush();
  129. Receiver();
  130.  
  131. }
  132. }
  133. else
  134. bw.Write(IM_WrongPass);
  135. }
  136. }
  137. else
  138. bw.Write(IM_NoExists);
  139. }
  140. }
  141. else
  142. Console.WriteLine("[{0}] MySQL Disconnected!", DateTime.Now);
  143. }
  144. }
  145. else
  146. bw.Write(IM_TooPassword);
  147. }
  148. else
  149. bw.Write(IM_TooUsername);
  150. }
  151. CloseConn();
  152. }
  153. catch { CloseConn(); }
  154. }
  155. void CloseConn() // Close connection.
  156. {
  157. try
  158. {
  159. string userConnected = userInfo.UserName;
  160. var dbCon = DBConnection.Instance();
  161. dbCon.DatabaseName = "dbName";
  162.  
  163. var dateValue = DateTime.Now;
  164. string dateMysql = dateValue.ToString("yyyy-MM-dd HH:mm:ss");
  165. string query7 = "UPDATE users SET apponline='0',webdt='" + dateMysql + "' WHERE username='" + userConnected.ToLower() + "'";
  166. using (MySqlCommand cmd = new MySqlCommand(query7, dbCon.Connection))
  167. {
  168. MySqlDataReader reader = cmd.ExecuteReader();
  169. reader.Read();
  170. reader.Close();
  171. }
  172.  
  173. userInfo.LoggedIn = false;
  174. prog.users.Remove(userConnected.ToLower());
  175. br.Close();
  176. bw.Close();
  177. ssl.Close();
  178. netStream.Close();
  179. client.Close();
  180. }
  181. catch { }
  182. }
  183. void Receiver()
  184. {
  185. var dbCon = DBConnection.Instance();
  186. dbCon.DatabaseName = "dbName";
  187. userInfo.LoggedIn = true;
  188.  
  189. try
  190. {
  191. while (client.Client.Connected) // while user is connected
  192. {
  193. byte type = br.ReadByte(); // incoming packet type.
  194.  
  195. if (type == IM_IsAvailable)
  196. {
  197. // chat form packet to check if user is online
  198. string who = br.ReadString();
  199.  
  200. bw.Write(IM_IsAvailable);
  201. bw.Write(who);
  202.  
  203. UserInfo info;
  204. if (prog.users.TryGetValue(who.ToLower(), out info))
  205. {
  206. if (info.LoggedIn)
  207. {
  208. bw.Write(true);
  209. }
  210. else
  211. bw.Write(false);
  212. }
  213. else
  214. bw.Write(false);
  215. bw.Flush();
  216. }
  217.  
  218. else if (type == IM_Send)
  219. {
  220. // chat form send message
  221. string to = br.ReadString();
  222. string msg = br.ReadString();
  223. UserInfo recipient;
  224. if (prog.users.TryGetValue(to.ToLower(), out recipient))
  225. {
  226. // recipient logged in?
  227. if (recipient.LoggedIn) // if yes
  228. {
  229. // send chat form opener packet
  230. recipient.Connection.bw.Write(IM_Newmsg);
  231. recipient.Connection.bw.Write(userInfo.UserName.ToLower());
  232. recipient.Connection.bw.Flush();
  233.  
  234. // then send a message
  235. recipient.Connection.bw.Write(IM_Received);
  236. recipient.Connection.bw.Write(userInfo.UserName.ToLower()); // From
  237. recipient.Connection.bw.Write(msg);
  238. recipient.Connection.bw.Flush();
  239. }
  240. }
  241. }
  242.  
  243. else if (type == IM_Online)
  244. {
  245. // timer every 10 sec to set "i'm online" checkin time and to fetch friends
  246. string u_id;
  247. string f_id;
  248. string f_username;
  249. string f_displayname;
  250. string f_webstatus;
  251. string f_checkin;
  252. bool f_apponline;
  253.  
  254. string who = br.ReadString();
  255.  
  256. var dateValue = DateTime.Now;
  257. string dateMysql = dateValue.ToString("yyyy-MM-dd HH:mm:ss");
  258. string query100 = "UPDATE users SET checkin='" + dateMysql + "' WHERE username='" + who.ToLower() + "'";
  259. using (MySqlCommand cmd100 = new MySqlCommand(query100, dbCon.Connection))
  260. {
  261. MySqlDataReader reader = cmd100.ExecuteReader();
  262. reader.Read();
  263. reader.Close();
  264. }
  265.  
  266. string query1 = "SELECT id FROM users WHERE username='" + who.ToLower() + "' ";
  267. using (MySqlCommand cmd1 = new MySqlCommand(query1, dbCon.Connection))
  268. {
  269. using (var reader1 = cmd1.ExecuteReader())
  270. {
  271. reader1.Read();
  272. u_id = reader1.GetString("id");
  273. reader1.Close();
  274.  
  275. 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 + "";
  276. using (MySqlCommand cmd0 = new MySqlCommand(query2, dbCon.Connection))
  277. {
  278. using (var reader = cmd0.ExecuteReader())
  279. {
  280. while (reader.Read())
  281. {
  282. f_id = reader.GetString("friend");
  283. f_username = reader.GetString("username");
  284. f_displayname = reader.GetString("displayname");
  285. f_webstatus = reader.GetString("webstatus");
  286. f_apponline = reader.GetBoolean("apponline");
  287. f_checkin = reader.GetString("checkin");
  288. DateTime now = DateTime.Now;
  289.  
  290. DateTime myDate;
  291. DateTime.TryParse(f_checkin, out myDate);
  292. var diffInSeconds = (DateTime.Now - myDate).TotalSeconds;
  293.  
  294. UserInfo info;
  295. if (prog.users.TryGetValue(f_username.ToLower(), out info) && diffInSeconds < 21)
  296. {
  297. if (info.LoggedIn)
  298. {
  299. // if friend is ONLINE
  300. bw.Write(IM_Here);
  301. bw.Write(true);
  302. bw.Write(f_username);
  303. bw.Write(f_displayname);
  304. bw.Write(f_webstatus);
  305. bw.Flush();
  306. }
  307. }
  308. else
  309. {
  310. // else friend is OFFLINE
  311. bw.Write(IM_Here);
  312. bw.Write(false);
  313. bw.Write(f_username);
  314. bw.Write(f_displayname);
  315. bw.Write(f_webstatus);
  316. bw.Flush();
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
  326. catch (IOException) { }
  327.  
  328. userInfo.LoggedIn = false;
  329. }
  330.  
  331. public const int IM_Hello = 2016; // Check Connection
  332. public const byte IM_OK = 0; // Connection OK
  333. public const byte IM_Login = 1; // Login
  334. public const byte IM_Register = 2; // Register - disabled because i don't need it
  335. public const byte IM_TooUsername = 3; // Too long username
  336. public const byte IM_TooPassword = 4; // Too long password
  337. public const byte IM_Exists = 5; // Already exists
  338. public const byte IM_NoExists = 6; // Doesn't exists
  339. public const byte IM_WrongPass = 7; // Wrong password
  340. public const byte IM_IsAvailable = 8; // Is user available?
  341. public const byte IM_Send = 9; // Send message
  342. public const byte IM_Received = 10; // Message received
  343. public const byte IM_Online = 11; // Receive "I'm Online" packet and sends Friends status (offline/online) to clients
  344. public const byte IM_Here = 12; // Get "I'm Here" user info from server
  345. public const byte IM_Newmsg = 13; // Chat form opener packet
  346. }
  347. }
  348.  
  349. [Serializable]
  350. public class UserInfo
  351. {
  352. public string UserName;
  353. public string Password;
  354. [NonSerialized] public bool LoggedIn; // is logged in and connected?
  355. [NonSerialized] public Client Connection; // connection info
  356.  
  357. public UserInfo(string user, string pass)
  358. {
  359. this.UserName = user;
  360. this.Password = pass;
  361. this.LoggedIn = false;
  362. }
  363. public UserInfo(string user, string pass, Client conn)
  364. {
  365. this.UserName = user;
  366. this.Password = pass;
  367. this.LoggedIn = true;
  368. this.Connection = conn;
  369. }
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement