private static void OnChanged(object source, FileSystemEventArgs e)
        {
            SVNHelper.htpasswdRead();

            foreach (var item in ProgramSetting.APIConfig)
            {
                bool is_Success = false;
                int  reTryCount = 3;
                if (is_Success == false && reTryCount > 0)
                {
                    // 加密发送消息
                    JsonPostModel jpm = new JsonPostModel();
                    jpm.Agent    = JsonPostModelAgent.Client.ToString();
                    jpm.DataType = JsonPostModelDataType.AllAuthInfo.ToString();
                    jpm.Data     = htpasswdUserAndPassword.UsersTable;
                    is_Success   = WebFunctionHelper.PostToAPI(EncryptsAndDecryptsHelper.Encrypt(JsonConvert.SerializeObject(jpm), item.SecretKey, item.IV), item.APIurl, item.SecretKey, item.IV);
                    if (is_Success)
                    {
                        is_Success = true;
                    }
                    else
                    {
                        LogHelper.WriteLog("发送Repo更新信息出现错误", LogHelper.Log4NetLevel.Error);
                        reTryCount--;
                        Thread.Sleep(3000);
                    }
                }
                else
                {
                    LogHelper.WriteLog("发送Repo更新信息重试3次之后依旧出错!", LogHelper.Log4NetLevel.Error);
                }
            }
        }
        // 文件/文件夹删除处理
        private static void OnDeleted(object source, FileSystemEventArgs e)
        {
            // 如果是目录被删除则为Repo被删除
            var dirName = FileHelper.GetGrampaDirectoryName(e.FullPath);

            if (File.GetAttributes(e.FullPath.Replace(e.Name, "")) == FileAttributes.Directory)
            {
                //删除这个Repo
                RepoDataSourcePermission.RepoPermissons.Remove(dirName);
                LogHelper.WriteLog(e.FullPath + "\n repo 被删除", LogHelper.Log4NetLevel.Debug);

                foreach (var item in ProgramSetting.APIConfig)
                {
                    bool is_Success = false;
                    int  reTryCount = 3;
                    if (is_Success == false && reTryCount > 0)
                    {
                        // 删除repo
                        DelRepo dr = new DelRepo();
                        dr.RepoName = dirName;
                        // 加密发送消息
                        JsonPostModel jpm = new JsonPostModel();
                        jpm.Agent    = JsonPostModelAgent.Client.ToString();
                        jpm.DataType = JsonPostModelDataType.DelRepo.ToString();
                        jpm.Data     = dr;
                        is_Success   = WebFunctionHelper.PostToAPI(EncryptsAndDecryptsHelper.Encrypt(JsonConvert.SerializeObject(jpm), item.SecretKey, item.IV), item.APIurl, item.SecretKey, item.IV);
                        if (is_Success)
                        {
                            is_Success = true;
                        }
                        else
                        {
                            LogHelper.WriteLog("发送删除Repo信息出现错误", LogHelper.Log4NetLevel.Error);
                            reTryCount--;
                            Thread.Sleep(3000);
                        }
                    }
                    else
                    {
                        LogHelper.WriteLog("发送删除Repo信息重试3次之后依旧出错!", LogHelper.Log4NetLevel.Error);
                    }
                }
            }
            else
            {
                Console.WriteLine("文件被删除");
            }
        }
        // 文件/文件夹改变处理
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // e.FullPath 是FileInfo类型 直接使用可输出完整路径
            var change = SVNHelper.ReadRepositoriesFile(e.FullPath);

            // 将变化写入
            SVNHelper.MergeRepoDictionary(RepoDataSourcePermission.RepoPermissons, change);
            LogHelper.WriteLog(e.FullPath + "\n 文件发生变化", LogHelper.Log4NetLevel.Debug);

            foreach (var item in ProgramSetting.APIConfig)
            {
                bool is_Success = false;
                int  reTryCount = 3;
                if (is_Success == false && reTryCount > 0)
                {
                    // 加密发送消息
                    JsonPostModel jpm = new JsonPostModel();
                    jpm.Agent    = JsonPostModelAgent.Client.ToString();
                    jpm.DataType = JsonPostModelDataType.ChangeRepo.ToString();
                    jpm.Data     = change;
                    is_Success   = WebFunctionHelper.PostToAPI(EncryptsAndDecryptsHelper.Encrypt(JsonConvert.SerializeObject(jpm), item.SecretKey, item.IV), item.APIurl, item.SecretKey, item.IV);
                    if (is_Success)
                    {
                        is_Success = true;
                    }
                    else
                    {
                        LogHelper.WriteLog("发送Repo更新信息出现错误", LogHelper.Log4NetLevel.Error);
                        reTryCount--;
                        Thread.Sleep(3000);
                    }
                }
                else
                {
                    LogHelper.WriteLog("发送Repo更新信息重试3次之后依旧出错!", LogHelper.Log4NetLevel.Error);
                }
            }
        }
Example #4
0
        public static void MainRunFunction()
        {
            string encryptsJson = "";

            // 初始化 读取所有仓库的信息
            try
            {
                SVNHelper.ReadRepositoriesAll();
                LogHelper.WriteLog("仓库信息初始化完成", LogHelper.Log4NetLevel.Info);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("初始化仓库出现错误\n" + ex, LogHelper.Log4NetLevel.Error);
            }

            foreach (var item in ProgramSetting.APIConfig)
            {
                JsonPostModel jpm = new JsonPostModel();
                jpm.Agent    = JsonPostModelAgent.Client.ToString();
                jpm.DataType = JsonPostModelDataType.AllUserTable.ToString();
                jpm.Data     = RepoDataSourcePermission.RepoPermissons;
                encryptsJson = EncryptsAndDecryptsHelper.Encrypt(JsonConvert.SerializeObject(jpm), item.SecretKey, item.IV);
                if (!WebFunctionHelper.PostToAPI(encryptsJson, item.APIurl, item.SecretKey, item.IV))
                {
                    LogHelper.WriteLog("初始化时发送所有仓库信息出现错误", LogHelper.Log4NetLevel.Error);
                }

                // 初始化 读取用户组信息
                try
                {
                    groupList.userGroup = SVNHelper.GetAllUserAndGroup();
                    LogHelper.WriteLog("用户与用户组信息初始化完成", LogHelper.Log4NetLevel.Info);
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog("初始化用户信息出现错误\n" + ex, LogHelper.Log4NetLevel.Error);
                }

                // 初始化 读取所有用户的密码表
                try
                {
                    SVNHelper.htpasswdRead();
                    LogHelper.WriteLog("用户密码表信息初始化完成", LogHelper.Log4NetLevel.Info);
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog("初始化用户密码表信息出现错误\n" + ex, LogHelper.Log4NetLevel.Error);
                }

                jpm.Agent    = JsonPostModelAgent.Client.ToString();
                jpm.DataType = JsonPostModelDataType.AllAuthInfo.ToString();
                jpm.Data     = htpasswdUserAndPassword.UsersTable;
                encryptsJson = EncryptsAndDecryptsHelper.Encrypt(JsonConvert.SerializeObject(jpm), item.SecretKey, item.IV);
                if (!WebFunctionHelper.PostToAPI(encryptsJson, item.APIurl, item.SecretKey, item.IV))
                {
                    LogHelper.WriteLog("初始化时发送用户密码信息出现错误", LogHelper.Log4NetLevel.Error);
                }
            }



            LogHelper.WriteLog("启动文件监控", LogHelper.Log4NetLevel.Info);

            // 启动仓库权限文件监控
            try
            {
                SVNFileWatcherHelper fwh = new SVNFileWatcherHelper();
                fwh.WatcherStrat(ProgramSetting.Repositoriespath, "VisualSVN-SvnAuthz.ini");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("启动 VisualSVN-SvnAuthz.ini 监控出现错误: " + ex.ToString(), LogHelper.Log4NetLevel.Error);
                throw;
            }

            // 启动htpasswd文件监控
            try
            {
                htpasswdWatcher hw = new htpasswdWatcher();
                hw.WatcherStrat(ProgramSetting.Repositoriespath, "htpasswd");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("启动 htpasswd 监控出现错误: " + ex.ToString(), LogHelper.Log4NetLevel.Error);
                throw;
            }

            // 启动用户组文件监控
            try
            {
                groupFileWatcher gfw = new groupFileWatcher();
                gfw.WatcherStrat(ProgramSetting.Repositoriespath, "groups.conf");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("启动 groups.conf 监控出现错误: " + ex.ToString(), LogHelper.Log4NetLevel.Error);
                throw;
            }

            RemoteCommandMonitoring.CheckRemoteCommand();


            Console.WriteLine("程序启动");
            LogHelper.WriteLog("程序启动完成.", LogHelper.Log4NetLevel.Info);
        }
Example #5
0
        public async Task <string> Process(JObject json)
        {
            //Serialize given json to model

            JsonPostModel model = json.ToObject <JsonPostModel>();
            //Get Api key from request headers
            var api_key = Request.Headers.Where(x => x.Key == "api_key").FirstOrDefault();

            if (api_key.Value != null)
            {
                string process = "";
                string method  = "";
                string version = "";

                //Check mandatory values
                if (model.process == null)
                {
                    HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Mandatory parameter missing: process");
                    throw new HttpResponseException(response);
                }
                else
                {
                    //Get process to be called from model
                    process = model.process;
                }
                if (model.method == null)
                {
                    HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Mandatory parameter missing: method");
                    throw new HttpResponseException(response);
                }
                else
                {
                    //Get process method from model
                    method = model.method;
                }
                if (model.version == null)
                {
                    HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Mandatory parameter missing: version");
                    throw new HttpResponseException(response);
                }
                else
                {
                    //Get Version
                    version = model.version;
                }



                //Create Resthelper
                RestHelper <object> rh = new RestHelper <object>();

                //Get Process info from API Discovery
                var data = await rh.GetConfig(process, method, api_key.Value.FirstOrDefault().ToString(), "1.0");

                if (data == null)
                {
                    //If process was not found, or process is not activated, return bad request
                    HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Process not found.");
                    throw new HttpResponseException(response);
                }

                else
                {
                    //Get process url from API Data
                    var url = data.api_url;
                    //Call Process
                    var response = await rh.CallPostProcess(data.name, version, method, model.parameters, url, api_key.Value.FirstOrDefault().ToString());

                    //Return Data to APP
                    return(response);
                }
            }
            else
            {
                //API Key missing, returning bad request
                HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "API key missing.");
                throw new HttpResponseException(response);
            }
        }