diff --git a/go.mod b/go.mod index 38ab2a0da..645550d10 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/Microsoft/go-winio v0.5.1 - github.com/Mrs4s/MiraiGo v0.0.0-20220322144437-665c6acf024a + github.com/Mrs4s/MiraiGo v0.0.0-20220323044857-868828f3da83 github.com/RomiChan/syncx v0.0.0-20220320130821-c88644afda9c github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc github.com/fumiama/go-hide-param v0.1.4 diff --git a/go.sum b/go.sum index 0b7e4b44c..cedc95790 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY= github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84= -github.com/Mrs4s/MiraiGo v0.0.0-20220322144437-665c6acf024a h1:rG0RcLo/kWRAXnVLgMkudktVO2yg3kVgdV1/Zc9fwh4= -github.com/Mrs4s/MiraiGo v0.0.0-20220322144437-665c6acf024a/go.mod h1:APw03y3A3Qp5In9gI/S9m7DBJkWsd6FQMV+fB636bmU= +github.com/Mrs4s/MiraiGo v0.0.0-20220323044857-868828f3da83 h1:2OA6ciqUytCaV7LQr/IvwCqRXJpc8bfvta63bn2XkHc= +github.com/Mrs4s/MiraiGo v0.0.0-20220323044857-868828f3da83/go.mod h1:APw03y3A3Qp5In9gI/S9m7DBJkWsd6FQMV+fB636bmU= github.com/RomiChan/protobuf v0.0.0-20220318113238-d8a99598f896 h1:UFAqSbH6VqW5mEzQV2HVB7+p3k9JfTbidWJ/9F15yz0= github.com/RomiChan/protobuf v0.0.0-20220318113238-d8a99598f896/go.mod h1:CKKOWC7mBxd36zxsCB1V8DTrwlTNRQvkSVbYqyUiGEE= github.com/RomiChan/syncx v0.0.0-20220320130821-c88644afda9c h1:zHWyqx7A71A/+mlzthPVcVrNGuTPyTpCW3meUPtaULU= diff --git a/server/http.go b/server/http.go index b289eef8e..a43a2408a 100644 --- a/server/http.go +++ b/server/http.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "io" + "net" "net/http" "net/url" "os" @@ -31,6 +32,7 @@ import ( // HTTPServer HTTP通信相关配置 type HTTPServer struct { Disabled bool `yaml:"disabled"` + Address string `yaml:"address"` Host string `yaml:"host"` Port int `yaml:"port"` Timeout int32 `yaml:"timeout"` @@ -242,12 +244,20 @@ func runHTTP(bot *coolq.CQBot, node yaml.Node) { return } - var addr string + network, addr := "tcp", "" s := &httpServer{accessToken: conf.AccessToken} - if conf.Host == "" || conf.Port == 0 { + if conf.Address != "" { + uri, err := url.Parse(conf.Address) + if err == nil && uri.Scheme != "" { + network = uri.Scheme + addr = uri.Host + } + } else if conf.Host != "" || conf.Port != 0 { + addr = fmt.Sprintf("%s:%d", conf.Host, conf.Port) + log.Warnln("HTTP 服务器使用了过时的配置格式,请更新配置文件!") + } else { goto client } - addr = fmt.Sprintf("%s:%d", conf.Host, conf.Port) s.api = api.NewCaller(bot) if conf.RateLimit.Enabled { s.api.Use(rateLimit(conf.RateLimit.Frequency, conf.RateLimit.Bucket)) @@ -255,20 +265,16 @@ func runHTTP(bot *coolq.CQBot, node yaml.Node) { if conf.LongPolling.Enabled { s.api.Use(longPolling(bot, conf.LongPolling.MaxQueueSize)) } - go func() { - log.Infof("CQ HTTP 服务器已启动: %v", addr) - server := &http.Server{ - Addr: addr, - Handler: s, - } - if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { - log.Error(err) - log.Infof("HTTP 服务启动失败, 请检查端口是否被占用.") + listener, err := net.Listen(network, addr) + if err != nil { + log.Infof("HTTP 服务启动失败, 请检查端口是否被占用: %v", err) log.Warnf("将在五秒后退出.") time.Sleep(time.Second * 5) os.Exit(1) } + log.Infof("CQ HTTP 服务器已启动: %v", listener.Addr()) + log.Fatal(http.Serve(listener, s)) }() client: for _, c := range conf.Post { diff --git a/server/websocket.go b/server/websocket.go index 20d3a73ba..888cd49c5 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -4,7 +4,9 @@ import ( "bytes" "encoding/json" "fmt" + "net" "net/http" + "net/url" "runtime/debug" "strconv" "strings" @@ -100,6 +102,7 @@ const wsReverseDefault = ` # 反向WS设置 // WebsocketServer 正向WS相关配置 type WebsocketServer struct { Disabled bool `yaml:"disabled"` + Address string `yaml:"address"` Host string `yaml:"host"` Port int `yaml:"port"` @@ -139,6 +142,17 @@ func runWSServer(b *coolq.CQBot, node yaml.Node) { return } + network, address := "tcp", conf.Address + if conf.Address == "" && (conf.Host != "" || conf.Port != 0) { + log.Warn("正向 Websocket 使用了过时的配置格式,请更新配置文件") + address = fmt.Sprintf("%s:%d", conf.Host, conf.Port) + } else { + addr, err := url.Parse(conf.Address) + if err == nil && addr.Scheme != "" { + network = addr.Scheme + address = addr.Host + } + } s := &webSocketServer{ bot: b, conf: &conf, @@ -146,7 +160,6 @@ func runWSServer(b *coolq.CQBot, node yaml.Node) { filter: conf.Filter, } filter.Add(s.filter) - addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port) s.handshake = fmt.Sprintf(`{"_post_method":2,"meta_event_type":"lifecycle","post_type":"meta_event","self_id":%d,"sub_type":"connect","time":%d}`, b.Client.Uin, time.Now().Unix()) b.OnEventPush(s.onBotPushEvent) @@ -154,8 +167,12 @@ func runWSServer(b *coolq.CQBot, node yaml.Node) { mux.HandleFunc("/event", s.event) mux.HandleFunc("/api", s.api) mux.HandleFunc("/", s.any) - log.Infof("CQ WebSocket 服务器已启动: %v", addr) - log.Fatal(http.ListenAndServe(addr, &mux)) + listener, err := net.Listen(network, address) + if err != nil { + log.Fatal(err) + } + log.Infof("CQ WebSocket 服务器已启动: %v", listener.Addr()) + log.Fatal(http.Serve(listener, &mux)) } // runWSClient 运行一个反向向WS client