有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

字节码Bytebuddy拦截java。网服务器socket构造函数

我试图截获java.net.ServerSocket的两个方法和一个构造函数。 拦截两种方法getLocalPortgetInetAddress效果很好。但是,应该处理构造函数ServerSocket(int)的类不会被触发。我的指令代码(在主项目中包含的另一个jar文件中):

包装检测

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());
    }
}

输出:

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

正如您所看到的,拦截getLocalPortgetInetAddress效果很好。但为什么不拦截构造函数


共 (1) 个答案

  1. # 1 楼答案

    这是因为ServerSocket接受int而不是Integer。然而,如果您尝试这样做,它将崩溃,因为JVM需要硬编码到任何构造函数中的超级方法调用。您需要在实际拦截之前运行:SuperMethodCall.INSTANCE.andThen(...)才能使其工作

    一般来说,我建议您在JVM类上使用Advice作为此类引导代理,在这些类中,您可以将代码内联到目标中。它将更加强大