Created
February 23, 2024 11:05
-
-
Save ifree/797a3239dd5a793abedf8aee8f8a52a9 to your computer and use it in GitHub Desktop.
PowerToys Run Proxy switch plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Threading; | |
using System.Windows; | |
using System.Windows.Controls; | |
using Microsoft.PowerToys.Settings.UI.Library; | |
using Wox.Plugin; | |
using Wox.Plugin.Logger; | |
using Shadowsocks.WPF.Services.SystemProxy; | |
using BrowserInfo = Wox.Plugin.Common.DefaultBrowserInfo; | |
namespace Community.Powertoys.Run.Plugin.BrowserSearch | |
{ | |
public class Main : IPlugin, ISettingProvider, IReloadable | |
{ | |
public static string PluginID => "027E3DC8F6B441A8BD44B1025DA8439A"; | |
public string Name => "Proxy Switch"; | |
public string Description => "Switch proxy with ease."; | |
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>() | |
{ | |
new() | |
{ | |
Key = ProxyAddr, | |
DisplayLabel = "Proxy address", | |
DisplayDescription = "Proxy address", | |
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox, | |
TextValue = "http://1.2.3.4:8080" | |
}, | |
new() | |
{ | |
Key = ProxyBypass, | |
DisplayLabel = "Proxy bypass", | |
DisplayDescription = "Proxy bypass, used in conjunction with the proxy address", | |
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox, | |
TextValue = "localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*;*.local" | |
}, | |
new() | |
{ | |
Key = PacAddr, | |
DisplayLabel = "Pac address", | |
DisplayDescription = "Pac address", | |
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox, | |
TextValue = "http://1.2.3.4/proxy.pac" | |
} | |
}; | |
private const string ProxyAddr = nameof(ProxyAddr); | |
private const string ProxyBypass = nameof(ProxyBypass); | |
private const string PacAddr = nameof(PacAddr); | |
private const string Enabled = nameof(Enabled); | |
private PluginInitContext? _context; | |
private long _lastUpdateTickCount = -300L; | |
private string _proxyAddr; | |
private string _proxyBypass; | |
private string _pacAddr; | |
public void Init(PluginInitContext context) | |
{ | |
_context = context ?? throw new ArgumentNullException(nameof(context)); | |
} | |
public void ReloadData() | |
{ | |
if (_context is null) | |
{ | |
return; | |
} | |
// When the plugin is disabled and then re-enabled, | |
// ReloadData() is called multiple times so this is needed | |
if (Environment.TickCount64 - _lastUpdateTickCount >= 300) | |
{ | |
_lastUpdateTickCount = Environment.TickCount64; | |
} | |
} | |
public Control CreateSettingPanel() | |
{ | |
throw new NotImplementedException(); | |
} | |
public void UpdateSettings(PowerLauncherPluginSettings settings) | |
{ | |
_proxyAddr = (settings?.AdditionalOptions?.FirstOrDefault(x => x.Key == ProxyAddr)?.TextValue ?? ""); | |
_proxyBypass = (settings?.AdditionalOptions?.FirstOrDefault(x => x.Key == ProxyBypass)?.TextValue ?? ""); | |
_pacAddr = (settings?.AdditionalOptions?.FirstOrDefault(x => x.Key == PacAddr)?.TextValue ?? ""); | |
} | |
public List<Result> Query(Query query) | |
{ | |
ArgumentNullException.ThrowIfNull(query); | |
List<Result> results = new(); | |
if (!string.IsNullOrEmpty(_proxyAddr)) | |
{ | |
results.Add(new Result | |
{ | |
Title = ProxyAddr, | |
SubTitle = _proxyAddr, | |
Action = ctx => | |
{ | |
WinINet.ProxyGlobal(_proxyAddr, _proxyBypass); | |
return true; | |
} | |
}); | |
} | |
if (!string.IsNullOrEmpty(_pacAddr)) | |
{ | |
results.Add(new Result | |
{ | |
Title = PacAddr, | |
SubTitle = _pacAddr, | |
Action = ctx => | |
{ | |
WinINet.ProxyPAC(_pacAddr); | |
return true; | |
} | |
}); | |
} | |
results.Add(new Result | |
{ | |
Title = "Disable proy", | |
Action = ctx => | |
{ | |
WinINet.Restore(); | |
return true; | |
} | |
}); | |
return results; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment