Browse Source

up pac

pull/354/head
2dust 5 years ago
parent
commit
cb074528f4
  1. 19
      v2rayN/v2rayN/Forms/MainForm.cs
  2. 3
      v2rayN/v2rayN/Forms/MainForm.resx
  3. 2
      v2rayN/v2rayN/Forms/MainForm.zh-Hans.resx
  4. 47
      v2rayN/v2rayN/Handler/DownloadHandle.cs
  5. 2
      v2rayN/v2rayN/Handler/V2rayConfigHandler.cs
  6. 93
      v2rayN/v2rayN/HttpProxyHandler/PACListHandle.cs
  7. 1
      v2rayN/v2rayN/v2rayN.csproj

19
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<int> lvSelecteds = new List<int>();
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

3
v2rayN/v2rayN/Forms/MainForm.resx

@ -823,6 +823,9 @@
<data name="tsbCheckClearPACList.Text" xml:space="preserve">
<value>Simplify PAC (please set Core route)</value>
</data>
<data name="tsbCheckClearPACList.Visible" type="System.Boolean, mscorlib">
<value>False</value>
</data>
<data name="tsbCheckUpdate.ImageTransparentColor" type="System.Drawing.Color, System.Drawing">
<value>Magenta</value>
</data>

2
v2rayN/v2rayN/Forms/MainForm.zh-Hans.resx

@ -432,7 +432,7 @@
<value>232, 22</value>
</data>
<data name="tsbCheckUpdatePACList.Text" xml:space="preserve">
<value>检查更新PAC (需要Http代理)</value>
<value>检查更新PAC</value>
</data>
<data name="tsbCheckClearPACList.Size" type="System.Drawing.Size, System.Drawing">
<value>232, 22</value>

47
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<string> 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<string> ParsePacResult(string response)
{
IEnumerable<char> IgnoredLineBegins = new[] { '!', '[' };
byte[] bytes = Convert.FromBase64String(response);
string content = Encoding.UTF8.GetString(bytes);
List<string> valid_lines = new List<string>();
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
}
}

2
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()))

93
v2rayN/v2rayN/HttpProxyHandler/PACListHandle.cs

@ -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
{
/// <summary>
/// 提供PAC功能支持
/// </summary>
class PACListHandle
{
public event EventHandler<ResultEventArgs> UpdateCompleted;
public event ErrorEventHandler Error;
public class ResultEventArgs : EventArgs
{
public bool Success;
public ResultEventArgs(bool success)
{
this.Success = success;
}
}
private static readonly IEnumerable<char> 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<string> 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<string> ParseResult(string response)
{
byte[] bytes = Convert.FromBase64String(response);
string content = Encoding.UTF8.GetString(bytes);
List<string> valid_lines = new List<string>();
using (var sr = new StringReader(content))
{
foreach (var line in sr.NonWhiteSpaceLines())
{
if (line.BeginWithAny(IgnoredLineBegins))
continue;
valid_lines.Add(line);
}
}
return valid_lines;
}
}
}

1
v2rayN/v2rayN/v2rayN.csproj

@ -187,7 +187,6 @@
<Compile Include="Base\HttpWebServer.cs" />
<Compile Include="Base\HttpWebServerB.cs" />
<Compile Include="HttpProxyHandler\PrivoxyHandler.cs" />
<Compile Include="HttpProxyHandler\PACListHandle.cs" />
<Compile Include="HttpProxyHandler\PACServerHandle.cs" />
<Compile Include="HttpProxyHandler\ProxySetting.cs" />
<Compile Include="HttpProxyHandler\HttpProxyHandle.cs" />

Loading…
Cancel
Save