Example #1
0
        public async Task <bool> AllClientsDoActionAsync(string address, WebsocketAction action)
        {
            var result = await FunctionUtil.TRY(async() =>
            {
                using (var resp = await(address + "/RemoteOP/AllClientsDoAction")
                                  .AsHttp("POST", action)
                                  .Config(new RequestOptions {
                    ContentType = "application/json"
                })
                                  .SendAsync())
                {
                    if (resp.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        var result = resp.Deserialize <dynamic>();

                        if ((bool)result.success)
                        {
                            return(true);
                        }
                    }

                    return(false);
                }
            }, 5);

            await _sysLogService.AddSysLogAsync(new SysLog
            {
                LogTime = DateTime.Now,
                LogType = result ? SysLogType.Normal : SysLogType.Warn,
                LogText = $"通知节点{address}所有客户端:{action.Action} 响应:{(result ? "成功" : "失败")}"
            });

            return(result);
        }
        public async Task <IActionResult> Add([FromBody] ServerNodeVM model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }
            model.Address = model.Address.TrimEnd('/');
            var oldNode = await _serverNodeService.GetAsync(model.Address);

            if (oldNode != null)
            {
                return(Json(new
                {
                    success = false,
                    message = "节点已存在,请重新输入。"
                }));
            }

            var node = new ServerNode();

            node.Address    = model.Address.TrimEnd('/');
            node.Remark     = model.Remark;
            node.Status     = NodeStatus.Offline;
            node.CreateTime = DateTime.Now;

            var result = await _serverNodeService.AddAsync(node);

            if (result)
            {
                await _sysLogService.AddSysLogAsync(new SysLog
                {
                    LogTime = DateTime.Now,
                    LogType = SysLogType.Normal,
                    LogText = $"新增节点:{node.Address}"
                });

                await _remoteServerNodeProxy.TestEchoAsync(node.Address);
            }

            return(Json(new
            {
                data = node,
                success = result,
                message = !result ? "添加节点失败,请查看错误日志" : ""
            }));
        }
        public async Task <IActionResult> Add([FromBody] AppVM model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            var oldApp = await _appService.GetAsync(model.Id);

            if (oldApp != null)
            {
                return(Json(new
                {
                    success = false,
                    message = "应用Id已存在,请重新输入。"
                }));
            }

            var app = new App();

            app.Id         = model.Id;
            app.Name       = model.Name;
            app.Secret     = model.Secret;
            app.Enabled    = model.Enabled;
            app.CreateTime = DateTime.Now;
            app.UpdateTime = null;

            var result = await _appService.AddAsync(app);

            if (result)
            {
                await _sysLogService.AddSysLogAsync(new SysLog
                {
                    LogTime = DateTime.Now,
                    LogType = SysLogType.Normal,
                    LogText = $"新增应用【AppId:{app.Id}】【AppName:{app.Name}】"
                });
            }

            return(Json(new
            {
                success = result,
                message = !result ? "新建应用失败,请查看错误日志" : ""
            }));
        }
        public async Task <IActionResult> Login4AntdPro([FromBody] LoginVM model)
        {
            string password = model.password;

            if (string.IsNullOrEmpty(password))
            {
                return(Json(new
                {
                    status = "error",
                    message = "密码不能为空"
                }));
            }

            var result = await _settingService.ValidateAdminPassword(password);

            if (result)
            {
                var jwt = JWT.GetToken();

                //addlog
                await _sysLogService.AddSysLogAsync(new Data.Entity.SysLog
                {
                    LogTime = DateTime.Now,
                    LogType = Data.Entity.SysLogType.Normal,
                    LogText = $"管理员登录成功"
                });

                return(Json(new
                {
                    status = "ok",
                    token = jwt,
                    type = "Bearer",
                    currentAuthority = "admin"
                }));
            }

            return(Json(new
            {
                status = "error",
                message = "密码错误"
            }));
        }
        public async Task <IActionResult> Login([FromForm] string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                ViewBag.ErrorMessage = "密码不能为空";
                return(View());
            }

            var result = await _settingService.ValidateAdminPassword(password);

            if (result)
            {
                var claims = new List <Claim>
                {
                    new Claim("UserName", "Administrator")
                };

                var claimsIdentity = new ClaimsIdentity(
                    claims, CookieAuthenticationDefaults.AuthenticationScheme);

                var authProperties = new AuthenticationProperties
                {
                    //AllowRefresh = <bool>,
                    // Refreshing the authentication session should be allowed.

                    //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
                    // The time at which the authentication ticket expires. A
                    // value set here overrides the ExpireTimeSpan option of
                    // CookieAuthenticationOptions set with AddCookie.

                    //IsPersistent = true,
                    // Whether the authentication session is persisted across
                    // multiple requests. Required when setting the
                    // ExpireTimeSpan option of CookieAuthenticationOptions
                    // set with AddCookie. Also required when setting
                    // ExpiresUtc.

                    //IssuedUtc = <DateTimeOffset>,
                    // The time at which the authentication ticket was issued.

                    //RedirectUri = <string>
                    // The full path or absolute URI to be used as an http
                    // redirect response value.
                };

                await HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(claimsIdentity),
                    authProperties);

                //addlog
                await _sysLogService.AddSysLogAsync(new Data.Entity.SysLog
                {
                    LogTime = DateTime.Now,
                    LogType = Data.Entity.SysLogType.Normal,
                    LogText = $"管理员登录成功"
                });

                return(Redirect("/"));
            }

            ViewBag.ErrorMessage = "登录失败:密码不正确";
            return(View());
        }
        public async Task <IActionResult> Add([FromBody] ConfigVM model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            var oldConfig = await _configService.GetByAppIdKey(model.AppId, model.Group, model.Key);

            if (oldConfig != null)
            {
                return(Json(new
                {
                    success = false,
                    message = "配置已存在,请更改输入的信息。"
                }));
            }

            var config = new Config();

            config.Id           = Guid.NewGuid().ToString("N");
            config.Key          = model.Key;
            config.AppId        = model.AppId;
            config.Description  = model.Description;
            config.Value        = model.Value;
            config.Group        = model.Group;
            config.Status       = ConfigStatus.Enabled;
            config.CreateTime   = DateTime.Now;
            config.UpdateTime   = null;
            config.OnlineStatus = OnlineStatus.WaitPublish;

            var result = await _configService.AddAsync(config);

            if (result)
            {
                //add syslog
                await _sysLogService.AddSysLogAsync(new SysLog
                {
                    LogTime = DateTime.Now,
                    LogType = SysLogType.Normal,
                    AppId   = config.AppId,
                    LogText = $"新增配置【Key:{config.Key}】【Value:{config.Value}】【Group:{config.Group}】【AppId:{config.AppId}】"
                });

                //add modify log
                await _modifyLogService.AddAsync(new ModifyLog
                {
                    Id         = Guid.NewGuid().ToString("N"),
                    ConfigId   = config.Id,
                    Key        = config.Key,
                    Group      = config.Group,
                    Value      = config.Value,
                    ModifyTime = config.CreateTime
                });
            }

            return(Json(new
            {
                success = result,
                message = !result ? "新建配置失败,请查看错误日志" : ""
            }));
        }
Example #7
0
        public async Task <IActionResult> Add([FromBody] AppVM model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            var oldApp = await _appService.GetAsync(model.Id);

            if (oldApp != null)
            {
                return(Json(new
                {
                    success = false,
                    message = "应用Id已存在,请重新输入。"
                }));
            }

            var app = new App();

            app.Id         = model.Id;
            app.Name       = model.Name;
            app.Secret     = model.Secret;
            app.Enabled    = model.Enabled;
            app.CreateTime = DateTime.Now;
            app.UpdateTime = null;
            app.Type       = model.Inheritanced ? AppType.Inheritance : AppType.PRIVATE;

            var inheritanceApps = new List <AppInheritanced>();

            if (!model.Inheritanced && model.inheritancedApps != null)
            {
                var sort = 0;
                model.inheritancedApps.ForEach(appId =>
                {
                    inheritanceApps.Add(new AppInheritanced
                    {
                        Id                = Guid.NewGuid().ToString("N"),
                        AppId             = app.Id,
                        InheritancedAppId = appId,
                        Sort              = sort++
                    });
                });
            }

            var result = await _appService.AddAsync(app, inheritanceApps);

            if (result)
            {
                await _sysLogService.AddSysLogAsync(new SysLog
                {
                    LogTime = DateTime.Now,
                    LogType = SysLogType.Normal,
                    LogText = $"新增应用【AppId:{app.Id}】【AppName:{app.Name}】"
                });
            }

            return(Json(new
            {
                data = app,
                success = result,
                message = !result ? "新建应用失败,请查看错误日志" : ""
            }));
        }
Example #8
0
        /// <summary>
        /// 注册添加系统日志事件
        /// </summary>
        private void RegisterAddSysLog()
        {
            TinyEventBus.Instance.Regist(EventKeys.ADMIN_LOGIN_SUCCESS, (parm) =>
            {
                var log = new SysLog
                {
                    LogTime = DateTime.Now,
                    LogType = SysLogType.Normal,
                    LogText = $"管理员登录成功"
                };
                _sysLogService.AddSysLogAsync(log);
            });

            TinyEventBus.Instance.Regist(EventKeys.INIT_ADMIN_PASSWORD_SUCCESS, (parm) =>
            {
                var log = new SysLog
                {
                    LogTime = DateTime.Now,
                    LogType = SysLogType.Normal,
                    LogText = $"管理员密码初始化成功"
                };
                _sysLogService.AddSysLogAsync(log);
            });

            TinyEventBus.Instance.Regist(EventKeys.RESET_ADMIN_PASSWORD_SUCCESS, (parm) =>
            {
                var log = new SysLog
                {
                    LogTime = DateTime.Now,
                    LogType = SysLogType.Normal,
                    LogText = $"修改管理员密码成功"
                };
                _sysLogService.AddSysLogAsync(log);
            });

            TinyEventBus.Instance.Regist(EventKeys.ADD_APP_SUCCESS, (param) =>
            {
                var app = param as App;
                if (app != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        LogText = $"新增应用【AppId:{app.Id}】【AppName:{app.Name}】"
                    };
                    _sysLogService.AddSysLogAsync(log);
                }
            });

            // app
            TinyEventBus.Instance.Regist(EventKeys.EDIT_APP_SUCCESS, (param) =>
            {
                var app = param as App;
                if (app != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        LogText = $"编辑应用【AppId:{app.Id}】【AppName:{app.Name}】"
                    };
                    _sysLogService.AddSysLogAsync(log);
                }
            });

            TinyEventBus.Instance.Regist(EventKeys.DISABLE_OR_ENABLE_APP_SUCCESS, (param) =>
            {
                var app = param as App;
                if (app != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        LogText = $"{(app.Enabled ? "启用" : "禁用")}应用【AppId:{app.Id}】"
                    };
                    _sysLogService.AddSysLogAsync(log);
                }
            });

            TinyEventBus.Instance.Regist(EventKeys.DELETE_APP_SUCCESS, (param) =>
            {
                var app = param as App;
                if (app != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        LogText = $"删除应用【AppId:{app.Id}】"
                    };
                    _sysLogService.AddSysLogAsync(log);
                }
            });

            TinyEventBus.Instance.Regist(EventKeys.DELETE_APP_SUCCESS, (param) =>
            {
                var app = param as App;
                if (app != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        LogText = $"删除应用【AppId:{app.Id}】"
                    };
                    _sysLogService.AddSysLogAsync(log);
                }
            });
            //config
            TinyEventBus.Instance.Regist(EventKeys.ADD_CONFIG_SUCCESS, (param) =>
            {
                var config = param as Config;
                if (config != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        AppId   = config.AppId,
                        LogText = $"新增配置【Key:{config.Key}】【Value:{config.Value}】【Group:{config.Group}】【AppId:{config.AppId}】"
                    };
                    _sysLogService.AddSysLogAsync(log);

                    _modifyLogService.AddAsync(new ModifyLog
                    {
                        Id         = Guid.NewGuid().ToString("N"),
                        ConfigId   = config.Id,
                        Key        = config.Key,
                        Group      = config.Group,
                        Value      = config.Value,
                        ModifyTime = config.CreateTime
                    });
                }
            });
            TinyEventBus.Instance.Regist(EventKeys.EDIT_CONFIG_SUCCESS, (param) =>
            {
                dynamic param_dy = param;
                Config config    = param_dy.config;
                Config oldConfig = param_dy.oldConfig;

                if (config != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        AppId   = config.AppId,
                        LogText = $"编辑配置【Key:{config.Key}】【Value:{config.Value}】【Group:{config.Group}】【AppId:{config.AppId}】"
                    };
                    _sysLogService.AddSysLogAsync(log);

                    _modifyLogService.AddAsync(new ModifyLog
                    {
                        Id         = Guid.NewGuid().ToString("N"),
                        ConfigId   = config.Id,
                        Key        = config.Key,
                        Group      = config.Group,
                        Value      = config.Value,
                        ModifyTime = config.UpdateTime.Value
                    });
                }
            });

            TinyEventBus.Instance.Regist(EventKeys.EDIT_CONFIG_SUCCESS, async(param) =>
            {
                dynamic param_dy = param;
                Config config    = param_dy.config;
                Config oldConfig = param_dy.oldConfig;

                if (config != null)
                {
                    if (config.OnlineStatus == OnlineStatus.Online)
                    {
                        //notice clients
                        var action = new WebsocketAction
                        {
                            Action = ActionConst.Update,
                            Item   = new ConfigItem {
                                group = config.Group, key = config.Key, value = config.Value
                            },
                            OldItem = new ConfigItem {
                                group = oldConfig.Group, key = oldConfig.Key, value = oldConfig.Value
                            }
                        };
                        var nodes = await _serverNodeService.GetAllNodesAsync();
                        foreach (var node in nodes)
                        {
                            if (node.Status == NodeStatus.Offline)
                            {
                                continue;
                            }
                            await _remoteServerNodeProxy.AppClientsDoActionAsync(node.Address, config.AppId, action);
                        }
                    }
                }
            });

            TinyEventBus.Instance.Regist(EventKeys.DELETE_CONFIG_SUCCESS, (param) =>
            {
                var config = param as Config;
                if (config != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        AppId   = config.AppId,
                        LogText = $"删除配置【Key:{config.Key}】【Value:{config.Value}】【Group:{config.Group}】【AppId:{config.AppId}】"
                    };
                    _sysLogService.AddSysLogAsync(log);
                }
            });
            TinyEventBus.Instance.Regist(EventKeys.DELETE_CONFIG_SUCCESS, async(param) =>
            {
                var config = param as Config;
                if (config != null)
                {
                    var action = await CreateRemoveWebsocketAction(config, config.AppId);
                    var nodes  = await _serverNodeService.GetAllNodesAsync();
                    foreach (var node in nodes)
                    {
                        if (node.Status == NodeStatus.Offline)
                        {
                            continue;
                        }
                        await _remoteServerNodeProxy.AppClientsDoActionAsync(node.Address, config.AppId, action);
                    }
                }
            });

            TinyEventBus.Instance.Regist(EventKeys.OFFLINE_CONFIG_SUCCESS, (param) =>
            {
                var config = param as Config;
                if (config != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        AppId   = config.AppId,
                        LogText = $"下线配置【Key:{config.Key}】【Value:{config.Value}】【Group:{config.Group}】【AppId:{config.AppId}】"
                    };
                    _sysLogService.AddSysLogAsync(log);
                }
            });
            TinyEventBus.Instance.Regist(EventKeys.OFFLINE_CONFIG_SUCCESS, async(param) =>
            {
                var config = param as Config;
                if (config != null)
                {
                    //notice clients the config item is offline
                    var action = new WebsocketAction {
                        Action = ActionConst.Remove, Item = new ConfigItem {
                            group = config.Group, key = config.Key, value = config.Value
                        }
                    };
                    var nodes = await _serverNodeService.GetAllNodesAsync();
                    foreach (var node in nodes)
                    {
                        if (node.Status == NodeStatus.Offline)
                        {
                            continue;
                        }
                        await _remoteServerNodeProxy.AppClientsDoActionAsync(node.Address, config.AppId, action);
                    }
                }
            });


            TinyEventBus.Instance.Regist(EventKeys.PUBLISH_CONFIG_SUCCESS, (param) =>
            {
                Config config = param as Config;

                if (config != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        AppId   = config.AppId,
                        LogText = $"上线配置【Key:{config.Key}】【Value:{config.Value}】【Group:{config.Group}】【AppId:{config.AppId}】"
                    };
                    _sysLogService.AddSysLogAsync(log);
                }
            });
            TinyEventBus.Instance.Regist(EventKeys.PUBLISH_CONFIG_SUCCESS, async(param) =>
            {
                Config config = param as Config;

                if (config != null)
                {
                    if (config != null && config.OnlineStatus == OnlineStatus.Online)
                    {
                        //notice clients config item is published
                        var action = new WebsocketAction
                        {
                            Action = ActionConst.Add,
                            Item   = new ConfigItem {
                                group = config.Group, key = config.Key, value = config.Value
                            }
                        };
                        var nodes = await _serverNodeService.GetAllNodesAsync();
                        foreach (var node in nodes)
                        {
                            if (node.Status == NodeStatus.Offline)
                            {
                                continue;
                            }
                            await _remoteServerNodeProxy.AppClientsDoActionAsync(node.Address, config.AppId, action);
                        }
                    }
                }
            });


            TinyEventBus.Instance.Regist(EventKeys.ROLLBACK_CONFIG_SUCCESS, (param) =>
            {
                dynamic param_dy    = param;
                Config config       = param_dy.config;
                ModifyLog modifyLog = param_dy.modifyLog;

                if (config != null && modifyLog != null)
                {
                    var log = new SysLog
                    {
                        LogTime = DateTime.Now,
                        LogType = SysLogType.Normal,
                        AppId   = config.AppId,
                        LogText = $"回滚配置【Key:{config.Key}】 【Group:{config.Group}】 【AppId:{config.AppId}】至历史记录:{modifyLog.Id}"
                    };
                    _sysLogService.AddSysLogAsync(log);

                    _modifyLogService.AddAsync(new ModifyLog
                    {
                        Id         = Guid.NewGuid().ToString("N"),
                        ConfigId   = config.Id,
                        Key        = config.Key,
                        Group      = config.Group,
                        Value      = config.Value,
                        ModifyTime = config.UpdateTime.Value
                    });
                }
            });
            TinyEventBus.Instance.Regist(EventKeys.ROLLBACK_CONFIG_SUCCESS, async(param) =>
            {
                dynamic param_dy = param;
                Config config    = param_dy.config;
                Config oldConfig = param_dy.oldConfig;

                ModifyLog modifyLog = param_dy.modifyLog;

                if (config != null && oldConfig != null)
                {
                    if (config.OnlineStatus == OnlineStatus.Online)
                    {
                        //notice clients
                        var action = new WebsocketAction
                        {
                            Action = ActionConst.Update,
                            Item   = new ConfigItem {
                                group = config.Group, key = config.Key, value = config.Value
                            },
                            OldItem = new ConfigItem {
                                group = oldConfig.Group, key = oldConfig.Key, value = oldConfig.Value
                            }
                        };
                        var nodes = await _serverNodeService.GetAllNodesAsync();
                        foreach (var node in nodes)
                        {
                            if (node.Status == NodeStatus.Offline)
                            {
                                continue;
                            }
                            await _remoteServerNodeProxy.AppClientsDoActionAsync(node.Address, config.AppId, action);
                        }
                    }
                }
            });
        }