using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; namespace Netch.Utils.HttpProxyHandler { /// /// 提供PAC功能支持 /// class PACListHandle { public event EventHandler UpdateCompleted; public class ResultEventArgs : EventArgs { public bool Success; public ResultEventArgs(bool success) { Success = success; } } private const string GFWLIST_URL = "https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"; private static readonly IEnumerable IgnoredLineBegins = new[] { '!', '[' }; /// /// 更新PAC文件(GFWList) /// //public void UpdatePACFromGFWList() //{ // WebClient http = new WebClient(); // http.DownloadStringCompleted += http_DownloadStringCompleted; // http.DownloadStringAsync(new Uri(GFWLIST_URL)); //} //private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) //{ // try // { // File.WriteAllText(NUtils.GetTempPath("gfwlist.txt"), e.Result, Encoding.UTF8); // List lines = ParseResult(e.Result); // string abpContent = NUtils.UnGzip(Resources.abp_js); // abpContent = abpContent.Replace("__RULES__", JsonConvert.SerializeObject(lines, Formatting.Indented)); // File.WriteAllText(NUtils.GetPath("pac.txt"), abpContent, Encoding.UTF8); // if (UpdateCompleted != null) UpdateCompleted(this, new ResultEventArgs(true)); // } // catch (Exception ex) // { // Logging.Error("http_DownloadStringCompleted():" + ex.Message); // } //} //public static List ParseResult(string response) //{ // byte[] bytes = Convert.FromBase64String(response); // string content = Encoding.ASCII.GetString(bytes); // List valid_lines = new List(); // using (var sr = new StringReader(content)) // { // foreach (var line in sr.NonWhiteSpaceLines()) // { // if (line.BeginWithAny(IgnoredLineBegins)) // continue; // valid_lines.Add(line); // } // } // return valid_lines; //} } }