/// <summary> /// Opens the socket and starts listening to a given port and IP /// </summary> /// <param name="port"></param> public void Listen(string ip, int port) { // Stop listening when application closes MstTimer.Instance.OnApplicationQuitEvent += Stop; if (ip == "127.0.0.1" | ip == "localhost") { server = new WebSocketServer(port, UseSsl); } else { server = new WebSocketServer(IPAddress.Parse(ip), port, UseSsl); } if (UseSsl) { if (string.IsNullOrEmpty(CertificatePath.Trim())) { Logs.Error("You are using secure connection, but no path to certificate defined. Stop connection process."); return; } server.SslConfiguration.ServerCertificate = new X509Certificate2(CertificatePath, CertificatePassword); } SetupService(server); server.Stop(); server.Start(); MstUpdateRunner.Instance.Add(this); }
/// <summary> /// Opens the socket and starts listening to a given port and IP /// </summary> /// <param name="port"></param> public void Listen(string ip, int port) { // Stop listening when application closes MstTimer.Instance.OnApplicationQuitEvent += Stop; if (server != null) { server.Stop(); } if (ip == "127.0.0.1" | ip == "localhost") { server = new WebSocketServer(port, UseSecure); } else { server = new WebSocketServer(IPAddress.Parse(ip), port, UseSecure); } if (UseSecure) { if (string.IsNullOrEmpty(CertificatePath.Trim())) { Logs.Error("You are using secure connection, but no path to certificate defined. Stop connection process."); return; } if (string.IsNullOrEmpty(CertificatePassword.Trim())) { server.SslConfiguration.ServerCertificate = new X509Certificate2(CertificatePath); } else { server.SslConfiguration.ServerCertificate = new X509Certificate2(CertificatePath, CertificatePassword); } server.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Ssl3 | System.Security.Authentication.SslProtocols.Default; } // Setup all services used by server SetupService(server); // Setup something else if needed OnBeforeServerStart?.Invoke(this); // Start server server.Start(); // Add this server to updater MstUpdateRunner.Instance.Add(this); }
public override void Initialize(IServer server) { // Setup secure connection UserSecure = MstApplicationConfig.Instance.UseSecure; CertificatePath = MstApplicationConfig.Instance.CertificatePath; CertificatePassword = MstApplicationConfig.Instance.CertificatePassword; httpPort = Mst.Args.AsInt(Mst.Args.Names.WebPort, httpPort); // Initialize server httpServer = new HttpServer(httpPort, UserSecure) { AuthenticationSchemes = authenticationSchemes, Realm = realm, UserCredentialsFinder = UserCredentialsFinder }; if (UserSecure) { if (string.IsNullOrEmpty(CertificatePath.Trim())) { logger.Error("You are using secure connection, but no path to certificate defined. Stop connection process."); return; } if (string.IsNullOrEmpty(CertificatePassword.Trim())) { httpServer.SslConfiguration.ServerCertificate = new X509Certificate2(CertificatePath); } else { httpServer.SslConfiguration.ServerCertificate = new X509Certificate2(CertificatePath, CertificatePassword); } httpServer.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Ssl3 | System.Security.Authentication.SslProtocols.Default; } // Initialize controllers list surfaceControllers = new Dictionary <Type, IHttpController>(); // Initialize handlers list httpRequestHandlers = new Dictionary <string, OnHttpRequestDelegate>(); // Find all controllers and add them to server foreach (var controller in GetComponentsInChildren <IHttpController>()) { if (surfaceControllers.ContainsKey(controller.GetType())) { throw new Exception("A controller already exists in the server: " + controller.GetType()); } surfaceControllers[controller.GetType()] = controller; controller.Initialize(this); } // Start listen to Get request httpServer.OnGet += HttpServer_OnGet; // Start listen to Post request httpServer.OnPost += HttpServer_OnPost; // Start listen to Put request httpServer.OnPut += HttpServer_OnPut; // Start http server httpServer.Start(); if (httpServer.IsListening) { logger.Info($"Http server is started and listening port: {httpServer.Port}"); } }
public override void Initialize(IServer server) { // Setup secure connection UserSecure = MstApplicationConfig.Instance.UseSecure; CertificatePath = MstApplicationConfig.Instance.CertificatePath; CertificatePassword = MstApplicationConfig.Instance.CertificatePassword; // Set port httpPort = Mst.Args.AsInt(Mst.Args.Names.WebPort, httpPort); // Set port httpAddress = Mst.Args.AsString(Mst.Args.Names.WebAddress, httpAddress); // Set root directory rootDirectory = Mst.Args.AsString(Mst.Args.Names.WebRootDir, rootDirectory); // Initialize server httpServer = new HttpServer(System.Net.IPAddress.Parse(httpAddress), httpPort, UserSecure) { AuthenticationSchemes = authenticationSchemes == AuthenticationSchemes.None ? AuthenticationSchemes.Anonymous : authenticationSchemes, Realm = realm, UserCredentialsFinder = UserCredentialsFinder }; // Init root directory. Create if exists InitRooDirectory(); if (UserSecure) { if (string.IsNullOrEmpty(CertificatePath.Trim())) { logger.Error("You are using secure connection, but no path to certificate defined. Stop connection process."); return; } if (string.IsNullOrEmpty(CertificatePassword.Trim())) { httpServer.SslConfiguration.ServerCertificate = new X509Certificate2(CertificatePath); } else { httpServer.SslConfiguration.ServerCertificate = new X509Certificate2(CertificatePath, CertificatePassword); } httpServer.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Ssl3 | System.Security.Authentication.SslProtocols.Default; } // Initialize controllers lists SurfaceControllers = new Dictionary <Type, IHttpController>(); WsControllers = new Dictionary <Type, IWsController>(); // Initialize handlers list httpRequestHandlers = new Dictionary <string, OnHttpRequestDelegate>(); // Find all surface controllers and add them to server foreach (var controller in GetComponentsInChildren <IHttpController>()) { if (SurfaceControllers.ContainsKey(controller.GetType())) { throw new Exception("A controller already exists in the server: " + controller.GetType()); } SurfaceControllers[controller.GetType()] = controller; controller.Initialize(this); } // Find all web socket controllers and add them to server foreach (var controller in GetComponentsInChildren <IWsController>()) { if (WsControllers.ContainsKey(controller.GetType())) { throw new Exception("A controller already exists in the server: " + controller.GetType()); } WsControllers[controller.GetType()] = controller; } // Start listen to Get request httpServer.OnGet += HttpServer_OnGet; // Start listen to Post request httpServer.OnPost += HttpServer_OnPost; // Start listen to Put request httpServer.OnPut += HttpServer_OnPut; // Register web socket controller service httpServer.AddWebSocketService <WsControllerService>($"/{wsServicePath}", (service) => { service.IgnoreExtensions = true; service.SetHttpServer(this); }); // Start http server httpServer.Start(); if (httpServer.IsListening) { logger.Info($"Web socket server is started and listening: {httpServer.Address}:{httpServer.Port}/{wsServicePath}"); logger.Info($"Http server is started and listening: {httpServer.Address}:{httpServer.Port}"); } }