-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
200 lines (189 loc) · 6.82 KB
/
Program.cs
File metadata and controls
200 lines (189 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Principal;
using System.Web;
using System.Windows.Forms;
namespace EdgeRemover
{
internal class Program
{
private const string EDGE_PROTOCOL = "microsoft-edge:?";
private const string IFEO_PATH = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\msedge.exe";
private const string EDGE_EXE = "msedge.exe";
private const string ER_FLAG = "--edge-remover";
private static string Lock => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "edge-remover.lock");
private static readonly string[] IGNORED_FLAGS = new string[] { "--single-argument" };
static void Main(string[] args)
{
if (args.Length == 0)
{
if (!IsUserAdmin())
{
RelaunchAsAdmin();
}
Register();
}
else
{
string url, extra;
var query = args.FirstOrDefault(v => v.StartsWith(EDGE_PROTOCOL));
if (query != null)
{
var index = query.IndexOf("url=");
if (index < 0)
{
MessageBox.Show(string.Format("Unexpected input: {0}", query), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}
var indexEnd = query.IndexOf('&', index + 1);
if (indexEnd < 0)
{
indexEnd = query.Length;
}
url = HttpUtility.UrlDecode(query.Substring(index + 4, indexEnd - index - 4));
extra = "";
}
else if (args[0].Contains(EDGE_EXE))
{
url = string.Empty;
extra = string.Empty;
if (args.Count() <= 1)
{
// this is a normal call
MessageBox.Show("Microsoft Edge won't be called in your system.", "EdgeRemover", MessageBoxButtons.OK, MessageBoxIcon.Information);
Environment.Exit(1);
}
else if (args.Contains(ER_FLAG) || DateTime.Now - ReadLastCall() < TimeSpan.FromSeconds(1))
{
// this is a loopback
MessageBox.Show("This file is set to be opened with Edge, which is blocked.\nTry another app.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}
else
{
for (var i = 1; i < args.Length; i++)
{
if (IGNORED_FLAGS.Contains(args[i]))
{
continue;
}
if (url == string.Empty)
{
url = args[i];
}
else
{
extra += args[i] + " ";
}
}
}
}
else
{
extra = string.Empty;
url = args[0];
for (var i = 1; i < args.Length; i++)
{
extra += args[i] + " ";
}
}
Open(url, extra);
}
}
static void Open(string file, string extraArg)
{
var timeInstant = DateTime.Now.ToString();
if (!File.Exists(Lock))
{
try
{
using (var writer = new StreamWriter(File.Create(Lock)))
{
writer.WriteLine(timeInstant);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} else {
File.WriteAllText(Lock, timeInstant);
}
var process = new ProcessStartInfo()
{
FileName = file,
Arguments = extraArg
};
Process.Start(process);
}
private static DateTime ReadLastCall()
{
if (!File.Exists(Lock))
{
throw new FileNotFoundException(Lock);
}
var str = File.ReadAllText(Lock);
return DateTime.Parse(str);
}
static bool IsUserAdmin()
{
bool isAdmin = false;
try
{
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(user);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (Exception)
{
}
return isAdmin;
}
static void RelaunchAsAdmin()
{
Console.WriteLine("This app is invoked by user acively.");
Console.WriteLine("==> Will register itself in Image File Execution Options registry.");
ProcessStartInfo proc = new ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Assembly.GetEntryAssembly().CodeBase,
Verb = "runas"
};
try
{
Process.Start(proc);
Environment.Exit(0);
}
catch (Exception)
{
MessageBox.Show(
"This program must be run as an administrator this time!",
"Registration failed",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
static void Register()
{
try
{
using (RegistryKey key = Registry.LocalMachine.CreateSubKey(IFEO_PATH))
{
key.SetValue("Debugger", Assembly.GetExecutingAssembly().Location);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Environment.Exit(1);
}
MessageBox.Show(string.Format(@"See Computer\HKEY_LOCAL_MACHINE\{0} for details.", IFEO_PATH), "Registration successful");
}
}
}