msg.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. // When frpc start, client send this message to login to server.
  61. type Login struct {
  62. Version string `json:"version,omitempty"`
  63. Hostname string `json:"hostname,omitempty"`
  64. Os string `json:"os,omitempty"`
  65. Arch string `json:"arch,omitempty"`
  66. User string `json:"user,omitempty"`
  67. PrivilegeKey string `json:"privilege_key,omitempty"`
  68. Timestamp int64 `json:"timestamp,omitempty"`
  69. RunID string `json:"run_id,omitempty"`
  70. Metas map[string]string `json:"metas,omitempty"`
  71. // Some global configures.
  72. PoolCount int `json:"pool_count,omitempty"`
  73. }
  74. type LoginResp struct {
  75. Version string `json:"version,omitempty"`
  76. RunID string `json:"run_id,omitempty"`
  77. Error string `json:"error,omitempty"`
  78. }
  79. // When frpc login success, send this message to frps for running a new proxy.
  80. type NewProxy struct {
  81. ProxyName string `json:"proxy_name,omitempty"`
  82. ProxyType string `json:"proxy_type,omitempty"`
  83. UseEncryption bool `json:"use_encryption,omitempty"`
  84. UseCompression bool `json:"use_compression,omitempty"`
  85. BandwidthLimit string `json:"bandwidth_limit,omitempty"`
  86. BandwidthLimitMode string `json:"bandwidth_limit_mode,omitempty"`
  87. Group string `json:"group,omitempty"`
  88. GroupKey string `json:"group_key,omitempty"`
  89. Metas map[string]string `json:"metas,omitempty"`
  90. // tcp and udp only
  91. RemotePort int `json:"remote_port,omitempty"`
  92. // http and https only
  93. CustomDomains []string `json:"custom_domains,omitempty"`
  94. SubDomain string `json:"subdomain,omitempty"`
  95. Locations []string `json:"locations,omitempty"`
  96. HTTPUser string `json:"http_user,omitempty"`
  97. HTTPPwd string `json:"http_pwd,omitempty"`
  98. HostHeaderRewrite string `json:"host_header_rewrite,omitempty"`
  99. Headers map[string]string `json:"headers,omitempty"`
  100. RouteByHTTPUser string `json:"route_by_http_user,omitempty"`
  101. // stcp, sudp, xtcp
  102. Sk string `json:"sk,omitempty"`
  103. AllowUsers []string `json:"allow_users,omitempty"`
  104. // tcpmux
  105. Multiplexer string `json:"multiplexer,omitempty"`
  106. }
  107. type NewProxyResp struct {
  108. ProxyName string `json:"proxy_name,omitempty"`
  109. RemoteAddr string `json:"remote_addr,omitempty"`
  110. Error string `json:"error,omitempty"`
  111. }
  112. type CloseProxy struct {
  113. ProxyName string `json:"proxy_name,omitempty"`
  114. }
  115. type NewWorkConn struct {
  116. RunID string `json:"run_id,omitempty"`
  117. PrivilegeKey string `json:"privilege_key,omitempty"`
  118. Timestamp int64 `json:"timestamp,omitempty"`
  119. }
  120. type ReqWorkConn struct{}
  121. type StartWorkConn struct {
  122. ProxyName string `json:"proxy_name,omitempty"`
  123. SrcAddr string `json:"src_addr,omitempty"`
  124. DstAddr string `json:"dst_addr,omitempty"`
  125. SrcPort uint16 `json:"src_port,omitempty"`
  126. DstPort uint16 `json:"dst_port,omitempty"`
  127. Error string `json:"error,omitempty"`
  128. }
  129. type NewVisitorConn struct {
  130. RunID string `json:"run_id,omitempty"`
  131. ProxyName string `json:"proxy_name,omitempty"`
  132. SignKey string `json:"sign_key,omitempty"`
  133. Timestamp int64 `json:"timestamp,omitempty"`
  134. UseEncryption bool `json:"use_encryption,omitempty"`
  135. UseCompression bool `json:"use_compression,omitempty"`
  136. }
  137. type NewVisitorConnResp struct {
  138. ProxyName string `json:"proxy_name,omitempty"`
  139. Error string `json:"error,omitempty"`
  140. }
  141. type Ping struct {
  142. PrivilegeKey string `json:"privilege_key,omitempty"`
  143. Timestamp int64 `json:"timestamp,omitempty"`
  144. }
  145. type Pong struct {
  146. Error string `json:"error,omitempty"`
  147. }
  148. type UDPPacket struct {
  149. Content string `json:"c,omitempty"`
  150. LocalAddr *net.UDPAddr `json:"l,omitempty"`
  151. RemoteAddr *net.UDPAddr `json:"r,omitempty"`
  152. }
  153. type NatHoleVisitor struct {
  154. TransactionID string `json:"transaction_id,omitempty"`
  155. ProxyName string `json:"proxy_name,omitempty"`
  156. PreCheck bool `json:"pre_check,omitempty"`
  157. Protocol string `json:"protocol,omitempty"`
  158. SignKey string `json:"sign_key,omitempty"`
  159. Timestamp int64 `json:"timestamp,omitempty"`
  160. MappedAddrs []string `json:"mapped_addrs,omitempty"`
  161. AssistedAddrs []string `json:"assisted_addrs,omitempty"`
  162. }
  163. type NatHoleClient struct {
  164. TransactionID string `json:"transaction_id,omitempty"`
  165. ProxyName string `json:"proxy_name,omitempty"`
  166. Sid string `json:"sid,omitempty"`
  167. MappedAddrs []string `json:"mapped_addrs,omitempty"`
  168. AssistedAddrs []string `json:"assisted_addrs,omitempty"`
  169. }
  170. type PortsRange struct {
  171. From int `json:"from,omitempty"`
  172. To int `json:"to,omitempty"`
  173. }
  174. type NatHoleDetectBehavior struct {
  175. Role string `json:"role,omitempty"` // sender or receiver
  176. Mode int `json:"mode,omitempty"` // 0, 1, 2...
  177. TTL int `json:"ttl,omitempty"`
  178. SendDelayMs int `json:"send_delay_ms,omitempty"`
  179. ReadTimeoutMs int `json:"read_timeout,omitempty"`
  180. CandidatePorts []PortsRange `json:"candidate_ports,omitempty"`
  181. SendRandomPorts int `json:"send_random_ports,omitempty"`
  182. ListenRandomPorts int `json:"listen_random_ports,omitempty"`
  183. }
  184. type NatHoleResp struct {
  185. TransactionID string `json:"transaction_id,omitempty"`
  186. Sid string `json:"sid,omitempty"`
  187. Protocol string `json:"protocol,omitempty"`
  188. CandidateAddrs []string `json:"candidate_addrs,omitempty"`
  189. AssistedAddrs []string `json:"assisted_addrs,omitempty"`
  190. DetectBehavior NatHoleDetectBehavior `json:"detect_behavior,omitempty"`
  191. Error string `json:"error,omitempty"`
  192. }
  193. type NatHoleSid struct {
  194. TransactionID string `json:"transaction_id,omitempty"`
  195. Sid string `json:"sid,omitempty"`
  196. Response bool `json:"response,omitempty"`
  197. Nonce string `json:"nonce,omitempty"`
  198. }
  199. type NatHoleReport struct {
  200. Sid string `json:"sid,omitempty"`
  201. Success bool `json:"success,omitempty"`
  202. }