dail.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package tinymq
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "strconv"
  7. "strings"
  8. "git.me9.top/git/tinymq/config"
  9. "git.me9.top/git/tinymq/conn"
  10. "git.me9.top/git/tinymq/conn/tcp2"
  11. "git.me9.top/git/tinymq/conn/ws2"
  12. )
  13. // 根据协议名建立连接,输出实际代码实现的协议
  14. // 会一直阻塞直到连接结束
  15. func Dail(host *conn.HostInfo, cf *config.Config) (conn conn.Connect, err error) {
  16. addr := net.JoinHostPort(host.Host, strconv.Itoa(int(host.Port)))
  17. if host.Version == ws2.VERSION && (host.Proto == ws2.PROTO || host.Proto == ws2.PROTO_STL) {
  18. if cf.PrintMsg {
  19. log.Println("[Dail]:", host.Proto, addr, host.Path, host.Hash)
  20. }
  21. conn, err = ws2.Dial(cf, host.Proto, addr, host.Path, host.Hash)
  22. } else if host.Version == tcp2.VERSION && host.Proto == tcp2.PROTO {
  23. if cf.PrintMsg {
  24. log.Println("[Dail]:", host.Proto, addr, host.Hash)
  25. }
  26. conn, err = tcp2.Dial(cf, addr, host.Hash)
  27. } else {
  28. err = fmt.Errorf("not correct protocol and version found in: %+v", host)
  29. }
  30. return
  31. }
  32. // 检验 AuthInfo
  33. func CheckAuthInfo(
  34. conn conn.Connect,
  35. host *conn.HostInfo,
  36. localChannel string,
  37. remoteChannel string,
  38. authFunc AuthFunc,
  39. checkAuthFunc CheckAuthFunc,
  40. ) (err error) {
  41. if err := conn.WriteAuthInfo(localChannel, authFunc(true, host.Proto, host.Version, remoteChannel, nil)); err != nil {
  42. log.Println("[CheckAuthInfo] WriteAuthInfo failed:", err)
  43. // conn.Close()
  44. // host.Errors++
  45. // host.Updated = time.Now()
  46. return err
  47. }
  48. // 接收频道信息
  49. proto, version, channel, auth, err := conn.ReadAuthInfo()
  50. if err != nil {
  51. log.Println("[CheckAuthInfo] ReadAuthInfo failed:", err)
  52. // conn.Close()
  53. // host.Errors++
  54. // host.Updated = time.Now()
  55. return err
  56. }
  57. // 检查版本和协议是否一致
  58. if version != host.Version || !strings.HasPrefix(host.Proto, proto) {
  59. err = fmt.Errorf("[CheckAuthInfo] version or protocol wrong: %d, %s", version, proto)
  60. log.Println(err)
  61. // conn.Close()
  62. // host.Errors++
  63. // host.Updated = time.Now()
  64. return err
  65. }
  66. // 检查频道名称是否匹配
  67. if !strings.Contains(channel, remoteChannel) {
  68. err = fmt.Errorf("[CheckAuthInfo] channel want %s, get %s", remoteChannel, channel)
  69. log.Println(err)
  70. // conn.Close()
  71. // host.Errors++
  72. // host.Updated = time.Now()
  73. return err
  74. }
  75. // 检查验证是否合法
  76. if !checkAuthFunc(true, proto, version, channel, auth) {
  77. err = fmt.Errorf("[CheckAuthInfo] client checkAuthFunc in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
  78. log.Println(err)
  79. // conn.Close()
  80. // host.Errors++
  81. // host.Updated = time.Now()
  82. return err
  83. }
  84. // 更新服务主机信息
  85. // host.Errors = 0
  86. // host.Updated = time.Now()
  87. return
  88. }