Skip to content

Commit

Permalink
Improve and refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Jul 25, 2024
1 parent 5a81441 commit 0f4884d
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 27 deletions.
9 changes: 9 additions & 0 deletions v2rayN/v2rayN/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ public static int LoadConfig(ref Config? config)
}
config.clashUIItem ??= new();

if (config.systemProxyItem == null)
{
config.systemProxyItem = new()
{
systemProxyExceptions = config.systemProxyExceptions,
systemProxyAdvancedProtocol = config.systemProxyAdvancedProtocol,
};
}

LazyConfig.Instance.SetConfig(config);
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions v2rayN/v2rayN/Handler/MainFormHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Icon GetNotifyIcon(Config config)
{
try
{
int index = (int)config.sysProxyType;
int index = (int)config.systemProxyItem.sysProxyType;

//Load from routing setting
var createdIcon = GetNotifyIcon4Routing(config);
Expand Down Expand Up @@ -56,7 +56,7 @@ public Icon GetNotifyIcon(Config config)
public System.Windows.Media.ImageSource GetAppIcon(Config config)
{
int index = 1;
switch (config.sysProxyType)
switch (config.systemProxyItem.sysProxyType)
{
case ESysProxyType.ForcedClear:
index = 1;
Expand Down Expand Up @@ -90,7 +90,7 @@ public System.Windows.Media.ImageSource GetAppIcon(Config config)
}

Color color = ColorTranslator.FromHtml("#3399CC");
int index = (int)config.sysProxyType;
int index = (int)config.systemProxyItem.sysProxyType;
if (index > 0)
{
color = (new[] { Color.Red, Color.Purple, Color.DarkGreen, Color.Orange, Color.DarkSlateBlue, Color.RoyalBlue })[index - 1];
Expand Down
10 changes: 5 additions & 5 deletions v2rayN/v2rayN/Handler/SysProxyHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class SysProxyHandle

public static bool UpdateSysProxy(Config config, bool forceDisable)
{
var type = config.sysProxyType;
var type = config.systemProxyItem.sysProxyType;

if (forceDisable && type != ESysProxyType.Unchanged)
{
Expand All @@ -30,19 +30,19 @@ public static bool UpdateSysProxy(Config config, bool forceDisable)
if (type == ESysProxyType.ForcedChange)
{
var strExceptions = "";
if (config.notProxyLocalAddress)
if (config.systemProxyItem.notProxyLocalAddress)
{
strExceptions = $"<local>;{config.constItem.defIEProxyExceptions};{config.systemProxyExceptions}";
strExceptions = $"<local>;{config.constItem.defIEProxyExceptions};{config.systemProxyItem.systemProxyExceptions}";
}

var strProxy = string.Empty;
if (Utils.IsNullOrEmpty(config.systemProxyAdvancedProtocol))
if (Utils.IsNullOrEmpty(config.systemProxyItem.systemProxyAdvancedProtocol))
{
strProxy = $"{Global.Loopback}:{port}";
}
else
{
strProxy = config.systemProxyAdvancedProtocol
strProxy = config.systemProxyItem.systemProxyAdvancedProtocol
.Replace("{ip}", Global.Loopback)
.Replace("{http_port}", port.ToString())
.Replace("{socks_port}", portSocks.ToString());
Expand Down
3 changes: 1 addition & 2 deletions v2rayN/v2rayN/Models/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public class Config

public string indexId { get; set; }
public string subIndexId { get; set; }
public ESysProxyType sysProxyType { get; set; }
public string systemProxyExceptions { get; set; }
public bool notProxyLocalAddress { get; set; } = true;
public string systemProxyAdvancedProtocol { get; set; }

public ECoreType runningCoreType { get; set; }
Expand Down Expand Up @@ -48,6 +46,7 @@ public bool IsRunningCore(ECoreType type)
public Mux4SboxItem mux4SboxItem { get; set; }
public HysteriaItem hysteriaItem { get; set; }
public ClashUIItem clashUIItem { get; set; }
public SystemProxyItem systemProxyItem { get; set; }
public List<InItem> inbound { get; set; }
public List<KeyEventItem> globalHotkeys { get; set; }
public List<CoreTypeItem> coreTypeItem { get; set; }
Expand Down
9 changes: 9 additions & 0 deletions v2rayN/v2rayN/Models/ConfigItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,13 @@ public class ClashUIItem
public bool connectionsAutoRefresh { get; set; }
public int connectionsRefreshInterval { get; set; } = 2;
}

[Serializable]
public class SystemProxyItem
{
public ESysProxyType sysProxyType { get; set; }
public string systemProxyExceptions { get; set; }
public bool notProxyLocalAddress { get; set; } = true;
public string systemProxyAdvancedProtocol { get; set; }
}
}
20 changes: 10 additions & 10 deletions v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue, Action<EV
y => y != null && !y.Text.IsNullOrEmpty())
.Subscribe(c => ServerSelectedChanged(c));

SystemProxySelected = (int)_config.sysProxyType;
SystemProxySelected = (int)_config.systemProxyItem.sysProxyType;
this.WhenAnyValue(
x => x.SystemProxySelected,
y => y >= 0)
Expand Down Expand Up @@ -429,7 +429,7 @@ private void Init()
//RefreshServers();

Reload();
ChangeSystemProxyStatus(_config.sysProxyType, true);
ChangeSystemProxyStatus(_config.systemProxyItem.sysProxyType, true);
}

private void OnProgramStarted(object state, bool timeout)
Expand Down Expand Up @@ -936,7 +936,7 @@ await Task.Run(() =>
//ConfigHandler.SaveConfig(_config, false);
ChangeSystemProxyStatus(_config.sysProxyType, false);
ChangeSystemProxyStatus(_config.systemProxyItem.sysProxyType, false);
});
}

Expand All @@ -955,21 +955,21 @@ private void CloseCore()

public void SetListenerType(ESysProxyType type)
{
if (_config.sysProxyType == type)
if (_config.systemProxyItem.sysProxyType == type)
{
return;
}
_config.sysProxyType = type;
_config.systemProxyItem.sysProxyType = type;
ChangeSystemProxyStatus(type, true);

SystemProxySelected = (int)_config.sysProxyType;
SystemProxySelected = (int)_config.systemProxyItem.sysProxyType;
ConfigHandler.SaveConfig(_config, false);
}

private void ChangeSystemProxyStatus(ESysProxyType type, bool blChange)
{
SysProxyHandle.UpdateSysProxy(_config, _config.tunModeItem.enableTun ? true : false);
_noticeHandler?.SendMessage($"{ResUI.TipChangeSystemProxy} - {_config.sysProxyType.ToString()}", true);
_noticeHandler?.SendMessage($"{ResUI.TipChangeSystemProxy} - {_config.systemProxyItem.sysProxyType.ToString()}", true);

Application.Current?.Dispatcher.Invoke((Action)(() =>
{
Expand Down Expand Up @@ -1046,7 +1046,7 @@ private void DoSystemProxySelected(bool c)
{
return;
}
if (_config.sysProxyType == (ESysProxyType)SystemProxySelected)
if (_config.systemProxyItem.sysProxyType == (ESysProxyType)SystemProxySelected)
{
return;
}
Expand Down Expand Up @@ -1223,7 +1223,7 @@ public void InboundDisplayStaus()
StringBuilder sb = new();
sb.Append($"[{EInboundProtocol.socks}:{LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks)}]");
sb.Append(" | ");
//if (_config.sysProxyType == ESysProxyType.ForcedChange)
//if (_config.systemProxyItem.sysProxyType == ESysProxyType.ForcedChange)
//{
// sb.Append($"[{Global.InboundHttp}({ResUI.SystemProxy}):{LazyConfig.Instance.GetLocalPort(Global.InboundHttp)}]");
//}
Expand Down Expand Up @@ -1293,4 +1293,4 @@ private void AutoHideStartup()

#endregion UI
}
}
}
12 changes: 6 additions & 6 deletions v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ public OptionSettingViewModel(Window view)

#region System proxy

notProxyLocalAddress = _config.notProxyLocalAddress;
systemProxyAdvancedProtocol = _config.systemProxyAdvancedProtocol;
systemProxyExceptions = _config.systemProxyExceptions;
notProxyLocalAddress = _config.systemProxyItem.notProxyLocalAddress;
systemProxyAdvancedProtocol = _config.systemProxyItem.systemProxyAdvancedProtocol;
systemProxyExceptions = _config.systemProxyItem.systemProxyExceptions;

#endregion System proxy

Expand Down Expand Up @@ -340,9 +340,9 @@ private void SaveSetting()
_config.uiItem.mainGirdOrientation = (EGirdOrientation)MainGirdOrientation;

//systemProxy
_config.systemProxyExceptions = systemProxyExceptions;
_config.notProxyLocalAddress = notProxyLocalAddress;
_config.systemProxyAdvancedProtocol = systemProxyAdvancedProtocol;
_config.systemProxyItem.systemProxyExceptions = systemProxyExceptions;
_config.systemProxyItem.notProxyLocalAddress = notProxyLocalAddress;
_config.systemProxyItem.systemProxyAdvancedProtocol = systemProxyAdvancedProtocol;

//tun mode
_config.tunModeItem.strictRoute = TunStrictRoute;
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/v2rayN.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<PackageReference Include="ReactiveUI.Validation" Version="4.0.9" />
<PackageReference Include="ReactiveUI.WPF" Version="20.1.1" />
<PackageReference Include="Splat.NLog" Version="15.1.1" />
<PackageReference Include="YamlDotNet" Version="15.3.0" />
<PackageReference Include="YamlDotNet" Version="16.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 0f4884d

Please sign in to comment.