proxy.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //go:build ignore
  2. // +build ignore
  3. package main
  4. import (
  5. "log"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. "time"
  10. "git.me9.top/git/tinymq"
  11. "git.me9.top/git/tinymq/config"
  12. "git.me9.top/git/tinymq/conn"
  13. )
  14. // 代理架构
  15. func main() {
  16. cf := config.NewConfig()
  17. localChannel := "/tinymq/proxy"
  18. var hub *tinymq.Hub
  19. hub = tinymq.NewHub(
  20. cf,
  21. localChannel,
  22. nil,
  23. func(host *conn.HostInfo, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
  24. log.Println("[AuthFunc]", host, proto, version, channel, string(remoteAuth))
  25. return []byte("tinymq-proxy")
  26. },
  27. func(host *conn.HostInfo, proto string, version uint8, channel string, auth []byte) bool {
  28. log.Println("[CheckAuthFunc]", host, proto, version, channel, string(auth))
  29. return true
  30. },
  31. func(conn *tinymq.Line) {
  32. log.Println("[Connect state change]", conn.Channel(), conn.State(), time.Since(conn.Started()))
  33. },
  34. )
  35. // tcp2协议
  36. bindTpv2Info := &conn.HostInfo{
  37. Proto: "tcp",
  38. Version: 2,
  39. Bind: "127.0.0.1",
  40. Port: 44222,
  41. Hash: "xor:1qaz2wsx3",
  42. }
  43. hub.BindForServer(bindTpv2Info)
  44. // ws2协议
  45. bindws2Info := &conn.HostInfo{
  46. Proto: "ws",
  47. Version: 2,
  48. Bind: "127.0.0.1",
  49. Port: 44211,
  50. Path: "/tinymq-xor",
  51. Hash: "xor:1qaz2wsx3edc",
  52. }
  53. hub.BindForServer(bindws2Info)
  54. // ws2协议,没有加密算法
  55. bindInfo := &conn.HostInfo{
  56. Proto: "ws",
  57. Version: 2,
  58. // Bind: "127.0.0.1",
  59. Port: 44211,
  60. Path: "/tinymq",
  61. }
  62. hub.BindForServer(bindInfo)
  63. // 初始化一个channel
  64. exit := make(chan os.Signal, 3)
  65. //notify方法用来监听收到的信号
  66. signal.Notify(exit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
  67. sig := <-exit
  68. log.Println("[Exist with]", sig.String())
  69. }