Location>code7788 >text

Socket practice: Use cloud servers to connect to the company's intranet, and simply implement cross-local network calls and API interfaces deployed in the company's LAN

Popularity:124 ℃/2025-05-04 11:25:24
The company's code can be accessed on the public network, but these codes use APIs deployed on the company's LAN. If you sometimes want to write code at home to see the operation of this WPF software, it will not work, because the main business must be connected to the API interface of the company's LAN. I just want to use my Alibaba Cloud server to build a proxy to access the company's LAN. The final implementation was done at home. One desktop, one laptop, two routers, and one telecom fiber entrance gateway at home. Then I connected the desktop and laptop to different LANs, and I could no longer use the laptop network on the desktop. Accessing the transit service deployed on the cloud server on desktop, and successfully accessing the API deployed on the laptop. In this way, changing the laptop environment to the company environment should also be able to successfully access the company's LAN API.
 
The specific steps are as follows:
1. On the access side A (my desktop, desktop ip: 192.168.31.11), deploy a local socket server with a listening port such as 7088, and change the access address of the API to: Http://192.168.31.11:7088/swagger/; Originally, the address of this API might be the address of the company's intranet, such as 192.168.2.14:8088. We changed it to be able to call the request to the socket we deployed on local A. Deploy this local socket server and test the request first. I found that the browser's http request can be printed to the console. This step is completed.
2. Deploy ProxyServer, a transit proxy server deployed to cloud services. It's also written by socket. The main function is to send client A's request to clientB deployed to company's ClientB, and return clientB's API's Response to ClientA, which means to play the role of relaying Http requests and return from local A and company's LAN B. In addition, there is also a SocketClient in this ProxyServer that receives the heartbeat of client B. The port that the transit service listens on needs to be released in the rules of Alibaba Cloud server, otherwise it will not be accessible.
 
3. Deployed in the company's ClientB. This ClientB mainly contains three socket clients, one is the Socket client connected to the transit server, which is used to receive requests sent by clientA to the transit server, and then sent by the transit server; one is the port of the API connecting the company's LAN, which is used to access the final API interface of the company's LAN; the other is the Socket jump client, which is used to wait for the company to be disconnected from the network and then be able to reconnect to the transit server after the network is restored.
4. Finally, you must start the transit server first and then start ClientB.
In general, the local A-side connects to ProxyServer, and the company's ClientB-side also connects to ProxyServer. The ClientB-side then connects to the port of the API deployed in the company's LAN. Then, the request sent by the local A-side to the B-side must first be called to ClientA, and then sent to ProxyServer by ClientA, forwarded to ClientB through ProxyServer, and finally sent to ClientB by ClientB to the port of the real API, and then returned to the receiving Response in turn, and finally presented to the A-side. By visiting the swagger homepage, you can see the swagger homepage in your browser, and requesting post on swagger, you can also receive the returned content.
The code is written in a simple way and cannot handle complex logic. After disconnecting the network and reconnecting, you still need to restart the local ClientA to continue accessing the company's LAN API. Through this practice, the main pitfalls I have stepped on are:
1. The method of judging whether the post and get request are a complete request body is different. Get request, just \r\n\r\n, but psot request must obtain the length of postData, so the final solution is to use the method given by deepseek.
2. A request and a response must correspond one by one. I encountered that after request A was sent, request B continued to send, but request A received the return content of length 0, then the subsequent request B could not receive the return, and the page would be stuck and the subsequent request could not be continued. So ClientA is sent to ProxyServer, ProxyServer is sent to ClientB, and it returns to ProxyServer from ClientB, and ProxyServer returns to ClientA. All are synchronized.
3. A new client must be created for a request to connect to ProxyServer, but only one ClientB connected to ProxyServer can be used.
4. You must determine whether all the returned Response has been read. I spliced ​​the Resposne read in each loop into a string. When the string ends with "0\r\n\r\n", I think that all the Respose has been returned. It is no problem to judge this at present.
5. In ClientB, the Host of the request body should be changed to the ip and port number of the actual API, otherwise the API will not be accessible. The host conversion code of the request body given by deepseek can also be used.