Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
In één oogopslag
Doel: Een aangepaste Dev Proxy-invoegtoepassing bouwen
Tijd: 30 minuten
Plugins: Aangepaste invoegtoepassing
Prerequisites:Set up Dev Proxy, .NET 10 SDK
In dit artikel leert u hoe u een aangepaste invoegtoepassing maakt voor de Dev Proxy. Door invoegtoepassingen voor Dev Proxy te maken, kunt u de functionaliteit uitbreiden en aangepaste functies toevoegen aan uw behoeften.
HTTP-invoegtoepassingen versus stdio-invoegtoepassingen
Dev Proxy ondersteunt twee typen invoegtoepassingen, afhankelijk van het verkeer dat u wilt onderscheppen:
HTTP-invoegtoepassingen onderscheppen HTTP-aanvragen en -antwoorden tussen uw app en API's. Ze nemen over van en overschrijven methoden zoals en . Gebruik HTTP-invoegtoepassingen als u API-fouten wilt simuleren, gesimuleerde reacties wilt toevoegen, aanvraagheaders wilt valideren of op een andere manier HTTP-verkeer wilt wijzigen en inspecteren.
Stdio-plugins onderscheppen berichten die worden verzonden via standaardinvoer/uitvoer (stdin, stdout, stderr) tussen een ouderproces en een kindproces. Ze implementeren de interface (die ook implementeert) en overschrijven methoden zoals , en . Gebruik stdio-invoegtoepassingen wanneer u werkt met hulpprogramma's die communiceren via stdio, zoals MCP-servers (Model Context Protocol).
Eén invoegtoepassingsklasse kan zowel HTTP- als stdio-verkeer verwerken door methoden uit beide sets te overschrijven.
Vereisten
Voordat u begint met het maken van een aangepaste invoegtoepassing, moet u ervoor zorgen dat u aan de volgende vereisten voldoet:
- .NET v10 Core SDK
- De nieuwste versie van het DLL-bestand met Dev Proxy Abstractions, die u kunt vinden op de pagina Dev Proxy GitHub releases
Een nieuwe invoegtoepassing maken
Volg de volgende stappen om een nieuw project te maken:
Maak een nieuw klassebibliotheekproject met behulp van de opdracht.
dotnet new classlib -n MyCustomPluginOpen het zojuist gemaakte project in Visual Studio Code.
code MyCustomPluginVoeg het DLL-bestand Dev Proxy Abstractions () toe aan de projectmap.
Voeg het bestand toe als verwijzing naar het projectbestand .
<ItemGroup> <Reference Include="DevProxy.Abstractions"> <HintPath>.\DevProxy.Abstractions.dll</HintPath> <Private>false</Private> <ExcludeAssets>runtime</ExcludeAssets> </Reference> </ItemGroup>Voeg de NuGet-pakketten toe die vereist zijn voor uw project.
dotnet add package Microsoft.Extensions.Configuration dotnet add package Microsoft.Extensions.Configuration.Binder dotnet add package Microsoft.Extensions.Logging.Abstractions dotnet add package Unobtanium.Web.ProxySluit de afhankelijkheid DLL's (Dynamic Link Libraries) uit van de build-uitvoer door een tag toe te voegen per in het bestand.
<ExcludeAssets>runtime</ExcludeAssets>Maak een nieuwe klasse die wordt overgenomen van de klasse.
using DevProxy.Abstractions.Plugins; using DevProxy.Abstractions.Proxy; using Microsoft.Extensions.Logging; namespace MyCustomPlugin; public sealed class CatchApiCallsPlugin( ILogger<CatchApiCallsPlugin> logger, ISet<UrlToWatch> urlsToWatch) : BasePlugin(logger, urlsToWatch) { public override string Name => nameof(CatchApiCallsPlugin); public override Task BeforeRequestAsync(ProxyRequestArgs e, CancellationToken cancellationToken) { Logger.LogTrace("{Method} called", nameof(BeforeRequestAsync)); ArgumentNullException.ThrowIfNull(e); if (!e.HasRequestUrlMatch(UrlsToWatch)) { Logger.LogRequest("URL not matched", MessageType.Skipped, new(e.Session)); return Task.CompletedTask; } var headers = e.Session.HttpClient.Request.Headers; var header = headers.Where(h => h.Name == "Authorization").FirstOrDefault(); if (header is null) { Logger.LogRequest($"Does not contain the Authorization header", MessageType.Warning, new LoggingContext(e.Session)); return Task.CompletedTask; } Logger.LogTrace("Left {Name}", nameof(BeforeRequestAsync)); return Task.CompletedTask; } }Bouw uw project.
dotnet build
Uw aangepaste invoegtoepassing gebruiken
Als u uw aangepaste invoegtoepassing wilt gebruiken, moet u deze toevoegen aan het configuratiebestand van de Dev Proxy:
Voeg de nieuwe configuratie van de invoegtoepassing toe aan het bestand.
Bestand: devproxyrc.json
{ "plugins": [{ "name": "CatchApiCallsPlugin", "enabled": true, "pluginPath": "./bin/Debug/net10.0/MyCustomPlugin.dll", }] }Voer de dev-proxy uit.
devproxy
De voorbeeldinvoegtoepassing controleert alle overeenkomende URL's voor de vereiste header. Als de koptekst niet aanwezig is, wordt er een waarschuwingsbericht weergegeven.
Aangepaste configuratie toevoegen aan uw invoegtoepassing (optioneel)
U kunt de logica van uw invoegtoepassing uitbreiden door aangepaste configuratie toe te voegen:
Erf van de klasse. Dev Proxy stelt tijdens runtime de geparseerde configuratie van de plugin bloot via de eigenschap.
using DevProxy.Abstractions.Plugins; using DevProxy.Abstractions.Proxy; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; namespace MyCustomPlugin; public sealed class CatchApiCallsConfiguration { public string? RequiredHeader { get; set; } } public sealed class CatchApiCallsPlugin( HttpClient httpClient, ILogger<CatchApiCallsPlugin> logger, ISet<UrlToWatch> urlsToWatch, IProxyConfiguration proxyConfiguration, IConfigurationSection pluginConfigurationSection) : BasePlugin<CatchApiCallsConfiguration>( httpClient, logger, urlsToWatch, proxyConfiguration, pluginConfigurationSection) { public override string Name => nameof(CatchApiCallsPlugin); public override Task BeforeRequestAsync(ProxyRequestArgs e, CancellationToken cancellationToken) { Logger.LogTrace("{Method} called", nameof(BeforeRequestAsync)); ArgumentNullException.ThrowIfNull(e); if (!e.HasRequestUrlMatch(UrlsToWatch)) { Logger.LogRequest("URL not matched", MessageType.Skipped, new(e.Session)); return Task.CompletedTask; } // Start using your custom configuration var requiredHeader = Configuration.RequiredHeader ?? string.Empty; if (string.IsNullOrEmpty(requiredHeader)) { // Required header is not set, so we don't need to do anything Logger.LogRequest("Required header not set", MessageType.Skipped, new LoggingContext(e.Session)); return Task.CompletedTask; } var headers = e.Session.HttpClient.Request.Headers; var header = headers.Where(h => h.Name == requiredHeader).FirstOrDefault(); if (header is null) { Logger.LogRequest($"Does not contain the {requiredHeader} header", MessageType.Warning, new LoggingContext(e.Session)); return Task.CompletedTask; } Logger.LogTrace("Left {Name}", nameof(BeforeRequestAsync)); return Task.CompletedTask; } }Bouw uw project.
dotnet buildWerk uw bestand bij om de nieuwe configuratie op te nemen.
Bestand: devproxyrc.json
{ "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.2.0/rc.schema.json", "plugins": [{ "name": "CatchApiCallsPlugin", "enabled": true, "pluginPath": "./bin/Debug/net10.0/MyCustomPlugin.dll", "configSection": "catchApiCalls" }], "catchApiCalls": { "requiredHeader": "Authorization" } }Voer de dev-proxy uit.
devproxy
Zie ook
- Architectuur van invoegtoepassingen
- Vooraf ingestelde configuraties gebruiken
- Dev-proxy configureren