By analyzing the log ngrok speculated that principle and mode

By analyzing the log ngrok speculated that principle and mode

https://bob.kim/articles/2018/05/09/1525819680566.html

grok is a proxy server to provide network proxy function, for example:

    +---------------+

    |  ngrok server |

    +---------------+

         /          \

        /             \

+---------------+     \

|  ngrok client |      \

+---------------+        \

    |                       \

+---------------+       +---------------+ 

|     A app |       |      B app    |

+---------------+       +---------------+

Following conditions:

1 A, B are two applications, including network operation;

2 ngrok server running a server in the public network;

A 3 ngrok client running on the same system;

//========================================================================

If B wants to interact with A, the following process:

1 B sends data to ngrok server;

2 ngrok server forwards the data to the ngrok client;

3 ngrok client forwards the data to A;

4 A data processing, the need to return to the data B, the process returns to the ngrok client;

5 ngrok client forwards the data to the ngrok server;

6 ngrok server forwards the data to the B;

Finally, A, B by forwarding it ngrok complete interaction data;

//========================================================================

We can think, ngrok A proxy objects outside the service provided, when B and A to interact with objects, only the object A proxy ngrok interact with it;

Well, all this interaction, there is a certain "protocol" communications;

The following explains the interactive process, involving the "Agreement":

Hereinafter referred to: client == ngrok client, server == ngrok server;

//========================================================================

(1) CMD = Auth

authentication --- Certification

client authentication to register with the server;

client starts, CMD = Auth send commands to the server; tell the server, my client needs to connect to the server, "certification"; for example:

{"Type":"Auth","Payload":{"Version":"2","MmVersion":"1.7","User":"","Password": "","OS":"darwin","Arch":"amd64","ClientId":""}}

//========================================================================

(2) CMD = AuthResp

authentication response --- authentication response

server response to client commands CMD = Auth; for example:

{"Type":"AuthResp","Payload":{"Version":"2","MmVersion":"1.7","ClientId":"c53d9a89c0ff87c37f14be30ff1f0ecb","Error":""}}

//========================================================================

(3) CMD = ReqTunnel


request tunnel --- request tunnel

client request to establish a tunnel to the Server; example:

{"Type":"ReqTunnel","Payload":{"Protocol":"http","ReqId":"NWLRB","Hostname": "","Subdomain":"wkf168","HttpAuth":"","RemotePort":0,"authtoken":""}

client sends their information to the server, requesting server establish a tunnel;

Protocol --- protocol request
an ID of the information ReqId --- Client;
Subdomain subdomain ---


//========================================================================

(4) CMD = ReqProxy

request proxy --- Request Broker

server to a client, client requests a "service agent"; for example:

{"Type":"ReqProxy","Payload":{}}

At this point, client creates a socket fd connected to server, server to accept to put the socket fd as a "tunnel";

//========================================================================

(5) CMD = RegProxy

register proxy --- registered agent

When the client receives server's "Request Broker" command, you can send a "registered agent" command to the server, for example:

{"Type":"RegProxy","Payload":{"ClientId":"c53d9a89c0ff87c37f14be30ff1f0ecb"}}

Which, Client ID is an authentication ID of the current client, it's time to carry AuthResp server command, information returned;

At this point, client sends this ID to sever, carry RegProxy command, pledged to register as an agent;

//========================================================================

(6) CMD = NewTunnel

Create a new tunnel --- tunnel;

After the server receives a client's registered agent command to the client registers a proxy identity;

At the same time, the client returns the information in the proxy authentication server, for example:

{"Type":"NewTunnel","Payload":{"ReqId":"NWLRB","Url":"http://wkf168.ngrok.bob.kim","Protocol":"http","Error":""}}

At this point, client has completed the creation of "agency status" in the server end, returned:

RegId --- is a client ID information carried when sending ReqTunnel command;
the Url --- is a server for the URL of the current distribution of the client agent;

Then, when other applications access Url, put the requested data is forwarded to the current client;

//========================================================================

In this case, after the above several steps to complete the establishment of a "tunnel" of;

So, when there are other applications to access Url, there is the following command interaction:

//========================================================================

(7) CMD = StartProxy

start proxy --- started agent

E.g:

{"Type":"StartProxy","Payload":{"Url":"http://wkf168.ngrok.bob.kim","ClientAddr":"183.14.133.76:49127"}}

When there is an application to access the Url, ngrok server receives the data; then, to the client to send StartProxy order requiring the client agent service started;

After then, in ReqProxy command, server sends the command to the client, client to create a socket fd is connected to the server;

So, we define the socket fd is fdA;

At this point, fdA it as a "tunnel" interfaces;

In this case, client is from this tunnel interface fdA received "StartProxy" command;

Then, client creates a new socket fd, connected to the local IP + PORT; we define the socket fd is fdB;

Finally, fdA relationship with fdB form of a map, are:

1 fdA received data, forwarded to the FDB;

2 fdB received data, forwarded to the FDA;

//========================================================================

(8) CMD = Ping --- used as a heartbeat packet
(9) CMD = Pong --- is a reply to the Ping command

//========================================================================

Like RTSP protocols, they are:

Between 1 Client and Server, as the channel has a socket fd "protocol interaction" used;

The command CMD is a transceiver mentioned above;

2 Client and Server re-transmission of audio and video data, the need to establish a new socket fd to interactive data;

Similarly, in ngrok in, too, when ngrok client begin to create a socket fd connected ngrok server, interactive communication command;

When ngrok client received when CMD = ReqProxy command creates a new socket fd, connected to ngrok server, as a "data exchange" tunnel interfaces;

//========================================================================

So, ngrok client and ngrok server during data forwarding tunnel when the following process:

1 client begins to establish a connection to the socket fd server, a command CMD interaction channel; socket fd is defined in the FDA;

2 server transmits to the client through the CMD command fdA = ReqProxy; requesting client proxy activity;

Then, client on the new socket fd is connected to a server, the definition of the socket fdB;

After receiving the connection fdB 3 server, and transmitting to the interface command CMD = StartProxy, the interface required to start the fdB agent activity;

Ultimately, this fdB to interact with the server as a client for forwarding data "tunnel" interfaces;

 

A more detailed analysis of the source code level reference: https://blog.csdn.net/lyb3290/article/details/80239890

Annex:
[ngrok client windows version 64 _bob.rar]


If you have questions or peers welcome to add the group to discuss: Platinum IT exchange group 151 258 054

Guess you like

Origin blog.csdn.net/az44yao/article/details/92425737