1
0

msg.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. ServerUDPPort int `json:"server_udp_port,omitempty"`
  78. Error string `json:"error,omitempty"`
  79. }
  80. // When frpc login success, send this message to frps for running a new proxy.
  81. type NewProxy struct {
  82. ProxyName string `json:"proxy_name,omitempty"`
  83. ProxyType string `json:"proxy_type,omitempty"`
  84. UseEncryption bool `json:"use_encryption,omitempty"`
  85. UseCompression bool `json:"use_compression,omitempty"`
  86. BandwidthLimit string `json:"bandwidth_limit,omitempty"`
  87. BandwidthLimitMode string `json:"bandwidth_limit_mode,omitempty"`
  88. Group string `json:"group,omitempty"`
  89. GroupKey string `json:"group_key,omitempty"`
  90. Metas map[string]string `json:"metas,omitempty"`
  91. // tcp and udp only
  92. RemotePort int `json:"remote_port,omitempty"`
  93. // http and https only
  94. CustomDomains []string `json:"custom_domains,omitempty"`
  95. SubDomain string `json:"subdomain,omitempty"`
  96. Locations []string `json:"locations,omitempty"`
  97. HTTPUser string `json:"http_user,omitempty"`
  98. HTTPPwd string `json:"http_pwd,omitempty"`
  99. HostHeaderRewrite string `json:"host_header_rewrite,omitempty"`
  100. Headers map[string]string `json:"headers,omitempty"`
  101. RouteByHTTPUser string `json:"route_by_http_user,omitempty"`
  102. // stcp
  103. Sk string `json:"sk,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. ProxyName string `json:"proxy_name,omitempty"`
  131. SignKey string `json:"sign_key,omitempty"`
  132. Timestamp int64 `json:"timestamp,omitempty"`
  133. UseEncryption bool `json:"use_encryption,omitempty"`
  134. UseCompression bool `json:"use_compression,omitempty"`
  135. }
  136. type NewVisitorConnResp struct {
  137. ProxyName string `json:"proxy_name,omitempty"`
  138. Error string `json:"error,omitempty"`
  139. }
  140. type Ping struct {
  141. PrivilegeKey string `json:"privilege_key,omitempty"`
  142. Timestamp int64 `json:"timestamp,omitempty"`
  143. }
  144. type Pong struct {
  145. Error string `json:"error,omitempty"`
  146. }
  147. type UDPPacket struct {
  148. Content string `json:"c,omitempty"`
  149. LocalAddr *net.UDPAddr `json:"l,omitempty"`
  150. RemoteAddr *net.UDPAddr `json:"r,omitempty"`
  151. }
  152. type NatHoleVisitor struct {
  153. TransactionID string `json:"transaction_id,omitempty"`
  154. ProxyName string `json:"proxy_name,omitempty"`
  155. PreCheck bool `json:"pre_check,omitempty"`
  156. Protocol string `json:"protocol,omitempty"`
  157. SignKey string `json:"sign_key,omitempty"`
  158. Timestamp int64 `json:"timestamp,omitempty"`
  159. MappedAddrs []string `json:"mapped_addrs,omitempty"`
  160. AssistedAddrs []string `json:"assisted_addrs,omitempty"`
  161. }
  162. type NatHoleClient struct {
  163. TransactionID string `json:"transaction_id,omitempty"`
  164. ProxyName string `json:"proxy_name,omitempty"`
  165. Sid string `json:"sid,omitempty"`
  166. MappedAddrs []string `json:"mapped_addrs,omitempty"`
  167. AssistedAddrs []string `json:"assisted_addrs,omitempty"`
  168. }
  169. type PortsRange struct {
  170. From int `json:"from,omitempty"`
  171. To int `json:"to,omitempty"`
  172. }
  173. type NatHoleDetectBehavior struct {
  174. Role string `json:"role,omitempty"` // sender or receiver
  175. Mode int `json:"mode,omitempty"` // 0, 1, 2...
  176. TTL int `json:"ttl,omitempty"`
  177. SendDelayMs int `json:"send_delay_ms,omitempty"`
  178. ReadTimeoutMs int `json:"read_timeout,omitempty"`
  179. CandidatePorts []PortsRange `json:"candidate_ports,omitempty"`
  180. SendRandomPorts int `json:"send_random_ports,omitempty"`
  181. ListenRandomPorts int `json:"listen_random_ports,omitempty"`
  182. }
  183. type NatHoleResp struct {
  184. TransactionID string `json:"transaction_id,omitempty"`
  185. Sid string `json:"sid,omitempty"`
  186. Protocol string `json:"protocol,omitempty"`
  187. CandidateAddrs []string `json:"candidate_addrs,omitempty"`
  188. AssistedAddrs []string `json:"assisted_addrs,omitempty"`
  189. DetectBehavior NatHoleDetectBehavior `json:"detect_behavior,omitempty"`
  190. Error string `json:"error,omitempty"`
  191. }
  192. type NatHoleSid struct {
  193. TransactionID string `json:"transaction_id,omitempty"`
  194. Sid string `json:"sid,omitempty"`
  195. Response bool `json:"response,omitempty"`
  196. Nonce string `json:"nonce,omitempty"`
  197. }
  198. type NatHoleReport struct {
  199. Sid string `json:"sid,omitempty"`
  200. Success bool `json:"success,omitempty"`
  201. }