Skip to content

Commit 98538e3

Browse files
committed
Code cleanup
1 parent f1042cb commit 98538e3

File tree

1 file changed

+11
-32
lines changed

1 file changed

+11
-32
lines changed

shadowsocks-csharp/Controller/Service/PACServer.cs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public override bool Handle(byte[] firstPacket, int length, Socket socket, objec
116116
}
117117
else
118118
{
119-
SendResponse(firstPacket, length, socket, useSocks);
119+
SendResponse(socket, useSocks);
120120
}
121121
return true;
122122
}
@@ -166,26 +166,24 @@ private string GetPACContent()
166166
}
167167
}
168168

169-
public void SendResponse(byte[] firstPacket, int length, Socket socket, bool useSocks)
169+
public void SendResponse(Socket socket, bool useSocks)
170170
{
171171
try
172172
{
173-
string pac = GetPACContent();
174-
175173
IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint;
176174

177-
string proxy = GetPACAddress(firstPacket, length, localEndPoint, useSocks);
175+
string proxy = GetPACAddress(localEndPoint, useSocks);
178176

179-
pac = pac.Replace("__PROXY__", proxy);
177+
string pacContent = GetPACContent().Replace("__PROXY__", proxy);
180178

181-
string text = String.Format(@"HTTP/1.1 200 OK
179+
string responseHead = String.Format(@"HTTP/1.1 200 OK
182180
Server: Shadowsocks
183181
Content-Type: application/x-ns-proxy-autoconfig
184182
Content-Length: {0}
185183
Connection: Close
186184
187-
", Encoding.UTF8.GetBytes(pac).Length) + pac;
188-
byte[] response = Encoding.UTF8.GetBytes(text);
185+
", Encoding.UTF8.GetBytes(pacContent).Length);
186+
byte[] response = Encoding.UTF8.GetBytes(responseHead + pacContent);
189187
socket.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), socket);
190188
Utils.ReleaseMemory(true);
191189
}
@@ -209,10 +207,7 @@ private void SendCallback(IAsyncResult ar)
209207

210208
private void WatchPacFile()
211209
{
212-
if (PACFileWatcher != null)
213-
{
214-
PACFileWatcher.Dispose();
215-
}
210+
PACFileWatcher?.Dispose();
216211
PACFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
217212
PACFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
218213
PACFileWatcher.Filter = PAC_FILE;
@@ -225,10 +220,7 @@ private void WatchPacFile()
225220

226221
private void WatchUserRuleFile()
227222
{
228-
if (UserRuleFileWatcher != null)
229-
{
230-
UserRuleFileWatcher.Dispose();
231-
}
223+
UserRuleFileWatcher?.Dispose();
232224
UserRuleFileWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory());
233225
UserRuleFileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
234226
UserRuleFileWatcher.Filter = USER_RULE_FILE;
@@ -274,22 +266,9 @@ private void UserRuleFileWatcher_Changed(object sender, FileSystemEventArgs e)
274266
}
275267
#endregion
276268

277-
private string GetPACAddress(byte[] requestBuf, int length, IPEndPoint localEndPoint, bool useSocks)
269+
private string GetPACAddress(IPEndPoint localEndPoint, bool useSocks)
278270
{
279-
//try
280-
//{
281-
// string requestString = Encoding.UTF8.GetString(requestBuf);
282-
// if (requestString.IndexOf("AppleWebKit") >= 0)
283-
// {
284-
// string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port;
285-
// proxy = "SOCKS5 " + address + "; SOCKS " + address + ";";
286-
// }
287-
//}
288-
//catch (Exception e)
289-
//{
290-
// Logging.LogUsefulException(e);
291-
//}
292-
return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";";
271+
return $"{(useSocks ? "SOCKS5" : "PROXY")} {localEndPoint.Address}:{_config.localPort};";
293272
}
294273
}
295274
}

0 commit comments

Comments
 (0)