msg.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. TypeNewVistorConn = 'v'
  29. TypeNewVistorConnResp = '3'
  30. TypePing = 'h'
  31. TypePong = '4'
  32. TypeUdpPacket = 'u'
  33. )
  34. var (
  35. TypeMap map[byte]reflect.Type
  36. TypeStringMap map[reflect.Type]byte
  37. )
  38. func init() {
  39. TypeMap = make(map[byte]reflect.Type)
  40. TypeStringMap = make(map[reflect.Type]byte)
  41. TypeMap[TypeLogin] = reflect.TypeOf(Login{})
  42. TypeMap[TypeLoginResp] = reflect.TypeOf(LoginResp{})
  43. TypeMap[TypeNewProxy] = reflect.TypeOf(NewProxy{})
  44. TypeMap[TypeNewProxyResp] = reflect.TypeOf(NewProxyResp{})
  45. TypeMap[TypeCloseProxy] = reflect.TypeOf(CloseProxy{})
  46. TypeMap[TypeNewWorkConn] = reflect.TypeOf(NewWorkConn{})
  47. TypeMap[TypeReqWorkConn] = reflect.TypeOf(ReqWorkConn{})
  48. TypeMap[TypeStartWorkConn] = reflect.TypeOf(StartWorkConn{})
  49. TypeMap[TypeNewVistorConn] = reflect.TypeOf(NewVistorConn{})
  50. TypeMap[TypeNewVistorConnResp] = reflect.TypeOf(NewVistorConnResp{})
  51. TypeMap[TypePing] = reflect.TypeOf(Ping{})
  52. TypeMap[TypePong] = reflect.TypeOf(Pong{})
  53. TypeMap[TypeUdpPacket] = reflect.TypeOf(UdpPacket{})
  54. for k, v := range TypeMap {
  55. TypeStringMap[v] = k
  56. }
  57. }
  58. // Message wraps socket packages for communicating between frpc and frps.
  59. type Message interface{}
  60. // When frpc start, client send this message to login to server.
  61. type Login struct {
  62. Version string `json:"version"`
  63. Hostname string `json:"hostname"`
  64. Os string `json:"os"`
  65. Arch string `json:"arch"`
  66. User string `json:"user"`
  67. PrivilegeKey string `json:"privilege_key"`
  68. Timestamp int64 `json:"timestamp"`
  69. RunId string `json:"run_id"`
  70. // Some global configures.
  71. PoolCount int `json:"pool_count"`
  72. }
  73. type LoginResp struct {
  74. Version string `json:"version"`
  75. RunId string `json:"run_id"`
  76. Error string `json:"error"`
  77. }
  78. // When frpc login success, send this message to frps for running a new proxy.
  79. type NewProxy struct {
  80. ProxyName string `json:"proxy_name"`
  81. ProxyType string `json:"proxy_type"`
  82. UseEncryption bool `json:"use_encryption"`
  83. UseCompression bool `json:"use_compression"`
  84. // tcp and udp only
  85. RemotePort int64 `json:"remote_port"`
  86. // http and https only
  87. CustomDomains []string `json:"custom_domains"`
  88. SubDomain string `json:"subdomain"`
  89. Locations []string `json:"locations"`
  90. HostHeaderRewrite string `json:"host_header_rewrite"`
  91. HttpUser string `json:"http_user"`
  92. HttpPwd string `json:"http_pwd"`
  93. // stcp
  94. Sk string `json:"sk"`
  95. }
  96. type NewProxyResp struct {
  97. ProxyName string `json:"proxy_name"`
  98. Error string `json:"error"`
  99. }
  100. type CloseProxy struct {
  101. ProxyName string `json:"proxy_name"`
  102. }
  103. type NewWorkConn struct {
  104. RunId string `json:"run_id"`
  105. }
  106. type ReqWorkConn struct {
  107. }
  108. type StartWorkConn struct {
  109. ProxyName string `json:"proxy_name"`
  110. }
  111. type NewVistorConn struct {
  112. ProxyName string `json:"proxy_name"`
  113. SignKey string `json:"sign_key"`
  114. Timestamp int64 `json:"timestamp"`
  115. UseEncryption bool `json:"use_encryption"`
  116. UseCompression bool `json:"use_compression"`
  117. }
  118. type NewVistorConnResp struct {
  119. ProxyName string `json:"proxy_name"`
  120. Error string `json:"error"`
  121. }
  122. type Ping struct {
  123. }
  124. type Pong struct {
  125. }
  126. type UdpPacket struct {
  127. Content string `json:"c"`
  128. LocalAddr *net.UDPAddr `json:"l"`
  129. RemoteAddr *net.UDPAddr `json:"r"`
  130. }