Method and device for detecting NAT (Network Address Translation) type

By setting multiple STUN server addresses in the terminal and obtaining candidate information to detect NAT types, the problem that the prior art cannot detect NAT types in the browser is solved, and NAT type detection on the web side and the success rate of UDP connections are improved.

CN119922162APending Publication Date: 2025-05-02SHANGHAI BILIBILI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510127791.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-27
Publication Date
2025-05-02

AI Technical Summary

Technical Problem

The prior art cannot detect NAT types in terminals connected through browsers, which affects the success rate of UDP protocol connections.

Method used

By creating an object and setting at least two different STUN server addresses, obtain candidate information for different STUN servers, and send a test request to the target server to determine the NAT type of the terminal if the same srflx address is included in the candidate information.

Benefits of technology

It realizes detection of NAT type in terminals running on the web side, and improves the success rate of UDP protocol connection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119922162A_ABST
    Figure CN119922162A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides an NAT (Network Address Translation) type detection method, and belongs to the technical field of network communication. The NAT type detection method is used for the terminal, and comprises the following steps: creating a first object, and setting at least two different STUN server addresses for the first object; obtaining candidate information of different STUN servers through the at least two different STUN server addresses; under the condition that the candidate information comprises one or more identical srflx addresses, creating a second object; and sending a test request to a target server based on the second object to determine the NAT type of the terminal. According to the technical scheme provided by the embodiment of the invention, the NAT type of the terminal can be determined, and the terminals with high success rates can establish connection with each other to improve the efficiency of a point-to-point network because the success rates of establishing connection between the terminals with different NAT types are different.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of Internet technology, and in particular, to a NAT type detection method, apparatus, computer equipment, computer-readable storage medium, and computer program product. Background Art

[0002] When using the UDP protocol to connect two terminals, the NAT type of the terminals is crucial to the success rate of the connection.

[0003] However, due to many limitations of the browser itself, the prior art cannot detect the NAT type when connecting two terminals through the browser.

[0004] It should be noted that the above content is not necessarily prior art, nor is it intended to limit the scope of patent protection of this application. Summary of the invention

[0005] The embodiments of the present application provide a NAT type detection method, apparatus, computer device, computer-readable storage medium, and computer program product to solve or alleviate one or more of the technical problems raised above.

[0006] One aspect of an embodiment of the present application provides a NAT type detection method for a terminal, the method comprising: Creating a first object, and setting at least two different STUN server addresses for the first object; Acquire candidate information of different STUN servers through the at least two different STUN server addresses; In case the candidate information includes one or more identical srflx addresses, creating a second object; A test request is sent to a target server based on the second object to determine the NAT type of the terminal.

[0007] Optionally, the method further comprises: In a case where the candidate information includes at least two different srflx addresses, it is determined that the NAT type is symmetric.

[0008] Optionally, the target server is configured with a first network address, a second network address and a first port; Sending a test request to a target server based on the second object to determine the NAT type of the terminal includes: Filling the first network address and the first port into the preset configured Answer to generate a remote session description; According to the remote session description, a first test request is sent to the target server; the target server is used to: receive the first test request from the first network address and the first port, and return a first test response message from any port of the second network address in response to the first test request; In case the first test response message is received, it is determined that the NAT type is a full cone type.

[0009] Optionally, the target server is further configured with a second port and a third port; and the method further comprises: If the first test response message is not received within a first preset time, adding first new candidate information to the remote session description; wherein the first new candidate information includes the first network address and the second port; Sending a second test request to the target server according to the first new candidate information; the target server is used to: receive the second test request from the first network address and the second port, and return a second test response message from the first network address and the third port in response to the second test request; When the second test response message is received, it is determined that the NAT type is an IP restriction type.

[0010] Optionally, the method further comprises: If the second test response message is not received within a second preset time, adding second new candidate information to the remote session description; wherein the second new candidate information includes the first network address and the third port; Sending a third test request to the target server according to the second new candidate information; the target server is used to: receive the third test request from the first network address and the third port, and return a third test response message from the first network address and the third port in response to the third test request; When the third test response message is received, it is determined that the NAT type is a port restriction type.

[0011] Optionally, the method further comprises: If the third test response message is not received within the third preset time, it is determined that the detection has failed.

[0012] Another aspect of an embodiment of the present application provides a NAT type detection method for a target server, wherein the target server is configured with a first network address, a second network address, and a first port; the method comprises: receiving a first test request from the first network address and the first port; In response to the first test request, returning a first test response message from any port of the second network address; Wherein, when the first test request is successfully returned to the terminal, the NAT type is determined to be a full cone type.

[0013] Optionally, the target server is further configured with a second port and a third port; and the method further comprises: receiving a second test request from the first network address and the second port; In response to the second test request, returning a second test response message from the first network address and the third port; Wherein, when the second test request is successfully returned to the terminal, it is determined that the NAT type is IP restriction type.

[0014] Optionally, the method further comprises: receiving a third test request from the first network address and the third port; In response to the third test request, returning a third test response message from the first network address and the third port; Wherein, when the third test request is successfully returned to the terminal, it is determined that the NAT type is a port restriction type.

[0015] Another aspect of an embodiment of the present application provides a NAT type detection device for a terminal, the device comprising: A first creation module, configured to create a first object and set at least two different STUN server addresses for the first object; An acquisition module, configured to acquire candidate information of different STUN servers through the at least two different STUN server addresses; A second creation module, configured to create a second object when the candidate information includes one or more identical srflx addresses; A determination module is used to send a test request to a target server based on the second object to determine the NAT type of the terminal.

[0016] Another aspect of an embodiment of the present application provides a NAT type detection device for a target server, wherein the target server is configured with a first network address, a second network address, and a first port; the device includes: A receiving module, configured to receive a first test request from the first network address and the first port; a returning module, configured to return a first test response message from any port of the second network address in response to the first test request; Wherein, when the first test request is successfully returned to the terminal, the NAT type is determined to be a full cone type.

[0017] Another aspect of an embodiment of the present application provides a computer device, including: at least one processor; and a memory communicatively coupled to the at least one processor; Wherein: the memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method as described above.

[0018] Another aspect of an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions, and when the computer instructions are executed by a processor, the method described above is implemented.

[0019] Another aspect of an embodiment of the present application provides a computer program product, including a computer program, which implements the method described above when executed by a processor.

[0020] The above technical solution adopted in the embodiment of the present application may have the following advantages: In the terminal, the candidate information of different STUN servers is obtained through different STUN server addresses. In this way, when the candidate information includes one or more identical srflx addresses, a test request is sent to the target server to determine the NAT type of the terminal, thereby realizing the detection of the NAT type by the terminal running in the form of a Web terminal. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] The accompanying drawings exemplarily illustrate the embodiments and constitute a part of the specification, and together with the text description of the specification, are used to explain the exemplary implementation of the embodiments. The embodiments shown are for illustrative purposes only and do not limit the scope of the claims. In all drawings, the same reference numerals refer to similar but not necessarily identical elements.

[0022] Figure 1 The following schematically shows an operating environment diagram of a NAT type detection method according to Embodiment 1 of the present application; Figure 2 The flowchart of the NAT type detection method according to the first embodiment of the present application is schematically shown; Figure 3 Schematically shows Figure 2 Flow chart of sub-steps of step S206; Figure 4 The newly added flow chart of the NAT type detection method according to the first embodiment of the present application is schematically shown; Figure 5 The newly added flow chart of the NAT type detection method according to the first embodiment of the present application is schematically shown; Fig. 6A A diagram schematically shows an application example of the NAT type detection method according to the first embodiment of the present application; Figure 6B A diagram schematically shows an application example of the NAT type detection method according to the first embodiment of the present application; Figure 7 The flowchart of the NAT type detection method according to the second embodiment of the present application is schematically shown; Figure 8 The newly added flow chart of the NAT type detection method according to the second embodiment of the present application is schematically shown; Fig. 9 The newly added flow chart of the NAT type detection method according to the second embodiment of the present application is schematically shown; Fig.10 A block diagram of a NAT type detection device according to Embodiment 3 of the present application is schematically shown; Fig.11 A block diagram of a NAT type detection device according to a fourth embodiment of the present application is schematically shown; Fig.12 The hardware architecture diagram of the computer device according to the fifth embodiment of the present application is schematically shown. DETAILED DESCRIPTION

[0023] In order to make the purpose, technical solutions and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not intended to limit the present application. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application.

[0024] It should be noted that the descriptions involving "first", "second", etc. in the embodiments of the present application are only for descriptive purposes and cannot be understood as indicating or implying their relative importance or implicitly indicating the number of technical features indicated. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In addition, the technical solutions between the various embodiments can be combined with each other, but they must be based on the ability of ordinary technicians in the field to implement them. When the combination of technical solutions is contradictory or cannot be implemented, it should be deemed that such combination of technical solutions does not exist and is not within the scope of protection required by this application.

[0025] In the description of the present application, it should be understood that the numerical labels before the steps do not indicate the order in which the steps are executed, but are only used to facilitate the description of the present application and to distinguish each step, and therefore should not be understood as a limitation on the present application.

[0026] First, the following terms are explained: WebRTC: A real-time communication solution that includes video and audio acquisition, encoding and decoding, data transmission, audio and video display, etc. It uses ICE to establish a connection. Modern mainstream browsers all support this protocol.

[0027] NAT: used to convert private network addresses in the internal network into public network addresses, or vice versa. It is used to solve the problem of insufficient IPv4 (Internet Protocol version 4) addresses and to enable communication between LAN devices and external networks.

[0028] STUN: is a network protocol that allows clients behind NAT (or multiple NATs) to find out their public network address, what type of NAT they are behind, and the Internet port to which the NAT is bound for a local port.

[0029] ICE: A framework that integrates various NAT penetration technologies, such as STUN, TURN (Traversal Using RelayNAT), RSIP (Realm Specific IP), etc.

[0030] P2P: Peer-to-peer (P2P), also known as peer-to-peer network, is an Internet system without a central server and relying on user groups (peers) to exchange information. Through P2P technology, users can connect to each other, and some users act as service providers, thereby reducing the server load in the system.

[0031] Web: refers to applications or web page interfaces built with web technologies (such as HTML, CSS, JavaScript), which can be accessed and interacted with through a browser.

[0032] UDP: A transport protocol located at the transport layer that focuses on fast data transmission without complex connection establishment and management.

[0033] IP: A protocol used to identify devices and transmit data on a network. IP specifies how data packets are transmitted from a source device to a destination device on a network.

[0034] Secondly, in order to facilitate those skilled in the art to understand the technical solutions provided in the embodiments of the present application, the relevant technologies are described below: The applicant understands that the STUN protocol can be used to detect the NAT type of the terminal, but the browser itself has various limitations and cannot fully control the communication of the UDP protocol, so the use of the STUN protocol to detect the NAT type of the terminal cannot be performed on the Web side. However, when using WebRTC in the browser to create a connection between two terminals, the NAT type of the two terminals is very critical to the connection success rate.

[0035] To this end, the embodiment of the present application provides a NAT type detection technical solution. In this technical solution, the NAT type detection is completed on the Web side. See below for details.

[0036] Finally, for ease of understanding, an exemplary operating environment is provided below.

[0037] like Figure 1 As shown, the environment diagram includes a terminal 2, a STUN server 4, a target server 6 and a network 8, wherein: The terminal 2 may be an electronic device running an operating system such as Windows, Android™ or iOS, such as a smart phone, a tablet device, a laptop computer, a virtual reality device, a gaming device, a set-top box, a vehicle-mounted terminal, or a smart TV. Based on the above operating system, various applications may be run, such as applications or web page interfaces built using Web technologies (such as HTML, CSS, and JavaScript).

[0038] The STUN server 4 can be used to help terminal devices discover their public IP addresses and ports in a network environment with NAT or firewalls, and realize direct communication between devices. It can be used for real-time communication (such as video calls, voice calls) or P2P (peer-to-peer) connections.

[0039] The target server 6 is configured with at least two public network addresses that can receive data. The target server 6 can be used to receive STUN PING packets in the ICE connection protocol and give a reply. The target server 6 can be replaced by a server cluster. For example, if the server has only one public network address, a server cluster can be composed of multiple servers to meet the configuration requirements of at least two public network addresses.

[0040] The STUN server 4 and the target server 6 may be configured to communicate with the terminal 2 via a network 8. The network 4 includes various network devices, such as routers, switches, multiplexers, hubs, modems, bridges, repeaters, firewalls, proxy devices, and / or the like. The network 4 may include physical links, such as coaxial cable links, twisted pair cable links, optical fiber links, combinations thereof, and the like, or wireless links, such as cellular links, satellite links, Wi-Fi links, and the like.

[0041] It should be noted that the above devices are exemplary, and the number and type of devices are adjustable in different scenarios or according to different needs.

[0042] The target server 6 is configured with a plurality of network addresses, such as a first network address and a second network address. The target server 6 is also configured with a plurality of ports, such as a first port, a second port and a third port.

[0043] The terminal 2 can be operated in the form of a Web terminal, that is, by running a browser as an application environment, and using Web technology to interact with a server or other terminals.

[0044] The following describes the technical solution of the present application through multiple embodiments, taking the terminal 2 or the target server 6 as the execution subject. It should be noted that these embodiments can be implemented in many different forms and should not be interpreted as being limited to the embodiments described here.

[0045] Embodiment 1 The NAT type detection method provided in this embodiment is used in terminal 2.

[0046] Figure 2 The flowchart of the NAT type detection method according to the first embodiment of the present application is schematically shown.

[0047] like Figure 2 As shown, the NAT type detection method may include steps S200 to S206, wherein: Step S200: create a first object, and set at least two different STUN server addresses for the first object.

[0048] Step S202: Acquire candidate information of different STUN servers through the at least two different STUN server addresses.

[0049] Step S204: When the candidate information includes one or more identical srflx addresses, create a second object.

[0050] Step S206: Send a test request to a target server based on the second object to determine the NAT type of the terminal.

[0051] In the terminal, the candidate information of different STUN servers is obtained through different STUN server addresses. In this way, when the candidate information includes one or more identical srflx addresses, a test request is sent to the target server to further determine the NAT type of the terminal, thereby realizing the detection of the NAT type by the terminal running in the form of a Web terminal.

[0052] The following combination Figure 2, each step in steps S200~S206 and other optional steps are described in detail.

[0053] Step S200 , create a first object, and set at least two different STUN server addresses for the first object.

[0054] The first object can be used to connect to a STUN server. Communication with the STUN server can be performed through the set STUN server address. The first object can be an RTCPeerConnection object. RTCPeerConnection is a core object in WebRTC, which is used to establish a point-to-point connection between two devices to transmit audio and video, etc.

[0055] Step S202 , obtaining candidate information of different STUN servers through the at least two different STUN server addresses.

[0056] Candidate information can be used to describe what transport protocol, network address, and port are used for communication. Candidate information includes network addresses or paths that can be used by ICE to establish point-to-point connections. In this embodiment, an Offer can be created and set as a local session description to wait for Candidate collection. An Offer is a string written according to the Session Description Protocol, which contains what protocol, address, and port are used for communication. The Session Description Protocol is a formatted text protocol for describing multimedia communication sessions. The local session description can be used to describe the local audio and video, data channel related parameters, supported codecs, network addresses, etc.

[0057] Step S204 , creating a second object when the candidate information includes one or more identical srflx addresses.

[0058] srflx (server reflexive) is a public network address returned by the STUN server, indicating the result of NAT mapping. When the candidate information includes one or more identical srflx addresses, it means that the terminal will not create different mappings for different STUN server addresses, and further creation of a second object is required to determine whether the terminal's NAT has restrictions on network addresses and ports to determine the terminal's NAT type. If there is no srflx address, it means that there is no NAT on the terminal or UDP is unavailable in the link between the terminal and the STUN server or the server is down. In some embodiments, if there are multiple identical srflx addresses, they can be regarded as one srflx address.

[0059] Regarding the candidate information including at least two different srflx addresses, in an optional embodiment, the method further includes: when the candidate information includes at least two different srflx addresses, determining that the NAT type is symmetric.

[0060] In the case where the candidate information includes at least two different srflx addresses, it means that the terminal will create different mappings for different STUN server addresses, and the NAT type of the terminal is determined to be symmetric. Symmetric NAT refers to NAT that creates different mappings for different target addresses, and the mapping of each target address and port is unique. The condition for allowing data packets to enter the terminal of symmetric NAT is that the target address and port must completely match the current mapping.

[0061] In this embodiment, since the candidate information includes at least two different srflx addresses, it can be determined that the NAT type is symmetric.

[0062] Step S206 , sending a test request to a target server based on the second object to determine the NAT type of the terminal.

[0063] Since the success rates of establishing connections between terminals of different NAT types are different, terminals with high success rates can be enabled to establish connections with each other to improve the efficiency of the peer-to-peer network. In some embodiments, the test request can be a STUN PING data packet. STUN PING is an operation performed through the STUN protocol, which can be used to detect the network connection status of the device, obtain the public network address of the device, and understand the NAT type of the device. Specifically, an Offer can be created, and then the username and password in the Offer can be modified to the values ​​pre-configured in the target server, and the Offer is set to a local description. The ICE protocol requires each communication end (local end and remote end) to generate a pair of unique identity credentials: username and password, called ICE Credentials. The credentials are used to identify the two communicating parties during the NAT traversal process to prevent data packets from being mistaken for belonging to other sessions.

[0064] The specific process of sending a test request to the target server based on the second object to determine the NAT type is exemplarily introduced below.

[0065] In an optional embodiment, if Figure 3 As shown, step S206 may include: Step S300, filling the first network address and the first port into the preset configured Answer to generate a remote session description; Step S302, sending a first test request to the target server according to the remote session description; the target server is used to: receive the first test request from the first network address and the first port, and return a first test response message from any port of the second network address in response to the first test request; Step S304: upon receiving the first test response message, determining that the NAT type is full cone.

[0066] Answer is a string written according to the session description protocol, which contains what protocol, address and port are used for communication. The remote session description is used to describe the audio and video of the remote end, the relevant parameters of the data channel, the supported codecs, the network address and other information. When the target server receives the first test request from the first network address and the first port, and returns the first test response message from any port of the second network address, the terminal receives the first test response message, which means that the terminal allows data packets from any source to be passed in, and then determines that the NAT type of the terminal is a full cone, that is, a full cone NAT. A full cone NAT refers to a NAT whose mapping relationship can allow data packets from any source to be passed in. The condition for allowing data packets to enter in a terminal of a full cone NAT is any external device that only needs to know the public network address and port of the terminal. In some embodiments, the terminal can send a first test request to the target server within WebRTC through WebRTC technology. The first test request and the first test response message can be STUN PING. When the first test response message is received, the IceConnectionState property of the RTCPeerConnection object will become connected, indicating that the ICE handshake has passed and the two ends can communicate. In a specific implementation process, when the target server responds to the STUN PING sent by the terminal, it can send two different messages to the terminal, one is a response to the STUN PING of WebRTC, and the other is an actively sent STUN PING, so that WebRTC can detect that the IceConnectionState becomes connected.

[0067] In this embodiment, when the network address requested by the terminal is different from the network address responded by the target server, if the terminal can receive the first test response message, it can be determined that the NAT type is a full cone type.

[0068] In an optional embodiment, if Figure 4 As shown, the method may also include: Step S400, when the first test response message is not received within a first preset time, adding first new candidate information to the remote session description; wherein the first new candidate information includes the first network address and the second port; Step S402: sending a second test request to the target server according to the first new candidate information; the target server is used to: receive the second test request from the first network address and the second port, and return a second test response message from the first network address and the third port in response to the second test request; Step S404: upon receiving the second test response message, determining that the NAT type is IP restricted.

[0069] When the target server receives the second test request from the first network address and the second port, and returns the second test response message from the first network address and the third port, the terminal receives the second test response message, which means that the terminal allows data packets from the network address that has been communicated to be transmitted, and then determines that the NAT type of the terminal is IP restricted, that is, IP restricted NAT. IP restricted NAT refers to a NAT in which the mapping relationship created only allows data packets from the IP address that has been communicated to be transmitted. In some embodiments, WebRTC will internally send a second test request to the target server. If IceConnectionState does not become connected within the first preset time (such as 5 seconds), it means that the terminal does not allow data packets from the same network address and different ports to be transmitted, and it can be preliminarily determined that there is a port restriction on the NAT type of the terminal or the target server is down.

[0070] In this embodiment, when the network address requested by the terminal is the same as the network address responded by the target server but the ports are different, if the terminal can receive the second test response message, it can be determined that the NAT type is IP restriction type.

[0071] In an optional embodiment, if Figure 5 As shown, the method may also include: Step S500: if the second test response message is not received within a second preset time, add second new candidate information to the remote session description; wherein the second new candidate information includes the first network address and the third port; Step S502: sending a third test request to the target server according to the second new candidate information; the target server is used to: receive the third test request from the first network address and the third port, and return a third test response message from the first network address and the third port in response to the third test request; Step S504: upon receiving the third test response message, determining that the NAT type is a port restriction type.

[0072] When the target server receives the third test request from the first network address and the third port, and returns the third test response message from the first network address and the third port, the terminal receives the third test response message, which means that the terminal allows data packets from the network address and port that have been communicated to be transmitted, and then determines that the NAT type of the terminal is port-restricted, that is, port-restricted NAT, and eliminates the possibility of server downtime. Port-restricted NAT refers to a NAT in which the mapping relationship created only allows data packets from the IP address and port that have been communicated to be transmitted. In some embodiments, WebRTC will internally send a third test request to the target server. If IceConnectionState becomes connected, it proves that the UDP of the current network is available and the target server is working properly, the NAT type is port-restricted, or a network firewall is configured in the operating system of the terminal.

[0073] In this embodiment, when the network address requested by the terminal is the same as the network address and port responded by the target server, if the terminal can receive the third test response message, it can be determined that the NAT type is port restriction type.

[0074] In an optional embodiment, the method may further include: If the third test response message is not received within the third preset time, it is determined that the detection has failed.

[0075] In some embodiments, if IceConnectionState does not change to connected within a period of time, it is determined that the detection fails. Since the detection of whether it is a symmetric NAT has been completed, it means that the current network can use UDP connection, and it is further determined that the target server may be in a downtime state.

[0076] In order to make this application easier to understand, the following Fig. 6A , 6B An exemplary application is provided.

[0077] S1: Create a PeerConnection object (first object) on the Web side (terminal) and set at least two different STUN server addresses for it.

[0078] S2: The Web side creates a data channel, which is used to prompt WebRTC to start collecting candidates.

[0079] S3: The Web client creates an Offer, sets the Offer as the local session description, and waits for Candidate information to be collected.

[0080] S4: The Web client obtains Candidate (candidate information).

[0081] S5: The Web client determines whether different srflx addresses appear in the Candidate (candidate information): (1) If yes, determine that the NAT type of the Web client is symmetric and end the detection.

[0082] (2) If not, proceed to step S6.

[0083] (3) If there is no srflx address, it means that the STUN server is down or UDP is unavailable.

[0084] S6: Create a PeerConnection object (second object) on the Web side.

[0085] S7: The Web side creates a data channel, which is used to prompt WebRTC to start collecting candidates.

[0086] S8: The Web client creates an Offer and sets the Offer as the local session description.

[0087] S9: The Web client fills in the IP address 1 (first network address) and port 1 (first port) in the predetermined Answer, and sets the Answer as the remote session description.

[0088] S10: The web client automatically sends a STUN PING (first test request) to the IP address 1 (first network address) and port 1 (first port) of the target server through WebRTC.

[0089] S11: The target server replies to the STUN PING (first test response message) from any port of the second network address of IP address 2.

[0090] S12: The Web client determines whether the IceConnectionState property of the PeerConnection object is connected: (1) When the IceConnectionState property of the PeerConnection object is connected (i.e., the first response message is received), it is determined that the NAT type of the Web client is full cone, and the detection ends.

[0091] (2) If the IceConnectionState attribute of the PeerConnection object does not change to connected (ie, the first test response message is not received within the first preset time), proceed to step S13.

[0092] S13: The Web end adds a Candidate (the first new candidate information) to the remote session description, using IP address 1 (the first network address) and port 2 (the second port).

[0093] S14: The web client automatically sends a STUN PING (a second test request) to the IP address 1 (the first network address) and port 2 (the second port) of the dedicated server (the target server) through WebRTC.

[0094] S15: The target server replies to the STUN PING (second test response message) from IP address 1 (first network address) and port 3 (third port).

[0095] S16: The Web client determines whether the IceConnectionState property of the PeerConnection object is connected: (1) When the IceConnectionState property of the PeerConnection object is connected (ie, the second test response message is received), it is determined that the NAT type of the Web client is IP restricted, and the detection ends.

[0096] (2) If the IceConnectionState attribute of the PeerConnection object does not change to connected (ie, the second test response message is not received within the second preset time), proceed to step S17.

[0097] S17: The Web end adds a Candidate (second new candidate information) to the remote session description, using IP address 1 (first network address) and port 3 (third port).

[0098] S18: The web client automatically sends a STUN PING (the third test request) to the IP address 1 (the first network address) and port 3 (the third port) of the target server through WebRTC.

[0099] S19: The target server replies to the STUN PING (third test response message) from IP address 1 (first network address) and port 3 (third port).

[0100] S20: The Web end determines whether the IceConnectionState property of the PeerConnection object is connected: (1) When the IceConnectionState property of the PeerConnection object is connected (ie, the third test response message is received), it is determined that the NAT type of the Web client is port restriction type, and the detection ends.

[0101] (2) If the IceConnectionState property of the PeerConnection object does not change to connected (ie, the third test response message is not received within the third preset time), the test fails, possibly because the server is down.

[0102] In this exemplary application, the detection of NAT type is implemented on the Web side.

[0103] Embodiment 2 The NAT type detection method provided in this embodiment is used in the target server, and the technical details can be referred to, introduced and combined with the above.

[0104] Figure 7 The flowchart of the NAT type detection method according to the second embodiment of the present application is schematically shown.

[0105] like Figure 7 As shown, the NAT type detection method may include steps S600 to S602, wherein: Step S600: receiving a first test request from the first network address and the first port.

[0106] Step S602: In response to the first test request, a first test response message is returned from any port of the second network address; wherein, when the first test request is successfully returned to the terminal, it is determined that the NAT type of the terminal is full cone.

[0107] In some embodiments, the target server can be configured to receive only STUN PING packets in the ICE connection protocol and give a reply. The target server can also include creating RTP (Real-time Transport Protocol) connections and SCTP (Stream Control Transmission Protocol) connections without affecting the process of detecting the terminal NAT type.

[0108] As an optional embodiment, Figure 8 As shown, the NAT type detection method may include steps S700 to S702: Step S700: receiving a second test request from the first network address and the second port.

[0109] Step S702, in response to the second test request, returning a second test response message from the first network address and the third port; wherein, when the second test request is successfully returned to the terminal, determining that the NAT type is IP restricted.

[0110] As an optional embodiment, Fig. 9 As shown, the NAT type detection method may include steps S800 to S802: Step S800: receiving a third test request from the first network address and the third port.

[0111] Step S802: In response to the third test request, return a third test response message from the first network address and the third port; wherein, when the third test request is successfully returned to the terminal, determine that the NAT type is a port restriction type.

[0112] Embodiment 3 Fig.10 The block diagram of the NAT type detection device according to the second embodiment of the present application is schematically shown. The device is used in a terminal and can be divided into one or more program modules. One or more program modules are stored in a storage medium and executed by one or more processors to complete the embodiment of the present application. The program module referred to in the embodiment of the present application refers to a series of computer program instruction segments that can complete specific functions. The following description will specifically introduce the functions of each program module in this embodiment. Fig.10 As shown, the device 1600 may include: a first creation module 1610, an acquisition module 1620, a second creation module 1630 and a determination module 1640, wherein: A first creation module 1610, configured to create a first object and set at least two different STUN server addresses for the first object; The acquisition module 1620 is configured to acquire candidate information of different STUN servers through the at least two different STUN server addresses; A second creation module 1630, configured to create a second object if the candidate information includes one or more identical srflx addresses; The determination module 1640 is used to send a test request to the target server based on the second object to determine the NAT type of the terminal.

[0113] As an optional embodiment, the acquisition module 1620 is further configured to: In a case where the candidate information includes at least two different srflx addresses, it is determined that the NAT type is symmetric.

[0114] As an optional embodiment, the target server is configured with a first network address, a second network address and a first port; the determination module 1640 is further used to: Filling the first network address and the first port into the preset configured Answer to generate a remote session description; According to the remote session description, a first test request is sent to the target server; the target server is used to: receive the first test request from the first network address and the first port, and return a first test response message from any port of the second network address in response to the first test request; In case the first test response message is received, it is determined that the NAT type is a full cone type.

[0115] As an optional embodiment, the target server is further configured with a second port and a third port; the determination module 1640 is further used to: If the first test response message is not received within a first preset time, adding first new candidate information to the remote session description; wherein the first new candidate information includes the first network address and the second port; Sending a second test request to the target server according to the first new candidate information; the target server is used to: receive the second test request from the first network address and the second port, and return a second test response message from the first network address and the third port in response to the second test request; When the second test response message is received, it is determined that the NAT type is an IP restriction type.

[0116] As an optional embodiment, the determining module 1640 is further configured to: If the second test response message is not received within a second preset time, adding second new candidate information to the remote session description; wherein the second new candidate information includes the first network address and the third port; Sending a third test request to the target server according to the second new candidate information; the target server is used to: receive the third test request from the first network address and the third port, and return a third test response message from the first network address and the third port in response to the third test request; When the third test response message is received, it is determined that the NAT type is a port restriction type.

[0117] As an optional embodiment, the determining module 1640 is further configured to: If the third test response message is not received within the third preset time, it is determined that the detection has failed.

[0118] Embodiment 4 Fig.11 The block diagram of the NAT type detection device according to the second embodiment of the present application is schematically shown. The device is used for the target server and can be divided into one or more program modules. One or more program modules are stored in a storage medium and executed by one or more processors to complete the embodiment of the present application. The program module referred to in the embodiment of the present application refers to a series of computer program instruction segments that can complete specific functions. The following description will specifically introduce the functions of each program module in this embodiment. Fig.11 As shown, the device 1700 may include: a receiving module 1710 and a returning module 1720, wherein: The receiving module 1710 is configured to receive a first test request from the first network address and the first port; A returning module 1720, configured to return a first test response message from any port of the second network address in response to the first test request; Wherein, when the first test request is successfully returned to the terminal, the NAT type is determined to be a full cone type.

[0119] As an optional embodiment, the target server is further configured with a second port and a third port; the device 1700 may also include: A second receiving module, configured to receive a second test request from the first network address and the second port; A second returning module, configured to return a second test response message from the first network address and the third port in response to the second test request; Wherein, when the second test request is successfully returned to the terminal, it is determined that the NAT type is IP restriction type.

[0120] As an optional embodiment, the device 1700 may further include: A third receiving module, configured to receive a third test request from the first network address and the third port; A third returning module, configured to return a third test response message from the first network address and the third port in response to the third test request; Wherein, when the third test request is successfully returned to the terminal, it is determined that the NAT type is a port restriction type.

[0121] Embodiment 5 Fig.12The schematic diagram of the hardware architecture of a computer device 10000 suitable for implementing the NAT type detection method according to the fifth embodiment of the present application is shown. In some embodiments, the computer device 10000 may be a terminal device such as a smart phone, a wearable device, a tablet computer, a personal computer, a vehicle terminal, a game console, a virtual device, a workbench, a digital assistant, a set-top box, a robot, etc. In other embodiments, the computer device 10000 may be a rack server, a blade server, a tower server, or a cabinet server (including an independent server, or a server cluster composed of multiple servers), etc. Fig.12 As shown, the computer device 10000 includes but is not limited to: a memory 10010, a processor 10020, and a network interface 10030 that can communicate with each other through a system bus. Among them: The memory 10010 includes at least one type of computer-readable storage medium, and the readable storage medium includes flash memory, hard disk, multimedia card, card-type memory (such as SD or DX memory), random access memory (RAM), static random access memory (SRAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), programmable read-only memory (PROM), magnetic memory, magnetic disk, optical disk, etc. In some embodiments, the memory 10010 can be an internal storage module of the computer device 10000, such as a hard disk or memory of the computer device 10000. In other embodiments, the memory 10010 can also be an external storage device of the computer device 10000, such as a plug-in hard disk equipped on the computer device 10000, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. Of course, the memory 10010 can also include both the internal storage module of the computer device 10000 and its external storage device. In this embodiment, the memory 10010 is generally used to store the operating system and various application software installed in the computer device 10000, such as the program code of the NAT type detection method, etc. In addition, the memory 10010 can also be used to temporarily store various data that have been output or will be output.

[0122] In some embodiments, the processor 10020 may be a central processing unit (CPU), a controller, a microcontroller, a microprocessor, or other chips. The processor 10020 is generally used to control the overall operation of the computer device 10000, such as performing control and processing related to data interaction or communication with the computer device 10000. In this embodiment, the processor 10020 is used to run the program code stored in the memory 10010 or process data.

[0123] The network interface 10030 may include a wireless network interface or a wired network interface, and the network interface 10030 is generally used to establish a communication link between the computer device 10000 and other computer devices. For example, the network interface 10030 is used to connect the computer device 10000 to an external terminal through a network, and to establish a data transmission channel and a communication link between the computer device 10000 and the external terminal. The network may be a wireless or wired network such as an intranet, the Internet, the Global System of Mobile communication (GSM), Wideband Code Division Multiple Access (WCDMA), 4G network, 5G network, Bluetooth, Wi-Fi, etc.

[0124] It should be pointed out that Fig.12 Only a computer device having components 10010 - 10030 is shown, but it should be understood that implementation of all of the components shown is not a requirement, and more or fewer components may alternatively be implemented.

[0125] In this embodiment, the NAT type detection method stored in the memory 10010 can also be divided into one or more program modules and executed by one or more processors (such as processor 10020) to complete the embodiment of the present application.

[0126] Embodiment 6 An embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored, wherein when the computer program is executed by a processor, the steps of the NAT type detection method in the embodiment are implemented.

[0127] In this embodiment, the computer-readable storage medium includes flash memory, hard disk, multimedia card, card-type memory (for example, SD or DX memory, etc.), random access memory (RAM), static random access memory (SRAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), programmable read-only memory (PROM), magnetic memory, disk, optical disk, etc. In some embodiments, the computer-readable storage medium may be an internal storage unit of a computer device, such as a hard disk or memory of the computer device. In other embodiments, the computer-readable storage medium may also be an external storage device of a computer device, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on the computer device. Of course, the computer-readable storage medium may also include both the internal storage unit of the computer device and its external storage device. In this embodiment, the computer-readable storage medium is generally used to store an operating system and various application software installed on the computer device, such as the program code of the NAT type detection method in the embodiment, etc. In addition, the computer-readable storage medium may also be used to temporarily store various types of data that have been output or are to be output.

[0128] Embodiment 7 An embodiment of the present application also provides a computer program product, including a computer program, which implements the method in the above embodiment when executed by a processor.

[0129] Obviously, those skilled in the art should understand that the modules or steps of the above-mentioned embodiments of the present application can be implemented by general-purpose computer devices, they can be concentrated on a single computer device, or distributed on a network composed of multiple computer devices, optionally, they can be implemented by executable program codes of computer devices, so that they can be stored in a storage device and executed by the computer device, and in some cases, the steps shown or described can be executed in a different order from that herein, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. In this way, the embodiments of the present application are not limited to any specific combination of hardware and software.

[0130] It should be noted that the above are only preferred embodiments of the present application, and the patent protection scope of the present application is not limited thereto. Any equivalent structure or equivalent process transformation made using the contents of the specification and drawings of the present application, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present application.

Claims

1. A method for detecting NAT type, characterized in that: For a terminal, the method comprises: Creating a first object, and setting at least two different STUN server addresses for the first object; Acquire candidate information of different STUN servers through the at least two different STUN server addresses; In case the candidate information includes one or more identical srflx addresses, creating a second object; A test request is sent to a target server based on the second object to determine the NAT type of the terminal.

2. The method according to claim 1, characterized in that The method further comprises: In a case where the candidate information includes at least two different srflx addresses, it is determined that the NAT type is symmetric.

3. The method according to claim 1, characterized in that The target server is configured with a first network address, a second network address and a first port; Sending a test request to a target server based on the second object to determine the NAT type of the terminal includes: Filling the first network address and the first port into the preset configured Answer to generate a remote session description; According to the remote session description, a first test request is sent to the target server; the target server is used to: receive the first test request from the first network address and the first port, and return a first test response message from any port of the second network address in response to the first test request; In case the first test response message is received, it is determined that the NAT type is a full cone type.

4. The method according to claim 3, characterized in that The target server is also configured with a second port and a third port; the method further comprises: If the first test response message is not received within a first preset time, adding first new candidate information to the remote session description; wherein the first new candidate information includes the first network address and the second port; Sending a second test request to the target server according to the first new candidate information; the target server is used to: receive the second test request from the first network address and the second port, and return a second test response message from the first network address and the third port in response to the second test request; When the second test response message is received, it is determined that the NAT type is IP restriction type.

5. The method according to claim 4, characterized in that The method further comprises: If the second test response message is not received within a second preset time, adding second new candidate information to the remote session description; wherein the second new candidate information includes the first network address and the third port; Sending a third test request to the target server according to the second new candidate information; the target server is used to: receive the third test request from the first network address and the third port, and return a third test response message from the first network address and the third port in response to the third test request; When the third test response message is received, it is determined that the NAT type is a port restriction type.

6. The method according to claim 5, characterized in that The method further comprises: If the third test response message is not received within the third preset time, it is determined that the detection has failed.

7. A method for detecting NAT type, characterized in that: For a target server, the target server is configured with a first network address, a second network address and a first port; the method comprises: receiving a first test request from the first network address and the first port; In response to the first test request, returning a first test response message from any port of the second network address; Wherein, when the first test request is successfully returned to the terminal, it is determined that the NAT type of the terminal is a full cone type.

8. The method according to claim 7, characterized in that The target server is also configured with a second port and a third port; the method further comprises: receiving a second test request from the first network address and the second port; In response to the second test request, returning a second test response message from the first network address and the third port; Wherein, when the second test request is successfully returned to the terminal, it is determined that the NAT type is IP restriction type.

9. The method according to claim 8, characterized in that The method further comprises: receiving a third test request from the first network address and the third port; In response to the third test request, returning a third test response message from the first network address and the third port; Wherein, when the third test request is successfully returned to the terminal, it is determined that the NAT type is a port restriction type.

10. A NAT type detection device, characterized in that: For a terminal, the device comprises: A first creation module, configured to create a first object and set at least two different STUN server addresses for the first object; An acquisition module, configured to acquire candidate information of different STUN servers through the at least two different STUN server addresses; A second creation module, configured to create a second object when the candidate information includes one or more identical srflx addresses; A determination module is used to send a test request to a target server based on the second object to determine the NAT type of the terminal.

11. A NAT type detection device, characterized in that: Used for a target server, the target server is configured with a first network address, a second network address and a first port; the device comprises: A receiving module, configured to receive a first test request from the first network address and the first port; a returning module, configured to return a first test response message from any port of the second network address in response to the first test request; Wherein, when the first test request is successfully returned to the terminal, the NAT type is determined to be a full cone type.

12. A computer device, characterized in that: include: at least one processor; and a memory communicatively connected to the at least one processor; wherein: The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method according to any one of claims 1 to 9.

13. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and when the computer instructions are executed by a processor, the method according to any one of claims 1 to 9 is implemented.

14. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to claims 1 to 9 are implemented.