//go:build ignore // +build ignore package main import ( "errors" "log" "os" "os/signal" "regexp" "syscall" "time" "git.me9.top/git/tinymq" "git.me9.top/git/tinymq/config" ) func main() { cf := config.NewConfig() localChannel := "/tinymq/server" remoteChannel := "/tinymq/client" var hub *tinymq.Hub hub = tinymq.NewHub(cf, localChannel, func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) { return nil, errors.New("not host found") }, func(proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) { // 从 remoteAuth 是否为空来判断是否需要返回信息 if len(remoteAuth) <= 0 { // 客户端调用,返回验证信息 return []byte("tinymq") } else { // 服务端调用,返回验证token,或者其他信息 return nil } }, func(proto string, version uint8, channel string, auth []byte) bool { return string(auth) == "tinymq" }, func(conn *tinymq.Line) { log.Println("[Connect state change]", conn.Channel(), conn.State(), time.Since(conn.Updated())) if conn.State() == tinymq.Connected { go hub.Get(regexp.MustCompile(remoteChannel), "hello", []byte("hello from server push"), func(response *tinymq.ResponseData) (ok bool) { log.Println("[hello response]", response.State, string(response.Data)) return true }) go hub.Get(regexp.MustCompile(remoteChannel), "nodata", nil, func(response *tinymq.ResponseData) (ok bool) { log.Println("[nodata response]", response.State, string(response.Data)) return true }) } }, ) // tcp2协议 bindTpv2Info := &tinymq.HostInfo{ Proto: "tcp", Version: 2, Bind: "127.0.0.1", Port: 34222, Hash: "xor:1qaz2wsx3", } hub.BindForServer(bindTpv2Info) // ws2协议 bindws2Info := &tinymq.HostInfo{ Proto: "ws", Version: 2, Bind: "127.0.0.1", Port: 34211, Path: "/tinymq-xor", Hash: "xor:1qaz2wsx3edc", } hub.BindForServer(bindws2Info) // ws2协议,没有加密算法 bindInfo := &tinymq.HostInfo{ Proto: "ws", Version: 2, // Bind: "127.0.0.1", Port: 34211, Path: "/tinymq", } hub.BindForServer(bindInfo) // 中间件 hub.UseMiddle(func(request *tinymq.RequestData) (response *tinymq.ResponseData) { log.Println("[Middle]", request.Conn().Channel(), request.Cmd) return nil }) // 订阅频道 hub.Subscribe(regexp.MustCompile(remoteChannel), "hello", func(request *tinymq.RequestData) (state uint8, result []byte) { log.Println("[server RECV]<-", string(request.Data)) return 1, []byte("tiny server") }, ) hub.Subscribe(regexp.MustCompile(remoteChannel), "nodata", func(request *tinymq.RequestData) (state uint8, result []byte) { log.Println("[server RECV]<-", string(request.Data)) return 1, nil }, ) hub.Subscribe(regexp.MustCompile(remoteChannel), "bigdata", func(request *tinymq.RequestData) (state uint8, result []byte) { 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}]`) }) hub.Subscribe(regexp.MustCompile(remoteChannel), "push", func(request *tinymq.RequestData) (state uint8, result []byte) { log.Println("[server RECV]<-", string(request.Data)) return 1, nil }, ) // log.Fatal(http.ListenAndServe(net.JoinHostPort(bindws2Info.Bind, strconv.Itoa(int(bindws2Info.Port))), nil)) // 初始化一个channel exit := make(chan os.Signal, 3) //notify方法用来监听收到的信号 signal.Notify(exit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) sig := <-exit log.Println("[Exist with]", sig.String()) }