msg.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package msg
  15. import (
  16. "net"
  17. "reflect"
  18. )
  19. const (
  20. TypeLogin = 'o'
  21. TypeLoginResp = '1'
  22. TypeNewProxy = 'p'
  23. TypeNewProxyResp = '2'
  24. TypeCloseProxy = 'c'
  25. TypeNewWorkConn = 'w'
  26. TypeReqWorkConn = 'r'
  27. TypeStartWorkConn = 's'
  28. TypeNewVisitorConn = 'v'
  29. TypeNewVisitorConnResp = '3'
  30. TypePing = 'h'
  31. TypePong = '4'
  32. TypeUDPPacket = 'u'
  33. TypeNatHoleVisitor = 'i'
  34. TypeNatHoleClient = 'n'
  35. TypeNatHoleResp = 'm'
  36. TypeNatHoleSid = '5'
  37. TypeNatHoleReport = '6'
  38. )
  39. var msgTypeMap = map[byte]interface{}{
  40. TypeLogin: Login{},
  41. TypeLoginResp: LoginResp{},
  42. TypeNewProxy: NewProxy{},
  43. TypeNewProxyResp: NewProxyResp{},
  44. TypeCloseProxy: CloseProxy{},
  45. TypeNewWorkConn: NewWorkConn{},
  46. TypeReqWorkConn: ReqWorkConn{},
  47. TypeStartWorkConn: StartWorkConn{},
  48. TypeNewVisitorConn: NewVisitorConn{},
  49. TypeNewVisitorConnResp: NewVisitorConnResp{},
  50. TypePing: Ping{},
  51. TypePong: Pong{},
  52. TypeUDPPacket: UDPPacket{},
  53. TypeNatHoleVisitor: NatHoleVisitor{},
  54. TypeNatHoleClient: NatHoleClient{},
  55. TypeNatHoleResp: NatHoleResp{},
  56. TypeNatHoleSid: NatHoleSid{},
  57. TypeNatHoleReport: NatHoleReport{},
  58. }
  59. var TypeNameNatHoleResp = reflect.TypeOf(&NatHoleResp{}).Elem().Name()
  60. type ClientSpec struct {
  61. // Due to the support of VirtualClient, frps needs to know the client type in order to
  62. // differentiate the processing logic.
  63. // Optional values: ssh-tunnel
  64. Type string `json:"type,omitempty"`
  65. // If the value is true, the client will not require authentication.
  66. AlwaysAuthPass bool `json:"always_auth_pass,omitempty"`
  67. }
  68. // When frpc start, client send this message to login to server.
  69. type Login struct {
  70. Version string `json:"version,omitempty"`
  71. Hostname string `json:"hostname,omitempty"`
  72. Os string `json:"os,omitempty"`
  73. Arch string `json:"arch,omitempty"`
  74. User string `json:"user,omitempty"`
  75. PrivilegeKey string `json:"privilege_key,omitempty"`
  76. Timestamp int64 `json:"timestamp,omitempty"`
  77. RunID string `json:"run_id,omitempty"`
  78. Metas map[string]string `json:"metas,omitempty"`
  79. // Currently only effective for VirtualClient.
  80. ClientSpec ClientSpec `json:"client_spec,omitempty"`
  81. // Some global configures.
  82. PoolCount int `json:"pool_count,omitempty"`
  83. }
  84. type LoginResp struct {
  85. Version string `json:"version,omitempty"`
  86. RunID string `json:"run_id,omitempty"`
  87. Error string `json:"error,omitempty"`
  88. }
  89. // When frpc login success, send this message to frps for running a new proxy.
  90. type NewProxy struct {
  91. ProxyName string `json:"proxy_name,omitempty"`
  92. ProxyType string `json:"proxy_type,omitempty"`
  93. UseEncryption bool `json:"use_encryption,omitempty"`
  94. UseCompression bool `json:"use_compression,omitempty"`
  95. BandwidthLimit string `json:"bandwidth_limit,omitempty"`
  96. BandwidthLimitMode string `json:"bandwidth_limit_mode,omitempty"`
  97. Group string `json:"group,omitempty"`
  98. GroupKey string `json:"group_key,omitempty"`
  99. Metas map[string]string `json:"metas,omitempty"`
  100. Annotations map[string]string `json:"annotations,omitempty"`
  101. // tcp and udp only
  102. RemotePort int `json:"remote_port,omitempty"`
  103. // http and https only
  104. CustomDomains []string `json:"custom_domains,omitempty"`
  105. SubDomain string `json:"subdomain,omitempty"`
  106. Locations []string `json:"locations,omitempty"`
  107. HTTPUser string `json:"http_user,omitempty"`
  108. HTTPPwd string `json:"http_pwd,omitempty"`
  109. HostHeaderRewrite string `json:"host_header_rewrite,omitempty"`
  110. Headers map[string]string `json:"headers,omitempty"`
  111. ResponseHeaders map[string]string `json:"response_headers,omitempty"`
  112. RouteByHTTPUser string `json:"route_by_http_user,omitempty"`
  113. // stcp, sudp, xtcp
  114. Sk string `json:"sk,omitempty"`
  115. AllowUsers []string `json:"allow_users,omitempty"`
  116. // tcpmux
  117. Multiplexer string `json:"multiplexer,omitempty"`
  118. }
  119. type NewProxyResp struct {
  120. ProxyName string `json:"proxy_name,omitempty"`
  121. RemoteAddr string `json:"remote_addr,omitempty"`
  122. Error string `json:"error,omitempty"`
  123. }
  124. type CloseProxy struct {
  125. ProxyName string `json:"proxy_name,omitempty"`
  126. }
  127. type NewWorkConn struct {
  128. RunID string `json:"run_id,omitempty"`
  129. PrivilegeKey string `json:"privilege_key,omitempty"`
  130. Timestamp int64 `json:"timestamp,omitempty"`
  131. }
  132. type ReqWorkConn struct{}
  133. type StartWorkConn struct {
  134. ProxyName string `json:"proxy_name,omitempty"`
  135. SrcAddr string `json:"src_addr,omitempty"`
  136. DstAddr string `json:"dst_addr,omitempty"`
  137. SrcPort uint16 `json:"src_port,omitempty"`
  138. DstPort uint16 `json:"dst_port,omitempty"`
  139. Error string `json:"error,omitempty"`
  140. }
  141. type NewVisitorConn struct {
  142. RunID string `json:"run_id,omitempty"`
  143. ProxyName string `json:"proxy_name,omitempty"`
  144. SignKey string `json:"sign_key,omitempty"`
  145. Timestamp int64 `json:"timestamp,omitempty"`
  146. UseEncryption bool `json:"use_encryption,omitempty"`
  147. UseCompression bool `json:"use_compression,omitempty"`
  148. }
  149. type NewVisitorConnResp struct {
  150. ProxyName string `json:"proxy_name,omitempty"`
  151. Error string `json:"error,omitempty"`
  152. }
  153. type Ping struct {
  154. PrivilegeKey string `json:"privilege_key,omitempty"`
  155. Timestamp int64 `json:"timestamp,omitempty"`
  156. }
  157. type Pong struct {
  158. Error string `json:"error,omitempty"`
  159. }
  160. type UDPPacket struct {
  161. Content string `json:"c,omitempty"`
  162. LocalAddr *net.UDPAddr `json:"l,omitempty"`
  163. RemoteAddr *net.UDPAddr `json:"r,omitempty"`
  164. }
  165. type NatHoleVisitor struct {
  166. TransactionID string `json:"transaction_id,omitempty"`
  167. ProxyName string `json:"proxy_name,omitempty"`
  168. PreCheck bool `json:"pre_check,omitempty"`
  169. Protocol string `json:"protocol,omitempty"`
  170. SignKey string `json:"sign_key,omitempty"`
  171. Timestamp int64 `json:"timestamp,omitempty"`
  172. MappedAddrs []string `json:"mapped_addrs,omitempty"`
  173. AssistedAddrs []string `json:"assisted_addrs,omitempty"`
  174. }
  175. type NatHoleClient struct {
  176. TransactionID string `json:"transaction_id,omitempty"`
  177. ProxyName string `json:"proxy_name,omitempty"`
  178. Sid string `json:"sid,omitempty"`
  179. MappedAddrs []string `json:"mapped_addrs,omitempty"`
  180. AssistedAddrs []string `json:"assisted_addrs,omitempty"`
  181. }
  182. type PortsRange struct {
  183. From int `json:"from,omitempty"`
  184. To int `json:"to,omitempty"`
  185. }
  186. type NatHoleDetectBehavior struct {
  187. Role string `json:"role,omitempty"` // sender or receiver
  188. Mode int `json:"mode,omitempty"` // 0, 1, 2...
  189. TTL int `json:"ttl,omitempty"`
  190. SendDelayMs int `json:"send_delay_ms,omitempty"`
  191. ReadTimeoutMs int `json:"read_timeout,omitempty"`
  192. CandidatePorts []PortsRange `json:"candidate_ports,omitempty"`
  193. SendRandomPorts int `json:"send_random_ports,omitempty"`
  194. ListenRandomPorts int `json:"listen_random_ports,omitempty"`
  195. }
  196. type NatHoleResp struct {
  197. TransactionID string `json:"transaction_id,omitempty"`
  198. Sid string `json:"sid,omitempty"`
  199. Protocol string `json:"protocol,omitempty"`
  200. CandidateAddrs []string `json:"candidate_addrs,omitempty"`
  201. AssistedAddrs []string `json:"assisted_addrs,omitempty"`
  202. DetectBehavior NatHoleDetectBehavior `json:"detect_behavior,omitempty"`
  203. Error string `json:"error,omitempty"`
  204. }
  205. type NatHoleSid struct {
  206. TransactionID string `json:"transaction_id,omitempty"`
  207. Sid string `json:"sid,omitempty"`
  208. Response bool `json:"response,omitempty"`
  209. Nonce string `json:"nonce,omitempty"`
  210. }
  211. type NatHoleReport struct {
  212. Sid string `json:"sid,omitempty"`
  213. Success bool `json:"success,omitempty"`
  214. }