Skip to content

Commit cade5ad

Browse files
Automates registering proxy on macOS. Partially closes #49 (#429)
1 parent fdd3df0 commit cade5ad

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

dev-proxy/ProxyEngine.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
namespace Microsoft.DevProxy;
1616

17+
enum ToggleSystemProxyAction
18+
{
19+
On,
20+
Off
21+
}
22+
1723
public 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}");

dev-proxy/dev-proxy.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
<None Update="devproxyrc.json">
5151
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
5252
</None>
53+
<None Update="toggle-proxy.sh">
54+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
55+
</None>
5356
<None Update="picture.jpg">
5457
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
5558
</None>

dev-proxy/toggle-proxy.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
toggle=$1
4+
ip=$2
5+
port=$3
6+
7+
network_services=$(networksetup -listallnetworkservices | tail -n +2)
8+
9+
if [[ "$toggle" == "on" ]]; then
10+
while IFS= read -r service; do
11+
networksetup -setsecurewebproxy "$service" $ip $port
12+
done <<<"$network_services"
13+
elif [[ "$toggle" == "off" ]]; then
14+
while IFS= read -r service; do
15+
networksetup -setsecurewebproxystate "$service" off
16+
done <<<"$network_services"
17+
else
18+
echo "Set the first argument to on or off."
19+
fi

0 commit comments

Comments
 (0)