Update HttpWebServerB.cs

2dust 2019-09-22 18:54:34 +08:00
parent b53ce37b0a
commit f41fe6d7a3
1 changed files with 117 additions and 49 deletions

View File

@ -32,62 +32,135 @@ namespace v2rayN.HttpProxyHandler
} }
Utils.SaveLog("WebserverB running..."); Utils.SaveLog("WebserverB running...");
AsyncCallback callback = null; //AsyncCallback callback = null;
listener.BeginAcceptTcpClient(callback = ((ares) => //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 try
{ {
if (listener != null) while (true)
{ {
TcpClient tcpClient = listener.EndAcceptTcpClient(ares); ThreadPool.QueueUserWorkItem((c) =>
listener.BeginAcceptTcpClient(callback, null);
if (tcpClient != null && _responderMethod != null)
{ {
string pac = _responderMethod(tcpClient); try
NetworkStream netStream = tcpClient.GetStream();
if (netStream.CanRead)
{ {
// Reads NetworkStream into a byte buffer. if (listener != null)
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); TcpClient tcpClient = listener.AcceptTcpClient();
//writeSuccess(writer, pac);
Byte[] sendBytes = ASCIIEncoding.ASCII.GetBytes(writeSuccess(pac)); if (tcpClient != null && _responderMethod != null)
writer.Write(sendBytes, 0, sendBytes.Length); {
writer.Flush(); string pac = _responderMethod(tcpClient);
writer.Close(); 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)
netStream.Close(); {
tcpClient.Close(); Utils.SaveLog(ex.Message, ex);
} }
});
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Utils.SaveLog(ex.Message, ex); Utils.SaveLog(ex.Message, ex);
} } // suppress any exceptions
//Console.WriteLine("Client connected completed"); });
}), null);
} }
public void Stop() public void Stop()
{ {
if (listener != null) if (listener != null)
@ -114,19 +187,14 @@ namespace v2rayN.HttpProxyHandler
private static string writeSuccess(string pac) private static string writeSuccess(string pac)
{ {
StringBuilder sb = new StringBuilder();
string content_type = "application/x-ns-proxy-autoconfig";
sb.Append("HTTP/1.0 200 OK"); string responseHead = String.Format(@"HTTP/1.1 200 OK
sb.AppendLine(); Content-Type: application/x-ns-proxy-autoconfig
sb.Append(String.Format("Content-Type:{0};", content_type)); Content-Length: {0}
sb.AppendLine(); Connection: Close
//sb.Append("Connection: close"); ", Encoding.UTF8.GetBytes(pac).Length);
//sb.AppendLine();
sb.Append(pac);
sb.AppendLine();
return sb.ToString(); return responseHead;
} }
} }