diff --git a/v2rayN/v2rayN/Handler/StatisticsHandler.cs b/v2rayN/v2rayN/Handler/StatisticsHandler.cs index 0258f4c9..455447b1 100644 --- a/v2rayN/v2rayN/Handler/StatisticsHandler.cs +++ b/v2rayN/v2rayN/Handler/StatisticsHandler.cs @@ -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(); exitFlag_ = false; @@ -282,11 +282,11 @@ namespace v2rayN.Handler /// UP [readable string] [amount] /// DOWN [readable string] [amount] /// 每行每个数据空格分隔 - /// - var overallPath = Path.Combine(logPath_, Global.StatisticLogOverall); - if (File.Exists(overallPath)) + + try { - try + var overallPath = Path.Combine(logPath_, Global.StatisticLogOverall); + if (File.Exists(overallPath)) { using (var overallReader = new StreamReader(overallPath)) { @@ -335,20 +335,22 @@ namespace v2rayN.Handler } } } + } } - catch (Exception ex) - { - Utils.SaveLog(ex.Message, ex); - } + } + catch (Exception ex) + { + Utils.SaveLog(ex.Message, ex); } - // 当天流量记录文件 - var dailyPath = Path.Combine(logPath_, $"{DateTime.Now.ToString("yyyy-MM-dd")}.txt"); - if (File.Exists(dailyPath)) + try { - try + // 当天流量记录文件 + var dailyPath = Path.Combine(logPath_, $"{DateTime.Now.ToString("yyyy-MM-dd")}.txt"); + if (File.Exists(dailyPath)) { + using (var dailyReader = new StreamReader(dailyPath)) { while (!dailyReader.EndOfStream) @@ -386,10 +388,10 @@ namespace v2rayN.Handler } } } - catch (Exception ex) - { - Utils.SaveLog(ex.Message, ex); - } + } + catch (Exception ex) + { + Utils.SaveLog(ex.Message, ex); } } diff --git a/v2rayN/v2rayN/HttpProxyHandler/HttpWebServer.cs b/v2rayN/v2rayN/HttpProxyHandler/HttpWebServer.cs index d70592d6..f906da12 100644 --- a/v2rayN/v2rayN/HttpProxyHandler/HttpWebServer.cs +++ b/v2rayN/v2rayN/HttpProxyHandler/HttpWebServer.cs @@ -8,9 +8,9 @@ namespace v2rayN.HttpProxyHandler public class HttpWebServer { private HttpListener _listener; - private Func _responderMethod; + private Func _responderMethod; - public HttpWebServer(string[] prefixes, Func method) + public HttpWebServer(string[] prefixes, Func method) { try { @@ -43,7 +43,7 @@ namespace v2rayN.HttpProxyHandler } } - public HttpWebServer(Func method, params string[] prefixes) + public HttpWebServer(Func 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; diff --git a/v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs b/v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs deleted file mode 100644 index fcda5f0a..00000000 --- a/v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs +++ /dev/null @@ -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 _responderMethod; - - public HttpWebServerB(int port, Func 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; - } - - } -} diff --git a/v2rayN/v2rayN/HttpProxyHandler/PACServerHandle.cs b/v2rayN/v2rayN/HttpProxyHandler/PACServerHandle.cs index db5e5d31..30ec6da1 100644 --- a/v2rayN/v2rayN/HttpProxyHandler/PACServerHandle.cs +++ b/v2rayN/v2rayN/HttpProxyHandler/PACServerHandle.cs @@ -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 diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index 5ad74255..d2b769bd 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -182,7 +182,6 @@ -