server.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. )
  13. func main() {
  14. cf := config.NewConfig()
  15. localChannel := "/tinymq/server"
  16. remoteChannel := "/tinymq/client"
  17. remoteFilter := tinymq.StrChannelFilter(remoteChannel)
  18. var hub *tinymq.Hub
  19. hub = tinymq.NewHub(
  20. cf,
  21. localChannel,
  22. nil,
  23. func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
  24. log.Println("[AuthFunc]", client, proto, version, channel, string(remoteAuth))
  25. return []byte("tinymq-server")
  26. },
  27. func(client bool, proto string, version uint8, channel string, auth []byte) bool {
  28. log.Println("[CheckAuthFunc]", client, proto, version, channel, string(auth))
  29. return string(auth) == "tinymq-client"
  30. },
  31. func(conn *tinymq.Line) {
  32. log.Println("[Connect state change]", conn.Channel(), conn.State(), time.Since(conn.Updated()))
  33. if conn.State() == tinymq.Connected {
  34. go hub.Get(remoteFilter, "hello", []byte("hello from server push"),
  35. func(response *tinymq.ResponseData) (ok bool) {
  36. log.Println("[hello response]", response.State, string(response.Data))
  37. return true
  38. })
  39. go hub.Get(remoteFilter, "nodata", nil,
  40. func(response *tinymq.ResponseData) (ok bool) {
  41. log.Println("[nodata response]", response.State, string(response.Data))
  42. return true
  43. })
  44. }
  45. },
  46. )
  47. // tcp2协议
  48. bindTpv2Info := &tinymq.HostInfo{
  49. Proto: "tcp",
  50. Version: 2,
  51. Bind: "127.0.0.1",
  52. Port: 34222,
  53. Hash: "xor:1qaz2wsx3",
  54. }
  55. hub.BindForServer(bindTpv2Info)
  56. // ws2协议
  57. bindws2Info := &tinymq.HostInfo{
  58. Proto: "ws",
  59. Version: 2,
  60. Bind: "127.0.0.1",
  61. Port: 34211,
  62. Path: "/tinymq-xor",
  63. Hash: "xor:1qaz2wsx3edc",
  64. }
  65. hub.BindForServer(bindws2Info)
  66. // ws2协议,没有加密算法
  67. bindInfo := &tinymq.HostInfo{
  68. Proto: "ws",
  69. Version: 2,
  70. // Bind: "127.0.0.1",
  71. Port: 34211,
  72. Path: "/tinymq",
  73. }
  74. hub.BindForServer(bindInfo)
  75. // 中间件
  76. hub.UseMiddle(func(request *tinymq.RequestData) (response *tinymq.ResponseData) {
  77. log.Println("[Middle]", request.Conn().Channel(), request.Cmd)
  78. return nil
  79. })
  80. // 订阅频道
  81. hub.Subscribe(remoteFilter, "hello",
  82. func(request *tinymq.RequestData) (state uint8, result any) {
  83. // log.Println("[server RECV]<-", string(request.Data))
  84. return 1, []byte("tiny server")
  85. },
  86. )
  87. hub.Subscribe(remoteFilter, "echo",
  88. func(request *tinymq.RequestData) (state uint8, result any) {
  89. // log.Println("[server RECV]<-", string(request.Data))
  90. return 1, request.Data
  91. },
  92. )
  93. hub.Subscribe(remoteFilter, "nodata",
  94. func(request *tinymq.RequestData) (state uint8, result any) {
  95. // log.Println("[server RECV]<-", string(request.Data))
  96. return 1, nil
  97. },
  98. )
  99. hub.Subscribe(remoteFilter, "bigdata", func(request *tinymq.RequestData) (state uint8, result any) {
  100. return 1, []byte(`[{"id": 2132, "ip": "139.162.47.112", "os": "debian12", "cpu": 1, "area": "sg:sg:ap-south", "disk": 25, "ipv6": "2400:8901::2000:8cff:fe0e:441", "keep": {"os": "alpine 3.21.3", "mem": 7851, "arch": "arm64", "cpus": 4, "disk": 59767, "created": 1751690386795, "run_bin": "-", "day_recv": 291810, "day_send": 358378, "month_recv": 928551, "month_send": 1134597}, "label": "sg3-heon-win-07022240", "price": 5.00, "state": "running", "subid": "79573337", "domain": "sg3.heon.win", "isp_id": 3, "memory": 1024, "created": 1751467233775, "traffic": 1000, "updated": 1751469255594, "wish_id": 18, "checking": "ssh", "hostname": "sg3.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "init_name": "root", "wish_name": "sg3.heon.win", "ping_delay": 292, "ping_updated": 1751674503977, "state_updated": 1751469255596}, {"id": 2131, "ip": "172.236.130.209", "os": "debian12", "cpu": 1, "area": "sg:sg:sg-sin-2", "disk": 25, "ipv6": "2600:3c15::2000:73ff:fe8f:66bd", "label": "sg3-heon-win-06232159", "price": 5.00, "state": "deleteSuccess", "subid": "79152316", "domain": "sg3.heon.win", "isp_id": 3, "memory": 1024, "created": 1750715959373, "traffic": 1000, "updated": 1750718536054, "wish_id": 18, "checking": "ssh", "hostname": "sg3.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "init_name": "root", "wish_name": "sg3.heon.win", "ping_delay": 0, "ping_updated": 1751465999352, "state_updated": 1751468009095}, {"id": 2130, "ip": "146.190.109.132", "os": "debian-12-x64", "cpu": 1, "area": "sg:sgp1:Singapore 1", "disk": 25, "ipv6": "2400:6180:0:d2:0:2:ea6:3000", "label": "sg3.heon.win.06232257", "price": 6.00, "state": "deleteSuccess", "subid": "503905326", "domain": "sg3.heon.win", "isp_id": 5, "memory": 1024, "created": 1750690642139, "traffic": 1024, "updated": 1750690732866, "wish_id": 18, "checking": "ssh", "hostname": "sg3.heon.win", "isp_name": "digitalocean-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "init_name": "root", "wish_name": "sg3.heon.win", "code_reduce": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1750693757458}, {"id": 2129, "ip": "134.209.104.238", "os": "debian-12-x64", "cpu": 1, "area": "sg:sgp1:Singapore 1", "disk": 25, "ipv6": "2400:6180:0:d2:0:2:493:8000", "label": "sg4.heon.win.06151809", "price": 6.00, "state": "running", "subid": "502434998", "domain": "sg4.heon.win", "isp_id": 5, "memory": 1024, "created": 1750010961467, "traffic": 1024, "updated": 1750014163199, "wish_id": 20, "checking": "ssh", "hostname": "sg4.heon.win", "isp_name": "digitalocean-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "init_name": "root", "wish_name": "sg4.heon.win", "ping_delay": 450, "ping_updated": 1751674540581, "state_updated": 1750014163204}, {"id": 2128, "ip": "139.162.35.161", "os": "debian12", "cpu": 1, "area": "sg:sg:ap-south", "disk": 25, "ipv6": "2400:8901::2000:80ff:fe34:f484", "label": "sg4-heon-win-06151907", "price": 5.00, "state": "deleteSuccess", "subid": "78675627", "domain": "sg4.heon.win", "isp_id": 3, "memory": 1024, "created": 1749985641467, "traffic": 1000, "updated": 1749985732238, "wish_id": 20, "checking": "ssh", "hostname": "sg4.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "init_name": "root", "wish_name": "sg4.heon.win", "code_reduce": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1749988158401}, {"id": 2127, "ip": "45.79.196.147", "os": "debian12", "cpu": 1, "area": "us:us:us-southeast", "disk": 25, "ipv6": "2600:3c02::2000:a5ff:feff:2fcb", "label": "us2-heon-win-06090939", "price": 5.00, "state": "running", "subid": "78304497", "domain": "us2.heon.win", "isp_id": 3, "memory": 1024, "created": 1749433160975, "traffic": 1000, "updated": 1749435250612, "wish_id": 2, "checking": "ssh", "hostname": "us2.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "init_name": "root", "wish_name": "us2.heon.win", "ping_delay": 463, "ping_updated": 1751674409206, "state_updated": 1749435250615}, {"id": 2126, "ip": "172.237.21.235", "os": "debian12", "cpu": 1, "area": "jp:jp:jp-tyo-3", "disk": 25, "ipv6": "2600:3c18::2000:c3ff:fe50:aa86", "label": "jp1-heon-win-05301340", "price": 5.00, "state": "running", "subid": "77800891", "domain": "jp1.heon.win", "isp_id": 3, "memory": 1024, "created": 1748583648413, "traffic": 1000, "updated": 1748585977441, "wish_id": 15, "checking": "ssh", "hostname": "jp1.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "init_name": "root", "wish_name": "jp1.heon.win", "ping_delay": 1270, "ping_updated": 1751674469953, "state_updated": 1748585977443}, {"id": 2125, "ip": "172.236.142.197", "os": "debian12", "cpu": 1, "area": "sg:sg:sg-sin-2", "disk": 25, "ipv6": "2600:3c15::2000:e6ff:fed7:2fc9", "label": "sg3-heon-win-05262338", "price": 5.00, "state": "deleteSuccess", "subid": "77592327", "domain": "sg3.heon.win", "isp_id": 3, "memory": 1024, "created": 1748273928367, "traffic": 1000, "updated": 1748275237233, "wish_id": 18, "checking": "ssh", "hostname": "sg3.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "init_name": "root", "wish_name": "sg3.heon.win", "ping_delay": 0, "ping_updated": 1750690774521, "state_updated": 1750691358932}, {"id": 2124, "ip": "139.180.191.200", "os": "Debian 12 x64", "cpu": 1, "area": "sg:sgp:Singapore", "disk": 25, "ipv6": "2401:c080:1400:7ef3:5400:5ff:fe6e:bbd9", "label": "sg3.heon.win.05070640", "price": 6.00, "state": "deleteSuccess", "subid": "e5195efc-ee7e-4330-989f-947f67e450ee", "domain": "sg3.heon.win", "isp_id": 4, "memory": 1024, "created": 1746600045172, "traffic": 2048, "updated": 1746602351039, "wish_id": 18, "checking": "ssh", "hostname": "sg3.heon.win", "isp_name": "vultr-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "init_name": "root", "wish_name": "sg3.heon.win", "ping_delay": -1, "ping_updated": 1748273684797, "state_updated": 1748274765455}, {"id": 2123, "ip": "207.148.122.67", "os": "Debian 12 x64", "cpu": 1, "area": "sg:sgp:Singapore", "disk": 25, "ipv6": "2401:c080:1400:7dd6:5400:5ff:fe6e:b1bd", "label": "sg4.heon.win.05070518", "price": 6.00, "state": "deleteSuccess", "subid": "4bab2150-0a75-4460-98df-c2681d906bce", "domain": "sg4.heon.win", "isp_id": 4, "memory": 1024, "created": 1746595125220, "traffic": 2048, "updated": 1746597440316, "wish_id": 20, "checking": "ssh", "hostname": "sg4.heon.win", "isp_name": "vultr-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "init_name": "root", "wish_name": "sg4.heon.win", "ping_delay": 0, "ping_updated": 1749985717334, "state_updated": 1749986358795}, {"id": 2122, "ip": "139.162.47.185", "os": "debian12", "cpu": 1, "area": "sg:sg:ap-south", "disk": 25, "ipv6": "2400:8901::f03c:95ff:fefe:dc38", "label": "sg3-heon-win-05062338", "price": 5.00, "state": "deleteSuccess", "subid": "76523934", "domain": "sg3.heon.win", "isp_id": 3, "memory": 1024, "created": 1746574725657, "traffic": 1000, "updated": 1746575766550, "wish_id": 18, "checking": "ssh", "hostname": "sg3.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker", "init_name": "root", "wish_name": "sg3.heon.win", "code_reduce": "lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1746578565861}, {"id": 2121, "ip": "172.236.137.110", "os": "debian12", "cpu": 1, "area": "sg:sg:sg-sin-2", "disk": 25, "ipv6": "2600:3c15::f03c:95ff:fefe:4f70", "label": "sg4-heon-win-05062216", "price": 5.00, "state": "deleteSuccess", "subid": "76521130", "domain": "sg4.heon.win", "isp_id": 3, "memory": 1024, "created": 1746569805686, "traffic": 1000, "updated": 1746571258653, "wish_id": 20, "checking": "ssh", "hostname": "sg4.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker", "init_name": "root", "wish_name": "sg4.heon.win", "code_reduce": "lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1746573465470}, {"id": 2120, "ip": "206.189.147.42", "os": "debian-12-x64", "cpu": 1, "area": "sg:sgp1:Singapore 1", "disk": 25, "ipv6": "2400:6180:0:d2:0:1:cf9f:d000", "label": "sg3.heon.win.05070036", "price": 6.00, "state": "deleteSuccess", "subid": "494032281", "domain": "sg3.heon.win", "isp_id": 5, "memory": 1024, "created": 1746549407053, "traffic": 1024, "updated": 1746550905004, "wish_id": 18, "checking": "ssh", "hostname": "sg3.heon.win", "isp_name": "digitalocean-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker", "init_name": "root", "wish_name": "sg3.heon.win", "code_reduce": "lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1746553064942}, {"id": 2119, "ip": "207.148.67.99", "os": "Debian 12 x64", "cpu": 1, "area": "sg:sgp:Singapore", "disk": 25, "ipv6": "2001:19f0:4400:2805:5400:5ff:fe6e:6cee", "label": "sg4.heon.win.05061514", "price": 6.00, "state": "deleteSuccess", "subid": "920ddfa1-3267-402e-895a-3e5695dca6ec", "domain": "sg4.heon.win", "isp_id": 4, "memory": 1024, "created": 1746544487742, "traffic": 2048, "updated": 1746545571773, "wish_id": 20, "checking": "ssh", "hostname": "sg4.heon.win", "isp_name": "vultr-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker", "init_name": "root", "wish_name": "sg4.heon.win", "code_reduce": "lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1746548266221}, {"id": 2118, "ip": "172.104.173.105", "os": "debian12", "cpu": 1, "area": "sg:sg:ap-south", "disk": 25, "ipv6": "2400:8901::f03c:95ff:fefe:d6d5", "label": "sg3-heon-win-05060934", "price": 5.00, "state": "deleteSuccess", "subid": "76483564", "domain": "sg3.heon.win", "isp_id": 3, "memory": 1024, "created": 1746524088429, "traffic": 1000, "updated": 1746524179033, "wish_id": 18, "checking": "ssh", "hostname": "sg3.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "init_name": "root", "wish_name": "sg3.heon.win", "code_reduce": "netup|docker|lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1746526973497}, {"id": 2117, "ip": "172.104.62.37", "os": "debian12", "cpu": 1, "area": "sg:sg:ap-south", "disk": 25, "ipv6": "2400:8901::f03c:95ff:feb8:5e6c", "label": "sg4-heon-win-05060812", "price": 5.00, "state": "deleteSuccess", "subid": "76480422", "domain": "sg4.heon.win", "isp_id": 3, "memory": 1024, "created": 1746519165129, "traffic": 1000, "updated": 1746520435639, "wish_id": 20, "checking": "ssh", "hostname": "sg4.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker", "init_name": "root", "wish_name": "sg4.heon.win", "code_reduce": "lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1746523065874}, {"id": 2116, "ip": "167.71.218.41", "os": "debian-12-x64", "cpu": 1, "area": "sg:sgp1:Singapore 1", "disk": 25, "ipv6": "2400:6180:0:d2:0:1:ceee:b000", "label": "sg4.heon.win.05060110", "price": 6.00, "state": "deleteSuccess", "subid": "493860997", "domain": "sg4.heon.win", "isp_id": 5, "memory": 1024, "created": 1746493847546, "traffic": 1024, "updated": 1746494938738, "wish_id": 20, "checking": "ssh", "hostname": "sg4.heon.win", "isp_name": "digitalocean-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker", "init_name": "root", "wish_name": "sg4.heon.win", "code_reduce": "lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1746497564763}, {"id": 2115, "ip": "172.237.95.76", "os": "debian12", "cpu": 1, "area": "sg:sg:sg-sin-2", "disk": 25, "ipv6": "2600:3c15::f03c:95ff:feb8:7dec", "label": "sg4-heon-win-05060208", "price": 5.00, "state": "deleteSuccess", "subid": "76447654", "domain": "sg4.heon.win", "isp_id": 3, "memory": 1024, "created": 1746468526325, "traffic": 1000, "updated": 1746469898005, "wish_id": 20, "checking": "ssh", "hostname": "sg4.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker", "init_name": "root", "wish_name": "sg4.heon.win", "code_reduce": "lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1746472066407}, {"id": 2114, "ip": "207.148.118.69", "os": "Debian 12 x64", "cpu": 1, "area": "sg:sgp:Singapore", "disk": 25, "ipv6": "2001:19f0:4400:410e:5400:5ff:fe6d:da67", "label": "sg4.heon.win.05051106", "price": 6.00, "state": "deleteSuccess", "subid": "3a04199e-0fa3-4beb-9565-b45e005a63b0", "domain": "sg4.heon.win", "isp_id": 4, "memory": 1024, "created": 1746443205298, "traffic": 2048, "updated": 1746444482089, "wish_id": 20, "checking": "ssh", "hostname": "sg4.heon.win", "isp_name": "vultr-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker", "init_name": "root", "wish_name": "sg4.heon.win", "code_reduce": "lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1746447466162}, {"id": 2113, "ip": "45.118.132.40", "os": "debian12", "cpu": 1, "area": "sg:sg:ap-south", "disk": 25, "ipv6": "2400:8901::f03c:95ff:fe0d:93e2", "label": "sg4-heon-win-05051204", "price": 5.00, "state": "deleteSuccess", "subid": "76407968", "domain": "sg4.heon.win", "isp_id": 3, "memory": 1024, "created": 1746417885840, "traffic": 1000, "updated": 1746419176611, "wish_id": 20, "checking": "ssh", "hostname": "sg4.heon.win", "isp_name": "linode-mc", "ssh_port": "22|1022|8011", "ssh_user": "mike", "code_done": "netup|docker", "init_name": "root", "wish_name": "sg4.heon.win", "code_reduce": "lego-warp|lego-radx|lego-reality|lego-hysteria|lego-ocnt|lego-pptp", "state_updated": 1746421665685}]`)
  101. })
  102. hub.Subscribe(remoteFilter, "push",
  103. func(request *tinymq.RequestData) (state uint8, result any) {
  104. // log.Println("[server RECV]<-", string(request.Data))
  105. return 1, nil
  106. },
  107. )
  108. // log.Fatal(http.ListenAndServe(net.JoinHostPort(bindws2Info.Bind, strconv.Itoa(int(bindws2Info.Port))), nil))
  109. // 初始化一个channel
  110. exit := make(chan os.Signal, 3)
  111. //notify方法用来监听收到的信号
  112. signal.Notify(exit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
  113. sig := <-exit
  114. log.Println("[Exist with]", sig.String())
  115. }