예제 #1
0
        private string GetLastBackUp(string url, string dbName, string destination)
        {
            ShowMessage?.Invoke("Checking for existing backup file...");
            var           checkUrl = url.AppendPathSegment(ApiUrl).AppendPathSegment(CheckFileMethod).AppendPathSegment(dbName);
            Task <string> b64      = _manager.Get(checkUrl);

            if (!string.IsNullOrEmpty(b64.Result))
            {
                var     json    = Encoding.UTF8.GetString(Convert.FromBase64String(b64.Result));
                dynamic jObject = JObject.Parse(json);
                if (jObject.result == 0)
                {
                    ShowMessage?.Invoke($"Found {dbName} backUp created at {jObject.fileInfo}");
                    var file = jObject.fileName;
                    ShowMessage?.Invoke("Start downloading...");
                    url = url.AppendPathSegment(ApiUrl).AppendPathSegment(GetFileMethod).AppendPathSegment(file);
                    var res = _manager.GetFile(url, destination);
                    if (!string.IsNullOrEmpty(res.Result))
                    {
                        ShowMessage?.Invoke(res.Result);
                        ShowBalloonTip?.Invoke(res.Result);
                    }

                    return(res.Result);
                }
                ShowMessage?.Invoke(jObject.message.ToString());
            }

            return(null);
        }
예제 #2
0
        private string CreateNewBackupRequest(string url, string dbName, string destination)
        {
            ShowMessage?.Invoke("Send request ... ");
            url = url.AppendPathSegment(ApiUrl);
            Log.InfoFormat("Send request to url {0}", url);
            Task <string> result = _manager.Post(url, dbName);

            ShowMessage?.Invoke(result.Result);

            if (!string.IsNullOrEmpty(result.Result))
            {
                dynamic jObject = JObject.Parse(result.Result);
                if (jObject.Result == 0)
                {
                    var file = jObject.File;
                    ShowMessage?.Invoke("Backup created, start downloading...");
                    url = url.AppendPathSegment(GetFileMethod).AppendPathSegment(file);
                    ShowMessage?.Invoke(url);
                    Log.InfoFormat("Try download file by url {0}", url);
                    var res = _manager.GetFile(url, destination);
                    if (!string.IsNullOrEmpty(res.Result))
                    {
                        ShowMessage?.Invoke(res.Result);
                        ShowBalloonTip?.Invoke(res.Result);
                    }

                    //return b64.Result;
                }
            }

            return(null);
        }
예제 #3
0
        private string CheckLastBackup(string url, string dbName)
        {
            ShowMessage?.Invoke("Checking last backup ...");
            url = url.AppendPathSegment(ApiUrl).AppendPathSegment(CheckFileMethod).AppendPathSegment(dbName);
            //url = string.Concat(url, CheckFileMethod, dbName);
            Task <string> b64 = _manager.Get(url);

            if (!string.IsNullOrEmpty(b64.Result))
            {
                var     json    = Encoding.UTF8.GetString(Convert.FromBase64String(b64.Result));
                dynamic jObject = JObject.Parse(json);
                if (jObject.result == 0)
                {
                    ShowMessage?.Invoke($"{dbName} created at {jObject.fileInfo}");
                    return(null);
                }

                ShowMessage?.Invoke(jObject.message.ToString());
                ShowBalloonTip?.Invoke(jObject.message.ToString());
            }

            return(null);
        }
예제 #4
0
        private void UpdatePACFile()
        {
            string gfwlist;

            try
            {
                gfwlist = File.ReadAllText(Path.Combine(directory, gfwlist_file), Encoding.UTF8);
            }
            catch
            {
                gfwlist = Resources.gfwlist;
                File.WriteAllText(Path.Combine(directory, gfwlist_file), gfwlist, Encoding.UTF8);
            }
            string userRule;
            string userPath = Path.Combine(directory, user_rule_file);

            if (!File.Exists(userPath))
            {
                userRule = Resources.user_rule;
                File.WriteAllText(Path.Combine(directory, user_rule_file), userRule, Encoding.UTF8);
            }
            else
            {
                //以只读共享方式打开文件
                using (FileStream fs = new FileStream(userPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    int    length  = (int)fs.Length;
                    byte[] content = new byte[length];
                    fs.Read(content, 0, content.Length);
                    userRule = Encoding.UTF8.GetString(content);
                }
            }
            string abpContent;

            try
            {
                abpContent = File.ReadAllText(Path.Combine(directory, abp_js_file), Encoding.UTF8);
            }
            catch
            {
                FileUtil.UnGzip(Path.Combine(directory, abp_js_file), Resources.abp_js);
                abpContent = File.ReadAllText(Path.Combine(directory, abp_js_file), Encoding.UTF8);
            }
            try
            {
                List <string> lines = FileUtil.ParseResult(gfwlist);
                using (var sr = new StringReader(userRule))
                {
                    foreach (var rule in sr.NonWhiteSpaceLines())
                    {
                        if (rule.BeginWithAny(FileUtil.IgnoredLineBegins))
                        {
                            continue;
                        }
                        lines.Add(rule);
                    }
                }
                abpContent = abpContent.Replace("__PROXY__", $"PROXY 127.0.0.1:{remotePort}");
                abpContent = abpContent.Replace("__RULES__", JsonUtil.SerializeObject(lines));
                if (File.Exists(Path.Combine(directory, pac_file)))
                {
                    string original = File.ReadAllText(Path.Combine(directory, pac_file), Encoding.UTF8);
                    if (original == abpContent)
                    {
                        return;
                    }
                }
                File.WriteAllText(Path.Combine(directory, pac_file), abpContent, Encoding.UTF8);
                BalloonTip tip = new BalloonTip()
                {
                    IconIndex = 1,
                    Message   = "Update PAC file success",
                    timeout   = 3000
                };
                ShowBalloonTip?.Invoke(this, tip);
            }
            catch (Exception)
            {
                BalloonTip tip = new BalloonTip()
                {
                    IconIndex = 1,
                    Message   = "Update PAC file fail",
                    timeout   = 3000
                };
                ShowBalloonTip?.Invoke(this, tip);
            }
        }