-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (81 loc) · 3.01 KB
/
Copy pathProgram.cs
File metadata and controls
93 lines (81 loc) · 3.01 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
using System;
using Titanium.Web.Proxy.Examples.Basic.Helpers;
namespace Titanium.Web.Proxy.Examples.Basic
{
public static class Program
{
private static readonly ProxyTestController controller = new ProxyTestController();
private static readonly object exitLock = new object();
private static bool exiting;
public static void Main(string[] args)
{
if (OperatingSystem.IsWindows())
// fix console hang due to QuickEdit mode
ConsoleHelper.DisableQuickEditMode();
Console.Title = "Titanium Web Proxy";
// Ctrl+C / Ctrl+Break: cancel the default terminate so finally can clear system proxy.
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
RequestExit();
};
try
{
controller.StartProxy();
Console.WriteLine("Traffic tape: one compact line per completed request (errors are one-liners).");
Console.WriteLine("Hit any key to exit..");
Console.WriteLine();
if (Console.IsInputRedirected)
// Console.Read() returns immediately (EOF) when stdin has no real interactive source
// (e.g. run under a process launcher with redirected/absent input), which would
// otherwise tear the proxy down right after starting it - block forever instead so the
// process behaves like a long-running service until explicitly killed.
// System proxy restore on kill relies on ProcessExit / console-control handlers in the library.
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
else
Console.Read();
}
finally
{
Shutdown();
}
}
private static void RequestExit()
{
// Second Ctrl+C: allow the runtime to terminate if shutdown is stuck.
lock (exitLock)
{
if (exiting)
Environment.Exit(0);
}
// Wake the main thread from Console.Read by closing stdin isn't portable;
// calling Exit runs ProcessExit (library restores proxy) then finally below if we Shutdown first.
Shutdown();
Environment.Exit(0);
}
private static void Shutdown()
{
lock (exitLock)
{
if (exiting) return;
exiting = true;
}
try
{
controller.Stop();
}
catch (Exception)
{
// Best-effort: Stop may throw if the proxy never started or already stopped.
}
try
{
controller.Dispose();
}
catch (Exception)
{
// Best-effort cleanup.
}
}
}
}