From cb074528f4278bedb0a2a4b1ea28e64e12b9b4fe Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Fri, 13 Dec 2019 09:49:17 +0800 Subject: [PATCH] up pac --- v2rayN/v2rayN/Forms/MainForm.cs | 19 ++-- v2rayN/v2rayN/Forms/MainForm.resx | 3 + v2rayN/v2rayN/Forms/MainForm.zh-Hans.resx | 2 +- v2rayN/v2rayN/Handler/DownloadHandle.cs | 47 +++++++++- v2rayN/v2rayN/Handler/V2rayConfigHandler.cs | 2 + .../v2rayN/HttpProxyHandler/PACListHandle.cs | 93 ------------------- v2rayN/v2rayN/v2rayN.csproj | 1 - 7 files changed, 65 insertions(+), 102 deletions(-) delete mode 100644 v2rayN/v2rayN/HttpProxyHandler/PACListHandle.cs diff --git a/v2rayN/v2rayN/Forms/MainForm.cs b/v2rayN/v2rayN/Forms/MainForm.cs index 718dd74b..2556ad7f 100644 --- a/v2rayN/v2rayN/Forms/MainForm.cs +++ b/v2rayN/v2rayN/Forms/MainForm.cs @@ -11,13 +11,14 @@ using v2rayN.Mode; using v2rayN.Base; using v2rayN.Tool; using System.Diagnostics; +using v2rayN.Properties; +using Newtonsoft.Json; namespace v2rayN.Forms { public partial class MainForm : BaseForm { - private V2rayHandler v2rayHandler; - private PACListHandle pacListHandle; + private V2rayHandler v2rayHandler; private List lvSelecteds = new List(); private StatisticsHandler statistics = null; @@ -1272,13 +1273,21 @@ namespace v2rayN.Forms private void tsbCheckUpdatePACList_Click(object sender, EventArgs e) { + DownloadHandle pacListHandle = null; if (pacListHandle == null) { - pacListHandle = new PACListHandle(); + pacListHandle = new DownloadHandle(); pacListHandle.UpdateCompleted += (sender2, args) => { if (args.Success) { + var result = args.Msg; + if (Utils.IsNullOrEmpty(result)) + { + return; + } + pacListHandle.GenPacFile(result); + AppendText(false, UIRes.I18N("MsgPACUpdateSuccessfully")); } else @@ -1292,7 +1301,7 @@ namespace v2rayN.Forms }; } AppendText(false, UIRes.I18N("MsgStartUpdatingPAC")); - pacListHandle.UpdatePACFromGFWList(config); + pacListHandle.WebDownloadString(config.urlGFWList); } private void tsbCheckClearPACList_Click(object sender, EventArgs e) @@ -1425,8 +1434,6 @@ namespace v2rayN.Forms downloadHandle3.WebDownloadString(url); AppendText(false, $"{hashCode}{UIRes.I18N("MsgStartGettingSubscriptions")}"); } - - } #endregion diff --git a/v2rayN/v2rayN/Forms/MainForm.resx b/v2rayN/v2rayN/Forms/MainForm.resx index 0045a460..1fa895d5 100644 --- a/v2rayN/v2rayN/Forms/MainForm.resx +++ b/v2rayN/v2rayN/Forms/MainForm.resx @@ -823,6 +823,9 @@ Simplify PAC (please set Core route) + + False + Magenta diff --git a/v2rayN/v2rayN/Forms/MainForm.zh-Hans.resx b/v2rayN/v2rayN/Forms/MainForm.zh-Hans.resx index d9fdbce0..1659d54e 100644 --- a/v2rayN/v2rayN/Forms/MainForm.zh-Hans.resx +++ b/v2rayN/v2rayN/Forms/MainForm.zh-Hans.resx @@ -432,7 +432,7 @@ 232, 22 - 检查更新PAC (需要Http代理) + 检查更新PAC 232, 22 diff --git a/v2rayN/v2rayN/Handler/DownloadHandle.cs b/v2rayN/v2rayN/Handler/DownloadHandle.cs index 7044e48e..72adfce8 100644 --- a/v2rayN/v2rayN/Handler/DownloadHandle.cs +++ b/v2rayN/v2rayN/Handler/DownloadHandle.cs @@ -1,9 +1,13 @@ -using System; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Net; +using System.Text; using v2rayN.Base; using v2rayN.Mode; +using v2rayN.Properties; namespace v2rayN.Handler { @@ -289,5 +293,46 @@ namespace v2rayN.Handler } #endregion + + #region PAC + + public string GenPacFile(string result) + { + try + { + File.WriteAllText(Utils.GetTempPath("gfwlist.txt"), result, Encoding.UTF8); + List lines = ParsePacResult(result); + string abpContent = Utils.UnGzip(Resources.abp_js); + abpContent = abpContent.Replace("__RULES__", JsonConvert.SerializeObject(lines, Formatting.Indented)); + File.WriteAllText(Utils.GetPath(Global.pacFILE), abpContent, Encoding.UTF8); + } + catch (Exception ex) + { + Utils.SaveLog(ex.Message, ex); + return ex.Message; + } + return string.Empty; + } + + private List ParsePacResult(string response) + { + IEnumerable IgnoredLineBegins = new[] { '!', '[' }; + + byte[] bytes = Convert.FromBase64String(response); + string content = Encoding.UTF8.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; + } + + #endregion } } diff --git a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs index fcf07549..60b9bdff 100644 --- a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs @@ -403,6 +403,8 @@ namespace v2rayN.Handler //远程服务器地址和端口 serversItem.address = config.address(); serversItem.port = config.port(); + serversItem.method = null; + serversItem.password = null; if (!Utils.IsNullOrEmpty(config.security()) && !Utils.IsNullOrEmpty(config.id())) diff --git a/v2rayN/v2rayN/HttpProxyHandler/PACListHandle.cs b/v2rayN/v2rayN/HttpProxyHandler/PACListHandle.cs deleted file mode 100644 index 85a53c14..00000000 --- a/v2rayN/v2rayN/HttpProxyHandler/PACListHandle.cs +++ /dev/null @@ -1,93 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Net; -using System.Text; -using Newtonsoft.Json; -using v2rayN.Base; -using v2rayN.Mode; -using v2rayN.Properties; - -namespace v2rayN.HttpProxyHandler -{ - /// - /// 提供PAC功能支持 - /// - class PACListHandle - { - public event EventHandler UpdateCompleted; - - public event ErrorEventHandler Error; - - public class ResultEventArgs : EventArgs - { - public bool Success; - - public ResultEventArgs(bool success) - { - this.Success = success; - } - } - - - - private static readonly IEnumerable IgnoredLineBegins = new[] { '!', '[' }; - - public void UpdatePACFromGFWList(Config config) - { - string url = Global.GFWLIST_URL; - if (!Utils.IsNullOrEmpty(config.urlGFWList)) - { - url = config.urlGFWList; - } - - //默认用户已开启系统代理 - //var httpProxy = config.inbound.FirstOrDefault(x => x.protocol=="http"); - //if (httpProxy == null) - //{ - // throw new Exception("未发现HTTP代理,无法设置代理更新"); - //} - var http = new WebClientEx(); - //http.Headers.Add("Connection", "Close"); - //http.Proxy = new WebProxy(IPAddress.Loopback.ToString(), httpProxy.localPort); - http.DownloadStringCompleted += http_DownloadStringCompleted; - http.DownloadStringAsync(new Uri(url)); - } - - private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) - { - try - { - File.WriteAllText(Utils.GetTempPath("gfwlist.txt"), e.Result, Encoding.UTF8); - List lines = ParseResult(e.Result); - string abpContent = Utils.UnGzip(Resources.abp_js); - abpContent = abpContent.Replace("__RULES__", JsonConvert.SerializeObject(lines, Formatting.Indented)); - File.WriteAllText(Utils.GetPath(Global.pacFILE), abpContent, Encoding.UTF8); - if (UpdateCompleted != null) UpdateCompleted(this, new ResultEventArgs(true)); - } - catch (Exception ex) - { - Utils.SaveLog(ex.Message, ex); - - if (Error != null) Error(this, new ErrorEventArgs(ex)); - } - } - - public static List ParseResult(string response) - { - byte[] bytes = Convert.FromBase64String(response); - string content = Encoding.UTF8.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; - } - } -} diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index d3561b38..b9bb5a70 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -187,7 +187,6 @@ -