Skip to content

Commit f9e00d8

Browse files
committed
2 parents 75a5b13 + d6089b4 commit f9e00d8

File tree

8 files changed

+388
-231
lines changed

8 files changed

+388
-231
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
version: 1.0.{build}
2-
image: Visual Studio 2015
2+
image: Visual Studio 2017
33
environment:
44
matrix:
55
- platform: x86

shadowsocks-csharp/Controller/ShadowsocksController.cs

Lines changed: 73 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.IO;
5+
using System.Linq;
56
using System.Net;
67
using System.Net.Sockets;
78
using System.Text;
89
using System.Threading;
910
using System.Web;
1011
using System.Windows.Forms;
11-
using Newtonsoft.Json;
1212

13+
using Shadowsocks.Controller.Service;
1314
using Shadowsocks.Controller.Strategy;
1415
using Shadowsocks.Model;
15-
using Shadowsocks.Properties;
1616
using Shadowsocks.Util;
17-
using System.Linq;
18-
using Shadowsocks.Controller.Service;
19-
using Shadowsocks.Proxy;
2017

2118
namespace Shadowsocks.Controller
2219
{
@@ -100,10 +97,7 @@ public void Start(bool regHotkeys = true)
10097

10198
protected void ReportError(Exception e)
10299
{
103-
if (Errored != null)
104-
{
105-
Errored(this, new ErrorEventArgs(e));
106-
}
100+
Errored?.Invoke(this, new ErrorEventArgs(e));
107101
}
108102

109103
public Server GetCurrentServer()
@@ -132,7 +126,7 @@ public IStrategy GetCurrentStrategy()
132126
{
133127
foreach (var strategy in _strategyManager.GetStrategies())
134128
{
135-
if (strategy.ID == this._config.strategy)
129+
if (strategy.ID == _config.strategy)
136130
{
137131
return strategy;
138132
}
@@ -197,9 +191,13 @@ public bool AddServerBySSURL(string ssURL)
197191
{
198192
try
199193
{
200-
if (ssURL.IsNullOrEmpty() || ssURL.IsWhiteSpace()) return false;
194+
if (ssURL.IsNullOrEmpty() || ssURL.IsWhiteSpace())
195+
return false;
196+
201197
var servers = Server.GetServers(ssURL);
202-
if (servers == null || servers.Count == 0) return false;
198+
if (servers == null || servers.Count == 0)
199+
return false;
200+
203201
foreach (var server in servers)
204202
{
205203
_config.configs.Add(server);
@@ -219,30 +217,24 @@ public void ToggleEnable(bool enabled)
219217
{
220218
_config.enabled = enabled;
221219
SaveConfig(_config);
222-
if (EnableStatusChanged != null)
223-
{
224-
EnableStatusChanged(this, new EventArgs());
225-
}
220+
221+
EnableStatusChanged?.Invoke(this, new EventArgs());
226222
}
227223

228224
public void ToggleGlobal(bool global)
229225
{
230226
_config.global = global;
231227
SaveConfig(_config);
232-
if (EnableGlobalChanged != null)
233-
{
234-
EnableGlobalChanged(this, new EventArgs());
235-
}
228+
229+
EnableGlobalChanged?.Invoke(this, new EventArgs());
236230
}
237231

238232
public void ToggleShareOverLAN(bool enabled)
239233
{
240234
_config.shareOverLan = enabled;
241235
SaveConfig(_config);
242-
if (ShareOverLANStatusChanged != null)
243-
{
244-
ShareOverLANStatusChanged(this, new EventArgs());
245-
}
236+
237+
ShareOverLANStatusChanged?.Invoke(this, new EventArgs());
246238
}
247239

248240
public void SaveProxy(ProxyConfig proxyConfig)
@@ -255,10 +247,8 @@ public void ToggleVerboseLogging(bool enabled)
255247
{
256248
_config.isVerboseLogging = enabled;
257249
SaveConfig(_config);
258-
if (VerboseLoggingStatusChanged != null)
259-
{
260-
VerboseLoggingStatusChanged(this, new EventArgs());
261-
}
250+
251+
VerboseLoggingStatusChanged?.Invoke(this, new EventArgs());
262252
}
263253

264254
public void SelectServerIndex(int index)
@@ -310,19 +300,15 @@ private void StopPlugins()
310300
public void TouchPACFile()
311301
{
312302
string pacFilename = _pacServer.TouchPACFile();
313-
if (PACFileReadyToOpen != null)
314-
{
315-
PACFileReadyToOpen(this, new PathEventArgs() { Path = pacFilename });
316-
}
303+
304+
PACFileReadyToOpen?.Invoke(this, new PathEventArgs() { Path = pacFilename });
317305
}
318306

319307
public void TouchUserRuleFile()
320308
{
321309
string userRuleFilename = _pacServer.TouchUserRuleFile();
322-
if (UserRuleFileReadyToOpen != null)
323-
{
324-
UserRuleFileReadyToOpen(this, new PathEventArgs() { Path = userRuleFilename });
325-
}
310+
311+
UserRuleFileReadyToOpen?.Invoke(this, new PathEventArgs() { Path = userRuleFilename });
326312
}
327313

328314
public string GetServerURLForCurrentServer()
@@ -381,81 +367,68 @@ public void UpdatePACFromGFWList()
381367

382368
public void UpdateStatisticsConfiguration(bool enabled)
383369
{
384-
if (availabilityStatistics == null) return;
385-
availabilityStatistics.UpdateConfiguration(this);
386-
_config.availabilityStatistics = enabled;
387-
SaveConfig(_config);
370+
if (availabilityStatistics != null)
371+
{
372+
availabilityStatistics.UpdateConfiguration(this);
373+
_config.availabilityStatistics = enabled;
374+
SaveConfig(_config);
375+
}
388376
}
389377

390378
public void SavePACUrl(string pacUrl)
391379
{
392380
_config.pacUrl = pacUrl;
393381
SaveConfig(_config);
394-
if (ConfigChanged != null)
395-
{
396-
ConfigChanged(this, new EventArgs());
397-
}
382+
383+
ConfigChanged?.Invoke(this, new EventArgs());
398384
}
399385

400386
public void UseOnlinePAC(bool useOnlinePac)
401387
{
402388
_config.useOnlinePac = useOnlinePac;
403389
SaveConfig(_config);
404-
if (ConfigChanged != null)
405-
{
406-
ConfigChanged(this, new EventArgs());
407-
}
390+
391+
ConfigChanged?.Invoke(this, new EventArgs());
408392
}
409393

410394
public void ToggleSecureLocalPac(bool enabled)
411395
{
412396
_config.secureLocalPac = enabled;
413397
SaveConfig(_config);
414-
if (ConfigChanged != null)
415-
{
416-
ConfigChanged(this, new EventArgs());
417-
}
398+
399+
ConfigChanged?.Invoke(this, new EventArgs());
418400
}
419401

420402
public void ToggleCheckingUpdate(bool enabled)
421403
{
422404
_config.autoCheckUpdate = enabled;
423405
Configuration.Save(_config);
424-
if (ConfigChanged != null)
425-
{
426-
ConfigChanged(this, new EventArgs());
427-
}
406+
407+
ConfigChanged?.Invoke(this, new EventArgs());
428408
}
429409

430410
public void ToggleCheckingPreRelease(bool enabled)
431411
{
432412
_config.checkPreRelease = enabled;
433413
Configuration.Save(_config);
434-
if (ConfigChanged != null)
435-
{
436-
ConfigChanged(this, new EventArgs());
437-
}
414+
ConfigChanged?.Invoke(this, new EventArgs());
438415
}
439416

440417
public void SaveLogViewerConfig(LogViewerConfig newConfig)
441418
{
442419
_config.logViewer = newConfig;
443420
newConfig.SaveSize();
444421
Configuration.Save(_config);
445-
if (ConfigChanged != null)
446-
{
447-
ConfigChanged(this, new EventArgs());
448-
}
422+
423+
ConfigChanged?.Invoke(this, new EventArgs());
449424
}
450425

451426
public void SaveHotkeyConfig(HotkeyConfig newConfig)
452427
{
453428
_config.hotkey = newConfig;
454429
SaveConfig(_config);
455-
if (ConfigChanged != null)
456-
{
457-
ConfigChanged(this, new EventArgs());
458-
}
430+
431+
ConfigChanged?.Invoke(this, new EventArgs());
459432
}
460433

461434
public void UpdateLatency(Server server, TimeSpan latency)
@@ -498,15 +471,15 @@ protected void Reload()
498471
if (_pacServer == null)
499472
{
500473
_pacServer = new PACServer();
501-
_pacServer.PACFileChanged += pacServer_PACFileChanged;
502-
_pacServer.UserRuleFileChanged += pacServer_UserRuleFileChanged;
474+
_pacServer.PACFileChanged += PacServer_PACFileChanged;
475+
_pacServer.UserRuleFileChanged += PacServer_UserRuleFileChanged;
503476
}
504477
_pacServer.UpdateConfiguration(_config);
505478
if (gfwListUpdater == null)
506479
{
507480
gfwListUpdater = new GFWListUpdater();
508-
gfwListUpdater.UpdateCompleted += pacServer_PACUpdateCompleted;
509-
gfwListUpdater.Error += pacServer_PACUpdateError;
481+
gfwListUpdater.UpdateCompleted += PacServer_PACUpdateCompleted;
482+
gfwListUpdater.Error += PacServer_PACUpdateError;
510483
}
511484

512485
availabilityStatistics.UpdateConfiguration(this);
@@ -536,21 +509,22 @@ protected void Reload()
536509

537510
TCPRelay tcpRelay = new TCPRelay(this, _config);
538511
UDPRelay udpRelay = new UDPRelay(this);
539-
List<Listener.IService> services = new List<Listener.IService>();
540-
services.Add(tcpRelay);
541-
services.Add(udpRelay);
542-
services.Add(_pacServer);
543-
services.Add(new PortForwarder(privoxyRunner.RunningPort));
512+
List<Listener.IService> services = new List<Listener.IService>
513+
{
514+
tcpRelay,
515+
udpRelay,
516+
_pacServer,
517+
new PortForwarder(privoxyRunner.RunningPort)
518+
};
544519
_listener = new Listener(services);
545520
_listener.Start(_config);
546521
}
547522
catch (Exception e)
548523
{
549524
// translate Microsoft language into human language
550525
// i.e. An attempt was made to access a socket in a way forbidden by its access permissions => Port already in use
551-
if (e is SocketException)
526+
if (e is SocketException se)
552527
{
553-
SocketException se = (SocketException)e;
554528
if (se.SocketErrorCode == SocketError.AddressAlreadyInUse)
555529
{
556530
e = new Exception(I18N.GetString("Port {0} already in use", _config.localPort), e);
@@ -564,10 +538,7 @@ protected void Reload()
564538
ReportError(e);
565539
}
566540

567-
if (ConfigChanged != null)
568-
{
569-
ConfigChanged(this, new EventArgs());
570-
}
541+
ConfigChanged?.Invoke(this, new EventArgs());
571542

572543
UpdateSystemProxy();
573544
Utils.ReleaseMemory(true);
@@ -590,25 +561,23 @@ private void UpdateSystemProxy()
590561
SystemProxy.Update(_config, false, _pacServer);
591562
}
592563

593-
private void pacServer_PACFileChanged(object sender, EventArgs e)
564+
private void PacServer_PACFileChanged(object sender, EventArgs e)
594565
{
595566
UpdateSystemProxy();
596567
}
597568

598-
private void pacServer_PACUpdateCompleted(object sender, GFWListUpdater.ResultEventArgs e)
569+
private void PacServer_PACUpdateCompleted(object sender, GFWListUpdater.ResultEventArgs e)
599570
{
600-
if (UpdatePACFromGFWListCompleted != null)
601-
UpdatePACFromGFWListCompleted(this, e);
571+
UpdatePACFromGFWListCompleted?.Invoke(this, e);
602572
}
603573

604-
private void pacServer_PACUpdateError(object sender, ErrorEventArgs e)
574+
private void PacServer_PACUpdateError(object sender, ErrorEventArgs e)
605575
{
606-
if (UpdatePACFromGFWListError != null)
607-
UpdatePACFromGFWListError(this, e);
576+
UpdatePACFromGFWListError?.Invoke(this, e);
608577
}
609578

610579
private static readonly IEnumerable<char> IgnoredLineBegins = new[] { '!', '[' };
611-
private void pacServer_UserRuleFileChanged(object sender, EventArgs e)
580+
private void PacServer_UserRuleFileChanged(object sender, EventArgs e)
612581
{
613582
if (!File.Exists(Utils.GetTempPath("gfwlist.txt")))
614583
{
@@ -630,8 +599,10 @@ public void CopyPacUrl()
630599

631600
private void StartReleasingMemory()
632601
{
633-
_ramThread = new Thread(new ThreadStart(ReleaseMemory));
634-
_ramThread.IsBackground = true;
602+
_ramThread = new Thread(new ThreadStart(ReleaseMemory))
603+
{
604+
IsBackground = true
605+
};
635606
_ramThread.Start();
636607
}
637608

@@ -655,8 +626,10 @@ private void StartTrafficStatistics(int queueMaxSize)
655626
{
656627
trafficPerSecondQueue.Enqueue(new TrafficPerSecond());
657628
}
658-
_trafficThread = new Thread(new ThreadStart(() => TrafficStatistics(queueMaxSize)));
659-
_trafficThread.IsBackground = true;
629+
_trafficThread = new Thread(new ThreadStart(() => TrafficStatistics(queueMaxSize)))
630+
{
631+
IsBackground = true
632+
};
660633
_trafficThread.Start();
661634
}
662635

@@ -666,10 +639,11 @@ private void TrafficStatistics(int queueMaxSize)
666639
while (true)
667640
{
668641
previous = trafficPerSecondQueue.Last();
669-
current = new TrafficPerSecond();
670-
671-
current.inboundCounter = InboundCounter;
672-
current.outboundCounter = OutboundCounter;
642+
current = new TrafficPerSecond
643+
{
644+
inboundCounter = InboundCounter,
645+
outboundCounter = OutboundCounter
646+
};
673647
current.inboundIncreasement = current.inboundCounter - previous.inboundCounter;
674648
current.outboundIncreasement = current.outboundCounter - previous.outboundCounter;
675649

shadowsocks-csharp/Data/ja.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ Port {0} is reserved by system= ポート番号 {0} はシステムによって
122122
Invalid server address=サーバーアドレスが無効です。
123123
Illegal port number format=ポート番号のフォーマットが無効です。
124124
Illegal timeout format=タイムアウト値のフォーマットが無効です。
125-
Please add at least one server=少なくとも一つのサーバーを指定して下さい。
126125
Server IP can not be blank=サーバー IP が指定されていません。
127126
Password can not be blank=パスワードが指定されていません。
128127
Port out of range=ポート番号は範囲外です。

0 commit comments

Comments
 (0)