msg.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "reflect"
  16. const (
  17. TypeLogin = 'o'
  18. TypeLoginResp = '1'
  19. TypeNewProxy = 'p'
  20. TypeNewProxyResp = '2'
  21. TypeNewWorkConn = 'w'
  22. TypeReqWorkConn = 'r'
  23. TypeStartWorkConn = 's'
  24. TypePing = 'h'
  25. TypePong = '4'
  26. )
  27. var (
  28. TypeMap map[byte]reflect.Type
  29. TypeStringMap map[reflect.Type]byte
  30. )
  31. func init() {
  32. TypeMap = make(map[byte]reflect.Type)
  33. TypeStringMap = make(map[reflect.Type]byte)
  34. TypeMap[TypeLogin] = getTypeFn((*Login)(nil))
  35. TypeMap[TypeLoginResp] = getTypeFn((*LoginResp)(nil))
  36. TypeMap[TypeNewProxy] = getTypeFn((*NewProxy)(nil))
  37. TypeMap[TypeNewProxyResp] = getTypeFn((*NewProxyResp)(nil))
  38. TypeMap[TypeNewWorkConn] = getTypeFn((*NewWorkConn)(nil))
  39. TypeMap[TypeReqWorkConn] = getTypeFn((*ReqWorkConn)(nil))
  40. TypeMap[TypeStartWorkConn] = getTypeFn((*StartWorkConn)(nil))
  41. TypeMap[TypePing] = getTypeFn((*Ping)(nil))
  42. TypeMap[TypePong] = getTypeFn((*Pong)(nil))
  43. for k, v := range TypeMap {
  44. TypeStringMap[v] = k
  45. }
  46. }
  47. func getTypeFn(obj interface{}) reflect.Type {
  48. return reflect.TypeOf(obj).Elem()
  49. }
  50. // Message wraps socket packages for communicating between frpc and frps.
  51. type Message interface{}
  52. // When frpc start, client send this message to login to server.
  53. type Login struct {
  54. Version string `json:"version"`
  55. Hostname string `json:"hostname"`
  56. Os string `json:"os"`
  57. Arch string `json:"arch"`
  58. User string `json:"user"`
  59. PrivilegeKey string `json:"privilege_key"`
  60. Timestamp int64 `json:"timestamp"`
  61. RunId string `json:"run_id"`
  62. // Some global configures.
  63. PoolCount int `json:"pool_count"`
  64. }
  65. type LoginResp struct {
  66. Version string `json:"version"`
  67. RunId string `json:"run_id"`
  68. Error string `json:"error"`
  69. }
  70. // When frpc login success, send this message to frps for running a new proxy.
  71. type NewProxy struct {
  72. ProxyName string `json:"proxy_name"`
  73. ProxyType string `json:"proxy_type"`
  74. UseEncryption bool `json:"use_encryption"`
  75. UseCompression bool `json:"use_compression"`
  76. // tcp and udp only
  77. RemotePort int64 `json:"remote_port"`
  78. // http and https only
  79. CustomDomains []string `json:"custom_domains"`
  80. SubDomain string `json:"subdomain"`
  81. Locations []string `json:"locations"`
  82. HostHeaderRewrite string `json:"host_header_rewrite"`
  83. HttpUser string `json:"http_user"`
  84. HttpPwd string `json:"http_pwd"`
  85. }
  86. type NewProxyResp struct {
  87. ProxyName string `json:"proxy_name"`
  88. Error string `json:"error"`
  89. }
  90. type NewWorkConn struct {
  91. RunId string `json:"run_id"`
  92. }
  93. type ReqWorkConn struct {
  94. }
  95. type StartWorkConn struct {
  96. ProxyName string `json:"proxy_name"`
  97. }
  98. type Ping struct {
  99. }
  100. type Pong struct {
  101. }