dail.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package tinymq
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "net"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "git.me9.top/git/tinymq/config"
  12. "git.me9.top/git/tinymq/conn"
  13. "git.me9.top/git/tinymq/conn/tcp2"
  14. "git.me9.top/git/tinymq/conn/ws2"
  15. )
  16. // 根据协议名建立连接,输出实际代码实现的协议
  17. // 会一直阻塞直到连接结束
  18. func Dail(host *conn.HostInfo, cf *config.Config) (conn conn.Connect, err error) {
  19. addr := net.JoinHostPort(host.Host, strconv.Itoa(int(host.Port)))
  20. if host.Version == ws2.VERSION && (host.Proto == ws2.PROTO || host.Proto == ws2.PROTO_STL) {
  21. if cf.PrintMsg {
  22. log.Println("[Dail]:", host.Proto, addr, host.Path, host.Hash)
  23. }
  24. conn, err = ws2.Dial(cf, host.Proto, addr, host.Path, host.Hash)
  25. } else if host.Version == tcp2.VERSION && host.Proto == tcp2.PROTO {
  26. if cf.PrintMsg {
  27. log.Println("[Dail]:", host.Proto, addr, host.Hash)
  28. }
  29. conn, err = tcp2.Dial(cf, addr, host.Hash)
  30. } else {
  31. err = fmt.Errorf("not correct protocol and version found in: %+v", host)
  32. }
  33. return
  34. }
  35. // 连接并超时检查
  36. func DailWithTimeout(ctx context.Context, host *conn.HostInfo, cf *config.Config) (conn conn.Connect, err error) {
  37. // 添加定时器
  38. cx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(cf.ConnectTimeout))
  39. defer cancel()
  40. taskCh := make(chan bool)
  41. done := false // 指示是否已经完成
  42. // 发送连接请求
  43. go func() {
  44. conn, err = Dail(host, cf)
  45. if done {
  46. if err != nil {
  47. log.Println("[ConnectToServer] dial failed:", err)
  48. }
  49. if conn != nil {
  50. conn.Close()
  51. }
  52. } else {
  53. taskCh <- err == nil
  54. }
  55. }()
  56. select {
  57. case ok := <-taskCh:
  58. cancel()
  59. if !ok || err != nil || conn == nil {
  60. log.Println("[ConnectToServer] connect failed:", err)
  61. host.Errors++
  62. host.Updated = time.Now()
  63. if err == nil {
  64. err = errors.New("unknown error")
  65. }
  66. return
  67. }
  68. case <-cx.Done():
  69. done = true
  70. return nil, errors.New("timeout")
  71. case <-ctx.Done():
  72. return nil, errors.New("quit")
  73. }
  74. return
  75. }
  76. // 客户端检验 AuthInfo
  77. func ClientCheckAuthInfo(
  78. conn conn.Connect,
  79. host *conn.HostInfo,
  80. localChannel string,
  81. remoteChannel string,
  82. authFunc AuthFunc,
  83. checkAuthFunc CheckAuthFunc,
  84. ) (err error) {
  85. // 发送验证信息
  86. if err := conn.WriteAuthInfo(localChannel, authFunc(true, host.Proto, host.Version, remoteChannel, nil)); err != nil {
  87. log.Println("[ClientCheckAuthInfo] WriteAuthInfo failed:", err)
  88. return err
  89. }
  90. // 接收频道信息
  91. proto, version, channel, auth, err := conn.ReadAuthInfo()
  92. if err != nil {
  93. log.Println("[ClientCheckAuthInfo] ReadAuthInfo failed:", err)
  94. return err
  95. }
  96. // 检查版本和协议是否一致
  97. if version != host.Version || !strings.HasPrefix(host.Proto, proto) {
  98. err = fmt.Errorf("[ClientCheckAuthInfo] version or protocol wrong: %d, %s", version, proto)
  99. log.Println(err)
  100. return err
  101. }
  102. // 检查频道名称是否匹配
  103. if !strings.Contains(channel, remoteChannel) {
  104. err = fmt.Errorf("[ClientCheckAuthInfo] channel want %s, get %s", remoteChannel, channel)
  105. log.Println(err)
  106. return err
  107. }
  108. // 检查验证是否合法
  109. if !checkAuthFunc(true, proto, version, channel, auth) {
  110. err = fmt.Errorf("[ClientCheckAuthInfo] failed in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
  111. log.Println(err)
  112. return err
  113. }
  114. return
  115. }