Skip to content

Commit

Permalink
feat: add disable system proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Jan 14, 2024
1 parent 2be36ef commit fa743e7
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 31 deletions.
5 changes: 5 additions & 0 deletions STranslate.Model/ConfigModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public partial class ConfigModel : ObservableObject
/// </summary>
public bool UnconventionalScreen { get; set; }

/// <summary>
/// 禁用系统代理
/// </summary>
public bool IsDisableSystemProxy { get; set; }

/// <summary>
/// OCR时是否自动复制文本
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions STranslate.Util/ProxyUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,29 @@ namespace STranslate.Helper
/// </summary>
public static class ProxyUtil
{
/// <summary>
/// 开始加载动态监听系统代理
/// </summary>
public static void LoadDynamicProxy()
{
HttpClient.DefaultProxy = _dynamicProxy;

ListeningAgent();
}

/// <summary>
/// 更新是否使用监听到的系统代理
/// 有时代理不稳定,还不如禁用使用系统代理访问
/// </summary>
/// <param name="flag">false: 使用默认系统代理,true: 不使用任何代理</param>
public static void UpdateDynamicProxy(bool flag)
{
HttpClient.DefaultProxy = flag ? new HttpNoProxy() : _dynamicProxy;
}

/// <summary>
/// 释放资源
/// </summary>
public static void UnLoadDynamicProxy() => _dynamicProxy.Dispose();

internal static async void ListeningAgent()
Expand Down
48 changes: 31 additions & 17 deletions STranslate/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using STranslate.Log;
using STranslate.Style.Controls;
using STranslate.Util;
using STranslate.Views;
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using STranslate.Helper;
using STranslate.Log;
using STranslate.Style.Controls;
using STranslate.Util;
using STranslate.Views;

namespace STranslate
{
Expand Down Expand Up @@ -45,17 +46,40 @@ protected override void OnStartup(StartupEventArgs e)
#elif !DEBUG
LogService.Register(minLevel: LogLevel.Info);
#endif
// 4. 开启监听系统代理
ProxyUtil.LoadDynamicProxy();

// 5. 软件配置涉及初始化操作
Singleton<ConfigHelper>.Instance.InitialOperate();

// 6. Open View
StartProgram();

// 7. 全局异常处理
ExceptionHandler();
}

protected override void OnExit(ExitEventArgs e)
{
//释放监听系统代理资源
ProxyUtil.UnLoadDynamicProxy();

//打印退出日志并释放日志资源
if (LogService.Logger != null)
{
LogService.Logger.Info($"{Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly()!.Location)} Closed...");
LogService.UnRegister();
}
base.OnExit(e);
}

private bool NeedAdministrator()
{
//加载配置
var isRole = Singleton<ConfigHelper>.Instance.CurrentConfig?.NeedAdministrator ?? false;

if (!isRole) return false;
if (!isRole)
return false;

WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
Expand Down Expand Up @@ -112,24 +136,14 @@ private void ExceptionHandler()
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
}

protected override void OnExit(ExitEventArgs e)
{
if (LogService.Logger != null)
{
LogService.Logger.Info($"{Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly()!.Location)} Closed...");
LogService.UnRegister();
}
base.OnExit(e);
}

//UI线程未捕获异常处理事件(UI主线程)
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Exception ex = e.Exception;
//异常信息 和 调用堆栈信息
//string msg = String.Format("{0}\n\n{1}", ex.Message, ex.StackTrace);
LogService.Logger.Error("UI线程异常", ex);
e.Handled = true;//表示异常已处理,可以继续运行
e.Handled = true; //表示异常已处理,可以继续运行
}

//非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
Expand Down
14 changes: 13 additions & 1 deletion STranslate/Helper/ConfigHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using STranslate.Helper;
using STranslate.Log;
using STranslate.Model;
using STranslate.Util;
Expand Down Expand Up @@ -34,11 +35,20 @@ public ConfigHelper()

//初始化时将初始值赋给Config属性
CurrentConfig = ResetConfig;
}

/// <summary>
/// 初始化操作
/// </summary>
public void InitialOperate()
{
//初始化主题
Application.Current.Resources.MergedDictionaries.First().Source = CurrentConfig.IsBright
Application.Current.Resources.MergedDictionaries.First().Source = CurrentConfig?.IsBright ?? true
? ConstStr.LIGHTURI
: ConstStr.DARKURI;

//初始化代理设置
ProxyUtil.UpdateDynamicProxy(CurrentConfig?.IsDisableSystemProxy ?? false);
}

/// <summary>
Expand Down Expand Up @@ -131,6 +141,7 @@ public bool WriteConfig(CommonViewModel model)
CurrentConfig.IsFollowMouse = model.IsFollowMouse;
CurrentConfig.CloseUIOcrRetTranslate = model.CloseUIOcrRetTranslate;
CurrentConfig.UnconventionalScreen = model.UnconventionalScreen;
CurrentConfig.IsDisableSystemProxy = model.IsDisableSystemProxy;
CurrentConfig.IsOcrAutoCopyText = model.IsOcrAutoCopyText;
CurrentConfig.IsAdjustContentTranslate = model.IsAdjustContentTranslate;
CurrentConfig.IsRemoveLineBreakGettingWords = model.IsRemoveLineBreakGettingWords;
Expand Down Expand Up @@ -212,6 +223,7 @@ private ConfigModel InitialConfig()
IsFollowMouse = false,
IsOcrAutoCopyText = false,
UnconventionalScreen = false,
IsDisableSystemProxy = false,
CloseUIOcrRetTranslate = false,
IsAdjustContentTranslate = false,
IsRemoveLineBreakGettingWords = false,
Expand Down
4 changes: 0 additions & 4 deletions STranslate/ViewModels/NotifyIconViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public partial class NotifyIconViewModel : ObservableObject

public NotifyIconViewModel()
{
ProxyUtil.LoadDynamicProxy();

UpdateToolTip();
Microsoft.Win32.SystemEvents.DisplaySettingsChanged += DisplaySettingsChanged;
}
Expand Down Expand Up @@ -340,8 +338,6 @@ private void DisplaySettingsChanged(object? sender, EventArgs e)
[RelayCommand]
private void Exit()
{
ProxyUtil.UnLoadDynamicProxy();

Microsoft.Win32.SystemEvents.DisplaySettingsChanged -= DisplaySettingsChanged;

OnExit?.Invoke();
Expand Down
16 changes: 16 additions & 0 deletions STranslate/ViewModels/Preference/CommonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ public bool IsBright
[ObservableProperty]
private bool unconventionalScreen = Singleton<ConfigHelper>.Instance.CurrentConfig?.UnconventionalScreen ?? false;

private bool isDisableSystemProxy = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsDisableSystemProxy ?? false;
public bool IsDisableSystemProxy
{
get => isDisableSystemProxy;
set
{
if (isDisableSystemProxy != value)
{
OnPropertyChanging(nameof(IsDisableSystemProxy));
isDisableSystemProxy = value;
ProxyUtil.UpdateDynamicProxy(value);
OnPropertyChanged(nameof(IsDisableSystemProxy));
}
}
}

[ObservableProperty]
private bool isOcrAutoCopyText = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsOcrAutoCopyText ?? false;

Expand Down
12 changes: 3 additions & 9 deletions STranslate/Views/OutputView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
d:DesignWidth="800"
mc:Ignorable="d">
<StackPanel>
<ListBox BorderThickness="0"
Background="Transparent"
<ListBox Background="Transparent"
BorderThickness="0"
ItemsSource="{Binding Translators}"
PreviewMouseWheel="ListBox_PreviewMouseWheel"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
Expand All @@ -28,14 +28,8 @@
<Expander VerticalAlignment="Top"
ExpandDirection="Down"
IsEnabled="{Binding Data, Converter={StaticResource String2IsEnableConverter}, Mode=OneWay}"
IsExpanded="{Binding Data, Converter={StaticResource String2IsExpandedConverter}}"
SnapsToDevicePixels="True">
<Expander.IsExpanded>
<Binding Path="Data">
<Binding.Converter>
<conv:String2IsExpandedConverter />
</Binding.Converter>
</Binding>
</Expander.IsExpanded>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<Image Width="18" Margin="10,5,0,0" Source="{Binding Icon, Converter={StaticResource String2IconConverter}}" />
Expand Down
6 changes: 6 additions & 0 deletions STranslate/Views/Preference/CommonPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
SelectedValue="{Binding DoubleTapTrayFunc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Value" />
</DockPanel>

<DockPanel Margin="20,10">
<TextBlock Text="禁用系统代理" />
<TextBlock Style="{DynamicResource InfoTextBlock}" ToolTip="是否禁用彻底不走系统代理,默认走系统代理(如果开启),禁用后不论系统代理是否开启均走直连" />
<ToggleButton Height="26" HorizontalAlignment="Right" IsChecked="{Binding IsDisableSystemProxy}" />
</DockPanel>
</StackPanel>
</Border>

Expand Down

0 comments on commit fa743e7

Please sign in to comment.