Skip to content

Commit

Permalink
* Check if the 'WinHttpAutoProxySvc' is running on the machine, if no…
Browse files Browse the repository at this point in the history
…t, set startup type to 'Manual', and notify the user to restart the machine

* Version Update
  • Loading branch information
ArachisH committed May 27, 2020
1 parent 0cc765e commit e7644c8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
77 changes: 66 additions & 11 deletions Tanji/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Security.Principal;
using System.Collections.Generic;

using Tanji.Windows;
Expand All @@ -14,36 +17,49 @@
using Flazzy;
using Flazzy.IO;

using Microsoft.Win32;

namespace Tanji
{
public static class Program
{
public static bool HasAdminPrivilages { get; private set; }
public static Dictionary<string, object> Settings { get; private set; }

[STAThread]
private static void Main(string[] args)
private static int Main(string[] args)
{
using (var identity = WindowsIdentity.GetCurrent())
{
HasAdminPrivilages = new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator);
}

Settings = LoadSettings();
if (args.Length > 0)
{
switch (args[0].Substring(args[0].Length - 3))
{
case "swf": PatchClient(new FileInfo(Path.GetFullPath(args[0]))); break;
case "ica": InstallCertificateAuthority(); break;
case "dcs": DestroyCertificates(); break;
case "ica": InstallCertificateAuthority(); break;
case "ems": return (int)EnsureManualWinHttpAutoProxySvcStartup();
case "swf": PatchClient(new FileInfo(Path.GetFullPath(args[0]))); break;
}
return;
return 0;
}

Eavesdropper.Certifier = new CertificateManager("Tanji", "Tanji Certificate Authority");
Eavesdropper.Overrides.AddRange(((string)Settings["ProxyOverrides"]).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
Eavesdropper.Terminate();
if (EnsureManualWinHttpAutoProxySvcStartup() == ServiceControllerStatus.Running)
{
Eavesdropper.Certifier = new CertificateManager("Tanji", "Tanji Certificate Authority");
Eavesdropper.Overrides.AddRange(((string)Settings["ProxyOverrides"]).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
Eavesdropper.Terminate();

AppDomain.CurrentDomain.UnhandledException += UnhandledException;
AppDomain.CurrentDomain.UnhandledException += UnhandledException;

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainFrm());
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainFrm());
}
return 1;
}

private static void DestroyCertificates()
Expand Down Expand Up @@ -78,6 +94,20 @@ private static void PatchClient(FileInfo clientInfo)
}
}

public static int RunTanjiAsAdmin(string argument)
{
using (var proc = new Process())
{
proc.StartInfo.FileName = Path.GetFullPath("Tanji.exe");
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.Verb = "runas";
proc.StartInfo.Arguments = argument;

proc.Start();
proc.WaitForExit();
return proc.ExitCode;
}
}
private static Dictionary<string, object> LoadSettings()
{
var settings = new Dictionary<string, object>();
Expand Down Expand Up @@ -108,6 +138,31 @@ private static Dictionary<string, object> LoadSettings()
}
return settings;
}

private static ServiceControllerStatus EnsureManualWinHttpAutoProxySvcStartup()
{
using (var controller = new ServiceController("WinHttpAutoProxySvc"))
{
ServiceControllerStatus status = controller.Status;
if (controller.StartType == ServiceStartMode.Disabled)
{
if (HasAdminPrivilages)
{
RegistryKey winHttpAutoProxySvcKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Services\WinHttpAutoProxySvc", true);
winHttpAutoProxySvcKey.SetValue("Start", ServiceStartMode.Manual, RegistryValueKind.DWord);
winHttpAutoProxySvcKey.Flush();

}
else status = (ServiceControllerStatus)RunTanjiAsAdmin("ems");
}
if (status != ServiceControllerStatus.Running && !HasAdminPrivilages)
{
// Tanji was opened again, but having already set the registry value, and not having done a full restart(smh)
MessageBox.Show("Changes have been made regarding the 'WinHttpAutoProxy' service, please restart your computer for them to take effect.", "Tanji - Alert! ", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
return status;
}
}
private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = (Exception)e.ExceptionObject;
Expand Down
2 changes: 1 addition & 1 deletion Tanji/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.0.0")]
[assembly: AssemblyFileVersion("1.4.1390.0")]
[assembly: AssemblyFileVersion("1.4.1400.0")]
1 change: 1 addition & 0 deletions Tanji/Tanji.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down

0 comments on commit e7644c8

Please sign in to comment.