33
44using Microsoft . DevProxy . Abstractions ;
55using System . CommandLine ;
6+ using System . Net ;
67
78namespace Microsoft . DevProxy ;
89
910internal 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