Because ipv4 public IP addresses are limited, many offices and campus networks share public IP addresses based on the NAT technology. The IP addresses assigned to the local machine are LAN IP addresses. The local machine can access the Internet through the router, but cannot be directly accessed by Internet services. So I believe that you have more or less encountered the following situations in development:
- The third-party service that is developed for interconnection has a callback notification, but needs to provide a public network access address. For example, wechat pay callback and public account development.
- Development requires debugging with a remote team, not all on the same LAN.
- I made an awesome project, and I want to show it to my gay friends on wechat in the middle of development.
Some people may say that it is ok to deploy to the server with public network for the above situation. But in fact, the lack of IDE on the server is very inconvenient for debugging and locating bugs in the development stage. In addition, some demo projects may only be needed for demonstration, so it is true that a public network server is a waste of resources. This article introduces an Intranet penetration tool, Ngrok, which can expose Intranet services to public network access with simple installation and deployment.
Ngrok introduction
Ngrok is an open source Intranet penetration tool. It is divided into client and server, and the client supports multiple platforms such as Windows, macOs, and Linux. Generally, you just need to download the client and start it.
The basic principle of
The basic principle is to establish a channel and mapping relationship between the client and the public network server, and then the server provides accessible public network addresses. When the external network accesses the public address, the Ngrok server finds the client based on the mapping and forwards the service to the client. No more words, then turn round!
Ngork client installed and started
The installation
First download the client and decompress it. The installation package can be downloaded from the official website.
# Take macOS as an example
unzip /path/to/ngrok.zip
Copy the code
Start the
Start and specify the port to expose
./ngrok http 8888
Copy the code
Then your local port will generate a corresponding accessible extranet address, as shown in the following figure:
Ngrok test
At this point I started a simple Python Web service on port 8888 and used the generated public domain name to test whether it was available.
# Python service code
from flask import Flask
app = Flask(__name__)
# Define a/Hello route
@app.route("/hello", methods=["POST"."GET"])
def push() :
print("OMG! i am free")
return "hello, world"
if __name__ == '__main__':
Service starts on port 8888
app.run(port=8888)
Copy the code
Then access the Intranet service using a browser and view the service logs. You can see that the Intranet service is successfully accessed using a public IP address.
Ngrok visual interface
In addition, after starting the client, you can also passhttp://localhost:4040/Access ngrok’s visual interface, which not only displays detailed request and response information, but also provides a Replay button. To tell the truth, this function can be too top, for example, when I do wechat payment docking debugging, payment generates a callback. With this request replay, I don’t have to pay to generate the request after debugging failure, just replay it.
Write in the back
Intranet penetration tool does bring great convenience to my daily development, and it solves my trouble and hair loss caused by frequent server debug! Of course, ngrok has much more functionality than this article, so you can explore it further if you need to. If you found this article helpful, please give it a thumbs up!
PS: Here is a recommended project to build a small program e-commerce mall from 0 to 1: mall, welcome to exchange and learn together!
Search the public account “Frank’s Somniloquy” on wechat and reply “100PDF” to get 100 computer related e-books!!