Method and device for automatically injecting network agent, storage medium and equipment
By dynamically modifying the configuration of the HTTP client instance using JVM probes and using the proxy server specified in the configuration center, the problem of manually configuring a proxy is solved, which is tedious and error-prone. This achieves proxy injection without code modification, improving testing efficiency and flexibility.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-31
- Publication Date
- 2026-04-03
AI Technical Summary
In a microservice architecture, manually configuring a proxy server is cumbersome and error-prone, leading to low testing efficiency, especially when there are many interfaces or frequent information changes.
By dynamically modifying the configuration of the HTTP client instance using JVM probes, it can be made to use the proxy server specified by the configuration center, thereby automatically injecting network proxies, simplifying the testing process and shortening the testing cycle.
Without modifying the application code, proxy injection is implemented, which simplifies the testing process, improves testing efficiency, and supports dynamic updates of proxy configurations to flexibly meet testing needs.
Smart Images

Figure CN121785937A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software application testing, and more specifically to a method, apparatus, storage medium, and device for automatically injecting network proxies. Background Technology
[0002] In the existing software development and testing process, the testing phase is crucial for gaining a deeper understanding of the interaction between the application and the server, and for improving the quality, performance, and security of the software. This requires analyzing the network data of the HTTP service to verify the application's fault tolerance and user experience.
[0003] To simulate a real network environment, external interfaces need to be tested. However, current technologies typically require manual proxy configuration for these interfaces. In a microservice architecture, with numerous network requests, manual proxy configuration is extremely costly, cumbersome, and error-prone. When the number of interfaces to be tested is large, the interface information is complex, or the interface information frequently changes, manually configuring proxy information severely reduces the testing efficiency of developers. Summary of the Invention
[0004] Based on this, the present invention provides a method, apparatus, storage medium and device for automatically injecting network proxies. By dynamically modifying the configuration of an HTTP client instance that matches the test object application through a JVM probe, the configuration is made to use the proxy server specified by the configuration center. The whole process is transparent to the application and proxy injection is achieved without modifying the application code, simplifying the testing process and shortening the application testing cycle.
[0005] In a first aspect, the present invention provides a method for automatically injecting a network proxy, comprising:
[0006] Once the JVM of the test application starts, instances of each HTTP client are detected using JVM probes.
[0007] If an HTTP client instance matching the application class name of the test object is detected, the HTTP client instance configuration is modified based on the address and port information of the proxy server specified in the configuration center.
[0008] Test the application of the test object based on the modified instance.
[0009] Furthermore, after the JVM of the test application starts, the instances of each HTTP client are detected through JVM probes, specifically as follows:
[0010] Once the JVM of the test application starts, the JVM probe is loaded, and the class instances of each HTTP client are detected through the JVM probe.
[0011] Compare the class instances of each HTTP client with the class names of the test object application to see if they match.
[0012] Furthermore, if an HTTP client instance matching the application class name of the test object is detected, the HTTP client instance configuration will be modified based on the proxy server address and port information specified in the configuration center. Specifically:
[0013] If an HTTP client instance matching the application class name of the test object is detected, the JVM probe is invoked to modify the address and port information in the HTTP client instance configuration, based on the address and port information of the proxy server specified in the configuration center.
[0014] Furthermore, the method for automatically injecting network proxies also includes:
[0015] If the class instance of each HTTP client does not match the class name of the test object, instruct the JVM probe to re-detect the class instance of each HTTP client after a specified interval.
[0016] Secondly, the present invention also provides an apparatus for automatically injecting network proxies, comprising:
[0017] The startup detection module is used to detect instances of each HTTP client through JVM probes after the JVM of the test object application starts.
[0018] The configuration modification module is used to modify the configuration of the HTTP client instance if an HTTP client instance matching the application class name of the test object is detected, based on the address and port information of the proxy server specified in the configuration center.
[0019] The testing module is used to test the application of the test object based on the modified instance.
[0020] Furthermore, the startup detection module includes:
[0021] The JVM startup unit is used to load JVM probes after the JVM of the test object application starts, and to detect class instances of each HTTP client through the JVM probes.
[0022] The instance comparison unit is used to compare whether the class instance of each HTTP client is consistent with the class name of the test object application.
[0023] Furthermore, if an HTTP client instance matching the application class name of the test object is detected, the HTTP client instance configuration is modified based on the address and port information of the proxy server specified in the configuration center. Specifically:
[0024] If an HTTP client instance matching the application class name of the test object is detected, the JVM probe is invoked to modify the address and port information in the HTTP client instance configuration, based on the address and port information of the proxy server specified in the configuration center.
[0025] Furthermore, the device for automatically injecting network proxies also includes:
[0026] The probe restart unit is used to instruct the JVM probe to re-detect the class instances of each HTTP client after a specified interval if the class names of the traversed HTTP clients are inconsistent with the class names of the test object application.
[0027] Thirdly, the present invention also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method for automatically injecting a network proxy as described in any of the first aspects.
[0028] Fourthly, the present invention also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the method for automatically injecting a network proxy as described in any one of the first aspects.
[0029] The beneficial effects of adopting the above technical solution are as follows: This embodiment dynamically modifies the configuration of detected client instances through JVM probes and uses the proxy server specified by the configuration center; code injection can be achieved without modifying the application code, greatly simplifying the testing process and shortening the application testing cycle. In addition, the unified management of proxy configurations through the configuration center facilitates the maintenance of application testing; at the same time, the configuration center supports dynamic updates of proxy configurations, enabling modification of proxy configurations without restarting the application, and providing more flexible responses to testing needs. Attached Figure Description
[0030] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below.
[0031] Figure 1 This is a schematic diagram of a method for automatically injecting a network proxy in one embodiment of this application;
[0032] Figure 2 This is a schematic diagram of an apparatus for automatically injecting a network proxy in one embodiment of this application. Detailed Implementation
[0033] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention. To describe the present invention in more detail, the method, apparatus, storage medium, and device for automatically injecting network proxies provided by the present invention will be specifically described below with reference to the accompanying drawings.
[0034] Unless otherwise defined, the technical or scientific terms used in this application shall have the ordinary meaning understood by one of ordinary skill in the art to which this invention pertains. The terms "first," "second," and similar terms used in this invention do not indicate any order, quantity, or importance, but are merely used to distinguish different components. Similarly, the terms "an," "a," or "the" do not indicate a quantity limitation, but rather indicate the presence of at least one. The terms "comprising" or "including" mean that the preceding element or object encompasses the elements or objects listed after the word and their equivalents, without excluding other elements or objects. The terms "connected" or "linked" are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. "Up," "down," "left," "right," etc., are used only to indicate relative positional relationships; when the absolute position of the described object changes, the relative positional relationship may also change accordingly.
[0035] During the development and testing of some software, it is necessary to analyze the network data of HTTP services. The general solution is to configure the application to make HTTP access go to a proxy server, and the proxy server will perform the actual forwarding and analysis of the HTTP network.
[0036] To simulate a real network environment and test external interfaces, manual proxy configuration is typically required. However, this method is not only cumbersome but also error-prone. Especially in microarchitecture service architectures, the complexity of applications increases significantly, and the number of network requests in the system is numerous, making manual proxy configuration extremely costly.
[0037] To better understand the automatic network proxy injection scheme in this embodiment, the application environment of this embodiment will be described in detail:
[0038] The JVM (Java Virtual Machine) is the core of the Java platform, responsible for translating bytecode into machine instructions, enabling "write once, run anywhere." In software testing, using the JVM is more helpful in analyzing performance bottlenecks, memory leaks, and stability issues. The core components of the JVM include the class loader, runtime data area, and execution engine. The class loader manages class file loading, the runtime data area (including heap memory and method area) stores data, and the execution engine is responsible for executing specific instructions.
[0039] JVM probes typically exist as JAR files and are mounted to the target JVM via the `-javaagent` startup parameter. Their core mechanism lies in "bytecode enhancement." JVM probes utilize the `java.lang.instrumentAPI` to dynamically modify the bytecode of the target class when it is loaded by the JVM, or to insert monitoring code before and after the `execute` method call in an HTTP request. Thus, each time an HTTP Client instance is created using `new` or reflection and its methods are called, the probe's pre-defined monitoring logic is automatically executed, generating call chains and performance metrics for recording start or end times, collecting call parameters, and catching exceptions.
[0040] The method for automatically injecting network proxies provided in this embodiment includes the following steps:
[0041] Step S100: After the JVM of the test object application starts, the instances of each HTTP client are detected by the JVM probe.
[0042] Specifically, this embodiment can identify whether the JVM is running through Java Agent technology, such as automatically checking whether the path ml-search-more[-javaagent]{text="-javaagent"} exists to determine whether the JVM is running.
[0043] After the JVM starts, a JVM probe can be loaded via `-javaagent:xxx.jar` to detect all HTTP client instances. In this embodiment, the HTTP clients include, but are not limited to, ApacheHttpClient, OkHttp, and HutoolHttpClient. ApacheHttpClient provides a high-efficiency, up-to-date, and feature-rich client programming toolkit supporting the HTTP protocol; OkHttp provides simple and efficient network requests, supporting both synchronous and asynchronous requests, and has a powerful built-in interceptor mechanism; HutoolHttpClient provides a simple encapsulation of HTTP requests, offering a more concise API.
[0044] Specifically, after the JVM of the test application starts, JVM probes are used to detect instances of each HTTP client.
[0045] Step S101: After the JVM of the test object application starts, load the JVM probe and detect the class instances of each HTTP client through the JVM probe.
[0046] Step S102: Compare the class instances of each HTTP client with the class names of the test object application to see if they are consistent.
[0047] The comparison of whether the class instance of each HTTP client is consistent with the class name of the test object application mainly involves comparing whether the class instance of the HTTP client stored in ApacheHttpClient, OkHttp, and HutoolHttpClient is consistent with the class name (i.e., class name) of the test object application.
[0048] It should be noted that in this embodiment, the class instance is used to represent the class and interface type information of the test object application. It is a reception window or image for the JVM to provide Java programs and access the class information.
[0049] Step S200: If an HTTP client instance matching the application class name of the test object is detected, modify the HTTP client instance configuration based on the address and port information of the proxy server specified in the configuration center.
[0050] If an HTTP client instance matching the application class name of the test object is detected, the HTTP client instance configuration is modified based on the proxy server address and port information specified in the configuration center. Specifically:
[0051] If an HTTP client instance matching the application class name of the test object is detected, the JVM probe is invoked to modify the address and port information in the HTTP client instance configuration, based on the address and port information of the proxy server specified in the configuration center.
[0052] The pseudocode for the JVM probe's instance configuration modification process in this embodiment is as follows:
[0053] HttpPost post = new HttpPost(url); / / Create a new HTTP client instance
[0054] RequestConfig requestConfig = RequestConfig.custom().setProxyHost(proxyIp).setProxyPort(proxyPort); / / Specific HTTP client configuration, injecting proxy host / port
[0055] post.setConfig(requestConfig); / / Put the configuration into the instance.
[0056] This step involves modification and replacement at the JVM level after application deployment, without requiring changes to the source code. It's a seamless change for the code, and the proxy access functionality won't be deployed to the production environment once the test object is retested or leaves the test environment. Furthermore, in this case, the HTTP network connection directly accesses the target server, ensuring data confidentiality and preventing modification by the proxy, thus adhering to security principles. In the test environment, developers don't need to modify the aforementioned security code. Simply start a web console, enter the deployment address and service port of the test application, and click "inject" to directly implement the injection. Developers and testers don't need to concern themselves with the intermediate proxy process.
[0057] In this embodiment, when the HTTP client instance initiates a request, the code must generate a new Java class called `RequestConfig`. This works by pre-preparing a `RequestConfig` Java class with `proxyIP` and `proxyPort`, so the proxy configuration will be included when the `RequestConfig` is generated in the code. The two parameters that must be modified are `httpProxyIP` and `httpProxyPort`, which point to the required proxy server. These two parameters are typically pre-configured in the proxy tool, defining which application uses which corresponding proxy service.
[0058] In addition, this embodiment also includes the following for comparing the class instance of the HTTP client with the class name of the test object application:
[0059] In step S202, if the class instance of each HTTP client does not match the class name of the test object application, the JVM probe is instructed to re-detect the class instance of each HTTP client after a specified interval. It should be noted that this embodiment includes a centralized configuration center for storing information such as the proxy server's address and port. This configuration center uniformly manages the proxy configuration, facilitating maintenance; furthermore, it supports dynamic updates, enabling modification of the proxy configuration without restarting the application, flexibly responding to testing needs.
[0060] Step S300: Test the external interface based on the modified instantiation. The automatic network proxy injection method provided in this embodiment allows for centralized configuration instead of application-specific settings, eliminating the need for individual modifications on each application and improving proxy configuration efficiency. By dynamically modifying the configuration of detected client instances using JVM probes and employing the proxy server specified by the configuration center, code injection can be achieved without modifying the application code, greatly simplifying the testing process and shortening the application testing cycle.
[0061] It should be understood that, although attached Figure 1 The steps in the flowchart are shown sequentially according to the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order requirement for the execution of these steps, and they can be executed in other orders. Furthermore, [the following is a list of steps]. Figure 1 At least some of the steps in the process may include multiple sub-steps or sub-stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least some of the sub-steps or stages of other steps.
[0062] The above-described embodiments of the present invention describe in detail a method for automatically injecting network proxies. Since this method can be implemented using various types of devices, the present invention also discloses an apparatus for automatically injecting network proxies. Figure 2 The following are specific embodiments for detailed explanation.
[0063] The startup detection module 401 is used to detect instances of each HTTP client through JVM probes after the JVM of the test object application starts.
[0064] The configuration modification module 402 is used to modify the configuration of the HTTP client instance if an HTTP client instance matching the application class name of the test object is detected, in combination with the address and port information of the proxy server specified in the configuration center.
[0065] Test module 403 is used to test the application of the test object based on the modified instance.
[0066] Furthermore, the startup detection module includes:
[0067] The JVM startup unit is used to load JVM probes after the JVM of the test object application starts, and to detect class instances of each HTTP client through the JVM probes.
[0068] The instance comparison unit is used to compare whether the class instance of each HTTP client is consistent with the class name of the test object application.
[0069] Furthermore, if an HTTP client instance matching the application class name of the test object is detected, the HTTP client instance configuration is modified based on the address and port information of the proxy server specified in the configuration center. Specifically:
[0070] If an HTTP client instance matching the application class name of the test object is detected, the JVM probe is invoked to modify the address and port information in the HTTP client instance configuration, based on the address and port information of the proxy server specified in the configuration center.
[0071] Furthermore, the device for automatically injecting network proxies also includes:
[0072] The probe restart unit is used to instruct the JVM probe to re-detect the class instances of each HTTP client after a specified interval if the class names of the traversed HTTP clients are inconsistent with the class names of the test object application.
[0073] For details regarding the device for automatically injecting network proxies, please refer to the method specifications above; they will not be repeated here. Each module in the aforementioned device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in the processor of the terminal device in hardware form or independently of it, or stored in the memory of the terminal device in software form, so that the processor can call and execute the corresponding operations of each module.
[0074] In one embodiment, the present invention also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the above-described method for automatically injecting a network proxy.
[0075] The computer-readable storage medium may be an electronic storage device such as flash memory, EEPROM (Electrically Erasable Programmable Read-Only Memory), EPROM (Erasable Programmable Read-Only Memory), hard disk, or ROM. Optionally, the computer-readable storage medium includes a non-transitory computer-readable storage medium. The computer-readable storage medium has storage space for program code that performs any of the method steps described above. This program code can be read from or written to one or more computer program products, and the program code may be compressed in an appropriate form.
[0076] In one embodiment, the present invention provides a computer device including a memory and a processor, the memory storing a computer program, and the processor executing the computer program to perform the above-described method for automatically injecting network proxies.
[0077] The computer device includes a memory, a processor, and one or more computer programs, wherein the one or more computer programs may be stored in the memory and configured to be executed by one or more processors, and one or more application programs are configured to perform the above-described method for automatically injecting network proxies.
[0078] A processor may include one or more processing cores. The processor connects to various parts of the computer device using various interfaces and lines, and performs various functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in memory, and by calling data stored in memory. Optionally, the processor may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor may integrate one or a combination of several of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), and modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the displayed content; and the modem handles wireless communication. It is understood that the modem may also be implemented separately as a communication chip, without being integrated into the processor.
[0079] The memory may include random access memory (RAM) or read-only memory (ROM). The memory can be used to store instructions, programs, code, code sets, or instruction sets. The memory may include a program storage area and a data storage area. The program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as touch functionality, sound playback functionality, image playback functionality, etc.), and instructions for implementing the various method embodiments described above. The data storage area may also store data created by the terminal device during use.
[0080] The above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for automatically injecting a network proxy, characterized in that, include: Once the JVM of the test application starts, instances of each HTTP client are detected using JVM probes. If an HTTP client instance matching the application class name of the test object is detected, the HTTP client instance configuration is modified based on the address and port information of the proxy server specified in the configuration center. Test the application of the test object based on the modified instance.
2. The method for automatically injecting network proxies as described in claim 1, characterized in that, Once the JVM of the test application starts, the JVM probe is used to detect instances of each HTTP client. Specifically: Once the JVM of the test application starts, the JVM probe is loaded, and the class instances of each HTTP client are detected through the JVM probe. Compare the class instances of each HTTP client with the class names of the test object application to see if they match.
3. The method for automatically injecting network proxies as described in claim 2, characterized in that, If an HTTP client instance matching the application class name of the test object is detected, the HTTP client instance configuration will be modified based on the proxy server address and port information specified in the configuration center, specifically as follows: If an HTTP client instance matching the application class name of the test object is detected, the JVM probe is invoked to modify the address and port information in the HTTP client instance configuration, based on the address and port information of the proxy server specified in the configuration center.
4. The method for automatically injecting network proxies as described in claim 3, characterized in that, Also includes: If the class instance of each HTTP client does not match the class name of the test object, instruct the JVM probe to re-detect the class instance of each HTTP client after a specified interval.
5. An apparatus for automatically injecting network proxies, characterized in that, include: The startup detection module is used to detect instances of each HTTP client through JVM probes after the JVM of the test object application starts. The configuration modification module is used to modify the configuration of the HTTP client instance if an HTTP client instance matching the application class name of the test object is detected, based on the address and port information of the proxy server specified in the configuration center. The testing module is used to test the application of the test object based on the modified instance.
6. The apparatus for automatically injecting network proxies as described in claim 5, characterized in that, The startup detection module includes: The JVM startup unit is used to load JVM probes after the JVM of the test object application starts, and to detect class instances of each HTTP client through the JVM probes. The instance comparison unit is used to compare whether the class instance of each HTTP client is consistent with the class name of the test object application.
7. The apparatus for automatically injecting network proxies as described in claim 6, characterized in that, If an HTTP client instance matching the application class name of the test object is detected, the HTTP client instance configuration is modified based on the address and port information of the proxy server specified in the configuration center. Specifically: If an HTTP client instance matching the application class name of the test object is detected, the JVM probe is invoked to modify the address and port information in the HTTP client instance configuration, based on the address and port information of the proxy server specified in the configuration center.
8. The apparatus for automatically injecting network proxies as described in claim 7, characterized in that, Also includes: The probe restart unit is used to instruct the JVM probe to re-detect the class instances of each HTTP client after a specified interval if the class names of the traversed HTTP clients are inconsistent with the class names of the test object application.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the method for automatically injecting network proxies as described in any one of claims 1-4.
10. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it performs the method of automatically injecting network proxies as described in any one of claims 1-4.