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);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 发送数据到服务器的方法
        /// </summary>
        /// <param name="jsonTxt">传入 Json 文本</param>
        /// <param name="url">API 地址</param>
        /// <returns>返回是否成功</returns>
        public static bool PostToAPI(string jsonTxt, string url, string SecretKey, string IV)
        {
            bool is_success     = false;
            var  httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";
            try
            {
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string postJSON = $"{{\"Data\":\"{jsonTxt}\"}}";
                    streamWriter.Write(postJSON);
                    streamWriter.Flush();
                    streamWriter.Close();
                }


                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    var resp   = EncryptsAndDecryptsHelper.Decrypt(result, SecretKey, IV);
                    try
                    {
                        var reader = JsonConvert.DeserializeObject <JsonResponse>(resp);
                        if (reader != null)
                        {
                            is_success = Convert.ToBoolean(reader.status);
                            return(is_success);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteLog("服务端返回状态有误,可能是密钥不对:\n" + ex.ToString(), LogHelper.Log4NetLevel.Error);
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex.ToString());
#endif

                LogHelper.WriteLog("连接API出现错误:" + ex.ToString(), LogHelper.Log4NetLevel.Error);
            }



            return(is_success);
        }
        // 文件/文件夹删除处理
        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);
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// 接收命令
        /// </summary>
        /// <param name="jsonTxt">传入 Json 文本</param>
        /// <param name="url">API 地址</param>
        /// <returns>返回值</returns>
        public static List <RemoteCommand> GetCmd(string jsonTxt, string url, string SecretKey, string IV)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";
            try
            {
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string postJSON = $"{{\"Data\":\"{jsonTxt}\"}}";
                    streamWriter.Write(postJSON);
                    streamWriter.Flush();
                    streamWriter.Close();
                }


                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    var resp   = EncryptsAndDecryptsHelper.Decrypt(result, SecretKey, IV);

                    if (resp != "{'CommandType':'NoCommand'}")
                    {
                        LogHelper.WriteLog(resp);
                        try
                        {
                            string[] r = resp.Split('|');

                            List <RemoteCommand> rm = new List <RemoteCommand>();
                            foreach (var item in r)
                            {
                                if (!string.IsNullOrEmpty(item))
                                {
                                    rm.Add(JsonConvert.DeserializeObject <RemoteCommand>(item));
                                }
                            }
                            if (rm != null)
                            {
                                return(rm);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.WriteLog("服务端返回有误,可能是密钥不对:\n" + ex.ToString(), LogHelper.Log4NetLevel.Error);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex.ToString());
#endif

                LogHelper.WriteLog("连接API出现错误:" + ex.ToString(), LogHelper.Log4NetLevel.Error);
            }



            return(null);
        }