簡體   English   中英

Bytebuddy - 攔截 java.net.ServerSocket 構造函數

[英]Bytebuddy - Intercept java.net.ServerSocket constructor

我試圖攔截java.net.ServerSocket的兩種方法和一個構造函數。 攔截getLocalPortgetInetAddress這兩種方法可以正常工作。 但是,不會觸發應該處理構造函數ServerSocket(int)的 class。 我要檢測的代碼(在主項目中包含的不同 jar 文件中):

package 儀表;

public class Instrumenting {

    private static final String CLASS_NAME = "java.net.ServerSocket";

    public static void instrument(Instrumentation instrumentation) throws Exception {
        System.out.println("[Instrumenting] starting to instrument '" + CLASS_NAME + "'");

        instrumentation.appendToBootstrapClassLoaderSearch(new JarFile("C:\\Users\\Moritz\\Instrumenting\\dist\\Instrumenting.jar"));

        File temp = Files.createTempDirectory("tmp").toFile();
        ClassInjector.UsingInstrumentation.of(temp, ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, instrumentation).inject(Collections.singletonMap(
            new TypeDescription.ForLoadedType(GetLocalPortIntercept.class),
            ClassFileLocator.ForClassLoader.read(GetLocalPortIntercept.class)));


        new AgentBuilder.Default()
                .ignore(ElementMatchers.none())
                .with(new AgentBuilder.InjectionStrategy.UsingInstrumentation(instrumentation, temp))
                .type(ElementMatchers.named(CLASS_NAME))

                .transform((DynamicType.Builder<?> builder, TypeDescription td, ClassLoader cl, JavaModule jm) -> 
                        builder
                                .method(ElementMatchers.named("getLocalPort"))
                                .intercept(MethodDelegation.to(GetLocalPortIntercept.class))

                                .method(ElementMatchers.named("getInetAddress"))
                                .intercept(MethodDelegation.to(GetInetAddressIntercept.class))

                                .constructor(ElementMatchers.takesArguments(1).and(ElementMatchers.takesArguments(Integer.class)))
                                .intercept(MethodDelegation.to(ServerSocketIntercept.class))
                ).installOn(instrumentation);

        System.out.println("[Instrumenting] done");
    }

    public static class ServerSocketIntercept {

        public static void intercept(int port) throws Exception {
            System.out.println("[ServerSocketIntercept] port: " + port);
        }

    }

    public static class GetLocalPortIntercept {

        public static int intercept() throws Exception {
            System.out.println("[GetLocalPortIntercept]");

            return 42;
        }

    }

    public static class GetInetAddressIntercept {

        public static InetAddress intercept() throws Exception {
            System.out.println("[GetInetAddressIntercept]");

            return InetAddress.getByAddress(new byte[] {1, 2, 3 ,4});
        }

    }
}

它是如何命名的:

import instrumenting.Instrumenting;

...


public class Main {

    public static void premain(String agentArgs, Instrumentation instrumentation) throws Exception {
        Instrumenting.instrument(instrumentation);
    }

    public static void main(String[] args) throws MalformedURLException, IOException {
        ServerSocket serverSocket = new ServerSocket(12345);
        System.out.println("Localport: " + serverSocket.getLocalPort());
        System.out.println("InetAddress: " + serverSocket.getInetAddress());
    }
}

output:

[Instrumentation] starting to instrument 'java.net.ServerSocket'
[Instrumentation] done 
[GetLocalPortIntercept] Localport: 42
[GetInetAddressIntercept] InetAddress: /1.2.3.4

如您所見,攔截getLocalPortgetInetAddress工作正常。 但是為什么構造函數沒有被攔截呢?

這是因為ServerSocket接受int而不是Integer 但是,如果您嘗試這樣做,它會崩潰,因為 JVM 需要硬編碼到任何構造函數中的超級方法調用。 您需要在實際攔截之前運行: SuperMethodCall.INSTANCE.andThen(...)以使其工作。

一般來說,我建議您在 JVM 類上使用此類引導代理的Advice ,您可以將代碼內聯到目標中。 它會更健壯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM