type.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package tinymq
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "regexp"
  7. "strconv"
  8. // "regexp"
  9. "strings"
  10. "time"
  11. )
  12. // 定义成功与失败的值
  13. const STATE_OK = 1
  14. const STATE_FAILED = 0
  15. // 中间件函数
  16. // 如果返回为空,表示处理完成,通过
  17. // 如果返回 NEXT_MIDDLE,表示需要下一个中间件函数处理;如果没有下一函数则默认通过
  18. type MiddleFunc func(request *RequestData) (response *ResponseData)
  19. // 订阅频道响应函数
  20. type SubscribeBackFunc func(request *RequestData) (state uint8, result any)
  21. // GET 获取数据的回调函数,如果返回 false 则提前结束
  22. type GetBackFunc func(response *ResponseData) (ok bool)
  23. // 线路状态改变时调用
  24. type ConnectStatusFunc func(conn *Line)
  25. // 频道过滤器函数,如果返回true表示成功匹配
  26. type FilterFunc func(conn *Line) (ok bool)
  27. // 通过过滤函数获取一个频道信息
  28. type FilterToChannelFunc func(filter FilterFunc) (channel string)
  29. // 订阅频道数据结构
  30. type SubscribeData struct {
  31. Filter FilterFunc // 频道匹配过滤
  32. Cmd string // 请求的命令
  33. BackFunc SubscribeBackFunc //回调函数,如果状态为 NEXT_SUBSCRIBE 将继续下一个频道调用
  34. }
  35. // 获取数据使用的数据结构
  36. type GetData struct {
  37. Filter FilterFunc // 命令匹配过滤
  38. Cmd string
  39. Data any
  40. Max int // 获取数据的频道最多有几个,如果为0表示没有限制
  41. Timeout int // 超时时间(毫秒)
  42. Rand bool // 是否使用随机的数列
  43. backchan chan *ResponseData // 获取响应返回的数据
  44. }
  45. // 连接状态
  46. type ConnectState byte
  47. const (
  48. Disconnected ConnectState = iota
  49. Connected
  50. Closed
  51. )
  52. func (t ConnectState) String() string {
  53. switch t {
  54. case Disconnected:
  55. return "Disconnected"
  56. case Connected:
  57. return "Connected"
  58. case Closed:
  59. return "Closed"
  60. default:
  61. return fmt.Sprintf("Unknown ConnectState (%d)", t)
  62. }
  63. }
  64. // 主机类型
  65. type HostType byte
  66. const (
  67. Direct HostType = iota
  68. Proxy
  69. Both
  70. )
  71. // 请求数据包
  72. type RequestData struct {
  73. Id uint16
  74. Cmd string
  75. Data []byte
  76. timeout int // 超时时间,单位为毫秒
  77. backchan chan *ResponseData // 返回数据的管道
  78. conn *Line // 将连接传递出去是为了能够让上层找回来
  79. }
  80. func (r *RequestData) Conn() *Line {
  81. return r.conn
  82. }
  83. type ResponseData struct {
  84. Id uint16
  85. State uint8
  86. Data []byte
  87. conn *Line
  88. }
  89. func (r *ResponseData) Conn() *Line {
  90. return r.conn
  91. }
  92. type PingData struct {
  93. Id uint16
  94. }
  95. // 请求信息,得到回应通过管道传递信息
  96. type GetMsg struct {
  97. out chan *ResponseData
  98. timer *time.Timer
  99. }
  100. // 连接服务结构
  101. type HostInfo struct {
  102. Proto string `json:"proto" yaml:"proto"` // 协议
  103. Version uint8 `json:"version" yaml:"version"` // 版本
  104. Host string `json:"host" yaml:"host"` // 连接的IP地址或者域名
  105. Bind string `json:"bind,omitempty" yaml:"bind"` // 绑定的地址
  106. Port uint16 `json:"port,omitempty" yaml:"port"` // 连接的端口
  107. Path string `json:"path,omitempty" yaml:"path"` // 连接的路径
  108. Hash string `json:"hash,omitempty" yaml:"hash"` // 连接验证使用,格式 method:key
  109. Proxy bool `json:"proxy,omitempty" yaml:"proxy"` // 是否代理
  110. Nat bool `json:"nat,omitempty" yaml:"nat"` // 是否是前端nat的方式处理
  111. Priority int16 `json:"priority,omitempty" yaml:"priority"` // 优先级,-1 表示不可用,0 表示最高优先级(为了兼容没有优先级的节点),1-100 表示优先级别,数值越高优先级越高
  112. Errors uint16 `json:"errors,omitempty" yaml:"errors"` // 连接失败计数,如果成功了则重置为0
  113. Updated time.Time `json:"updated,omitempty" yaml:"updated"` // 节点信息刷新时间
  114. }
  115. // 从 url 中解析信息
  116. // url 格式:ws2://xor:s^7mv7L!Mrn8Y!vn@127.0.0.1:14541/wsv2?proxy=1
  117. // 仅支持客户端连接使用
  118. func ParseUrl(url string) (hostInfo *HostInfo, err error) {
  119. mx := regexp.MustCompile(`^([a-z]+)([0-9]*)://([^#/\?]+)(/[\w\-/]+)?`).FindStringSubmatch(url)
  120. if mx == nil {
  121. return nil, errors.New("invalid url")
  122. }
  123. protocol := mx[1]
  124. version, _ := strconv.Atoi(mx[2])
  125. host := mx[3]
  126. index := strings.LastIndex(host, "@")
  127. hash := ""
  128. if index >= 0 {
  129. hash = host[0:index]
  130. host = host[index+1:]
  131. }
  132. // 检查是否ipv6,解析出ip和端口
  133. index = strings.Index(host, "]:")
  134. port := 0
  135. if index > 0 {
  136. // ipv6 地址和端口
  137. port, err = strconv.Atoi(host[index+2:])
  138. if err != nil {
  139. return nil, err
  140. }
  141. host = host[1:index]
  142. } else {
  143. hs := strings.Split(host, ":")
  144. if len(hs) == 2 {
  145. host = hs[0]
  146. port, err = strconv.Atoi(hs[1])
  147. if err != nil {
  148. return nil, err
  149. }
  150. }
  151. }
  152. path := ""
  153. if len(mx) > 4 {
  154. path = mx[4]
  155. }
  156. hostInfo = &HostInfo{
  157. Proto: protocol,
  158. Version: uint8(version),
  159. Host: host,
  160. Port: uint16(port),
  161. Hash: hash,
  162. Path: path,
  163. }
  164. // 查找是否代理
  165. url = url[len(mx[0]):]
  166. if regexp.MustCompile(`[\?&]proxy=1`).MatchString(url) {
  167. hostInfo.Proxy = true
  168. }
  169. if regexp.MustCompile(`[\?&]nat=1`).MatchString(url) {
  170. hostInfo.Nat = true
  171. }
  172. priorityM := regexp.MustCompile(`[\?&]priority=(-?\d+)`).FindStringSubmatch(url)
  173. if priorityM != nil {
  174. priority, err := strconv.Atoi(priorityM[1])
  175. if err != nil {
  176. return nil, err
  177. }
  178. hostInfo.Priority = int16(priority)
  179. }
  180. return hostInfo, nil
  181. }
  182. // 只输出客户端要连接的信息
  183. func (h *HostInfo) Url() string {
  184. var b bytes.Buffer
  185. b.WriteString(fmt.Sprintf("%s%d://", h.Proto, h.Version))
  186. if h.Hash != "" {
  187. b.WriteString(h.Hash + "@")
  188. }
  189. if strings.Contains(h.Host, ":") {
  190. // ipv6
  191. b.WriteString("[" + h.Host + "]")
  192. } else {
  193. b.WriteString(h.Host)
  194. }
  195. if h.Port > 0 {
  196. b.WriteString(fmt.Sprintf(":%d", h.Port))
  197. }
  198. if h.Path != "" {
  199. b.WriteString(h.Path)
  200. }
  201. param := make([]string, 0)
  202. if h.Proxy {
  203. param = append(param, "proxy=1")
  204. }
  205. if h.Nat {
  206. param = append(param, "nat=1")
  207. }
  208. if h.Priority != 0 {
  209. param = append(param, fmt.Sprintf("priority=%d", h.Priority))
  210. }
  211. if len(param) > 0 {
  212. b.WriteString("?" + strings.Join(param, "&"))
  213. }
  214. return b.String()
  215. }
  216. // 输出代表一个节点的关键信息
  217. func (h *HostInfo) Key() string {
  218. return fmt.Sprintf("%s%d://%s:%d%s", h.Proto, h.Version, h.Host, h.Port, h.Path)
  219. }
  220. // 获取对应频道的一个连接地址
  221. type ConnectHostFunc func(channel string, hostType HostType) (hostInfo *HostInfo, err error)
  222. // 获取认证信息
  223. type AuthFunc func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte)
  224. // 认证合法性函数
  225. type CheckAuthFunc func(client bool, proto string, version uint8, channel string, auth []byte) bool
  226. // 验证发送的数据条件是否满足
  227. type CheckConnectOkFunc = func(line *Line, data *GetData) bool