Browse Source

Code cleanup

tags/4.1.2
celeron533 6 years ago
parent
commit
98538e32c0
1 changed files with 11 additions and 32 deletions
  1. +11
    -32
      shadowsocks-csharp/Controller/Service/PACServer.cs

+ 11
- 32
shadowsocks-csharp/Controller/Service/PACServer.cs View File

@@ -116,7 +116,7 @@ namespace Shadowsocks.Controller
}
else
{
SendResponse(firstPacket, length, socket, useSocks);
SendResponse(socket, useSocks);
}
return true;
}
@@ -166,26 +166,24 @@ namespace Shadowsocks.Controller
}
}
public void SendResponse(byte[] firstPacket, int length, Socket socket, bool useSocks)
public void SendResponse(Socket socket, bool useSocks)
{
try
{
string pac = GetPACContent();
IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint;
string proxy = GetPACAddress(firstPacket, length, localEndPoint, useSocks);
string proxy = GetPACAddress(localEndPoint, useSocks);
pac = pac.Replace("__PROXY__", proxy);
string pacContent = GetPACContent().Replace("__PROXY__", proxy);
string text = String.Format(@"HTTP/1.1 200 OK
string responseHead = String.Format(@"HTTP/1.1 200 OK
Server: Shadowsocks
Content-Type: application/x-ns-proxy-autoconfig
Content-Length: {0}
Connection: Close
", Encoding.UTF8.GetBytes(pac).Length) + pac;
byte[] response = Encoding.UTF8.GetBytes(text);
", Encoding.UTF8.GetBytes(pacContent).Length);
byte[] response = Encoding.UTF8.GetBytes(responseHead + pacContent);
socket.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), socket);
Utils.ReleaseMemory(true);
}
@@ -209,10 +207,7 @@ Connection: Close
private void WatchPacFile()
{
if (PACFileWatcher != null)
{
PACFileWatcher.Dispose();
}
PACFileWatcher?.Dispose();
PACFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
PACFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
PACFileWatcher.Filter = PAC_FILE;
@@ -225,10 +220,7 @@ Connection: Close
private void WatchUserRuleFile()
{
if (UserRuleFileWatcher != null)
{
UserRuleFileWatcher.Dispose();
}
UserRuleFileWatcher?.Dispose();
UserRuleFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
UserRuleFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
UserRuleFileWatcher.Filter = USER_RULE_FILE;
@@ -274,22 +266,9 @@ Connection: Close
}
#endregion
private string GetPACAddress(byte[] requestBuf, int length, IPEndPoint localEndPoint, bool useSocks)
private string GetPACAddress(IPEndPoint localEndPoint, bool useSocks)
{
//try
//{
// string requestString = Encoding.UTF8.GetString(requestBuf);
// if (requestString.IndexOf("AppleWebKit") >= 0)
// {
// string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port;
// proxy = "SOCKS5 " + address + "; SOCKS " + address + ";";
// }
//}
//catch (Exception e)
//{
// Logging.LogUsefulException(e);
//}
return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";";
return $"{(useSocks ? "SOCKS5" : "PROXY")} {localEndPoint.Address}:{_config.localPort};";
}
}
}

Loading…
Cancel
Save