Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingxi-Li committed Jul 23, 2023
0 parents commit d82df16
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin
obj
.vs
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>
97 changes: 97 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Microsoft.Win32;
using System;
using System.Drawing;
using System.Management;
using System.Security.Principal;
using System.Windows.Forms;

namespace ProxyTray
{
internal static class Program
{
const string KeyPath = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
static readonly string StatusChangeEventQuery = (
"SELECT * FROM RegistryValueChangeEvent WHERE " +
$"Hive = 'HKEY_USERS' AND KeyPath = '{WindowsIdentity.GetCurrent().User.Value}\\{KeyPath}' " +
"AND (ValueName = 'ProxyEnable' or ValueName = 'ProxyServer')"
).Replace(@"\", @"\\");
const string None = "<None>";

static Icon ProxyOnIcon;
static Icon ProxyOffIcon;
static NotifyIcon Tray;

static bool ProxyEnable;
static string ProxyServer;

static void Initialize()
{
ProxyOnIcon = new Icon("ProxyOn.ico");
ProxyOffIcon = new Icon("ProxyOff.ico");

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
}

static void CreateTray()
{
var menuItem = new MenuItem
{
Index = 0,
Text = "Quit"
};
menuItem.Click += (sender, e) => Application.Exit();
Tray = new NotifyIcon
{
ContextMenu = new ContextMenu(new[] { menuItem }),
Visible = true
};
Tray.MouseClick += (sender, e) =>
{
if (e.Button != MouseButtons.Left) return;
if (ProxyServer == None)
{
MessageBox.Show("'ProxyServer' not configured");
return;
}
using (var key = Registry.CurrentUser.OpenSubKey(KeyPath, true))
{
key.SetValue("ProxyEnable", ProxyEnable ? 0 : 1);
}
};
}

static void RefreshTray()
{
using (var key = Registry.CurrentUser.OpenSubKey(KeyPath))
{
ProxyEnable = (int)(key.GetValue("ProxyEnable", 0)) != 0;
ProxyServer = (string)(key.GetValue("ProxyServer", None));
}
Tray.Icon = ProxyEnable ? ProxyOnIcon : ProxyOffIcon;
Tray.Text = ProxyEnable ? ProxyServer : "Direct";
}

[STAThread]
static void Main()
{
try
{
Initialize();
CreateTray();
RefreshTray();

using (var watcher = new ManagementEventWatcher(StatusChangeEventQuery))
{
watcher.EventArrived += (sender, e) => RefreshTray();
watcher.Start();
Application.Run();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
36 changes: 36 additions & 0 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Proxy Tray")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Proxy Tray")]
[assembly: AssemblyCopyright("Copyright © 2023 Lingxi Li")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c48afb87-3942-40af-be7c-7d9f7e508458")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Binary file added ProxyOff.ico
Binary file not shown.
Binary file added ProxyOn.ico
Binary file not shown.
65 changes: 65 additions & 0 deletions ProxyTray.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C48AFB87-3942-40AF-BE7C-7D9F7E508458}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>ProxyTray</RootNamespace>
<AssemblyName>ProxyTray</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="ProxyOff.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="ProxyOn.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
25 changes: 25 additions & 0 deletions ProxyTray.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProxyTray", "ProxyTray.csproj", "{C48AFB87-3942-40AF-BE7C-7D9F7E508458}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C48AFB87-3942-40AF-BE7C-7D9F7E508458}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C48AFB87-3942-40AF-BE7C-7D9F7E508458}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C48AFB87-3942-40AF-BE7C-7D9F7E508458}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C48AFB87-3942-40AF-BE7C-7D9F7E508458}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {26037BBC-A4A2-477A-8BF1-5BB89FD1A223}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Proxy Tray

A Windows tray app that monitors WinINet proxy settings for LAN (not VPN) and enables quick proxy on/off switch.

- A blue/yellow tray icon indicates proxy off/on status, respectively.
- A mouse hover-over tip shows proxy server info when proxy is on or "Direct" when off.
- Left click on the tray icon toggles proxy on/off status. Proxy server info must be pre-configured externally.
- Quit with "Quit" in right-click context menu.

Targets .NET Framework 4.8 that is included in Windows 11.

0 comments on commit d82df16

Please sign in to comment.