/// <returns></returns> private string SendFileInfo(string localurl, string filename, string filesize, string serverDirKey) { long requestId = CommonUtil.GetRandomRequiredRequestID(); string requestjson = "{\"localurl\":\"" + localurl + "\",\"filename\":\"" + filename + "\",\"filesize\":\"" + filesize + "\",\"requestid\":\"" + requestId + "\",\"serverDirKey\":\"" + serverDirKey + "\",\"message\":\"success\",\"headingcode\":\"3f74a7cf0b351cbaaf4875f7331bc25d\"}"; string response = SocketUtil.socket(ClientCache.GetCurrentServerIP(), Convert.ToInt32(ClientCache.GetInterflowPort()), requestjson); JObject json = new JSONUtil(response).GetJObject(); return(json["message"].ToString()); }
private string GetPrivateKey() { long requestid = CommonUtil.GetRandomRequiredRequestID(); //请求服务器获得本次操作授权的秘钥 string requestjson = "{\"requestid\":\"" + requestid + "\",\"message\":\"success\",\"headingcode\":\"4f79a3158d691c5a7c1cda522763a247\"}"; string response = SocketUtil.socket(ClientCache.GetCurrentServerIP(), Convert.ToInt32(ClientCache.GetInterflowPort()), requestjson); JObject json = new JSONUtil(response).GetJObject(); return(json["message"].ToString()); }
/// <summary> /// 远程项目备份按钮点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button7_Click(object sender, EventArgs e) { try { //检查是否选中了服务项 string servername = cb_remoteServer.Text; if (servername == null || string.Empty.Equals(servername) || cb_remoteServer.SelectedIndex == 0) { MessageBox.Show("请先指定需要备份的远程项目!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); cb_remoteServer.Focus(); return; } //禁用此按钮,避免重复请求. 此时还应当禁用MD5校验,和覆盖上传按钮 button7.Enabled = false; button2.Enabled = false; button1.Enabled = false; //因为线上文件一般较大,因此在用户进行此操作时应当做适当的提醒 DialogResult result = MessageBox.Show("由于线上项目文件较大,因此备份(ZIP)过程会因为比较漫长而导致页面假死. 请耐心等待", "备份", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk); if (result == DialogResult.OK) { string requestjson = "{\"requestid\":\"" + CommonUtil.GetRandomRequiredRequestID() + "\",\"message\":\"" + servername + "\",\"headingcode\":\"ed9ea8907f1963430bc8be4689d446b4\"}"; string response = SocketUtil.socket(ClientCache.GetCurrentServerIP(), Convert.ToInt32(ClientCache.GetInterflowPort()), requestjson); JSONUtil json = new JSONUtil(response); string message = Convert.ToString(json.GetJObject()["message"]); if ("failed".Equals(message)) { MessageBox.Show(servername + "压缩失败,请通过查看前后端日志以了解或解决此问题!"); } else { MessageBox.Show("压缩成功,文件已保存至服务器:" + message); } } button7.Enabled = true; button2.Enabled = true; button1.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Comparison_Load(object sender, EventArgs e) { try { //初始化远程服务器项目列表到下拉列表 string json = "{\"requestid\":100011111,\"message\":\"这是客户端传入的message\",\"headingcode\":\"b1bb0e5951a6a8f3ff7524cd0ad92ff1\"}"; string response = SocketUtil.socket(ClientCache.GetCurrentServerIP(), Convert.ToInt32(ClientCache.GetInterflowPort()), json); JSONUtil jsonUtil = new JSONUtil(response); JObject jObject = jsonUtil.GetJObject(); JArray array = (JArray)jObject["data"]["projects"]; cb_remoteServer.Items.Add("-- 请选择服务器项目 --"); for (int i = 0; i < array.Count; i++) { cb_remoteServer.Items.Add(array[i]["name"].ToString()); } cb_remoteServer.SelectedIndex = 0; } catch (Exception ex) { MessageBox.Show("获取服务器端在线列表出现错误" + ex.Message, "在线获取", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// MD5比对 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { listView1.Items.Clear(); ControlUtil.ClearComparisons(); string remoteServer = cb_remoteServer.Text; if (remoteServer == null || remoteServer == "" || cb_remoteServer.SelectedIndex == 0) { MessageBox.Show("请先选择远程服务器!"); cb_remoteServer.Focus(); return; } if (listBox1.Items.Count <= 0) { MessageBox.Show("请先选择本地项目文件,可直接拖放!"); return; } DialogResult result = MessageBox.Show("此操作耗时较长,可能会因为长时间阻塞导致页面无响应. 计算过程中请勿关闭窗口! 点击\"取消\"可撤回此操作. ", "MD5计算", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (result == DialogResult.OK) { //计算过程中先禁用此按钮,防止重复产生点击事件 button2.Text = "MD5(计算中...)"; button2.Enabled = false; List <Dictionary <string, string> > clientMD5s = new List <Dictionary <string, string> >(); for (int i = 0; i < listBox1.Items.Count; i++) { listBox1.SelectedIndex = i; string listTag = listBox1.Tag.ToString(); string path = listBox1.Items[i].ToString(); FileInfo fileinfo = new FileInfo(path); long size = fileinfo.Length; string md5Value = MD5Util.GetMD5HashFromFile(path); Dictionary <string, string> map = new Dictionary <string, string>(); path = path.Replace(listTag, "").Trim(); map.Add("md5", md5Value); map.Add("url", path); map.Add("size", Convert.ToString(size)); clientMD5s.Add(map); } //请求服务器计算服务端MD5值 string serverResponse = SocketUtil.SocketMD5(ClientCache.GetCurrentServerIP(), Convert.ToInt32(ClientCache.GetInterflowPort()), JSONUtil.RequestServerMD5InfoJsonStr(remoteServer)); List <Dictionary <string, string> > serverMD5s = JSONUtil.ParseServerMD5Info(serverResponse); List <string> usedList = new List <string>(); int count = 1; //开始比对和生成记录到ListView控件中,以本地项目为参照物. 因为一般情况下本地项目的文件会多于服务器端 foreach (Dictionary <string, string> client in clientMD5s)//Loop client { string clientUrl = client["url"]; string clientMd5 = client["md5"]; string clientSize = client["size"]; foreach (Dictionary <string, string> server in serverMD5s)//Loop server { string serverUrl = server["url"]; string serverMd5 = server["md5"]; string serverSize = server["size"]; //这一个替换在操作线上代码时格外重要! 线上Linux是/而windows是\\ serverUrl = serverUrl.Replace("/", "\\");//Linux. Windows下注释此代码 if (clientUrl.Equals(serverUrl)) { Dictionary <string, string> map = new Dictionary <string, string>(); map.Add("num", Convert.ToString(count++)); map.Add("urlremote", serverUrl); map.Add("urllocal", clientUrl); map.Add("sizeremote", serverSize); map.Add("sizelocal", clientSize); //存在相同的url,即文件 . 比对MD5值是否一致 if (clientMd5.Equals(serverMd5)) { //MD5也一致,视为没有任何更改 map.Add("result", "一致"); ControlUtil.AddListViewLineOfComparison(map, Color.White, "一致"); usedList.Add(clientUrl);//将客户端本数据标记为已匹配过 } else { //客户端和服务端同一个文件MD5不一致,视为客户端有更改. 需要后续上传 map.Add("result", "不一致"); ControlUtil.AddListViewLineOfComparison(map, Color.Red, "不一致"); usedList.Add(clientUrl);//将客户端本数据标记为已匹配过 } } } } //检查客户端剩下的没有检查过的数据,一般视为本地新增的数据 foreach (Dictionary <string, string> client in clientMD5s) { string clientUrl = client["url"]; string clientMd5 = client["md5"]; string clientSize = client["size"]; if (usedList.Contains(clientUrl)) { continue; } Dictionary <string, string> map = new Dictionary <string, string>(); map.Add("num", Convert.ToString(count++)); map.Add("urlremote", ""); map.Add("urllocal", clientUrl); map.Add("sizeremote", "0"); map.Add("sizelocal", clientSize); map.Add("result", "新增"); ControlUtil.AddListViewLineOfComparison(map, Color.Green, "新增"); } ControlUtil.RefueshToControlComparisons(listView1, "all"); button2.Text = "MD5"; button2.Enabled = true; } }