【愚公系列】2023年06月 .NET CORE工具案例-Lib.Harmony之AOP拦截

463 阅读2分钟

前言

API拦截是指通过拦截某个应用程序编程接口(API)的调用,限制或控制程序的功能和行为。它通常是在应用程序和操作系统之间进行拦截,以防止应用程序访问某些操作系统功能或资源。

API拦截可以用来监控和分析API调用,以确保API调用者遵守安全规则。它也可以用来确保API不被滥用,以及确保API调用者支付合理的费用。此外,它还可以用来收集API调用的统计数据,以让开发人员和运营人员了解API的使用情况。

Lib.Harmony的Github网址:github.com/pardeike/Ha… 在这里插入图片描述 Lib.Harmony官网:harmony.pardeike.net/ 在这里插入图片描述

一、自定义拦截

1.安装包

Lib.Harmony

在这里插入图片描述

2.基本使用

2.1 定义被拦截的类

public class Student
{
    public string GetDetails(string name)
    {
        return $"大家好,我是博主:{name}";
    }
}

在这里插入图片描述

2.2 定义拦截类

/// <summary>
/// 自定义拦截器(拦截Student和GetDetails方法)
/// </summary>
[HarmonyPatch(typeof(Student))]
[HarmonyPatch(nameof(Student.GetDetails))]
public class HookStudent
{
    public static bool Prefix()
    {
        Console.WriteLine($"Prefix");
        return true;
    }

    public static void Postfix()
    {
        Console.WriteLine($"Postfix");
    }

    public static void Finalizer()
    {
        Console.WriteLine($"Finalizer");
    }
}

在这里插入图片描述

2.3 运行

using ConsoleTest;
using HarmonyLib;
using System.Reflection;

var student = new Student();
Console.WriteLine(student.GetDetails("愚公搬代码"));

var harmony = new Harmony("https://blog.csdn.net/aa2528877987");
harmony.PatchAll(Assembly.GetExecutingAssembly());

Console.WriteLine(student.GetDetails("愚公搬代码"));

Console.ReadLine();

在这里插入图片描述 使用PatchAll能自动发现拦截类,从而自动拦截GetDetails方法调用,发现第二次调用方法时,Harmony的三个生命周期方法都被调用了。

3.参数篡改

3.1 拦截类修改

/// <summary>
/// 自定义拦截器(拦截Student和GetDetails方法)
/// </summary>
[HarmonyPatch(typeof(Student))]
[HarmonyPatch(nameof(Student.GetDetails))]
public class HookStudent
{
    public static bool Prefix(ref string name, ref string __result)
    {
        if ("愚公搬代码".Equals(name))
        {
            __result = $"愚公搬代码已经改名了";
            return false;
        }

        if ("代码搬愚公".Equals(name))
        {
            name = "代码搬愚公";
        }

        return true;
    }

    public static void Postfix()
    {
        Console.WriteLine($"Postfix");
    }

    public static void Finalizer()
    {
        Console.WriteLine($"Finalizer");
    }
}

3.2 运行

using ConsoleTest;
using HarmonyLib;
using System.Reflection;

var student = new Student();
Console.WriteLine(student.GetDetails("愚公搬代码"));

var harmony = new Harmony("https://blog.csdn.net/aa2528877987");
harmony.PatchAll(Assembly.GetExecutingAssembly());

Console.WriteLine(student.GetDetails("愚公搬代码"));
Console.WriteLine(student.GetDetails("代码搬愚公"));


Console.ReadLine();

在这里插入图片描述 Prefix方法默认返回的true,表示需要调用原生方法,这里会将篡改的参数传入原生方法。

二、WPF自定义拦截

自定义消息框

public class TG_MessageBox
{
    public void TG_Show(string messageBoxText)
    {
        MessageBox.Show(messageBoxText);
    }
}

1.App中注册自动拦截

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    //自动拦截注册
    protected override void OnStartup(StartupEventArgs e)
    {

        base.OnStartup(e);

        var harmony = new Harmony("https://blog.csdn.net/aa2528877987");
        harmony.PatchAll(Assembly.GetExecutingAssembly());
    }
}

在这里插入图片描述

2.自动拦截类

[HarmonyPatch(typeof(TG_MessageBox))]
[HarmonyPatch(nameof(TG_MessageBox.TG_Show))]
[HarmonyPatch(new[] { typeof(string) })]
public class HookMessageBox
{
    public static bool Prefix(ref string messageBoxText)
    {
        if (messageBoxText.Contains("愚公"))
        {
            messageBoxText = "愚公的博客:https://blog.csdn.net/aa2528877987";
        }

        return true;
    }
}

在这里插入图片描述

3.运行

在窗体MainWindow.xaml里添加两个弹出提示框的按钮:

<Window x:Class="WpfTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <Button Content="弹出默认提示框" Width="120" Height="30" Click="ShowDialog_OnClick"></Button>
        <Button Content="愚公博客网站" Width="120" Height="30" Click="ShowBadMessageDialog_OnClick"></Button>
    </StackPanel>
</Window>
public partial class MainWindow : Window
{
    public TG_MessageBox tG_MessageBox { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        tG_MessageBox = new TG_MessageBox();
    }

    private void ShowDialog_OnClick(object sender, RoutedEventArgs e)
    {

        tG_MessageBox.TG_Show("https://blog.csdn.net/aa2528877987 是一个热衷于技术分享的程序员网站");
    }

    private void ShowBadMessageDialog_OnClick(object sender, RoutedEventArgs e)
    {
        tG_MessageBox.TG_Show("愚公");
    }
}

在这里插入图片描述 在这里插入图片描述 可以看到弹出消息被篡改