From 74852be8165ae77cd824eb4e051cec377fb8e9e8 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 30 Sep 2019 19:07:40 +0800 Subject: [PATCH] e --- .../v2rayN/HttpProxyHandler/HttpWebServerB.cs | 181 ------------------ .../HttpProxyHandler/PACServerHandle.cs | 28 ++- v2rayN/v2rayN/v2rayN.csproj | 1 - 3 files changed, 13 insertions(+), 197 deletions(-) delete mode 100644 v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs diff --git a/v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs b/v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs deleted file mode 100644 index f3143936..00000000 --- a/v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs +++ /dev/null @@ -1,181 +0,0 @@ -using System; -using System.Collections; -using System.IO; -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Threading; - -namespace v2rayN.HttpProxyHandler -{ - public class HttpWebServerB - { - TcpListener listener; - bool is_active = true; - private Stream inputStream; - public StreamWriter outputStream; - - public String http_method; - public String http_url; - public String http_protocol_versionstring; - public Hashtable httpHeaders = new Hashtable(); - - private int port; - private Func _responderMethod; - - public HttpWebServerB(int port, Func method) - { - this.port = port; - this._responderMethod = method; - - Thread thread = new Thread(WorkThread); - thread.IsBackground = true; - thread.Start(); - } - - public void Stop() - { - //if (listener != null) - //{ - // listener.Stop(); - // listener = null; - //} - } - - public void WorkThread() - { - listen(); - } - - private void listen() - { - listener = new TcpListener(IPAddress.Any, port); - listener.Start(); - while (is_active) - { - TcpClient socket = listener.AcceptTcpClient(); - //HttpProcessor processor = new HttpProcessor(s, this); - //Thread thread = new Thread(new ThreadStart(process)); - Thread thread = new Thread(new ParameterizedThreadStart(process)); - thread.Start(socket); - - //thread.Start(); - Thread.Sleep(1); - } - } - private void process(object obj) - { - try - { - var socket = obj as TcpClient; - - inputStream = new BufferedStream(socket.GetStream()); - outputStream = new StreamWriter(new BufferedStream(socket.GetStream())); - parseRequest(); - readHeaders(); - if (http_method.Equals("GET")) - { - handleGETRequest(socket); - } - - outputStream.BaseStream.Flush(); - inputStream = null; outputStream = null; // bs = null; - socket.Close(); - } - catch (Exception ex) - { - - } - } - - /// - /// 复制请求 - /// - private void parseRequest() - { - String request = streamReadLine(inputStream); - string[] tokens = request.Split(' '); - if (tokens.Length != 3) - { - throw new Exception("invalid http request line"); - } - http_method = tokens[0].ToUpper(); - http_url = tokens[1]; - http_protocol_versionstring = tokens[2]; - } - /// - /// 读取请求头 - /// - private void readHeaders() - { - String line; - while ((line = streamReadLine(inputStream)) != null) - { - if (line.Equals("")) - { - return; - } - - int separator = line.IndexOf(':'); - if (separator == -1) - { - throw new Exception("invalid http header line: " + line); - } - String name = line.Substring(0, separator); - int pos = separator + 1; - while ((pos < line.Length) && (line[pos] == ' ')) - { - pos++; // strip any spaces - } - - string value = line.Substring(pos, line.Length - pos); - httpHeaders[name] = value; - } - } - private string streamReadLine(Stream inputStream) - { - int next_char; - string data = ""; - while (true) - { - next_char = inputStream.ReadByte(); - if (next_char == '\n') { break; } - if (next_char == '\r') { continue; } - if (next_char == -1) { Thread.Sleep(1); continue; }; - data += Convert.ToChar(next_char); - } - return data; - } - - /// - /// 重载相应Get方法 - /// - /// - private void handleGETRequest(TcpClient socket) - { - String executeResult = String.Empty; - if (http_url.ToLower().Contains("/pac/")) - { - var address = ((IPEndPoint)socket.Client.LocalEndPoint).Address.ToString(); - string pac = _responderMethod(address); - - writeSuccess("application/x-ns-proxy-autoconfig"); - outputStream.WriteLine(pac); - outputStream.Flush(); - return; - } - } - - /// - /// 回复成功 - /// - /// - private void writeSuccess(string content_type = "text/html") - { - outputStream.WriteLine("HTTP/1.0 200 OK"); - outputStream.WriteLine(String.Format("Content-Type:{0};", content_type)); - outputStream.WriteLine("Connection: close"); - outputStream.WriteLine(""); - } - } -} diff --git a/v2rayN/v2rayN/HttpProxyHandler/PACServerHandle.cs b/v2rayN/v2rayN/HttpProxyHandler/PACServerHandle.cs index 67c5d09e..2c466ec5 100644 --- a/v2rayN/v2rayN/HttpProxyHandler/PACServerHandle.cs +++ b/v2rayN/v2rayN/HttpProxyHandler/PACServerHandle.cs @@ -26,22 +26,20 @@ namespace v2rayN.HttpProxyHandler public static void Init(Config config) { - var serverB = new HttpWebServerB(Global.pacPort, SendResponse); - pacPort = Global.pacPort; - //if (InitServer("*")) - //{ - // pacPort = Global.pacPort; - //} - //else if (InitServer("127.0.0.1")) - //{ - // pacPort = Global.pacPort; - //} - //else - //{ - // Utils.SaveLog("Webserver init failed "); - // pacPort = 0; - //} + if (InitServer("*")) + { + pacPort = Global.pacPort; + } + else if (InitServer("127.0.0.1")) + { + pacPort = Global.pacPort; + } + else + { + Utils.SaveLog("Webserver init failed "); + pacPort = 0; + } } private static bool InitServer(string address) 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 @@ -