remove pacB

2dust 2019-09-23 17:23:39 +08:00
parent f41fe6d7a3
commit 1943757f43
5 changed files with 31 additions and 296 deletions

View File

@ -70,7 +70,7 @@ namespace v2rayN.Handler
enabled_ = config.enableStatistics;
UpdateUI = false;
updateFunc_ = update;
logPath_ = Utils.GetPath($"{Global.StatisticLogDirectory}\\");
logPath_ = Utils.GetPath(Global.StatisticLogDirectory);
Statistic = new List<Mode.ServerStatistics>();
exitFlag_ = false;
@ -282,11 +282,11 @@ namespace v2rayN.Handler
/// UP [readable string] [amount]
/// DOWN [readable string] [amount]
/// 每行每个数据空格分隔
///
try
{
var overallPath = Path.Combine(logPath_, Global.StatisticLogOverall);
if (File.Exists(overallPath))
{
try
{
using (var overallReader = new StreamReader(overallPath))
{
@ -335,20 +335,22 @@ namespace v2rayN.Handler
}
}
}
}
}
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
try
{
// 当天流量记录文件
var dailyPath = Path.Combine(logPath_, $"{DateTime.Now.ToString("yyyy-MM-dd")}.txt");
if (File.Exists(dailyPath))
{
try
{
using (var dailyReader = new StreamReader(dailyPath))
{
while (!dailyReader.EndOfStream)
@ -386,12 +388,12 @@ namespace v2rayN.Handler
}
}
}
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
}
private void DeleteExpiredLog()

View File

@ -8,9 +8,9 @@ namespace v2rayN.HttpProxyHandler
public class HttpWebServer
{
private HttpListener _listener;
private Func<HttpListenerRequest, string> _responderMethod;
private Func<string, string> _responderMethod;
public HttpWebServer(string[] prefixes, Func<HttpListenerRequest, string> method)
public HttpWebServer(string[] prefixes, Func<string, string> method)
{
try
{
@ -43,7 +43,7 @@ namespace v2rayN.HttpProxyHandler
}
}
public HttpWebServer(Func<HttpListenerRequest, string> method, params string[] prefixes)
public HttpWebServer(Func<string, string> method, params string[] prefixes)
: this(prefixes, method) { }
public void Run()
@ -60,8 +60,9 @@ namespace v2rayN.HttpProxyHandler
var ctx = c as HttpListenerContext;
try
{
string rstr = _responderMethod(ctx.Request);
byte[] buf = ASCIIEncoding.ASCII.GetBytes(rstr);
string address = ctx.Request.LocalEndPoint.Address.ToString();
string rstr = _responderMethod(address);
byte[] buf = Encoding.ASCII.GetBytes(rstr);
ctx.Response.StatusCode = 200;
ctx.Response.ContentType = "application/x-ns-proxy-autoconfig";
ctx.Response.ContentLength64 = buf.Length;

View File

@ -1,201 +0,0 @@
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace v2rayN.HttpProxyHandler
{
public class HttpWebServerB
{
private int port;
private TcpListener listener;
private Func<TcpClient, string> _responderMethod;
public HttpWebServerB(int port, Func<TcpClient, string> method)
{
try
{
this.port = port;
this._responderMethod = method;
listener = new TcpListener(new IPEndPoint(IPAddress.Any, port));
listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
listener.Start();
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
throw;
}
Utils.SaveLog("WebserverB running...");
//AsyncCallback callback = null;
//listener.BeginAcceptTcpClient(callback = ((ares) =>
//{
// try
// {
// if (listener != null)
// {
// TcpClient tcpClient = listener.EndAcceptTcpClient(ares);
// if (tcpClient != null && _responderMethod != null)
// {
// string pac = _responderMethod(tcpClient);
// NetworkStream netStream = tcpClient.GetStream();
// if (netStream.CanRead)
// {
// // Reads NetworkStream into a byte buffer.
// byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
// // Read can return anything from 0 to numBytesToRead.
// // This method blocks until at least one byte is read.
// netStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
// // Returns the data received from the host to the console.
// string returndata = Encoding.UTF8.GetString(bytes);
// if (!Utils.IsNullOrEmpty(returndata)
// && returndata.IndexOf("/pac/") >= 0
// && netStream.CanWrite)
// {
// BinaryWriter writer = new BinaryWriter(netStream);
// Byte[] sendBytes = ASCIIEncoding.ASCII.GetBytes(writeSuccess(pac) + pac);
// writer.Write(sendBytes, 0, sendBytes.Length);
// //writer.Flush();
// writer.Close();
// }
// }
// netStream.Close();
// tcpClient.Close();
// }
// }
// listener.BeginAcceptTcpClient(callback, listener);
// }
// catch (Exception ex)
// {
// Utils.SaveLog(ex.Message, ex);
// }
// //Console.WriteLine("Client connected completed");
//}), null);
}
public void Run()
{
ThreadPool.QueueUserWorkItem((o) =>
{
Utils.SaveLog("Webserver running...");
try
{
while (true)
{
ThreadPool.QueueUserWorkItem((c) =>
{
try
{
if (listener != null)
{
TcpClient tcpClient = listener.AcceptTcpClient();
if (tcpClient != null && _responderMethod != null)
{
string pac = _responderMethod(tcpClient);
NetworkStream netStream = tcpClient.GetStream();
if (netStream.CanRead)
{
// Reads NetworkStream into a byte buffer.
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
// Read can return anything from 0 to numBytesToRead.
// This method blocks until at least one byte is read.
netStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);
// Returns the data received from the host to the console.
string returndata = Encoding.UTF8.GetString(bytes);
if (!Utils.IsNullOrEmpty(returndata)
&& returndata.IndexOf("/pac/") >= 0
&& netStream.CanWrite)
{
//BinaryWriter writer = new BinaryWriter(netStream);
Byte[] sendBytes = Encoding.UTF8.GetBytes(writeSuccess(pac) );
netStream.Write(sendBytes, 0, sendBytes.Length);
sendBytes = Encoding.UTF8.GetBytes( pac);
netStream.Write(sendBytes, 0, sendBytes.Length);
//writer.Flush();
//writer.Close();
Console.WriteLine("Connection accepted22.");
}
}
netStream.Close();
tcpClient.Close();
}
}
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
});
}
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
} // suppress any exceptions
});
}
public void Stop()
{
if (listener != null)
{
listener.Stop();
listener = null;
}
}
//private static void writeSuccess(BinaryWriter writer, string pac)
//{
// writer.Write("HTTP/1.0 200 OK");
// writer.Write(Environment.NewLine);
// writer.Write("Content-Type:application/x-ns-proxy-autoconfig; charset=UTF-8");
// writer.Write(Environment.NewLine);
// writer.Write("Content-Length: " + pac.Length);
// writer.Write(Environment.NewLine);
// writer.Write(Environment.NewLine);
// writer.Write(pac);
// writer.Flush();
//}
private static string writeSuccess(string pac)
{
string responseHead = String.Format(@"HTTP/1.1 200 OK
Content-Type: application/x-ns-proxy-autoconfig
Content-Length: {0}
Connection: Close
", Encoding.UTF8.GetBytes(pac).Length);
return responseHead;
}
}
}

View File

@ -17,20 +17,14 @@ namespace v2rayN.HttpProxyHandler
{
private static int pacPort = 0;
private static HttpWebServer server;
private static HttpWebServerB serverB;
public static void Init(Config config)
{
//if (InitServer("*"))
//{
// pacPort = Global.pacPort;
//}
//else if (InitServer("127.0.0.1"))
//{
// pacPort = Global.pacPort;
//}
//else if (InitServerB("127.0.0.1"))
if (InitServerB("127.0.0.1"))
if (InitServer("*"))
{
pacPort = Global.pacPort;
}
else if (InitServer("127.0.0.1"))
{
pacPort = Global.pacPort;
}
@ -39,15 +33,6 @@ namespace v2rayN.HttpProxyHandler
Utils.SaveLog("Webserver init failed ");
pacPort = 0;
}
//if (Utils.IsAdministrator())
//{
// InitServer("127.0.0.1");
//}
//else
//{
// InitServerB("127.0.0.1");
//}
}
public static bool InitServer(string address)
@ -84,40 +69,10 @@ namespace v2rayN.HttpProxyHandler
}
public static bool InitServerB(string address)
public static string SendResponse(string address)
{
try
{
if (pacPort != Global.pacPort)
{
if (serverB != null)
{
serverB.Stop();
serverB = null;
}
if (serverB == null)
{
serverB = new HttpWebServerB(Global.pacPort, SendResponse);
serverB.Run();
//pacPort = Global.pacPort;
}
}
Utils.SaveLog("WebserverB at " + address);
}
catch (Exception ex)
{
Utils.SaveLog("WebserverB InitServer " + ex.Message);
return false;
}
return true;
}
public static string SendResponse(HttpListenerRequest request)
{
try
{
string address = request.LocalEndPoint.Address.ToString();
var pac = GetPacList(address);
return pac;
}
@ -128,27 +83,6 @@ namespace v2rayN.HttpProxyHandler
}
}
public static string SendResponse(TcpClient tcpClient)
{
try
{
var address = ((IPEndPoint)tcpClient.Client.LocalEndPoint).Address.ToString();
var pac = GetPacList(address);
Console.WriteLine("SendResponse addr " + address);
//Utils.SaveLog("SendResponse addr " + address);
return pac;
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
return "";
}
public static void Stop()
{
//try

View File

@ -182,7 +182,6 @@
<Compile Include="Handler\StatisticsHandler.cs" />
<Compile Include="Handler\V2rayUpdateHandle.cs" />
<Compile Include="HttpProxyHandler\HttpWebServer.cs" />
<Compile Include="HttpProxyHandler\HttpWebServerB.cs" />
<Compile Include="HttpProxyHandler\PACFileWatcherHandle.cs" />
<Compile Include="HttpProxyHandler\PrivoxyHandler.cs" />
<Compile Include="HttpProxyHandler\PACListHandle.cs" />