Startup example
To understand how the library works, let's kick off with a trivial example.
One the simplest example is to add or to modify request and response headers.
The following example was tested with .NET 8.0, however it should work with any .NET version greater than .NET 6.0.
Start by creating a new console app
dotnet new console
Then install the fluxzy nuget package :
dotnet add package Fluxzy.Core
using System.Net;
using Fluxzy;
using Fluxzy.Rules.Actions;
using Fluxzy.Rules.Filters;
var fluxzyStartupSetting = FluxzySetting
.CreateDefault(IPAddress.Loopback, 8000)
.AddAlterationRules(
new UpdateRequestHeaderAction("user-agent", "{previous} - MITM-ed by fluxzy"),
AnyFilter.Default
)
.AddAlterationRules(
new AddResponseHeaderAction("X-Fluxzy-Appended-Response-Header", "Hello"),
AnyFilter.Default
);
await using (var proxy = new Proxy(fluxzyStartupSetting))
{
var _ = proxy.Run();
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
The FluxzySetting
class holds the the startup configuration of the proxy. It can configured in a fluent fashion by calling successive extension methods.
The first method CreateDefault()
creates a default configuration. Here we supply the bound address and the port (8000) of the proxy. If you omit the arguments, fluxzy will listen to 127.0.0.1 on port 44344.
var fluxzyStartupSetting = FluxzySetting
.CreateDefault(IPAddress.Loopback, 8000)
The fluent method AddAlterationRules()
adds an alteration rule to the proxy, more precisely in this sample :
The first call will modify any existing user-agent by appending a suffix on the original value. Note that the keyword {previous}
is a placeholder that will be replaced by the original value.
.AddAlterationRules(
new UpdateRequestHeaderAction("user-agent", "{{previous}} - Relayed by fluxzy"),
AnyFilter.Default
)
The second call to AddAlterationRules()
appends a response header with name X-Fluxzy-Appended-Response-Header and value Hello.
.AddAlterationRules(
new AddResponseHeaderAction("X-Fluxzy-Appended-Response-Header", "Hello"),
AnyFilter.Default
)
Note
- A rule is combination of a filter and multiple action that is evaluated during the traversal of the request (visit core concepts to learn more ).
- You can find an exhaustive list of available actions and filters on this search page.
To test this sample, execute the application above along with following `curl` command on the same machine.
curl -x 127.0.0.1:8000 -I -k https://example.com
This command will print the response header directly to the console. This response will contains the appended response header X-Fluxzy-Appended-Response-Header with the value Hello.
Let's break down the curl command to know what happens :
- -x option orders curl to use the proxy at 127.0.0.1:44344, which points to our fluxzy instance
- -I option : make curl send an HEAD method, meaning that it's requesting only response header with any body. Additionally, this option will make curl print the request header.
- -k option : instruct curl to ignore any SSL certificate error. There are several ways to make curl trust the certificate generated by fluxzy, but this is out of the scope of this sample.