type.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package tinymq
  2. import (
  3. "bytes"
  4. "fmt"
  5. // "regexp"
  6. "strings"
  7. "time"
  8. )
  9. // 中间件函数
  10. // 如果返回为空,表示处理完成,通过
  11. // 如果返回 NEXT_MIDDLE,表示需要下一个中间件函数处理;如果没有下一函数则默认通过
  12. type MiddleFunc func(request *RequestData) (response *ResponseData)
  13. // 订阅频道响应函数
  14. type SubscribeBackFunc func(request *RequestData) (state uint8, result any)
  15. // GET 获取数据的回调函数,如果返回 false 则提前结束
  16. type GetBackFunc func(response *ResponseData) (ok bool)
  17. // 线路状态改变时调用
  18. type ConnectStatusFunc func(conn *Line)
  19. // 频道过滤器函数,如果返回true表示成功匹配
  20. type FilterFunc func(conn *Line) (ok bool)
  21. // 订阅频道数据结构
  22. type SubscribeData struct {
  23. Filter FilterFunc // 频道匹配过滤
  24. Cmd string // 请求的命令
  25. BackFunc SubscribeBackFunc //回调函数,如果状态为 NEXT_SUBSCRIBE 将继续下一个频道调用
  26. }
  27. // 获取数据使用的数据结构
  28. type GetData struct {
  29. Filter FilterFunc // 命令匹配过滤
  30. Cmd string
  31. Data any
  32. Max int // 获取数据的频道最多有几个,如果为0表示没有限制
  33. Timeout int // 超时时间(毫秒)
  34. Rand bool // 是否使用随机的数列
  35. backchan chan *ResponseData // 获取响应返回的数据
  36. }
  37. // 连接状态
  38. type ConnectState byte
  39. const (
  40. Disconnected ConnectState = iota
  41. Connected
  42. Closed
  43. )
  44. func (t ConnectState) String() string {
  45. switch t {
  46. case Disconnected:
  47. return "Disconnected"
  48. case Connected:
  49. return "Connected"
  50. case Closed:
  51. return "Closed"
  52. default:
  53. return fmt.Sprintf("Unknown ConnectState (%d)", t)
  54. }
  55. }
  56. // 主机类型
  57. type HostType byte
  58. const (
  59. Direct HostType = iota
  60. Proxy
  61. Both
  62. )
  63. // 请求数据包
  64. type RequestData struct {
  65. Id uint16
  66. Cmd string
  67. Data []byte
  68. timeout int // 超时时间,单位为毫秒
  69. backchan chan *ResponseData // 返回数据的管道
  70. conn *Line // 将连接传递出去是为了能够让上层找回来
  71. }
  72. func (r *RequestData) Conn() *Line {
  73. return r.conn
  74. }
  75. type ResponseData struct {
  76. Id uint16
  77. State uint8
  78. Data []byte
  79. conn *Line
  80. }
  81. func (r *ResponseData) Conn() *Line {
  82. return r.conn
  83. }
  84. type PingData struct {
  85. Id uint16
  86. }
  87. // 请求信息,得到回应通过管道传递信息
  88. type GetMsg struct {
  89. out chan *ResponseData
  90. timer *time.Timer
  91. }
  92. // 连接服务结构
  93. type HostInfo struct {
  94. Proto string `json:"proto" yaml:"proto"` // 协议
  95. Version uint8 `json:"version" yaml:"version"` // 版本
  96. Host string `json:"host" yaml:"host"` // 连接的IP地址或者域名
  97. Bind string `json:"bind" yaml:"bind"` // 绑定的地址
  98. Port uint16 `json:"port" yaml:"port"` // 连接的端口
  99. Path string `json:"path" yaml:"path"` // 连接的路径
  100. Hash string `json:"hash" yaml:"hash"` // 连接验证使用,格式 method:key
  101. Proxy bool `json:"proxy" yaml:"proxy"` // 是否代理
  102. Priority int16 `json:"priority" yaml:"priority"` // 优先级,-1 表示不可用,0 表示最高优先级(为了兼容没有优先级的节点),1-100 表示优先级别,数值越高优先级越高
  103. Errors uint16 `json:"errors" yaml:"errors"` // 连接失败计数,如果成功了则重置为0
  104. Updated time.Time `json:"updated" yaml:"updated"` // 节点信息刷新时间
  105. }
  106. // 只输出客户端要连接的信息
  107. func (h *HostInfo) String() string {
  108. var b bytes.Buffer
  109. b.WriteString(fmt.Sprintf("%s%d://", h.Proto, h.Version))
  110. if h.Hash != "" {
  111. b.WriteString(h.Hash + "@")
  112. }
  113. if strings.Contains(h.Host, ":") {
  114. // ipv6
  115. b.WriteString("[" + h.Host + "]")
  116. } else {
  117. b.WriteString(h.Host)
  118. }
  119. if h.Port > 0 {
  120. b.WriteString(fmt.Sprintf(":%d", h.Port))
  121. }
  122. if h.Path != "" {
  123. b.WriteString(h.Path)
  124. }
  125. param := make([]string, 0)
  126. if h.Proxy {
  127. param = append(param, "proxy=1")
  128. }
  129. if h.Priority != 0 {
  130. param = append(param, fmt.Sprintf("priority=%d", h.Priority))
  131. }
  132. if len(param) > 0 {
  133. b.WriteString("?" + strings.Join(param, "&"))
  134. }
  135. return b.String()
  136. }
  137. // 输出代表一个节点的关键信息
  138. func (h *HostInfo) Key() string {
  139. return fmt.Sprintf("%s%d://%s:%d%s", h.Proto, h.Version, h.Host, h.Port, h.Path)
  140. }
  141. // 获取对应频道的一个连接地址
  142. type ConnectHostFunc func(channel string, hostType HostType) (hostInfo *HostInfo, err error)
  143. // 获取认证信息
  144. type AuthFunc func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte)
  145. // 认证合法性函数
  146. type CheckAuthFunc func(client bool, proto string, version uint8, channel string, auth []byte) bool
  147. // 验证发送的数据条件是否满足
  148. type CheckConnectOkFunc = func(line *Line, data *GetData) bool