// Console command implementations public void Listen(string args_str) { if (server != null) { CrestronConsole.PrintLine("Server is already online. Disconnect it first"); return; } int bufsize = 100; // sample size for the server sockets' incoming data buffers int max_connections = 3; // sample size for the maximum number of simultaneous server sockets // Parse command-line arguments // You can optionally associate the client object with a certifiate and private key, which both must be in DER format, from the file system // For this particular example, the filenames must not contains spaces and must be located in the application directory string[] args = args_str.Split(' '); if (args.Length != 1 && args.Length != 3) { CrestronConsole.PrintLine("usage: listen [<cert_file> <key_file>] <port>"); return; } bool provideCert = false; string cert_fn = null; // certificate filename string key_fn = null; // private key filename int start = 0; // starting index of the hostname and port arguments in args if (args.Length == 3) // user provides filenames for the cert/key before the hostname and port arguments. { provideCert = true; cert_fn = args[0]; key_fn = args[1]; start += 2; } int port = 0; try { port = int.Parse(args[start]); } catch { PrintAndLog("Error: port number passed in is not numeric"); return; } if (port > 65535 || port < 0) { CrestronConsole.PrintLine("Port number is out of range"); return; } ErrorLog.Notice("Instantiating server object..."); try { server = new SecureTCPServer(port, bufsize, EthernetAdapterType.EthernetUnknownAdapter, max_connections); server.SocketStatusChange += new SecureTCPServerSocketStatusChangeEventHandler(ServerSocketStatusChanged); } catch (Exception e) { PrintAndLog("Error encountered while instantiating the server object: " + e.Message); return; } if (provideCert) { X509Certificate cert; byte[] key; // Populate cert and key loadCertAndKey(cert_fn, key_fn, out cert, out key); // Set the server's certificate and private key /* * The X509Certificate passed to SetServerCertificate should have the following attributes in these extension * fields: * * [...] * X509v3 Basic Constraints: critical * CA:FALSE * X509v3 Key Usage: critical * Digital Signature, Key Encipherment, Key Agreement * X509v3 Extended Key Usage: * TLS Web Client Authentication, TLS Web Server Authentication * [...] */ // Only call SetServerCertificate and SetServerPrivateKey if loadCertAndKey succeeded in populating cert and key. // Otherwise, the server will be associated with a default key and certificate determined by the control system's SSL settings if (cert != null && key != null) { PrintAndLog("Associating user-specified certificate and key with server..."); server.SetServerCertificate(cert); // The private key set here must correspond to the public key embedded in the server's certificate server.SetServerPrivateKey(key); } else { PrintAndLog("Associating default certificate and key with server..."); } } SocketErrorCodes err; ErrorLog.Notice("Begin listening for clients..."); // ServerConnectedCallback will get invoked once a client either // connects successfully or if the connection encounters an error err = server.WaitForConnectionAsync(ServerConnectedCallback); PrintAndLog("WaitForConnectionAsync returned: " + err); }