Apache Commons Pool2-based SSH connection pool device, method, equipment and storage medium
Through the SSH connection pooling device of Apache Commons Pool2, SSH connections are managed using object pooling technology, solving the problem of frequent SSH connections occupying resources and security risks, realizing the reduction of resource consumption and security, and ensuring system performance and integrity.
Patent Information
- Application Number
- CN202411313971.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-20
- Publication Date
- 2024-12-10
AI Technical Summary
Frequent SSH connections occupy server resources, affect performance and response speed, and pose security risks due to frequent authentication and connection operations.
The SSH connection pooling device based on Apache Commons Pool2 manages SSH connection session objects through object pooling technology, reducing frequent creation and destruction of connections, and reducing resource consumption and security risks using connection pool multiplexing technology.
It effectively reduces server resource consumption and security risks, ensures the performance and integrity of the system, and keeps other functional characteristics of the application system unaffected.
Smart Images

Figure CN119109965A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of communication between an SSH protocol and a server, and in particular to an SSH connection pool device, method, equipment and storage medium based on Apache Commons Pool 2. Background Art
[0002] SSH (Secure Shell) is an encrypted network communication protocol designed for secure remote login and data transmission in an insecure network environment. It uses advanced encryption technology to ensure the confidentiality and integrity of data during communication, and supports multiple authentication mechanisms, such as password and public key authentication, to effectively protect the security of user identities. SSH is widely used in scenarios such as server management, remote command execution, file transfer, and port forwarding, and has become an indispensable tool in the field of network security and remote access.
[0003] Apache SSHJ is an open source Java library developed and maintained by Hierynomus. It provides support for the SSH (Secure Shell) protocol. SSHJ enables users to implement secure remote command execution, file transfer and other functions in Java applications. The SSHJ API is designed to be simple and easy to use, and is suitable for Java projects that require SSH communication functions. With SSHJ, developers can easily build stable and secure remote communication solutions.
[0004] Apache Commons Pool2 is an open source project under Apache Commons, mainly used to implement and manage object pools. Object pooling is a common design pattern that optimizes resource utilization and improves application performance by amortizing the cost of creating and destroying expensive objects through reuse. Commons Pool2 provides a set of interface methods for implementing object pooling, and has built-in a variety of object pool implementations with unique features. Its implementation provides some parameters to control the behavior of the object pool, such as the maximum number of pooled objects, maximum idle time, minimum idle number, etc., which can be flexibly configured according to different application scenarios. In addition, Commons Pool2 also provides some commonly used implementation classes, based on which we can implement, configure and extend to create an SSH custom connection pool tool for client use.
[0005] When we need to frequently interact with the server through the client using the SSH communication protocol, we will face the following problems:
[0006] Resource consumption: Frequent SSH connections will occupy the server's CPU, memory, and network resources. Especially when there are a large number of connections or the connection time is long, it may affect the overall performance and response speed of the server.
[0007] Security risks: Each SSH connection requires authentication. If the password is not set properly or there are loopholes, it may increase the risk of malicious attacks. In addition, frequent SSH connections also increase the chance of exposing server information. Summary of the invention
[0008] Purpose of the invention: The present invention provides an SSH connection pool device, method, equipment and storage medium based on Apache Commons Pool2, which is extended and configured based on the object pooling interface of Commons Pool2 to implement a custom SSH connection pool to manage SSH connection session objects, and utilizes the reuse technology of connection pool connection objects to reduce the frequent creation, destruction and initiation of connections of SSH connection objects, thereby reducing resource consumption and security risks brought to the server in the business.
[0009] Technical solution: The SSH connection pool device based on Apache Commons Pool2 described in the present invention includes: a user, a client interface, a connection session factory, a connection session and a server; the user calls the client interface with server connection information to initiate an SSH connection request, and after the request reaches the connection pool through the client interface, an idle connection session is obtained from the connection pool, and the current user uses this session to interact with the server.
[0010] Furthermore, if there is no idle connection session in the SSH connection pool, the connection session creation method is executed, and after successful creation, it is provided for use by the client.
[0011] Accordingly, a method for constructing an SSH connection pool device based on Apache Commons Pool2 includes the following steps:
[0012] Step 1. Create a connection session factory class, implement the key-value pair-based pool management factory interface in Apache Commons Pool2, and override the methods in this interface, including methods for creating connection objects and methods for checking whether the connection objects are available.
[0013] Step 2. In the "method for creating a connection object" in step 1, parse the key information to obtain the server's domain name, port, user name, password, and authentication information, then call the parameterless constructor of the SSH client class in Apache SSHJ, and use the parsed connection information to create a connection session object; finally, use the "connection session object" as a parameter, use the constructor of the "default pooling class" in Apache Commons Pool2 to generate an instance and return it to the factory;
[0014] Step 3, in the "method for checking whether the connection object is available" of step 1, obtain the connection session object through the parameter of the method, call the function of whether it is in the connection state in this object, and return the result of whether it is available to the factory;
[0015] Step 4: Implement a custom connection session factory by extending the factory interface method of the key-value pair-based pool management object according to the above;
[0016] Step 5. Declare a connection pool class, and reference the "key-value object pool configuration class" in the parameterless constructor of the class to create a configuration object and set relevant parameters;
[0017] Step 6. Declare a function to obtain the connection pool in the connection pool class; the function references the "connection pool configuration object" and "connection session factory object" as parameters to create a connection pool object;
[0018] Step 7: According to the above, by configuring the connection pool parameters and referencing the connection pool factory object, the SSH connection pool object is successfully constructed;
[0019] Step 8. Declare a client interface class, declare a connection pool static variable in the class, and obtain its value by calling the constructor in the "connection pool class"; declare a parameterized constructor of the client interface class, concatenate the connection information into a string in Json format to form a unique key; finally, use this key to obtain the connection object in the connection pool through the "connection object function" of the connection pool, and assign it to the connection session static variable in the class;
[0020] Step 9. Declare a function interface that can initiate SSH access in the client interface class; use the connection object obtained from the connection pool in the declared function body to establish a connection with the server and communicate. After the communication is completed, call the return method of the "connection pool" to put the used "connection object" back into the connection pool;
[0021] Step 10: According to the above steps, an SSH connection pool client is successfully constructed and combined with the constructed connection pool object to form "an SSH connection pool device based on Apache Commons Pool2".
[0022] Furthermore, in step 1, the factory interface of the object pool management object based on key-value pairs in Apache Commons Pool2 needs to be genericized, with the key being a string type and the value being the connection object client of Apache SSHJ, to ensure that all overridden method parameters in the entire factory class are safe, standardized, and rigorous.
[0023] Furthermore, in step 1, the methods in the interface also include: a method for activating a connection object, a method for destroying a connection object, and a method for passivating a connection object.
[0024] Furthermore, in step 2, when parsing the key information, the key information is converted into an easily accessible Json object, and it is determined whether the parameters in the Json object are legal. If legal, the logic of creating a connection object is executed. If illegal, an exception message is returned to the client and the program execution is terminated.
[0025] Accordingly, a connection device of an SSH connection pool device based on Apache Commons Pool2 is characterized by comprising: one or more processors;
[0026] A storage device for storing one or more programs and user data;
[0027] When the one or more programs are executed by one or more processors, the one or more processors implement the connection method of the SSH connection pool device based on Apache Commons Pool2 as described in any one of claims 3 to 6.
[0028] Correspondingly, a connection storage medium of an SSH connection pool device based on Apache Commons Pool2 is characterized in that a computer program is stored thereon, and when the program is executed by a processor, a connection method of an SSH connection pool device based on Apache Commons Pool2 as described in any one of claims 3 to 6 is implemented.
[0029] Beneficial effects: Compared with the prior art, the present invention has the following significant advantages: the present invention is based on the relevant components of Apache Commons Pool2 and Apache SSHJ framework, and uses its extended interface for secondary development, which ensures the security, standardization and compatibility of the code, and not only meets the needs of the client to frequently access the server, but also avoids the resource consumption problem caused by frequent creation and destruction of connection objects, and does not affect other functional characteristics of the application system, thereby ensuring the integrity of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] Figure 1 It is a schematic diagram of the SSH connection pool principle of the present invention.
[0031] Figure 2 It is a schematic diagram of the connection pool structure of the present invention.
[0032] Figure 3 It is a schematic diagram of mapping keys and connection session objects in the connection pool of the present invention. DETAILED DESCRIPTION
[0033] like Figure 1 As shown, an SSH connection pool device based on Apache Commons Pool2 includes: a user, a client interface, a connection session factory, a connection session and a server; the user calls the client interface with the server connection information to initiate an SSH connection request, and after the request reaches the connection pool through the client interface, an idle connection session is obtained from the connection pool, and the current user uses this session to interact with the server.
[0034] like Figure 2 As shown, a method for constructing an SSH connection pool device based on Apache Commons Pool2 includes the following steps:
[0035] Step 1. Create a connection session factory class and implement the key-value pair-based pool management factory interface in Apache Commons Pool2 (where the key is the server's domain name address, which is the unique identifier of the SSH connection client object in the connection pool, and the value is the SSH connection client session object), and rewrite the methods in this interface, including: methods for activating connection objects, destroying connection objects, creating connection objects, passivating connection objects, and checking whether connection objects are available.
[0036] Step 2: In the "method for creating a connection object" of step 1, parse the key information to obtain the server's domain name, port, user name, password, authentication information, etc., then call the no-parameter constructor of the SSH client class in Apache SSHJ, and use the parsed connection information to create a connection session object. Finally, use the "connection session object" as a parameter, use the constructor of the "default pooling class" in Apache Commons Pool2 to generate an instance and return it to the factory.
[0037] Step 3. In the "Method for checking whether the connection object is available", obtain the connection session object through the parameter of the method, call the function of whether it is in the connection state in this object, and return the result of whether it is available to the factory.
[0038] Step 4: Based on the above, a custom connection session factory is implemented by extending the factory interface method of the key-value pair-based pool management object.
[0039] Step 5. Declare a connection pool class, and reference the "key-value object pool configuration class" in the parameterless constructor of the class to create a configuration object, and set relevant parameters, mainly including: maximum number of resources, maximum number of resources corresponding to each key, maximum number of keys, minimum number of keys, etc.
[0040] Step 6: Declare a function to obtain the connection pool in the connection pool class. The function references the "connection pool configuration object" and "connection session factory object" as parameters to create a connection pool object.
[0041] Step 7: According to the above, by configuring the connection pool parameters and referencing the connection pool factory object, the SSH connection pool object is successfully constructed.
[0042] Step 8. Declare a client interface class and declare a connection pool static variable in the class. Its value is obtained by calling the constructor in the "connection pool class". Declare a parameterized constructor of the client interface class and concatenate the connection information into a string in Json format to form a unique key. Finally, use this key to obtain the connection object in the connection pool through the "connection object function" of the connection pool and assign it to the connection session static variable in the class.
[0043] Step 9. Declare a function interface that can initiate SSH access in the client interface class; use the connection object obtained from the connection pool in the declared function body to establish a connection with the server and communicate. After the communication is completed, call the return method of the "connection pool" and put the used "connection object" back into the connection pool.
[0044] Step 10: According to the above steps, an SSH connection pool client is successfully constructed and combined with the constructed connection pool object to form "an SSH connection pool device based on Apache Commons Pool2".
[0045] like Figure 3 As shown, there are multiple server string keys in the connection pool, each string key corresponds to a server session set, and there are multiple sessions in each session set.
[0046] The present invention not only ensures the security, standardization and compatibility of communication with the server, but also meets the needs of frequent access to specific servers, while also avoiding the resource consumption problem caused by frequent creation and destruction of connection objects; other functional characteristics and integrity of the entire business are also guaranteed.
Claims
1. An SSH connection pool device based on Apache Commons Pool2, characterized in that: include: Users, client interfaces, connection session factories, connection sessions and servers; The user calls the client interface with the server connection information to initiate an SSH connection request. After the request reaches the connection pool through the client interface, an idle connection session is obtained from the connection pool. The current user uses this session to interact with the server.
2. The SSH connection pool device based on Apache Commons Pool2 according to claim 1, characterized in that: If there is no idle connection session in the SSH connection pool, the connection session creation method is executed, and the connection session is successfully created for use by the client.
3. A method for constructing an SSH connection pool device based on Apache Commons Pool2 as claimed in claim 1, characterized in that: The steps include: Step 1. Create a connection session factory class, implement the key-value pair-based pool management factory interface in Apache Commons Pool2, and override the methods in this interface, including methods for creating connection objects and methods for checking whether the connection objects are available. Step 2. In the "method for creating a connection object" of step 1, parse the key information to obtain the server's domain name, port, user name, password, and authentication information, then call the parameterless constructor of the SSH client class in Apache SSHJ, and use the parsed connection information to create a connection session object; finally, use the "connection session object" as a parameter, use the constructor of the "default pooling class" in Apache Commons Pool2 to generate an instance and return it to the factory; Step 3, in the "method for checking whether the connection object is available" of step 1, obtain the connection session object through the parameter of the method, call the function of whether it is in the connection state in this object, and return the result of whether it is available to the factory; Step 4: Implement a custom connection session factory by extending the factory interface method of the key-value pair-based pool management object according to the above; Step 5. Declare a connection pool class, and reference the "key-value object pool configuration class" in the parameterless constructor of the class to create a configuration object and set relevant parameters; Step 6. Declare a function to obtain the connection pool in the connection pool class; the function references the "connection pool configuration object" and "connection session factory object" as parameters to create a connection pool object; Step 7: According to the above, by configuring the connection pool parameters and referencing the connection pool factory object, the SSH connection pool object is successfully constructed; Step 8. Declare a client interface class, declare a connection pool static variable in the class, and obtain its value by calling the constructor in the "connection pool class"; declare a parameterized constructor of the client interface class, concatenate the connection information into a string in Json format to form a unique key; finally, use this key to obtain the connection object in the connection pool through the "connection object function" of the connection pool, and assign it to the connection session static variable in the class; Step 9. Declare a function interface that can initiate SSH access in the client interface class; use the connection object obtained from the connection pool in the declared function body to establish a connection with the server and communicate. After the communication is completed, call the return method of the "connection pool" to put the used "connection object" back into the connection pool; Step 10: According to the above steps, an SSH connection pool client is successfully constructed and combined with the constructed connection pool object to form "an SSH connection pool device based on Apache Commons Pool2".
4. The connection method of the SSH connection pool device based on Apache Commons Pool2 as claimed in claim 3, characterized in that: In step 1, it is necessary to perform generic processing on the factory interface of the object pool management object based on key-value pairs in Apache Commons Pool2, where the key is a string type and the value is a connection object client of Apache SSHJ.
5. The connection method of the SSH connection pool device based on Apache Commons Pool2 as claimed in claim 3, characterized in that: In step 1, the methods in the interface also include: a method for activating a connection object, a method for destroying a connection object, and a method for passivating a connection object.
6. The method according to claim 1, wherein: In step 2, when parsing the key information, the key information is converted into an easily accessible Json object, and it is determined whether the parameters in the Json object are legal. If legal, the logic of creating a connection object is executed. If illegal, an exception message is returned to the client and the program execution is terminated.
7. A connection device of an SSH connection pool device based on Apache Commons Pool2, characterized in that: include: one or more processors; A storage device for storing one or more programs and user data; When the one or more programs are executed by one or more processors, the one or more processors implement the connection method of the SSH connection pool device based on Apache Commons Pool2 as described in any one of claims 3 to 6.
8. A connection storage medium of an SSH connection pool device based on Apache Commons Pool2, characterized in that: A computer program is stored thereon, and when the program is executed by a processor, a connection method of an SSH connection pool device based on Apache Commons Pool2 as described in any one of claims 3 to 6 is implemented.
