Пример #1
0
        private WSServer()
        {
            var plugin = Registry.Resolve <PluginMain>();
            var cfg    = plugin.Config;

            _failed = false;

            try
            {
                var sslPath = GetCertPath();
                var secure  = cfg.WSServerSSL && File.Exists(sslPath);

                _server = new HttpServer(IPAddress.Parse(cfg.WSServerIP), cfg.WSServerPort, secure);
                _server.ReuseAddress = true;
                _server.Log.Output  += (LogData d, string msg) =>
                {
                    Log(LogLevel.Info, "WS: {0}: {1} {2}", d.Level.ToString(), d.Message, msg);
                };
                _server.Log.Level = WebSocketSharp.LogLevel.Info;

                if (secure)
                {
                    Log(LogLevel.Debug, Resources.WSLoadingCert, sslPath);

                    // IMPORTANT: Do *not* change the password here. This is the default password that mkcert uses.
                    // If you use a different password here, you'd have to pass it to mkcert and update the text on the WSServer tab to match.
                    _server.SslConfiguration.ServerCertificate   = new X509Certificate2(sslPath, "changeit");
                    _server.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls12;
                }

                _server.AddWebSocketService <SocketHandler>("/ws");
                _server.AddWebSocketService <LegacyHandler>("/MiniParse");
                _server.AddWebSocketService <LegacyHandler>("/BeforeLogLineRead");

                _server.OnGet += (object sender, HttpRequestEventArgs e) =>
                {
                    if (e.Request.RawUrl == "/")
                    {
                        var builder = new StringBuilder();
                        builder.Append(@"<!DOCTYPE html>
<html>
    <head>
        <title>OverlayPlugin WSServer</title>
    </head>
    <script type='text/javascript'>
        window.addEventListener('load', () => {
            var links = document.querySelectorAll('a');
            for (var i = 0; i < links.length; i++) {
                var data = JSON.parse(links[i].getAttribute('data-info'));
                var endpoint = data.arg === 'OVERLAY_WS' ? 'ws' : '';
                links[i].href = data.url + '?' + data.arg + '=' + location.href.replace('http', 'ws') + endpoint;
                
                if (data.url.indexOf('file://') === 0) {
                    links[i].parentNode.innerText = 'Local: ' + links[i].innerText + ': ' + links[i].href;
                }
            }
        });
    </script>
    <body>
        " + Resources.WSIndexPage + @"
        <ul>");

                        foreach (var overlay in plugin.Overlays)
                        {
                            var argName = "OVERLAY_WS";
                            if (overlay.GetType() == typeof(MiniParseOverlay))
                            {
                                var config = (MiniParseOverlayConfig)overlay.Config;
                                if (config.ActwsCompatibility)
                                {
                                    argName = "HOST_PORT";
                                }
                            }

                            var jsonInfo = JsonConvert.SerializeObject(new
                            {
                                url = overlay.Config.Url,
                                arg = argName
                            });

                            // HTML escaping
                            jsonInfo = jsonInfo.Replace("\"", "&quot;").Replace(">", "&gt;");
                            var overlayName = overlay.Name.Replace("&", "&amp;").Replace("<", "&lt;");

                            builder.Append($"<li><a href=\"#\" data-info=\"{jsonInfo}\">{overlayName}</a></li>");
                        }

                        builder.Append("</body></html>");

                        var res = e.Response;
                        res.StatusCode  = 200;
                        res.ContentType = "text/html";
                        Ext.WriteContent(res, Encoding.UTF8.GetBytes(builder.ToString()));
                    }
                };

                _server.Start();
                OnStateChanged?.Invoke(this, new StateChangedArgs(true, false));
            }
            catch (Exception e)
            {
                _failed = true;
                Log(LogLevel.Error, Resources.WSStartFailed, e);
                OnStateChanged?.Invoke(this, new StateChangedArgs(false, true));
            }
        }