Skip to content

Commit a591134

Browse files
Adds support for macOS
1 parent f9a1d52 commit a591134

File tree

6 files changed

+115
-10
lines changed

6 files changed

+115
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,5 @@ MigrationBackup/
348348

349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351+
352+
.DS_Store

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ After installing the certificate, you'll be also prompted to allow Graph Chaos P
5454

5555
#### macOS
5656

57+
- make the Graph Chaos Proxy binary executable:
58+
- open terminal and change the working directory to the location of Graph Chaos Proxy
59+
- run `chmod +x ./msgraph-chaos-proxy`
60+
- open the location of Graph Chaos Proxy in Finder
61+
- on the keyboard press **Option** and open the context menu of the **msgraph-chaos-proxy** executable. From the context menu, choose **Open**, in the dialog choose **Open**
62+
- trust the certificate to decrypt SSL traffic:
63+
- Open **KeyChain Access**
64+
- in the search box, search for **Titanium Root Certificate Authority**
65+
- open the certificate and in the **Trust** section, set **Always Trust**
66+
- close the certificate window and confirm changes
67+
- set Graph Chaos Proxy as your system proxy:
68+
- in **Network preferences**, select your adapter and click the **Advanced...** button
69+
- open **Proxies**
70+
- select **Secure Web Proxy (HTTPS)**
71+
- in the **Secure Web Proxy Server** enter `0.0.0.0` and the port you're running the proxy on (`8000` by default)
72+
- click **OK** and then **Apply** to save the changes
73+
74+
When you're finished using the proxy, in the **Network preferences**, disable the **Secure Web Proxy (HTTPS)**. Next time you want to use the proxy, you'll only need to enable this setting again.
75+
5776
#### Linux
5877

5978
### Use the proxy
@@ -72,6 +91,8 @@ Depending on the configured fail ratio, the proxy will either pass the request t
7291

7392
Remove the folder with proxy from your disk. Graph Chaos Proxy doesn't create any additional files or registry entries (Windows) on your machine. Remove the certificate installed by Graph Chaos Proxy.
7493

94+
On macOS remove the **~/.config/rootCert.pfx** file.
95+
7596
## Configuration
7697

7798
### Mock responses
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
// Use IntelliSense to find out which attributes exist for C# debugging
6+
// Use hover for the description of the existing attributes
7+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/net6.0/msgraph-chaos-proxy.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
17+
"console": "internalConsole",
18+
"stopAtEntry": false,
19+
"launchSettingsProfile": "Default"
20+
},
21+
{
22+
"name": ".NET Core Attach",
23+
"type": "coreclr",
24+
"request": "attach"
25+
}
26+
]
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/msgraph-chaos-proxy.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/msgraph-chaos-proxy.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/msgraph-chaos-proxy.csproj"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}

msgraph-chaos-proxy/ChaosEngine.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System.Net;
2+
using System.Security.Cryptography.X509Certificates;
23
using System.Text.Json;
34
using System.Text.RegularExpressions;
45
using Titanium.Web.Proxy;
56
using Titanium.Web.Proxy.EventArguments;
7+
using Titanium.Web.Proxy.Helpers;
68
using Titanium.Web.Proxy.Http;
79
using Titanium.Web.Proxy.Models;
10+
using Titanium.Web.Proxy.Network;
811

912
namespace Microsoft.Graph.ChaosProxy {
1013
internal enum FailMode {
@@ -99,12 +102,15 @@ public async Task Run(CancellationToken? cancellationToken) {
99102
_proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
100103
cancellationToken?.Register(OnCancellation);
101104

102-
_explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, _config.Port, true) {
103-
// Use self-issued generic certificate on all https requests
104-
// Optimizes performance by not creating a certificate for each https-enabled domain
105-
// Useful when certificate trust is not required by proxy clients
106-
//GenericCertificate = new X509Certificate2(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "genericcert.pfx"), "password")
107-
};
105+
_explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, _config.Port, true);
106+
if (!RunTime.IsWindows) {
107+
// we need to change this to a value lower than 397
108+
// to avoid the ERR_CERT_VALIDITY_TOO_LONG error in Edge
109+
_proxyServer.CertificateManager.CertificateValidDays = 365;
110+
// we need to call it explicitly for non-Windows OSes because it's
111+
// a part of the SetAsSystemHttpProxy that works only on Windows
112+
_proxyServer.CertificateManager.EnsureRootCertificate();
113+
}
108114

109115
// Fired when a CONNECT request is received
110116
_explicitEndPoint.BeforeTunnelConnectRequest += OnBeforeTunnelConnectRequest;
@@ -117,9 +123,17 @@ public async Task Run(CancellationToken? cancellationToken) {
117123
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
118124
}
119125

120-
// Only explicit proxies can be set as system proxy!
121-
_proxyServer.SetAsSystemHttpProxy(_explicitEndPoint);
122-
_proxyServer.SetAsSystemHttpsProxy(_explicitEndPoint);
126+
if (RunTime.IsWindows) {
127+
// Only explicit proxies can be set as system proxy!
128+
_proxyServer.SetAsSystemHttpProxy(_explicitEndPoint);
129+
_proxyServer.SetAsSystemHttpsProxy(_explicitEndPoint);
130+
}
131+
else {
132+
var color = Console.ForegroundColor;
133+
Console.ForegroundColor = ConsoleColor.Yellow;
134+
Console.WriteLine("Configure your operating system to use this proxy's port and address");
135+
Console.ForegroundColor = color;
136+
}
123137

124138
// wait here (You can use something else as a wait function, I am using this as a demo)
125139
Console.WriteLine("Press Enter to stop the Microsoft Graph Chaos Proxy");

msgraph-chaos-proxy/ChaosHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public RootCommand GetRootCommand() {
3636
allowedErrorsOption.ArgumentHelpName = "allowed errors";
3737
allowedErrorsOption.AllowMultipleArgumentsPerToken = true;
3838
allowedErrorsOption.SetDefaultValue(new List<int> { 429, 500, 502, 503, 504, 507 });
39-
39+
4040
var command = new RootCommand
4141
{
4242
portOption,

0 commit comments

Comments
 (0)