1414
1515namespace Microsoft . DevProxy ;
1616
17+ enum ToggleSystemProxyAction
18+ {
19+ On ,
20+ Off
21+ }
22+
1723public class ProxyEngine {
1824 private readonly PluginEvents _pluginEvents ;
1925 private readonly ILogger _logger ;
@@ -37,6 +43,30 @@ public ProxyEngine(ProxyConfiguration config, ISet<UrlToWatch> urlsToWatch, Plug
3743 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
3844 }
3945
46+ private void ToggleSystemProxy ( ToggleSystemProxyAction toggle , string ? ipAddress = null , int ? port = null )
47+ {
48+ var bashScriptPath = Path . Join ( ProxyUtils . AppFolder , "toggle-proxy.sh" ) ;
49+ var args = toggle switch
50+ {
51+ ToggleSystemProxyAction . On => $ "on { ipAddress } { port } ",
52+ ToggleSystemProxyAction . Off => "off" ,
53+ _ => throw new NotImplementedException ( )
54+ } ;
55+
56+ ProcessStartInfo startInfo = new ProcessStartInfo ( )
57+ {
58+ FileName = "/bin/bash" ,
59+ Arguments = $ "{ bashScriptPath } { args } ",
60+ RedirectStandardOutput = true ,
61+ UseShellExecute = false ,
62+ CreateNoWindow = true
63+ } ;
64+
65+ var process = new Process ( ) { StartInfo = startInfo } ;
66+ process . Start ( ) ;
67+ process . WaitForExit ( ) ;
68+ }
69+
4070 public async Task Run ( CancellationToken ? cancellationToken ) {
4171 if ( ! _urlsToWatch . Any ( ) ) {
4272 _logger . LogInfo ( "No URLs to watch configured. Please add URLs to watch in the devproxyrc.json config file." ) ;
@@ -88,6 +118,10 @@ public async Task Run(CancellationToken? cancellationToken) {
88118 _proxyServer . SetAsSystemHttpProxy ( _explicitEndPoint ) ;
89119 _proxyServer . SetAsSystemHttpsProxy ( _explicitEndPoint ) ;
90120 }
121+ else if ( RunTime . IsMac )
122+ {
123+ ToggleSystemProxy ( ToggleSystemProxyAction . On , _config . IPAddress , _config . Port ) ;
124+ }
91125 else {
92126 _logger . LogWarn ( "Configure your operating system to use this proxy's port and address" ) ;
93127 }
@@ -230,6 +264,11 @@ private void StopProxy() {
230264
231265 _proxyServer . Stop ( ) ;
232266 }
267+
268+ if ( RunTime . IsMac )
269+ {
270+ ToggleSystemProxy ( ToggleSystemProxyAction . Off ) ;
271+ }
233272 }
234273 catch ( Exception ex ) {
235274 _logger . LogError ( $ "Exception: { ex . Message } ") ;
0 commit comments