Skip to content

Commit 322321d

Browse files
Add option to not run as system proxy and do not install self-signed cert (#530)
* Add --do-not-act-as-system-proxy option #505 * Add --do-not-install-self-signed-cert option #505 * Remove unnecessary launch settings * Rename launch options to avoid double negations * Run EnsureRootCertificate only on non-windows OS * Rework AsSystemProxy condition * Combine conditions in FirstRunSetup * Update --as-system-proxy description * Unify EOL in ProxyHost * Adds comment * Update ProxyHost.cs * Change loading of cert if it should not be installed --------- Co-authored-by: Waldek Mastykarz <[email protected]>
1 parent 6b7e9a6 commit 322321d

File tree

5 files changed

+68
-22
lines changed

5 files changed

+68
-22
lines changed

Diff for: dev-proxy/Properties/launchSettings.json

100644100755
File mode changed.

Diff for: dev-proxy/ProxyCommandHandler.cs

100644100755
+16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class ProxyCommandHandler : ICommandHandler
1818
public Option<IEnumerable<string>?> WatchProcessNames { get; set; }
1919
public Option<int?> Rate { get; set; }
2020
public Option<bool?> NoFirstRun { get; set; }
21+
public Option<bool?> AsSystemProxy { get; set; }
22+
public Option<bool?> InstallCert { get; set; }
2123

2224
private readonly PluginEvents _pluginEvents;
2325
private readonly ISet<UrlToWatch> _urlsToWatch;
@@ -31,6 +33,8 @@ public ProxyCommandHandler(Option<int?> port,
3133
Option<IEnumerable<string>?> watchProcessNames,
3234
Option<int?> rate,
3335
Option<bool?> noFirstRun,
36+
Option<bool?> asSystemProxy,
37+
Option<bool?> installCert,
3438
PluginEvents pluginEvents,
3539
ISet<UrlToWatch> urlsToWatch,
3640
ILogger logger)
@@ -43,6 +47,8 @@ public ProxyCommandHandler(Option<int?> port,
4347
WatchProcessNames = watchProcessNames ?? throw new ArgumentNullException(nameof(watchProcessNames));
4448
Rate = rate ?? throw new ArgumentNullException(nameof(rate));
4549
NoFirstRun = noFirstRun ?? throw new ArgumentNullException(nameof(noFirstRun));
50+
AsSystemProxy = asSystemProxy ?? throw new ArgumentNullException(nameof(asSystemProxy));
51+
InstallCert = installCert ?? throw new ArgumentNullException(nameof(installCert));
4652
_pluginEvents = pluginEvents ?? throw new ArgumentNullException(nameof(pluginEvents));
4753
_urlsToWatch = urlsToWatch ?? throw new ArgumentNullException(nameof(urlsToWatch));
4854
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
@@ -95,6 +101,16 @@ public async Task<int> InvokeAsync(InvocationContext context)
95101
{
96102
Configuration.NoFirstRun = noFirstRun.Value;
97103
}
104+
var asSystemProxy = context.ParseResult.GetValueForOption(AsSystemProxy);
105+
if (asSystemProxy is not null)
106+
{
107+
Configuration.AsSystemProxy = asSystemProxy.Value;
108+
}
109+
var installCert = context.ParseResult.GetValueForOption(InstallCert);
110+
if (installCert is not null)
111+
{
112+
Configuration.InstallCert = installCert.Value;
113+
}
98114

99115
CancellationToken? cancellationToken = (CancellationToken?)context.BindingContext.GetService(typeof(CancellationToken?));
100116

Diff for: dev-proxy/ProxyConfiguration.cs

100644100755
+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class ProxyConfiguration : IProxyConfiguration
3636
[JsonPropertyName("rate")]
3737
public int Rate { get; set; } = 50;
3838
public bool NoFirstRun { get; set; } = false;
39+
public bool AsSystemProxy { get; set; } = true;
40+
public bool InstallCert { get; set; } = true;
3941
public string ConfigFile { get; set; } = "devproxyrc.json";
4042
[JsonPropertyName("newVersionNotification")]
4143
[JsonConverter(typeof(JsonStringEnumConverter))]

Diff for: dev-proxy/ProxyEngine.cs

100644100755
+29-20
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public async Task Run(CancellationToken? cancellationToken)
8686

8787
_proxyServer.CertificateManager.RootCertificateName = "Dev Proxy CA";
8888
_proxyServer.CertificateManager.CertificateStorage = new CertificateDiskCache();
89+
// we need to change this to a value lower than 397
90+
// to avoid the ERR_CERT_VALIDITY_TOO_LONG error in Edge
91+
_proxyServer.CertificateManager.CertificateValidDays = 365;
92+
_proxyServer.CertificateManager.CreateRootCertificate();
8993
_proxyServer.BeforeRequest += OnRequest;
9094
_proxyServer.BeforeResponse += OnBeforeResponse;
9195
_proxyServer.AfterResponse += OnAfterResponse;
@@ -95,18 +99,16 @@ public async Task Run(CancellationToken? cancellationToken)
9599

96100
var ipAddress = string.IsNullOrEmpty(_config.IPAddress) ? IPAddress.Any : IPAddress.Parse(_config.IPAddress);
97101
_explicitEndPoint = new ExplicitProxyEndPoint(ipAddress, _config.Port, true);
98-
if (!RunTime.IsWindows)
102+
// Fired when a CONNECT request is received
103+
_explicitEndPoint.BeforeTunnelConnectRequest += OnBeforeTunnelConnectRequest;
104+
if (_config.InstallCert)
99105
{
100-
// we need to change this to a value lower than 397
101-
// to avoid the ERR_CERT_VALIDITY_TOO_LONG error in Edge
102-
_proxyServer.CertificateManager.CertificateValidDays = 365;
103-
// we need to call it explicitly for non-Windows OSes because it's
104-
// a part of the SetAsSystemHttpProxy that works only on Windows
105106
_proxyServer.CertificateManager.EnsureRootCertificate();
106107
}
107-
108-
// Fired when a CONNECT request is received
109-
_explicitEndPoint.BeforeTunnelConnectRequest += OnBeforeTunnelConnectRequest;
108+
else
109+
{
110+
_explicitEndPoint.GenericCertificate = _proxyServer.CertificateManager.LoadRootCertificate();
111+
}
110112

111113
_proxyServer.AddEndPoint(_explicitEndPoint);
112114
_proxyServer.Start();
@@ -119,18 +121,24 @@ public async Task Run(CancellationToken? cancellationToken)
119121
_logger.LogInfo($"Listening on {endPoint.IpAddress}:{endPoint.Port}...");
120122
}
121123

122-
if (RunTime.IsWindows)
123-
{
124-
// Only explicit proxies can be set as system proxy!
125-
_proxyServer.SetAsSystemHttpsProxy(_explicitEndPoint);
126-
}
127-
else if (RunTime.IsMac)
124+
if (_config.AsSystemProxy)
128125
{
129-
ToggleSystemProxy(ToggleSystemProxyAction.On, _config.IPAddress, _config.Port);
126+
if (RunTime.IsWindows)
127+
{
128+
_proxyServer.SetAsSystemHttpsProxy(_explicitEndPoint);
129+
}
130+
else if (RunTime.IsMac)
131+
{
132+
ToggleSystemProxy(ToggleSystemProxyAction.On, _config.IPAddress, _config.Port);
133+
}
134+
else
135+
{
136+
_logger.LogWarn("Configure your operating system to use this proxy's port and address");
137+
}
130138
}
131139
else
132140
{
133-
_logger.LogWarn("Configure your operating system to use this proxy's port and address");
141+
_logger.LogInfo("Configure your application to use this proxy's port and address");
134142
}
135143

136144
_logger.LogInfo("Press CTRL+C to stop Dev Proxy");
@@ -156,7 +164,8 @@ private void FirstRunSetup()
156164
{
157165
if (!RunTime.IsMac ||
158166
_config.NoFirstRun ||
159-
!IsFirstRun())
167+
!IsFirstRun() ||
168+
!_config.InstallCert)
160169
{
161170
return;
162171
}
@@ -340,7 +349,7 @@ private void StopProxy()
340349
_proxyServer.Stop();
341350
}
342351

343-
if (RunTime.IsMac)
352+
if (RunTime.IsMac && _config.AsSystemProxy)
344353
{
345354
ToggleSystemProxy(ToggleSystemProxyAction.Off);
346355
}
@@ -562,4 +571,4 @@ Task OnCertificateSelection(object sender, CertificateSelectionEventArgs e)
562571
// set e.clientCertificate to override
563572
return Task.CompletedTask;
564573
}
565-
}
574+
}

Diff for: dev-proxy/ProxyHost.cs

100644100755
+21-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ internal class ProxyHost
1919
private static Option<string?>? _configFileOption;
2020
private Option<int?> _rateOption;
2121
private Option<bool?> _noFirstRunOption;
22+
private Option<bool?> _asSystemProxyOption;
23+
private Option<bool?> _installCertOption;
2224
private static Option<IEnumerable<string>?>? _urlsToWatchOption;
2325

2426
private static bool _configFileResolved = false;
@@ -216,6 +218,21 @@ public ProxyHost()
216218

217219
_noFirstRunOption = new Option<bool?>("--no-first-run", "Skip the first run experience");
218220

221+
_asSystemProxyOption = new Option<bool?>("--as-system-proxy", "Set Dev Proxy as the system proxy");
222+
_asSystemProxyOption.SetDefaultValue(true);
223+
224+
_installCertOption = new Option<bool?>("--install-cert", "Install self-signed certificate");
225+
_installCertOption.SetDefaultValue(true);
226+
_installCertOption.AddValidator((input) =>
227+
{
228+
var asSystemProxy = input.GetValueForOption(_asSystemProxyOption) ?? true;
229+
var installCert = input.GetValueForOption(_installCertOption) ?? true;
230+
if (asSystemProxy && !installCert)
231+
{
232+
input.ErrorMessage = $"Requires option '--{_asSystemProxyOption.Name}' to be 'false'";
233+
}
234+
});
235+
219236
_urlsToWatchOption = new("--urls-to-watch", "The list of URLs to watch for requests")
220237
{
221238
ArgumentHelpName = "urlsToWatch",
@@ -243,6 +260,8 @@ public RootCommand GetRootCommand(ILogger logger)
243260
// `ProxyCommandHandler.Configuration`. As such, it's always set here
244261
_configFileOption!,
245262
_noFirstRunOption,
263+
_asSystemProxyOption,
264+
_installCertOption,
246265
// _urlsToWatchOption is set while initialize the Program
247266
// As such, it's always set here
248267
_urlsToWatchOption!
@@ -256,7 +275,7 @@ public RootCommand GetRootCommand(ILogger logger)
256275
command.Add(msGraphDbCommand);
257276

258277
var presetCommand = new Command("preset", "Manage Dev Proxy presets");
259-
278+
260279
var presetGetCommand = new Command("get", "Download the specified preset from the Sample Solution Gallery");
261280
var presetIdArgument = new Argument<string>("preset-id", "The ID of the preset to download");
262281
presetGetCommand.AddArgument(presetIdArgument);
@@ -268,6 +287,6 @@ public RootCommand GetRootCommand(ILogger logger)
268287
return command;
269288
}
270289

271-
public ProxyCommandHandler GetCommandHandler(PluginEvents pluginEvents, ISet<UrlToWatch> urlsToWatch, ILogger logger) => new ProxyCommandHandler(_portOption, _ipAddressOption, _logLevelOption!, _recordOption, _watchPidsOption, _watchProcessNamesOption, _rateOption, _noFirstRunOption, pluginEvents, urlsToWatch, logger);
290+
public ProxyCommandHandler GetCommandHandler(PluginEvents pluginEvents, ISet<UrlToWatch> urlsToWatch, ILogger logger) => new ProxyCommandHandler(_portOption, _ipAddressOption, _logLevelOption!, _recordOption, _watchPidsOption, _watchProcessNamesOption, _rateOption, _noFirstRunOption, _asSystemProxyOption, _installCertOption, pluginEvents, urlsToWatch, logger);
272291
}
273292

0 commit comments

Comments
 (0)