@@ -116,7 +116,7 @@ public override bool Handle(byte[] firstPacket, int length, Socket socket, objec
116
116
}
117
117
else
118
118
{
119
- SendResponse ( firstPacket , length , socket , useSocks ) ;
119
+ SendResponse ( socket , useSocks ) ;
120
120
}
121
121
return true ;
122
122
}
@@ -166,26 +166,24 @@ private string GetPACContent()
166
166
}
167
167
}
168
168
169
- public void SendResponse ( byte [ ] firstPacket , int length , Socket socket , bool useSocks )
169
+ public void SendResponse ( Socket socket , bool useSocks )
170
170
{
171
171
try
172
172
{
173
- string pac = GetPACContent ( ) ;
174
-
175
173
IPEndPoint localEndPoint = ( IPEndPoint ) socket . LocalEndPoint ;
176
174
177
- string proxy = GetPACAddress ( firstPacket , length , localEndPoint , useSocks ) ;
175
+ string proxy = GetPACAddress ( localEndPoint , useSocks ) ;
178
176
179
- pac = pac . Replace ( "__PROXY__" , proxy ) ;
177
+ string pacContent = GetPACContent ( ) . Replace ( "__PROXY__" , proxy ) ;
180
178
181
- string text = String . Format ( @"HTTP/1.1 200 OK
179
+ string responseHead = String . Format ( @"HTTP/1.1 200 OK
182
180
Server: Shadowsocks
183
181
Content-Type: application/x-ns-proxy-autoconfig
184
182
Content-Length: {0}
185
183
Connection: Close
186
184
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 ) ;
189
187
socket . BeginSend ( response , 0 , response . Length , 0 , new AsyncCallback ( SendCallback ) , socket ) ;
190
188
Utils . ReleaseMemory ( true ) ;
191
189
}
@@ -209,10 +207,7 @@ private void SendCallback(IAsyncResult ar)
209
207
210
208
private void WatchPacFile ( )
211
209
{
212
- if ( PACFileWatcher != null )
213
- {
214
- PACFileWatcher . Dispose ( ) ;
215
- }
210
+ PACFileWatcher ? . Dispose ( ) ;
216
211
PACFileWatcher = new FileSystemWatcher ( Directory . GetCurrentDirectory ( ) ) ;
217
212
PACFileWatcher . NotifyFilter = NotifyFilters . LastWrite | NotifyFilters . FileName | NotifyFilters . DirectoryName ;
218
213
PACFileWatcher . Filter = PAC_FILE ;
@@ -225,10 +220,7 @@ private void WatchPacFile()
225
220
226
221
private void WatchUserRuleFile ( )
227
222
{
228
- if ( UserRuleFileWatcher != null )
229
- {
230
- UserRuleFileWatcher . Dispose ( ) ;
231
- }
223
+ UserRuleFileWatcher ? . Dispose ( ) ;
232
224
UserRuleFileWatcher = new FileSystemWatcher ( Directory . GetCurrentDirectory ( ) ) ;
233
225
UserRuleFileWatcher . NotifyFilter = NotifyFilters . LastWrite | NotifyFilters . FileName | NotifyFilters . DirectoryName ;
234
226
UserRuleFileWatcher . Filter = USER_RULE_FILE ;
@@ -274,22 +266,9 @@ private void UserRuleFileWatcher_Changed(object sender, FileSystemEventArgs e)
274
266
}
275
267
#endregion
276
268
277
- private string GetPACAddress ( byte [ ] requestBuf , int length , IPEndPoint localEndPoint , bool useSocks )
269
+ private string GetPACAddress ( IPEndPoint localEndPoint , bool useSocks )
278
270
{
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 } ;";
293
272
}
294
273
}
295
274
}
0 commit comments