dail.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // 如果返回的 channel 有值,说明需要代理连接
  78. func ClientCheckAuthInfo(
  79. conn conn.Connect,
  80. host *conn.HostInfo,
  81. localChannel string,
  82. remoteChannel string,
  83. authFunc AuthFunc,
  84. checkAuthFunc CheckAuthFunc,
  85. ) (channel string, err error) {
  86. // 发送验证信息
  87. if err := conn.WriteAuthInfo(localChannel, authFunc(host, host.Proto, host.Version, remoteChannel, nil)); err != nil {
  88. log.Println("[ClientCheckAuthInfo] WriteAuthInfo failed:", err)
  89. return "", err
  90. }
  91. // 接收频道信息
  92. proto, version, channel, auth, err := conn.ReadAuthInfo()
  93. if err != nil {
  94. log.Println("[ClientCheckAuthInfo] ReadAuthInfo failed:", err)
  95. return "", err
  96. }
  97. // 检查版本和协议是否一致
  98. if version != host.Version || !strings.HasPrefix(host.Proto, proto) {
  99. err = fmt.Errorf("[ClientCheckAuthInfo] version or protocol wrong: %d, %s", version, proto)
  100. log.Println(err)
  101. return "", err
  102. }
  103. // 检查频道名称是否匹配,只匹配最终的频道
  104. if !strings.HasPrefix(channel, strings.Split(remoteChannel, "<-")[0]) {
  105. // 代理节点
  106. if host.Proxy {
  107. // 还需要再建立连接
  108. return channel, nil
  109. }
  110. err = fmt.Errorf("[ClientCheckAuthInfo] channel want %s, get %s", remoteChannel, channel)
  111. log.Println(err)
  112. return "", err
  113. }
  114. // 检查验证是否合法
  115. if !checkAuthFunc(host, proto, version, channel, auth) {
  116. err = fmt.Errorf("[ClientCheckAuthInfo] failed in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
  117. log.Println(err)
  118. return "", err
  119. }
  120. return "", nil
  121. }
  122. // 服务端验证
  123. func ServerCheckAuthInfo(
  124. conn conn.Connect,
  125. host *conn.HostInfo,
  126. localChannel string,
  127. authFunc AuthFunc,
  128. checkAuthFunc CheckAuthFunc,
  129. ) (channel string, err error) {
  130. var proto string
  131. var version uint8
  132. var auth []byte
  133. proto, version, channel, auth, err = conn.ReadAuthInfo()
  134. if err != nil {
  135. log.Println("[BindForServer ReadAuthInfo ERROR]", err)
  136. return
  137. }
  138. if version != host.Version || !strings.HasPrefix(host.Proto, proto) {
  139. err = fmt.Errorf("wrong version (%d, %d) or protocol (%s, %s)", version, host.Version, proto, host.Proto)
  140. log.Println(err)
  141. return
  142. }
  143. if !checkAuthFunc(nil, proto, version, channel, auth) {
  144. err = fmt.Errorf("[server checkAuthFunc ERROR] in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
  145. log.Println(err)
  146. return
  147. }
  148. // 发送频道信息
  149. if err = conn.WriteAuthInfo(localChannel, authFunc(nil, proto, version, channel, auth)); err != nil {
  150. log.Println("[WriteAuthInfo ERROR]", err)
  151. return
  152. }
  153. return
  154. }