Ngrok穿透原理
流程图
- Client与Server建立一个scoket连接,然后发送一个Auth请求,Server验证后,返回AuthResp
- 接着Client发送ReqTunnel像服务器注册通道,比如,HTTP,HTTPS,TCP,其中包含想要申请的二级域名,服务器返回NewTunnel,如果Client的二级域名请求为空,服务器会随机分配。
- Server等待浏览器,APP等访问,当有APP访问,Server会检查二级域名是否是已经注册了的,如果是,则发送ReqProxy给Client。Client收到请求后会创建一个新的Socket连接到Server,并发送RegProxy请求,服务器收到后,返回StartProxy,并开始使用新的Socket连接做中继。
通信数据格式
数据通过Socket通信,主要以Json格式数据为主。
在数据的开头,有2个分别以64bit的无符号整型<message length><message payload>
表示数据的长度和负载。
数据格式:
- Auth
type Auth struct {
Version string // protocol version
MmVersion string // major/minor software version (informational only)
User string
Password string
OS string
Arch string
ClientId string // empty for new sessions
}
-
AuthResp
type AuthResp struct { Version string MmVersion string ClientId string Error string }
-
ReqTunnel
type ReqTunnel struct { ReqId string Protocol string // http only Hostname string Subdomain string HttpAuth string // tcp only RemotePort uint16 }
-
NewTunnel
type NewTunnel struct { ReqId string Url string Protocol string Error string }
-
ReqProxy
type ReqProxy struct { }
-
RegProxy
type RegProxy struct { ClientId string }
-
StartProxy
type StartProxy struct { Url string // URL of the tunnel this connection connection is being proxied for ClientAddr string // Network address of the client initiating the connection to the tunnel }
-
Ping
type Ping struct { }
-
Pong
type Pong struct { }