Ngrok內網穿透原理 原

Ngrok穿透原理

流程圖

Ngrok流程

  1. Client與Server建立一個scoket連接,然後發送一個Auth請求,Server驗證後,返回AuthResp
  2. 接着Client發送ReqTunnel像服務器註冊通道,比如,HTTP,HTTPS,TCP,其中包含想要申請的二級域名,服務器返回NewTunnel,如果Client的二級域名請求爲空,服務器會隨機分配。
  3. 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 {
    }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章