static Logger() { ConsoleServer = new ConsoleServer(); ConsoleServer.ReceivedCommand += ConsoleServerOnReceivedCommand; CrestronEnvironment.ProgramStatusEventHandler += OnProgramStatusEventHandler; AddCommand(PrintCommandHelp, "Help", "Get list of commands", "command"); AddCommand(TailLog, "Tail", "Tail the logger entries", "count"); AddCommand(ConsoleLog, "Log", "Write a log entry"); AddCommand(GetIpTable, "IPTable", "Print the IP Table for the current running program"); AddCommand((argString, args, connection, respond) => { respond($"Logging level is set to: {_level}"); }, "Level", "Get the current logging level"); AddCommand(SetStreamLevel, "LogStreamLevel", "Set the level logs stream on this connection", "level"); AddCommand(ListAssemblies, "ListAssemblies", "List the available assemblies in the app"); if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance) { AddCommand((argString, args, connection, respond) => { var response = string.Empty; CrestronConsole.SendControlSystemCommand($"progreset -P:{InitialParametersClass.ApplicationNumber}", ref response); }, "RestartApp", "Restart the running application"); AddCommand(GetAutoDiscovery, "AutoDiscover", "Get the system autodiscovery results"); AddCommand(RelayConsoleCommand, "Console", "Send a command to the Crestron console"); } var ass = Assembly.GetExecutingAssembly().GetName(); Highlight($"{ass.Name}.{nameof(Logger)} started, version {ass.Version}"); }
static void TestCommandServer(TransportInterface ti, ushort port) { ConsoleServer consoleServer = new ConsoleServer(); RakNetCommandParser rcp = new RakNetCommandParser(); LogCommandParser lcp = new LogCommandParser(); uint lastlog = 0; RakPeerInterface rakPeer = RakNetworkFactory.GetRakPeerInterface(); IntPtr testChannel = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi("TestChannel"); // you must call FreeHGlobal Console.Write("This sample demonstrates the command console server, which can be.\n"); Console.Write("a standalone application or part of your game server. It allows you to\n"); Console.Write("easily parse text strings sent from a client using either secure RakNet\n"); Console.Write("or Telnet.\n"); Console.Write("See the 'CommandConsoleClient' project for the RakNet client.\n"); Console.Write("Difficulty: Intermediate\n\n"); Console.Write("Command server started on port {0}.\n", port); consoleServer.AddCommandParser(rcp); consoleServer.AddCommandParser(lcp); consoleServer.SetTransportProvider(ti, port); rcp.SetRakPeerInterface(rakPeer); lcp.AddChannel(testChannel); while (true) { consoleServer.Update(); if (RakNetBindings.GetTime() > lastlog + 4000) { lcp.WriteLog(testChannel, "Test of logger"); lastlog = RakNetBindings.GetTime(); } System.Threading.Thread.Sleep(30); } }
private void EnrollPhase() { clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string IP = textBox_IP.Text; textBox_IP.Text = ""; int port; string EncDecPublic; string signVerPublic; ConsoleServer.AppendText("Connecting\n"); using (System.IO.StreamReader fileReader = new System.IO.StreamReader(@"C:\server_enc_dec_pub.txt")) { EncDecPublic = fileReader.ReadLine(); } using (System.IO.StreamReader fileReader = new System.IO.StreamReader(@"C:\server_signing_verification_pub.txt")) { signVerPublic = fileReader.ReadLine(); } //if(string.IsNullOrWhiteSpace(textBox_IP.Text) && string) if (Int32.TryParse(textBox_Port.Text, out port)) { try { clientSocket.Connect(IP, port); } catch { ConsoleServer.AppendText("Connection is not succesfull\nRe-enter Port and IP\n"); } ConsoleServer.AppendText("Connected to the server\n"); /* * ENROLLMENT * sending username and password with sha * * */ ConsoleServer.AppendText("Enrolling to the server\n"); byte[] loginReqByte = Encoding.Default.GetBytes("enroll"); byte[] loginReqEncByte = encryptWithRSA(Encoding.Default.GetString(loginReqByte), 3072, EncDecPublic); sendMessage(Encoding.Default.GetString(loginReqEncByte)); // sends ENROLL request to the server byte[] passwordHash = hashWithSHA256(textBox_Password.Text); // Girilen PASSWORD byte[] usernameByte = Encoding.Default.GetBytes(textBox_UserName.Text); // Girilen USERNAME byte[] concatenatedByte = new byte[16 + textBox_UserName.Text.Length]; Array.Copy(passwordHash, 0, concatenatedByte, 0, 16); Array.Copy(usernameByte, 0, concatenatedByte, 16, textBox_UserName.Text.Length); byte[] encryptedRSA = encryptWithRSA(Encoding.Default.GetString(concatenatedByte), 3072, EncDecPublic); sendMessage(Encoding.Default.GetString(encryptedRSA)); // Girilen PASSWORD ve USERNAME ikilisi birleştirilip yollanır
private static void Main(string[] args) { var config = new StaticConfig { DesiredConnections = 22, MaxConnections = 22, ConnectCooldown = TimeSpan.FromMilliseconds(100), DisconnectCooldown = TimeSpan.FromMilliseconds(100), MapUpdateCooldown = TimeSpan.FromMilliseconds(50), KeySendCooldown = TimeSpan.FromSeconds(10), ConnectingSocketMaxTTL = TimeSpan.FromMilliseconds(50), ConnectingSocketsToConnectionsMultiplier = 5, PreconfiguredNodes = Enumerable.Range(1, 30).Select(i => new TcpAddress(new IPEndPoint(IPAddress.Parse(string.Format(AddressFormat, i)), 16800)) as IAddress).ToList(), LocalAddress = GetLocalAddress(16800), LongNames = true, DoLogMap = true }; var node = CreateNode(config, config, "storage"); var consoleServer = new ConsoleServer(new IPEndPoint(IPAddress.Any, 16801), node); consoleServer.Start(); node.Start(); }
private void LoginPhase() { clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string IP = textBox_IP.Text; textBox_IP.Text = ""; int port; string EncDecPublic; string signVerPublic; bool verificationResult = false; ConsoleServer.AppendText("Connecting to the server"); using (System.IO.StreamReader fileReader = new System.IO.StreamReader(@"C:\server_enc_dec_pub.txt")) { EncDecPublic = fileReader.ReadLine(); } using (System.IO.StreamReader fileReader = new System.IO.StreamReader(@"C:\server_signing_verification_pub.txt")) { signVerPublic = fileReader.ReadLine(); } //if(string.IsNullOrWhiteSpace(textBox_IP.Text) && string) if (Int32.TryParse(textBox_Port.Text, out port)) { try { clientSocket.Connect(IP, port); } catch { ConsoleServer.AppendText("Connection is not succesfull\nRe-enter Port and IP\n"); } /* * LOGING * sending username and password with sha * * */ byte[] loginReqEncByte = encryptWithRSA("login", 3072, EncDecPublic); try { sendMessage(Encoding.Default.GetString(loginReqEncByte)); // sends login request to the server } catch { ConsoleServer.AppendText("A problem occured sending message ERROR0.001\n"); } //byte[] sha256 = hashWithSHA256(textBox_Password.Text); /********** AUTHENTICATION REQUEST TO THE SERVER TOGETHER WITH USERNAME **************/ byte[] hashOfUsername = hashWithSHA256(textBox_UserName.Text); byte[] usernameByte = Encoding.Default.GetBytes(textBox_UserName.Text); byte[] concatenatedByte = new byte[32 + usernameByte.Length]; Array.Copy(hashOfUsername, 0, concatenatedByte, 0, 32); Array.Copy(usernameByte, 0, concatenatedByte, 32, usernameByte.Length); byte[] authenticationRequest = encryptWithRSA(Encoding.Default.GetString(concatenatedByte), 3072, EncDecPublic); try { sendMessage(Encoding.Default.GetString(authenticationRequest)); //sends Challange Response protochols result SEEEND } catch { ConsoleServer.AppendText("A problem occured sending message ERROR0.002\n"); } /*******************************************************************************/ //Server sends the 128bit random number string serversMessage = ""; try { serversMessage = receiveMessage(); } catch { ConsoleServer.AppendText("A problem occured while Receiving message ERROR0.003\n"); } byte[] receivedBytes = Encoding.Default.GetBytes(serversMessage); byte[] hashOfRandomNum = new byte[32]; byte[] random128BitNum = new byte[16]; Array.Copy(receivedBytes, 0, hashOfRandomNum, 0, 32); Array.Copy(receivedBytes, 32, random128BitNum, 0, 16); if (Encoding.Default.GetString(hashWithSHA256(Encoding.Default.GetString(random128BitNum))) == Encoding.Default.GetString(hashOfRandomNum)) // Yollanan random number kazasız belasız ulaşmış mı ? { byte[] hashOfPassword = hashWithSHA256(textBox_Password.Text); byte[] upperhalfOfPassword = new byte[16]; Array.Copy(hashOfPassword, 0, upperhalfOfPassword, 0, 16); byte[] hmacsha256 = applyHMACwithSHA256(Encoding.Default.GetString(random128BitNum), upperhalfOfPassword); try { sendMessage(Encoding.Default.GetString(hmacsha256)); } catch { ConsoleServer.AppendText("A problem occured while Sending message ERROR0.004\n"); } // SIGNED ACKNOWLEDGEMENT RECEIVED FROM SERVER AFTER HMAC AUTHENTICATION try { serversMessage = receiveMessage(); } catch { ConsoleServer.AppendText("A problem occured while receiving message ERROR0.005\n"); } receivedBytes = Encoding.Default.GetBytes(serversMessage); byte[] signOfAcknowledgement = new byte[384]; byte[] acknowledgementMessage = new byte[receivedBytes.Length - 384]; Array.Copy(receivedBytes, 0, signOfAcknowledgement, 0, 384); Array.Copy(receivedBytes, 384, acknowledgementMessage, 0, receivedBytes.Length - 384); //TRY TO VERIFY SIGNED MESSAGE WHETHER THE SERVER IS VALID verificationResult = verifyWithRSA(Encoding.Default.GetString(acknowledgementMessage), 3072, signVerPublic, signOfAcknowledgement); if (verificationResult == true) { ConsoleServer.AppendText("Login Acknowledgement Comes From VALID Server\n"); if (Encoding.Default.GetString(acknowledgementMessage) == "Succesfully Verified") { //***************** ARTIK GİRİS YAPABİLİRSİN DOSTUM ******************// ConsoleServer.AppendText("You are SUCCESFULLY Connectected to the Server\n"); ConsoleServer.AppendText("You are now Logged in\n"); button_connect.Enabled = false; button_Enroll.Enabled = false; connected = true; //GET SESSION KEYS FROM SERVER PHASE serversMessage = receiveMessage(); receivedBytes = Encoding.Default.GetBytes(serversMessage); byte[] sessionKeyMesSign = new byte[384]; byte[] sessionKeyMes = new byte[receivedBytes.Length - 384]; Array.Copy(receivedBytes, 0, sessionKeyMes, 0, receivedBytes.Length - 384); Array.Copy(receivedBytes, 384, sessionKeyMesSign, receivedBytes.Length - 384, 384); verificationResult = verifyWithRSA(serversMessage, 3072, signVerPublic, sessionKeyMesSign); if (verificationResult == true) { byte[] SessionkeysEncrypted = new byte[32]; Array.Copy(SessionkeysEncrypted, 0, sessionKeyMes, 0, 32); // Seperate session key from "OK" byte[] Sessionkeys = decryptWithAES128(Encoding.Default.GetString(SessionkeysEncrypted), upperhalfOfPassword, random128BitNum); // Decrypte session key using AES128 byte[] SymEncDec128BitNum = new byte[16]; // First 128Bit is AES keys byte[] SessionKey128BitNum = new byte[16]; // Second 128Bit is Authentcation Key for Session Array.Copy(Sessionkeys, 0, SymEncDec128BitNum, 0, 16); Array.Copy(Sessionkeys, 16, SessionKey128BitNum, 0, 16); Thread thread = new Thread(() => ReceiveFunc(SymEncDec128BitNum, SessionKey128BitNum)); // Serverın yollayacakları için ReceiveFunc Thread i başlatılıyor thread.Start(); } } else if (Encoding.Default.GetString(acknowledgementMessage) == "Succesfully Verified But You are Allready In Server") { //************ VERIFY EDİLEN KULLANICI ZATEN SERVER DA DOSTUM ********// ConsoleServer.AppendText("You are SUCCESFULLY Verified by the Server but BRO You are Allready In the Server \n Please Try again\n"); } else if (Encoding.Default.GetString(acknowledgementMessage) == "You CAN'T Verified") { ConsoleServer.AppendText("You CAN'T Login The System \n Please Try again\n"); } } else { ConsoleServer.AppendText("Login Acknowledgement Comes From INVALID Server\n"); } } } else { textBox_Port.Text = ""; ConsoleServer.AppendText("Check the port\n"); } }