-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
552 lines (462 loc) · 17.5 KB
/
Program.cs
File metadata and controls
552 lines (462 loc) · 17.5 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Windows.Forms;
internal static class Program
{
private static readonly string AppDir =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "LinkAction");
private static readonly string BrowserFile = Path.Combine(AppDir, "browser.txt");
private static readonly string LogFile = Path.Combine(AppDir, "log.txt");
private static readonly string AllowlistFile = Path.Combine(AppDir, "allowlist.txt");
// Only show logic for Barracuda-protected URLs
private const string CudaFilter = "https://linkprotect.cudasvc.com/url?a=";
[STAThread]
[SupportedOSPlatform("windows")]
private static void Main(string[] args)
{
try
{
Directory.CreateDirectory(AppDir);
EnsureAllowlistTemplateExists();
Log("=== LinkAction started ===");
Log($"Args count={args.Length}; Args={string.Join(" | ", args)}");
if (args.Length == 0)
{
Log("No URL argument. Exiting.");
return;
}
var raw = args[0].Trim('"', ' ');
Log($"Raw arg[0]={raw}");
if (string.IsNullOrWhiteSpace(raw))
{
Log("Empty URL argument. Exiting.");
return;
}
var url = NormalizeUrl(raw);
Log($"Normalized URL={url}");
if (string.IsNullOrWhiteSpace(url))
{
Log("Not a valid http/https URL. Exiting.");
return;
}
// 1) Only care about Barracuda-protected URLs at all.
if (!url.Contains(CudaFilter, StringComparison.OrdinalIgnoreCase))
{
Log("URL does not match CudaFilter. Forwarding without prompt.");
OpenInRealBrowser(url);
return;
}
// 2) For Cuda URLs, only prompt when launched from Outlook / olk / etc.
bool fromOutlook = IsCallerOutlook();
Log($"IsCallerOutlook={fromOutlook}");
if (!fromOutlook)
{
Log("Cuda URL but caller is not Outlook. Forwarding without prompt.");
OpenInRealBrowser(url);
return;
}
// 3) Resolve the real target host for allowlist + display
var targetHost = GetTargetHost(url);
Log($"Resolved target host: {targetHost}");
// 4) If host is in external allowlist, skip prompt.
if (!string.IsNullOrEmpty(targetHost) && IsAllowedDomain(targetHost))
{
Log($"Target host '{targetHost}' is allowlisted. Forwarding without prompt.");
OpenInRealBrowser(url);
return;
}
// 5) Build friendly display text for popup
var displayTarget = GetDisplayTarget(url, targetHost);
Log($"Display target: {displayTarget}");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Build the text you already have
var displayText = $"Are you sure you want to open this link?\n\n{displayTarget}";
// Show our tiny custom dialog with the embedded logo
DialogResult result;
using (var dlg = new ConfirmDialog(displayText, LoadEmbeddedLogo()))
{
result = dlg.ShowDialog();
}
Log($"User clicked: {result}");
if (result == DialogResult.Yes)
{
Log("User confirmed. Opening in real browser.");
OpenInRealBrowser(url);
}
else
{
Log("User declined. Not opening URL.");
}
Log($"User clicked: {result}");
if (result == DialogResult.Yes)
{
Log("User confirmed. Opening in real browser.");
OpenInRealBrowser(url);
}
else
{
Log("User declined. Not opening URL.");
}
}
catch (Exception ex)
{
try { Log("ERROR: " + ex); } catch { /* ignore */ }
}
}
// ----- URL helpers -----
private static string NormalizeUrl(string input)
{
if (input.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
input.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
return input;
}
return string.Empty;
}
/// <summary>
/// Extract the real target host from Barracuda SafeLink (a= param).
/// Returns null/empty if it cannot be determined.
/// </summary>
private static string GetTargetHost(string url)
{
try
{
var outer = new Uri(url);
if (!outer.Host.Contains("linkprotect.cudasvc.com", StringComparison.OrdinalIgnoreCase))
return outer.Host ?? string.Empty;
var query = outer.Query.TrimStart('?')
.Split('&', StringSplitOptions.RemoveEmptyEntries);
foreach (var part in query)
{
var idx = part.IndexOf('=');
if (idx <= 0) continue;
var key = part[..idx];
var val = part[(idx + 1)..];
if (!key.Equals("a", StringComparison.OrdinalIgnoreCase))
continue;
var decoded = Uri.UnescapeDataString(val);
if (Uri.TryCreate(decoded, UriKind.Absolute, out var inner))
return inner.Host ?? string.Empty;
return string.Empty;
}
return string.Empty;
}
catch (Exception ex)
{
Log("GetTargetHost error: " + ex.Message);
return string.Empty;
}
}
/// <summary>
/// Human-friendly text for the popup.
/// Prefer the resolved host; fall back to original URL.
/// </summary>
private static string GetDisplayTarget(string originalUrl, string targetHost)
{
if (!string.IsNullOrWhiteSpace(targetHost))
return targetHost;
return originalUrl;
}
// ----- Allowlist (file-based) -----
private static void EnsureAllowlistTemplateExists()
{
try
{
if (!File.Exists(AllowlistFile))
{
var template = new[]
{
"# LinkAction allowlist",
"# One domain per line.",
"# Examples:",
"# yourcompany.com",
"# corp.yourcompany.com",
"# intranet.yourcompany.local",
""
};
File.WriteAllLines(AllowlistFile, template);
Log("Created allowlist template file.");
}
}
catch
{
// Non-fatal; if we can't create it, we just won't use allowlist.
}
}
private static bool IsAllowedDomain(string host)
{
try
{
if (string.IsNullOrWhiteSpace(host))
return false;
host = host.ToLowerInvariant();
if (!File.Exists(AllowlistFile))
return false;
var lines = File.ReadAllLines(AllowlistFile);
foreach (var raw in lines)
{
var d = raw.Trim();
// Skip comments and blanks
if (string.IsNullOrEmpty(d) || d.StartsWith("#"))
continue;
d = d.ToLowerInvariant();
// Exact host or subdomain match
if (host == d || host.EndsWith("." + d, StringComparison.Ordinal))
return true;
}
}
catch (Exception ex)
{
Log("IsAllowedDomain error: " + ex.Message);
}
return false;
}
// ----- Browser launching -----
private static void OpenInRealBrowser(string url)
{
var browserPath = GetPreferredBrowserPath();
if (!string.IsNullOrWhiteSpace(browserPath) && File.Exists(browserPath))
{
Log($"Launching real browser: {browserPath} {url}");
Process.Start(new ProcessStartInfo
{
FileName = browserPath,
Arguments = $"\"{url}\"",
UseShellExecute = false
});
}
else
{
Log("No explicit browser found; using system default.");
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
}
private static string GetPreferredBrowserPath()
{
// Optional override: %LOCALAPPDATA%\LinkAction\browser.txt
try
{
if (File.Exists(BrowserFile))
{
var p = File.ReadAllText(BrowserFile).Trim();
if (!string.IsNullOrEmpty(p) && File.Exists(p))
{
Log($"Using browser from browser.txt: {p}");
return p;
}
}
}
catch (Exception ex)
{
Log("GetPreferredBrowserPath error: " + ex.Message);
}
var candidates = new[]
{
@"C:\Program Files\Google\Chrome\Application\chrome.exe",
@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
@"C:\Program Files\Microsoft\Edge\Application\msedge.exe",
@"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe",
@"C:\Program Files\Mozilla Firefox\firefox.exe"
};
var found = candidates.FirstOrDefault(File.Exists) ?? string.Empty;
if (!string.IsNullOrEmpty(found))
Log($"Detected browser: {found}");
return found;
}
// ----- Outlook caller detection via Toolhelp snapshot -----
private static bool IsCallerOutlook()
{
try
{
using var current = Process.GetCurrentProcess();
int pid = current.Id;
// Walk up to 5 levels of parent processes
for (int depth = 0; depth < 5; depth++)
{
int parentId = GetParentProcessId(pid);
if (parentId <= 0)
break;
using var parent = Process.GetProcessById(parentId);
var name = parent.ProcessName.ToLowerInvariant();
Log($"Ancestor[{depth}]: {name} (PID={parentId})");
if (IsOutlookProcessName(name))
return true;
pid = parentId;
}
}
catch (Exception ex)
{
Log("IsCallerOutlook error: " + ex.Message);
}
return false;
}
private static bool IsOutlookProcessName(string name)
{
return name.Contains("outlook")
|| name.Contains("hxoutlook")
|| name == "olk"
|| name.Contains("microsoft.outlook");
}
// ----- P/Invoke: get parent process ID -----
private static int GetParentProcessId(int pid)
{
const uint TH32CS_SNAPPROCESS = 0x00000002;
IntPtr snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == IntPtr.Zero || snapshot.ToInt64() == -1)
return 0;
try
{
PROCESSENTRY32 procEntry = new PROCESSENTRY32();
procEntry.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
if (!Process32First(snapshot, ref procEntry))
return 0;
do
{
if (procEntry.th32ProcessID == pid)
return (int)procEntry.th32ParentProcessID;
}
while (Process32Next(snapshot, ref procEntry));
}
finally
{
CloseHandle(snapshot);
}
return 0;
}
[StructLayout(LayoutKind.Sequential)]
private struct PROCESSENTRY32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szExeFile;
}
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);
[DllImport("kernel32.dll")]
private static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
[DllImport("kernel32.dll")]
private static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);
// ----- Logging -----
private static void Log(string message)
{
File.AppendAllText(LogFile, $"{DateTime.Now:O} {message}{Environment.NewLine}");
}
// Loads the embedded PNG (see embedding step below)
private static Image? LoadEmbeddedLogo()
{
try
{
const string resourceName = "LinkAction.logo.png"; // LogicalName in .csproj
var asm = Assembly.GetExecutingAssembly();
using var s = asm.GetManifestResourceStream(resourceName);
if (s == null) return null;
return Image.FromStream(s);
}
catch { return null; }
}
// Lightweight dialog that mimics MessageBox with Yes/No but shows a logo
internal sealed class ConfirmDialog : Form
{
public ConfirmDialog(string message, Image? logo)
{
Text = "Security Warning LinkAction";
StartPosition = FormStartPosition.CenterScreen;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
TopMost = true;
BackColor = Color.White;
AutoScaleMode = AutoScaleMode.Dpi;
Padding = new Padding(0); // we'll pad via the panel
var tl = new TableLayoutPanel
{
Dock = DockStyle.Fill,
Padding = new Padding(16),
ColumnCount = 2,
RowCount = 3
};
tl.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
tl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
tl.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tl.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tl.RowStyles.Add(new RowStyle(SizeType.AutoSize));
Controls.Add(tl);
var pic = new PictureBox
{
Size = new Size(64, 64),
SizeMode = PictureBoxSizeMode.Zoom,
Margin = new Padding(0, 0, 16, 0),
Image = logo ?? SystemIcons.Warning.ToBitmap()
};
var title = new Label
{
AutoSize = true,
Text = "Are you sure you want to open this link?",
Font = new Font(SystemFonts.MessageBoxFont, FontStyle.Bold),
ForeColor = Color.Black,
Margin = new Padding(0, 4, 0, 8)
};
var body = new Label
{
AutoSize = true,
Text = message.Contains("\n\n") ? message.Split("\n\n")[1] : message, // show target on its own line
Font = new Font(SystemFonts.MessageBoxFont.FontFamily, SystemFonts.MessageBoxFont.Size + 1),
ForeColor = Color.FromArgb(32, 32, 32),
Margin = new Padding(0, 0, 0, 12),
MaximumSize = new Size(680, 0) // wrap long domains/URLs
};
var buttons = new FlowLayoutPanel
{
FlowDirection = FlowDirection.RightToLeft,
Dock = DockStyle.Fill,
AutoSize = true
};
var yes = new Button { Text = "Yes", DialogResult = DialogResult.Yes, AutoSize = true, Padding = new Padding(16, 6, 16, 6) };
var no = new Button { Text = "No", DialogResult = DialogResult.No, AutoSize = true, Padding = new Padding(16, 6, 16, 6) };
buttons.Controls.Add(yes);
buttons.Controls.Add(no);
AcceptButton = yes; // Enter
CancelButton = no; // Esc
no.Select(); // default focus
// Layout
tl.Controls.Add(pic, 0, 0);
tl.SetRowSpan(pic, 2);
tl.Controls.Add(title, 1, 0);
tl.Controls.Add(body, 1, 1);
tl.Controls.Add(buttons, 1, 2);
// --- Robust sizing: measure & clamp to screen ---
tl.PerformLayout();
var work = Screen.PrimaryScreen!.WorkingArea;
// Ask the panel what size it wants if it could grow reasonably wide
var preferred = tl.GetPreferredSize(new Size(720, 0));
int desiredW = Math.Min(preferred.Width + 32, Math.Max(480, work.Width - 120));
int desiredH = Math.Min(preferred.Height + 32, Math.Max(180, work.Height - 200));
// Apply as ClientSize so borders are accounted for by WinForms
ClientSize = new Size(desiredW, desiredH);
// Dont let the OS shrink us to nothing because of DPI/AutoSize quirks
MinimumSize = new Size(480, 180);
}
}
}