up pac server

pull/31/head 2.35
2dust 2019-08-21 16:35:01 +08:00
parent 5a1e5707b8
commit dd1a01556f
6 changed files with 151 additions and 188 deletions

View File

@ -2,6 +2,7 @@
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace v2rayN.HttpProxyHandler
@ -37,12 +38,42 @@ namespace v2rayN.HttpProxyHandler
{
if (listener != null)
{
TcpClient client = listener.EndAcceptTcpClient(ares);
TcpClient tcpClient = listener.EndAcceptTcpClient(ares);
listener.BeginAcceptTcpClient(callback, null);
if (client != null && _responderMethod != null)
if (tcpClient != null && _responderMethod != null)
{
_responderMethod(client);
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 (!string.IsNullOrEmpty(returndata)
&& returndata.IndexOf("/pac/") >= 0
&& netStream.CanWrite)
{
BinaryWriter writer = new BinaryWriter(netStream);
//writeSuccess(writer, pac);
Byte[] sendBytes = Encoding.UTF8.GetBytes(writeSuccess(pac));
writer.Write(sendBytes, 0, sendBytes.Length);
writer.Flush();
writer.Close();
}
}
netStream.Close();
tcpClient.Close();
}
}
}
@ -64,6 +95,38 @@ namespace v2rayN.HttpProxyHandler
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)
{
StringBuilder sb = new StringBuilder();
string content_type = "application/x-ns-proxy-autoconfig";
sb.Append("HTTP/1.0 200 OK");
sb.AppendLine();
sb.Append(String.Format("Content-Type:{0};charset=utf-8", content_type));
sb.AppendLine();
//sb.Append("Connection: close");
//sb.AppendLine();
sb.Append(pac);
sb.AppendLine();
return sb.ToString();
}
}
}

View File

@ -1,63 +0,0 @@
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace v2rayN.HttpProxyHandler
{
public class HttpWebServerC
{
private int port;
private TcpListener listener;
private bool is_active = true;
private string pacRespone = string.Empty;
public HttpWebServerC(int port, string pacRespone)
{
this.port = port;
this.pacRespone = pacRespone;
}
public void WorkThread()
{
is_active = true;
Listen();
}
public void Listen()
{
try
{
listener = new TcpListener(new IPEndPoint(IPAddress.Any, port));
listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
listener.Start();
Utils.SaveLog("WebserverB running...");
while (is_active)
{
TcpClient client = listener.AcceptTcpClient();
//HttpWebProcessor processor = new HttpWebProcessor(client, pacRespone);
//Thread thread = new Thread(new ThreadStart(processor.process));
//thread.Start();
//Thread.Sleep(1);
}
}
catch (Exception ex)
{
Utils.SaveLog(ex.Message, ex);
}
}
public void Stop()
{
if (listener != null)
{
is_active = false;
listener.Stop();
listener = null;
}
}
}
}

View File

@ -15,43 +15,26 @@ namespace v2rayN.HttpProxyHandler
/// </summary>
class PACServerHandle
{
//private static Hashtable httpWebServer = new Hashtable();
//private static Hashtable pacList = new Hashtable();
//private static string pac = "";
private static int pacPort = 0;
private static HttpWebServerB server;
// private static HttpWebServerC server;
//static Thread thread;
private static HttpWebServer server;
private static HttpWebServerB serverB;
public static void Init(Config config)
{
InitServer("127.0.0.1");
//if (config.allowLANConn)
//{
// List<string> lstIPAddress = Utils.GetHostIPAddress();
// if (lstIPAddress.Count <= 0)
// {
// return;
// }
// InitServer(lstIPAddress[0]);
// //foreach (string str in lstIPAddress)
// //{
// // InitServer(str);
// //}
//}
//else
//{
// InitServer("127.0.0.1");
//}
if (Utils.IsAdministrator())
{
InitServer("127.0.0.1");
}
else
{
InitServerB("127.0.0.1");
}
}
public static void InitServer(string address)
{
try
{
//pac = GetPacList(address);
if (pacPort != Global.pacPort)
{
if (server != null)
@ -62,44 +45,66 @@ namespace v2rayN.HttpProxyHandler
if (server == null)
{
server = new HttpWebServerB(Global.pacPort, SendResponse);
//server = new HttpWebServerC(Global.pacPort, pac);
string prefixes = string.Format("http://{0}:{1}/pac/", "+", Global.pacPort);
Utils.SaveLog("Webserver prefixes " + prefixes);
HttpWebServer ws = new HttpWebServer(SendResponse, prefixes);
ws.Run();
pacPort = Global.pacPort;
}
}
//thread = new Thread(server.WorkThread);
//thread.IsBackground = true;
//thread.Start();
Utils.SaveLog("Webserver at " + address);
}
catch (Exception ex)
{
Utils.SaveLog("Webserver InitServer " + ex.Message);
}
// if (!pacList.ContainsKey(address))
// {
// pacList.Add(address, GetPacList(address));
// }
// string prefixes = string.Format("http://{0}:{1}/pac/", address, Global.pacPort);
// Utils.SaveLog("Webserver prefixes " + prefixes);
// HttpWebServer ws = new HttpWebServer(SendResponse, prefixes);
// ws.Run();
// if (!httpWebServer.ContainsKey(address) && ws != null)
// {
// httpWebServer.Add(address, ws);
// }
//}
//catch (Exception ex)
//{
// Utils.SaveLog("Webserver InitServer " + ex.Message);
//}
}
public static void InitServerB(string address)
{
try
{
if (pacPort != Global.pacPort)
{
if (serverB != null)
{
serverB.Stop();
serverB = null;
}
if (serverB == null)
{
serverB = new HttpWebServerB(Global.pacPort, SendResponse);
pacPort = Global.pacPort;
}
}
Utils.SaveLog("Webserver at " + address);
}
catch (Exception ex)
{
Utils.SaveLog("Webserver InitServer " + ex.Message);
}
}
public static string SendResponse(HttpListenerRequest request)
{
try
{
string address = request.LocalEndPoint.Address.ToString();
var pac = GetPacList(address);
return pac;
}
catch (Exception ex)
{
Utils.SaveLog("Webserver SendResponse " + ex.Message);
return ex.Message;
}
}
public static string SendResponse(TcpClient tcpClient)
{
try
@ -108,33 +113,9 @@ namespace v2rayN.HttpProxyHandler
var pac = GetPacList(address);
Console.WriteLine("SendResponse addr " + address);
Utils.SaveLog("SendResponse addr " + address);
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 (!string.IsNullOrEmpty(returndata)
&& returndata.IndexOf("/pac/") >= 0
&& netStream.CanWrite)
{
Byte[] sendBytes = Encoding.UTF8.GetBytes(writeSuccess(pac));
netStream.Write(sendBytes, 0, sendBytes.Length);
}
}
netStream.Close();
tcpClient.Close();
return "";
//Utils.SaveLog("SendResponse addr " + address);
return pac;
}
catch (Exception ex)
{
@ -144,44 +125,6 @@ namespace v2rayN.HttpProxyHandler
}
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");
sb.AppendLine();
sb.Append(String.Format("Content-Type:{0};charset=utf-8", content_type));
sb.AppendLine();
sb.Append("Connection: close");
sb.AppendLine();
sb.Append(pac);
sb.AppendLine();
return sb.ToString();
}
/*
public static string SendResponse(HttpListenerRequest request)
{
try
{
string[] arrAddress = request.UserHostAddress.Split(':');
string address = "127.0.0.1";
if (arrAddress.Length > 0)
{
address = arrAddress[0];
}
return pacList[address].ToString();
}
catch (Exception ex)
{
Utils.SaveLog("Webserver SendResponse " + ex.Message);
return ex.Message;
}
}
*/
public static void Stop()
{
//try

View File

@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("v2rayN")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2018")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -33,4 +33,4 @@ using System.Runtime.InteropServices;
// 方法是按如下所示使用“*”:
//[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("2.34")]
[assembly: AssemblyFileVersion("2.35")]

View File

@ -17,6 +17,7 @@ using System.Drawing;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using System.Security.Principal;
namespace v2rayN
{
@ -671,6 +672,25 @@ namespace v2rayN
return string.Empty;
}
/// <summary>
/// IsAdministrator
/// </summary>
/// <returns></returns>
public static bool IsAdministrator()
{
try
{
WindowsIdentity current = WindowsIdentity.GetCurrent();
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
//WindowsBuiltInRole可以枚举出很多权限例如系统用户、User、Guest等等
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch
{
return false;
}
}
#endregion
#region TempPath

View File

@ -145,11 +145,11 @@
<Compile Include="Handler\V2rayUpdateHandle.cs" />
<Compile Include="HttpProxyHandler\HttpWebServer.cs" />
<Compile Include="HttpProxyHandler\HttpWebServerB.cs" />
<Compile Include="HttpProxyHandler\HttpWebServerC.cs" />
<Compile Include="HttpProxyHandler\PACFileWatcherHandle.cs" />
<Compile Include="HttpProxyHandler\PrivoxyHandler.cs" />
<Compile Include="HttpProxyHandler\PACListHandle.cs" />
<Compile Include="HttpProxyHandler\PACServerHandle.cs" />
<Compile Include="HttpProxyHandler\ProxySetting.cs" />
<Compile Include="HttpProxyHandler\SysProxyHandle.cs" />
<Compile Include="HttpProxyHandler\HttpProxyHandle.cs" />
<Compile Include="HttpProxyHandler\WebClientEx.cs">