Skip to content

Commit 0594857

Browse files
Adds IP Address option. Closes #380
1 parent f91dc3e commit 0594857

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

dev-proxy-abstractions/IProxyConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public enum LabelMode {
1616

1717
public interface IProxyConfiguration {
1818
int Port { get; }
19+
string? IPAddress { get; }
1920
LabelMode LabelMode { get; }
2021
bool Record { get; }
2122
LogLevel LogLevel { get; }

dev-proxy-plugins/RequestLogs/MinimalPermissionsGuidancePlugin.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,12 @@ private async Task EvaluateMinimalScopes(IEnumerable<Tuple<string, string>> endp
361361
using (var client = new HttpClient())
362362
{
363363
var stringPayload = JsonSerializer.Serialize(payload);
364-
_logger?.LogDebug($"Calling {url} with payload{Environment.NewLine}{stringPayload}");
364+
_logger?.LogDebug(string.Format("Calling {0} with payload{1}{2}", url, Environment.NewLine, stringPayload));
365365

366366
var response = await client.PostAsJsonAsync(url, payload);
367367
var content = await response.Content.ReadAsStringAsync();
368368

369-
_logger?.LogDebug($"Response:{Environment.NewLine}{content}");
369+
_logger?.LogDebug(string.Format("Response:{0}{1}", Environment.NewLine, content));
370370

371371
var resultsAndErrors = JsonSerializer.Deserialize<ResultsAndErrors>(content);
372372
var minimalPermissions = resultsAndErrors?.Results?.Select(p => p.Value).ToArray() ?? Array.Empty<string>();
@@ -411,7 +411,7 @@ private async Task EvaluateMinimalScopes(IEnumerable<Tuple<string, string>> endp
411411
}
412412
catch (Exception ex)
413413
{
414-
_logger?.LogError($"An error has occurred while retrieving minimal permissions: {ex.Message}");
414+
_logger?.LogError(string.Format("An error has occurred while retrieving minimal permissions: {0}", ex.Message));
415415
}
416416
}
417417

@@ -420,14 +420,14 @@ private Tuple<string, string> GetMethodAndUrl(string message)
420420
var info = message.Split(" ");
421421
if (info.Length > 2)
422422
{
423-
info = new[] { info[0], String.Join(" ", info.Skip(1)) };
423+
info = [info[0], string.Join(" ", info.Skip(1))];
424424
}
425425
return new Tuple<string, string>(info[0], info[1]);
426426
}
427427

428428
private string GetTokenizedUrl(string absoluteUrl)
429429
{
430430
var sanitizedUrl = ProxyUtils.SanitizeUrl(absoluteUrl);
431-
return "/" + String.Join("", new Uri(sanitizedUrl).Segments.Skip(2).Select(s => Uri.UnescapeDataString(s)));
431+
return "/" + string.Join("", new Uri(sanitizedUrl).Segments.Skip(2).Select(Uri.UnescapeDataString));
432432
}
433433
}

dev-proxy/ProxyCommandHandler.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Microsoft.DevProxy;
1010

1111
public class ProxyCommandHandler : ICommandHandler {
1212
public Option<int?> Port { get; set; }
13+
public Option<string?> IPAddress { get; set; }
1314
public Option<LogLevel?> LogLevel { get; set; }
1415
public Option<bool?> Record { get; set; }
1516
public Option<IEnumerable<int>?> WatchPids { get; set; }
@@ -21,6 +22,7 @@ public class ProxyCommandHandler : ICommandHandler {
2122
private readonly ILogger _logger;
2223

2324
public ProxyCommandHandler(Option<int?> port,
25+
Option<string?> ipAddress,
2426
Option<LogLevel?> logLevel,
2527
Option<bool?> record,
2628
Option<IEnumerable<int>?> watchPids,
@@ -30,6 +32,7 @@ public ProxyCommandHandler(Option<int?> port,
3032
ISet<UrlToWatch> urlsToWatch,
3133
ILogger logger) {
3234
Port = port ?? throw new ArgumentNullException(nameof(port));
35+
IPAddress = ipAddress ?? throw new ArgumentNullException(nameof(ipAddress));
3336
LogLevel = logLevel ?? throw new ArgumentNullException(nameof(logLevel));
3437
Record = record ?? throw new ArgumentNullException(nameof(record));
3538
WatchPids = watchPids ?? throw new ArgumentNullException(nameof(watchPids));
@@ -49,6 +52,10 @@ public async Task<int> InvokeAsync(InvocationContext context) {
4952
if (port is not null) {
5053
Configuration.Port = port.Value;
5154
}
55+
var ipAddress = context.ParseResult.GetValueForOption(IPAddress);
56+
if (ipAddress is not null) {
57+
Configuration.IPAddress = ipAddress;
58+
}
5259
var logLevel = context.ParseResult.GetValueForOption(LogLevel);
5360
if (logLevel is not null) {
5461
_logger.LogLevel = logLevel.Value;

dev-proxy/ProxyConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Microsoft.DevProxy;
99
public class ProxyConfiguration: IProxyConfiguration {
1010
[JsonPropertyName("port")]
1111
public int Port { get; set; } = 8000;
12+
[JsonPropertyName("ipAddress")]
13+
public string? IPAddress { get; set; }
1214
[JsonPropertyName("labelMode")]
1315
[JsonConverter(typeof(JsonStringEnumConverter))]
1416
public LabelMode LabelMode { get; set; } = LabelMode.Text;

dev-proxy/ProxyEngine.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public async Task Run(CancellationToken? cancellationToken) {
7373
var _logger2 = (ILogger)_logger.Clone();
7474
_logger2.LogLevel = LogLevel.Warn;
7575
// let's not await so that it doesn't block the proxy startup
76-
MSGraphDbCommandHandler.GenerateMsGraphDb(_logger2, true);
76+
_ = MSGraphDbCommandHandler.GenerateMsGraphDb(_logger2, true);
7777

7878
_proxyServer = new ProxyServer();
7979

@@ -85,7 +85,8 @@ public async Task Run(CancellationToken? cancellationToken) {
8585
_proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
8686
cancellationToken?.Register(OnCancellation);
8787

88-
_explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, _config.Port, true);
88+
var ipAddress = string.IsNullOrEmpty(_config.IPAddress) ? IPAddress.Any : IPAddress.Parse(_config.IPAddress);
89+
_explicitEndPoint = new ExplicitProxyEndPoint(ipAddress, _config.Port, true);
8990
if (!RunTime.IsWindows) {
9091
// we need to change this to a value lower than 397
9192
// to avoid the ERR_CERT_VALIDITY_TOO_LONG error in Edge

dev-proxy/ProxyHost.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
using Microsoft.DevProxy.Abstractions;
55
using System.CommandLine;
6+
using System.Net;
67

78
namespace Microsoft.DevProxy;
89

910
internal class ProxyHost {
1011
private Option<int?> _portOption;
12+
private Option<string?> _ipAddressOption;
1113
private Option<LogLevel?> _logLevelOption;
1214
private Option<bool?> _recordOption;
1315
private Option<IEnumerable<int>?> _watchPidsOption;
@@ -61,10 +63,20 @@ public static string ConfigFile {
6163
}
6264

6365
public ProxyHost() {
64-
_portOption = new Option<int?>("--port", "The port for the proxy server to listen on");
66+
_portOption = new Option<int?>("--port", "The port for the proxy to listen on");
6567
_portOption.AddAlias("-p");
6668
_portOption.ArgumentHelpName = "port";
67-
69+
70+
_ipAddressOption = new Option<string?>("--ip-address", "The IP address for the proxy to bind to")
71+
{
72+
ArgumentHelpName = "ipAddress"
73+
};
74+
_ipAddressOption.AddValidator(input => {
75+
if (!IPAddress.TryParse(input.Tokens.First().Value, out var ipAddress)) {
76+
input.ErrorMessage = $"{input.Tokens.First().Value} is not a valid IP address";
77+
}
78+
});
79+
6880
_logLevelOption = new Option<LogLevel?>("--log-level", $"Level of messages to log. Allowed values: {string.Join(", ", Enum.GetNames(typeof(LogLevel)))}");
6981
_logLevelOption.ArgumentHelpName = "logLevel";
7082
_logLevelOption.AddValidator(input => {
@@ -99,6 +111,7 @@ public ProxyHost() {
99111
public RootCommand GetRootCommand(ILogger logger) {
100112
var command = new RootCommand {
101113
_portOption,
114+
_ipAddressOption,
102115
_logLevelOption,
103116
_recordOption,
104117
_watchPidsOption,
@@ -119,6 +132,6 @@ public RootCommand GetRootCommand(ILogger logger) {
119132
return command;
120133
}
121134

122-
public ProxyCommandHandler GetCommandHandler(PluginEvents pluginEvents, ISet<UrlToWatch> urlsToWatch, ILogger logger) => new ProxyCommandHandler(_portOption, _logLevelOption, _recordOption, _watchPidsOption, _watchProcessNamesOption, _rateOption, pluginEvents, urlsToWatch, logger);
135+
public ProxyCommandHandler GetCommandHandler(PluginEvents pluginEvents, ISet<UrlToWatch> urlsToWatch, ILogger logger) => new ProxyCommandHandler(_portOption, _ipAddressOption, _logLevelOption, _recordOption, _watchPidsOption, _watchProcessNamesOption, _rateOption, pluginEvents, urlsToWatch, logger);
123136
}
124137

0 commit comments

Comments
 (0)