mirror of
https://github.com/netchx/netch.git
synced 2026-05-07 22:44:03 +08:00
✨PAC
This commit is contained in:
@@ -34,7 +34,6 @@ namespace Netch.Utils.HttpProxyHandler
|
||||
|
||||
_responderMethod = method;
|
||||
_listener.Start();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -43,7 +42,9 @@ namespace Netch.Utils.HttpProxyHandler
|
||||
}
|
||||
|
||||
public HttpWebServer(Func<HttpListenerRequest, string> method, params string[] prefixes)
|
||||
: this(prefixes, method) { }
|
||||
: this(prefixes, method)
|
||||
{
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
@@ -68,7 +69,7 @@ namespace Netch.Utils.HttpProxyHandler
|
||||
}
|
||||
catch
|
||||
{
|
||||
} // suppress any exceptions
|
||||
} // suppress any exceptions
|
||||
finally
|
||||
{
|
||||
// always close the stream
|
||||
@@ -94,6 +95,5 @@ namespace Netch.Utils.HttpProxyHandler
|
||||
_listener = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace Netch.Utils.HttpProxyHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// 提供PAC功能支持
|
||||
/// </summary>
|
||||
class PACListHandle
|
||||
{
|
||||
public event EventHandler<ResultEventArgs> 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<char> IgnoredLineBegins = new[] { '!', '[' };
|
||||
|
||||
/// <summary>
|
||||
/// 更新PAC文件(GFWList)
|
||||
/// </summary>
|
||||
//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<string> 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<string> ParseResult(string response)
|
||||
//{
|
||||
// byte[] bytes = Convert.FromBase64String(response);
|
||||
// string content = Encoding.ASCII.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;
|
||||
//}
|
||||
}
|
||||
}
|
||||
56
Netch/Utils/HttpProxyHandler/PACUtil.cs
Normal file
56
Netch/Utils/HttpProxyHandler/PACUtil.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Netch.Forms;
|
||||
using Netch.Properties;
|
||||
|
||||
namespace Netch.Utils.HttpProxyHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// 提供PAC功能支持
|
||||
/// </summary>
|
||||
class PACUtil
|
||||
{
|
||||
private static readonly IEnumerable<char> IgnoredLineBegins = new[] {'!', '['};
|
||||
|
||||
public static List<string> ParseResult(string response)
|
||||
{
|
||||
byte[] bytes = Convert.FromBase64String(response);
|
||||
string content = Encoding.ASCII.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;
|
||||
}
|
||||
|
||||
public static string UnGzip(byte[] buf)
|
||||
{
|
||||
byte[] buffer = new byte[1024];
|
||||
int n;
|
||||
using (MemoryStream sb = new MemoryStream())
|
||||
{
|
||||
using (GZipStream input = new GZipStream(new MemoryStream(buf),
|
||||
CompressionMode.Decompress,
|
||||
false))
|
||||
{
|
||||
while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
|
||||
{
|
||||
sb.Write(buffer, 0, n);
|
||||
}
|
||||
}
|
||||
return System.Text.Encoding.UTF8.GetString(sb.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user