Skip to content

Commit

Permalink
Adds support for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz committed Sep 23, 2022
1 parent f9a1d52 commit a591134
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,5 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/

.DS_Store
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ After installing the certificate, you'll be also prompted to allow Graph Chaos P

#### macOS

- make the Graph Chaos Proxy binary executable:
- open terminal and change the working directory to the location of Graph Chaos Proxy
- run `chmod +x ./msgraph-chaos-proxy`
- open the location of Graph Chaos Proxy in Finder
- 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**
- trust the certificate to decrypt SSL traffic:
- Open **KeyChain Access**
- in the search box, search for **Titanium Root Certificate Authority**
- open the certificate and in the **Trust** section, set **Always Trust**
- close the certificate window and confirm changes
- set Graph Chaos Proxy as your system proxy:
- in **Network preferences**, select your adapter and click the **Advanced...** button
- open **Proxies**
- select **Secure Web Proxy (HTTPS)**
- in the **Secure Web Proxy Server** enter `0.0.0.0` and the port you're running the proxy on (`8000` by default)
- click **OK** and then **Apply** to save the changes

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.

#### Linux

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

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.

On macOS remove the **~/.config/rootCert.pfx** file.

## Configuration

### Mock responses
Expand Down
27 changes: 27 additions & 0 deletions msgraph-chaos-proxy/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": "0.2.0",
"configurations": [
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net6.0/msgraph-chaos-proxy.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false,
"launchSettingsProfile": "Default"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions msgraph-chaos-proxy/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/msgraph-chaos-proxy.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/msgraph-chaos-proxy.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/msgraph-chaos-proxy.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
32 changes: 23 additions & 9 deletions msgraph-chaos-proxy/ChaosEngine.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Text.RegularExpressions;
using Titanium.Web.Proxy;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Http;
using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Network;

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

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

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

// Only explicit proxies can be set as system proxy!
_proxyServer.SetAsSystemHttpProxy(_explicitEndPoint);
_proxyServer.SetAsSystemHttpsProxy(_explicitEndPoint);
if (RunTime.IsWindows) {
// Only explicit proxies can be set as system proxy!
_proxyServer.SetAsSystemHttpProxy(_explicitEndPoint);
_proxyServer.SetAsSystemHttpsProxy(_explicitEndPoint);
}
else {
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Configure your operating system to use this proxy's port and address");
Console.ForegroundColor = color;
}

// wait here (You can use something else as a wait function, I am using this as a demo)
Console.WriteLine("Press Enter to stop the Microsoft Graph Chaos Proxy");
Expand Down
2 changes: 1 addition & 1 deletion msgraph-chaos-proxy/ChaosHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public RootCommand GetRootCommand() {
allowedErrorsOption.ArgumentHelpName = "allowed errors";
allowedErrorsOption.AllowMultipleArgumentsPerToken = true;
allowedErrorsOption.SetDefaultValue(new List<int> { 429, 500, 502, 503, 504, 507 });

var command = new RootCommand
{
portOption,
Expand Down

0 comments on commit a591134

Please sign in to comment.