1
0

msg.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. )
  38. var (
  39. TypeMap map[byte]reflect.Type
  40. TypeStringMap map[reflect.Type]byte
  41. )
  42. func init() {
  43. TypeMap = make(map[byte]reflect.Type)
  44. TypeStringMap = make(map[reflect.Type]byte)
  45. TypeMap[TypeLogin] = reflect.TypeOf(Login{})
  46. TypeMap[TypeLoginResp] = reflect.TypeOf(LoginResp{})
  47. TypeMap[TypeNewProxy] = reflect.TypeOf(NewProxy{})
  48. TypeMap[TypeNewProxyResp] = reflect.TypeOf(NewProxyResp{})
  49. TypeMap[TypeCloseProxy] = reflect.TypeOf(CloseProxy{})
  50. TypeMap[TypeNewWorkConn] = reflect.TypeOf(NewWorkConn{})
  51. TypeMap[TypeReqWorkConn] = reflect.TypeOf(ReqWorkConn{})
  52. TypeMap[TypeStartWorkConn] = reflect.TypeOf(StartWorkConn{})
  53. TypeMap[TypeNewVisitorConn] = reflect.TypeOf(NewVisitorConn{})
  54. TypeMap[TypeNewVisitorConnResp] = reflect.TypeOf(NewVisitorConnResp{})
  55. TypeMap[TypePing] = reflect.TypeOf(Ping{})
  56. TypeMap[TypePong] = reflect.TypeOf(Pong{})
  57. TypeMap[TypeUdpPacket] = reflect.TypeOf(UdpPacket{})
  58. TypeMap[TypeNatHoleVisitor] = reflect.TypeOf(NatHoleVisitor{})
  59. TypeMap[TypeNatHoleClient] = reflect.TypeOf(NatHoleClient{})
  60. TypeMap[TypeNatHoleResp] = reflect.TypeOf(NatHoleResp{})
  61. TypeMap[TypeNatHoleSid] = reflect.TypeOf(NatHoleSid{})
  62. for k, v := range TypeMap {
  63. TypeStringMap[v] = k
  64. }
  65. }
  66. // Message wraps socket packages for communicating between frpc and frps.
  67. type Message interface{}
  68. // When frpc start, client send this message to login to server.
  69. type Login struct {
  70. Version string `json:"version"`
  71. Hostname string `json:"hostname"`
  72. Os string `json:"os"`
  73. Arch string `json:"arch"`
  74. User string `json:"user"`
  75. PrivilegeKey string `json:"privilege_key"`
  76. Timestamp int64 `json:"timestamp"`
  77. RunId string `json:"run_id"`
  78. // Some global configures.
  79. PoolCount int `json:"pool_count"`
  80. }
  81. type LoginResp struct {
  82. Version string `json:"version"`
  83. RunId string `json:"run_id"`
  84. ServerUdpPort int `json:"server_udp_port"`
  85. Error string `json:"error"`
  86. }
  87. // When frpc login success, send this message to frps for running a new proxy.
  88. type NewProxy struct {
  89. ProxyName string `json:"proxy_name"`
  90. ProxyType string `json:"proxy_type"`
  91. UseEncryption bool `json:"use_encryption"`
  92. UseCompression bool `json:"use_compression"`
  93. // tcp and udp only
  94. RemotePort int `json:"remote_port"`
  95. // http and https only
  96. CustomDomains []string `json:"custom_domains"`
  97. SubDomain string `json:"subdomain"`
  98. Locations []string `json:"locations"`
  99. HostHeaderRewrite string `json:"host_header_rewrite"`
  100. HttpUser string `json:"http_user"`
  101. HttpPwd string `json:"http_pwd"`
  102. // stcp
  103. Sk string `json:"sk"`
  104. }
  105. type NewProxyResp struct {
  106. ProxyName string `json:"proxy_name"`
  107. RemoteAddr string `json:"remote_addr"`
  108. Error string `json:"error"`
  109. }
  110. type CloseProxy struct {
  111. ProxyName string `json:"proxy_name"`
  112. }
  113. type NewWorkConn struct {
  114. RunId string `json:"run_id"`
  115. }
  116. type ReqWorkConn struct {
  117. }
  118. type StartWorkConn struct {
  119. ProxyName string `json:"proxy_name"`
  120. }
  121. type NewVisitorConn struct {
  122. ProxyName string `json:"proxy_name"`
  123. SignKey string `json:"sign_key"`
  124. Timestamp int64 `json:"timestamp"`
  125. UseEncryption bool `json:"use_encryption"`
  126. UseCompression bool `json:"use_compression"`
  127. }
  128. type NewVisitorConnResp struct {
  129. ProxyName string `json:"proxy_name"`
  130. Error string `json:"error"`
  131. }
  132. type Ping struct {
  133. }
  134. type Pong struct {
  135. }
  136. type UdpPacket struct {
  137. Content string `json:"c"`
  138. LocalAddr *net.UDPAddr `json:"l"`
  139. RemoteAddr *net.UDPAddr `json:"r"`
  140. }
  141. type NatHoleVisitor struct {
  142. ProxyName string `json:"proxy_name"`
  143. SignKey string `json:"sign_key"`
  144. Timestamp int64 `json:"timestamp"`
  145. }
  146. type NatHoleClient struct {
  147. ProxyName string `json:"proxy_name"`
  148. Sid string `json:"sid"`
  149. }
  150. type NatHoleResp struct {
  151. Sid string `json:"sid"`
  152. VisitorAddr string `json:"visitor_addr"`
  153. ClientAddr string `json:"client_addr"`
  154. }
  155. type NatHoleSid struct {
  156. Sid string `json:"sid"`
  157. }