msg.go 3.5 KB

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