Ejemplo n.º 1
0
    //public void indexAction() {
    //	if (_helper.Render.isEnabled) {
    //	}
    //}

    public void loginAction()
    {
        if (_helper.returnType == ReturnType.DefaultContent)
        {
            if (_helper.Request.HttpMethod == "POST")
            {
                _helper.Render.DissableRender();
                var res = (int)auth._Work(_helper, "loginUser");
                _helper = auth._GetHelper();
                if (res == 0)
                {
                    EchoJson(new { state = true, redirect = $"https://{_helper.domainName}/chat" });
                }
                else
                {
                    EchoJson(new { state = false, code = res });
                }
            }
            else
            {
                Echo("<link href='/assets/css/styleform.css' rel='stylesheet'>");
                Echo("<div id='containerauth'>");
                Echo("<div id='wrapper'>");
                Echo("<div id='authform'>");
                Echo($"<form name='login' action='https://{_helper.domainName}/auth/login' method='POST'>");
                Echo("<h1>Log in</h1>");
                //Echo("<p>");
                Echo("<label for='login' class='uname' data-icon='u'> Your username </label>");
                Echo("<input type='text' id='login' name='login' />");
                //Echo("</p>");
                //Echo("<p>");
                Echo("<label for='pass' class='youpasswd' data-icon='p'> Your password </label>");
                Echo("<input type='password' id='password' name='pass' />");
                //Echo("</p>");
                //Echo("<p class='keeplogin'> ");
                //Echo("<input type='checkbox' name='loginkeeping' id='loginkeeping' value='loginkeeping' />");
                //Echo("<label for='loginkeeping'>Keep me logged in</label>");
                //Echo("</p>");
                Echo("</form>");
                Echo("<button class='btn btn-primary btn-lg'>Login</button>");
                Echo("<p class='change_link'>Not a member yet ?<a href='/auth/signin' class='to_subscribe'>Join us</a></p>");
                formJS();
                Echo("</div></div></div>");
            }
        }
    }
Ejemplo n.º 2
0
        public void WorkWS(HelperClass _helper)
        {
            _helper.ContextWs.WebSocket.OnMessage += (sender, args) => {
                HelperClass helper = (HelperClass)WSclients[sender];
                var         json   = Newtonsoft.Json.Linq.JObject.Parse(args.Data);
                if (!json.ContainsKey("path") || !json.ContainsKey("type") || !json.ContainsKey("body"))
                {
                    helper.ContextWs.WebSocket.Close(WebSocketSharp.CloseStatusCode.InvalidData, "Invalid message content");
                    return;
                }
                string         path      = (string)json["path"];
                Queue <string> pathSplit = new Queue <string>(path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries));
                if (pathSplit.Count == 0)
                {
                    return;
                }
                ControllerTreeElement tree = controllerTree.SearchWS(ref pathSplit, out string action);
                if (tree != null)
                {
                    ControllerWorker controller = (ControllerWorker)tree.plugin.GetPluginRefObject();
                    try {
                        GetStaticPlugins(controller, ref helper);
                        controller._WorkWS(helper, action, new object[] { (string)json["type"], (string)json["body"] });
                        helper.GetData(controller._GetHelper());

                        if (helper.returnType == ReturnType.DefaultContent)
                        {
                            string bufS = "";
                            ResentContent(ref helper, ref controller, ref bufS);
                            helper.ContextWs.WebSocket.Send(bufS);
                            bufS = null;
                        }
                    }
                    catch (Exception e) {
                        if (helper.ContextWs.WebSocket.IsAlive)
                        {
                            Log.Error("Ошибка обработки сообщения WebSocket", e);
                        }
                    }
                }
            };
            _helper.ContextWs.WebSocket.OnClose += (sender, args) => {
                HelperClass helper = (HelperClass)WSclients[sender];
                WSclients.Remove(sender);
            };

            if (WebSocketInit)
            {
                PluginWorker httpInitPlugin = GetPlugin(WebSocketinitName);
                GetStaticPlugins(httpInitPlugin, ref _helper);
                httpInitPlugin._Work(_helper);
                _helper.GetData(httpInitPlugin._GetHelper());
                if (_helper.Auth.Status)
                {
                    WSclients.Add(_helper.ContextWs.WebSocket, _helper);
                    _helper.ContextWs.WebSocket.Accept();
                    return;
                }
            }
            _helper.ContextWs.WebSocket.Close(WebSocketSharp.CloseStatusCode.Normal, "Unathorized");
        }
Ejemplo n.º 3
0
        public void Work(HelperClass _helper)
        {
            string         path      = $"{_helper.Context.Request.Url.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped)}";
            Queue <string> pathSplit = new Queue <string>(path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries));

            if (pathSplit.Count == 0)
            {
                pathSplit.Enqueue("index");
            }
            ControllerTreeElement tree = controllerTree.Search(ref pathSplit, out string action);

            if (tree != null)
            {
                ControllerWorker controller = (ControllerWorker)tree.plugin.GetPluginRefObject();
                try {
                    if (HTTPinit)
                    {
                        PluginWorker httpInitPlugin = GetPlugin(HTTPinitName);
                        GetStaticPlugins(httpInitPlugin, ref _helper);
                        httpInitPlugin._Work(_helper);
                        _helper.GetData(httpInitPlugin._GetHelper());
                    }
                    GetStaticPlugins(controller, ref _helper);

                    controller._Work(_helper, action);
                    _helper.GetData(controller._GetHelper());

                    _helper.Context.Response.Headers.Add(_helper.Responce.Headers);
                    _helper.Context.Response.StatusCode        = _helper.Responce.StatusCode;
                    _helper.Context.Response.StatusDescription = _helper.Responce.StatusDescription;
                }
                catch (Exception e) {
                    Log.Error("Внутренняя ошибка подключения", e);
                    _helper.Answer500(e);
                }

                if (_helper.returnType == ReturnType.DefaultContent)
                {
                    _helper.Context.Response.ContentType = _helper.Responce.ContentType;

                    string bufS = "";
                    if (_helper.Render.isEnabled)
                    {
                        ResentLayout(ref _helper, ref controller, ref bufS, _helper.Render.layout, true);
                    }
                    else
                    {
                        ResentContent(ref _helper, ref controller, ref bufS);
                    }
                    //byte[] buf; // не работает в данной реализации HttpListenerContext
                    //while (bufS.Length > 0) {
                    //	buf = Encoding.UTF8.GetBytes(bufS.Substring(0, bufS.Length > 16000 ? 16000 : bufS.Length));
                    //	bufS = bufS.Remove(0, bufS.Length > 16000 ? 16000 : bufS.Length);
                    //	helper.Context.Response.OutputStream.Write(buf, 0, buf.Length);
                    //}
                    byte[] buf = Encoding.UTF8.GetBytes(bufS);
                    _helper.Context.Response.ContentLength64 = buf.Length;
                    _helper.Context.Response.OutputStream.Write(buf, 0, buf.Length);
                }
                else if (_helper.returnType == ReturnType.Info)
                {
                    if (_helper.Responce.StatusCode / 100 == 3)
                    {
                        _helper.Context.Response.Redirect(_helper.Responce.RedirectLocation);
                    }
                }
                if (_helper.WShelper.Acts.Count > 0)
                {
                    if (_helper.WShelper.ActsForAll)
                    {
                        foreach (HelperClass WSclient in WSclients.Values)
                        {
                            foreach (WebSocketAct act in _helper.WShelper.Acts)
                            {
                                WSclient.ContextWs.WebSocket.Send(act.Body);
                            }
                        }
                    }
                    else
                    {
                        foreach (HelperClass WSclient in WSclients.Values)
                        {
                            foreach (WebSocketAct act in _helper.WShelper.Acts)
                            {
                                if (act.Recepients.Contains(WSclient.Auth.Login))
                                {
                                    WSclient.ContextWs.WebSocket.Send(act.Body);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                bool resFile = false;
                try {
                    FileInfo fi = new FileInfo($"{baseDirectory.FullName}public{Path.DirectorySeparatorChar}{path.Replace('/', Path.DirectorySeparatorChar)}");
                    if (fi.Exists)
                    {
                        using (FileStream reader = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read)) {
                            resFile = true;
                            int    pos    = fi.Name.LastIndexOf('.') + 1;
                            string format = fi.Name.Substring(pos, fi.Name.Length - pos);
                            if (format == "css")
                            {
                                _helper.Context.Response.ContentType = "text/css";
                            }
                            else if (format == "js")
                            {
                                _helper.Context.Response.ContentType = "text/javascript";
                            }
                            _helper.Context.Response.ContentLength64 = reader.Length;
                            _helper.Context.Response.Headers.Add(HttpResponseHeader.CacheControl, "max-age=86400");
                            reader.CopyTo(_helper.Context.Response.OutputStream, 16384);
                        }
                    }
                    else
                    {
                        resFile = false;
                    }
                }
                catch {
                    resFile = false;
                }
                if (!resFile)
                {
                    _helper.Context.Response.StatusCode        = 404;
                    _helper.Context.Response.StatusDescription = "Not found";
                }
            }
        }