public string Connect(string ip, int port, string usr, string pwd) { var ips = IPAddress.Parse(ip); var Endpoint = new IPEndPoint(ips, port); //tcpClient = new TcpClient(Endpoint); tcpClient.Connect(Endpoint); //tcpClient.Connect(ip, port); usrname = usr; password = pwd; stream = tcpClient.GetStream(); Writer = new StreamWriter(stream); Reader = new StreamReader(stream); AdvancedStream.SendMessage(ref Writer, $"L2KDB:Basic:OpenSession|{usr}|{pwd}"); string content = AdvancedStream.ReadToCurrentEnd(ref Reader); var Responses = content.Split(','); if (Responses[0] == "L2KDB:Basic:ConnectionAccept") { sessionID = Responses[1]; aes = new CustomedAES(); aesKey = Responses[2]; aesIV = Responses[3]; aes.Key = aesKey; aes.IV = aesIV; isConnected = true; return("[S]"); } else { return("[F]" + Responses[0]); } }
public string OpenDatabase(string name, string key, string iv) { if (isConnected == true) { AdvancedStream.SendMessage(ref Writer, $"L2KDB:Basic:OpenDatabase,{name},{key},{iv}|{sessionID}", aes); string content = AdvancedStream.ReadToCurrentEnd(ref Reader, aes); if (content == "L2KDB:Basic:DatabaseOpen") { return("[S]"); } else if (content == "L2KDB:Basic:AccessForbidden") { return("[F]Access Forbidden"); } else if (content == "L2KDB:Basic:DatabaseNoFound") { return("[F]NotFound"); } else { return(content); } } return("[F]Unconnected"); }
public List <string> GetID2s(string id1) { List <string> vs = new List <string>(); if (isConnected == true) { { AdvancedStream.SendMessage(ref Writer, $"L2KDB:Basic:GetID2,{id1}|{sessionID}", aes); var Command = aes.Decrypt(Reader.ReadLine()); var data = AdvancedStream.ReadToCurrentEnd(ref Reader, aes, false); if (Command == "L2KDB:Basic:DatabaseGetID2Result") { StringReader stringReader = new StringReader(data); var temp = ""; while ((temp = stringReader.ReadLine()) != null) { if (temp == "L2KDB:Basic:NoID2") { } else { vs.Add(temp); } } } else { throw new Exception("" + Command); } } return(vs); } throw new Exception("Unconnected"); }
public void Stop(StopReason stopReason = StopReason.ShutdownServer) { willStop = true; try { AdvancedStream.SendMessage(ref Writer, "L2KDB:Basic:Stop," + stopReason.ToString(), CustomedAES); } catch (Exception) { } try { Reader.Dispose(); } catch (Exception) { } try { Writer.Dispose(); } catch (Exception) { } try { OriginalStream.Dispose(); } catch (Exception) { } try { Client.Dispose(); } catch (Exception) { } try { ServerCore.SessionPool.Remove(this); } catch (Exception) { } System.GC.Collect(); }
static void Main(string[] args) { TcpClient client = new TcpClient(); client.Connect("127.0.0.1", 9341); var s = client.GetStream(); StreamWriter streamWriter = new StreamWriter(s); { AdvancedStream.SendMessage(ref streamWriter, "L2KDB:Basic:OpenSession|Creeper Lv|123456\r\nL2KDB:Basic:EndOfCurrentTransmission"); //streamWriter.Flush(); } String SessionID = ""; String Key = ""; String IV = ""; CustomedAES aes = new CustomedAES(); StreamReader streamReader = new StreamReader(s); { string receive; receive = AdvancedStream.ReadToCurrentEnd(ref streamReader); Console.WriteLine(receive); var blocks = receive.Split(':'); if (blocks[2].StartsWith("ConnectionAccept")) { var c = blocks[2].Split(','); SessionID = c[1]; Key = c[2]; IV = c[3]; aes.Key = Key; aes.IV = IV; Console.WriteLine($"Obtain:{SessionID}\t{Key}\t{IV}"); { AdvancedStream.SendMessage(ref streamWriter, "L2KDB:Basic:GetDatabaseVersion|" + SessionID, aes); Console.WriteLine(AdvancedStream.ReadToCurrentEnd(ref streamReader, aes)); } { AdvancedStream.SendMessage(ref streamWriter, "L2KDB:Basic:OpenDatabase,TestDataBase,,|" + SessionID, aes); Console.WriteLine(AdvancedStream.ReadToCurrentEnd(ref streamReader, aes)); } } else { } } //Console.WriteLine("Hello World!"); }
public string Query(string id1, string id2, string content) { if (isConnected == true) { { AdvancedStream.SendMessage(ref Writer, $"L2KDB:Basic:Query,{id1},{id2}|{sessionID}", aes); var Command = aes.Decrypt(Reader.ReadLine()); var data = AdvancedStream.ReadToCurrentEnd(ref Reader, aes, false); if (Command == "L2KDB:Basic:DatabaseQueryResult") { return(data); } else { throw new Exception("" + Command); } } } throw new Exception("Unconnected"); }
public string OpenForm(string name) { if (isConnected == true) { AdvancedStream.SendMessage(ref Writer, $"L2KDB:Basic:OpenForm,{name}|{sessionID}", aes); string content = AdvancedStream.ReadToCurrentEnd(ref Reader, aes); if (content == "L2KDB:Basic:FromOpen") { return("[S]"); } else if (content == "L2KDB:Basic:UnknownError") { return("[F]An Error Has Occurred On Server"); } else if (content == "L2KDB:Basic:AccessForbidden") { return("[F]Access Forbidden"); } } return("[F]Unconnected"); }
public string Save(string id1, string id2, string content) { if (isConnected == true) { { } AdvancedStream.SendMessage(ref Writer, $"L2KDB:Basic:Save,{id1},{id2}|{sessionID}", content, aes); string result = AdvancedStream.ReadToCurrentEnd(ref Reader, aes); if (result == "L2KDB:Basic:OperationCompleted") { return("[S]"); } else if (result == "L2KDB:Basic:UnknownError") { return("[F]An Error Has Occurred On Server"); } else if (result == "L2KDB:Basic:AccessForbidden") { return("[F]Access Forbidden"); } } return("[F]Unconnected"); }
public async void SessionWorker() { SessionID = Guid.NewGuid(); Diagnotor.CurrentDiagnotor.Log("Session Opened:" + SessionID + ", AuthID=" + AuthID); SessionKey = CustomedAES.GenerateKey(); SessionIV = CustomedAES.GenerateIV(); CustomedAES.Key = SessionKey; CustomedAES.IV = SessionIV; int ExceptionOccurred = 0; AdvancedStream.SendMessage(ref Writer, "L2KDB:Basic:ConnectionAccept," + SessionID.ToString() + $",{SessionKey},{SessionIV}"); while (willStop == false) { try { var Command = CustomedAES.Decrypt(await Reader.ReadLineAsync()); var data = AdvancedStream.ReadToCurrentEnd(ref Reader, CustomedAES, false); /** * Data Structure: * [Command]|[SessionID]|[Key(Optional)]|[IV(Optional)] * [Cont... * en... * t] **/ Diagnotor.CurrentDiagnotor.Log($"Command from {SessionID}:" + Command); var cmd = Command.Split('|'); if (cmd[1] != SessionID.ToString()) { Stop(StopReason.Unknown); } var result = CmdletProcesser(cmd, data); if (result != "-1") { AdvancedStream.SendMessage(ref Writer, $"{result}", CustomedAES); } } catch (Exception e) { if (e.GetType() == typeof(SocketException)) { Diagnotor.CurrentDiagnotor.Log($"Shutting {SessionID} Down."); Stop(); break; } else if (e.GetType() == typeof(IOException)) { Diagnotor.CurrentDiagnotor.Log($"Shutting {SessionID} Down."); Stop(); break; } else { ExceptionOccurred++; Diagnotor.CurrentDiagnotor.LogWarning("Captured:" + e.Message); if (ExceptionOccurred >= 100) { Diagnotor.CurrentDiagnotor.LogError($"Session:{SessionID} occurred too many errors, force to shutdown."); Stop(); } } } } }
public void SendData(string title, String Data) { AdvancedStream.SendMessage(ref Writer, title, Data, CustomedAES); }
public async Task SendDataAsync(string Data) { AdvancedStream.SendMessage(ref Writer, Data, CustomedAES); }