Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
celeron533 committed Sep 6, 2018
1 parent f1042cb commit 98538e3
Showing 1 changed file with 11 additions and 32 deletions.
43 changes: 11 additions & 32 deletions shadowsocks-csharp/Controller/Service/PACServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public override bool Handle(byte[] firstPacket, int length, Socket socket, objec
}
else
{
SendResponse(firstPacket, length, socket, useSocks);
SendResponse(socket, useSocks);
}
return true;
}
Expand Down Expand Up @@ -166,26 +166,24 @@ private string GetPACContent()
}
}

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);
}
Expand All @@ -209,10 +207,7 @@ private void SendCallback(IAsyncResult ar)

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;
Expand All @@ -225,10 +220,7 @@ private void WatchPacFile()

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;
Expand Down Expand Up @@ -274,22 +266,9 @@ private void UserRuleFileWatcher_Changed(object sender, FileSystemEventArgs e)
}
#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};";
}
}
}

0 comments on commit 98538e3

Please sign in to comment.