mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
server: new config format for HTTP server and Websocket server
For #1415 Fixes #1438
This commit is contained in:
parent
e4d10eb2ae
commit
f63c59f1a4
2
go.mod
2
go.mod
@ -4,7 +4,7 @@ go 1.18
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Microsoft/go-winio v0.5.1
|
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/syncx v0.0.0-20220320130821-c88644afda9c
|
||||||
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc
|
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc
|
||||||
github.com/fumiama/go-hide-param v0.1.4
|
github.com/fumiama/go-hide-param v0.1.4
|
||||||
|
4
go.sum
4
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 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
|
||||||
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
|
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-20220323044857-868828f3da83 h1:2OA6ciqUytCaV7LQr/IvwCqRXJpc8bfvta63bn2XkHc=
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20220322144437-665c6acf024a/go.mod h1:APw03y3A3Qp5In9gI/S9m7DBJkWsd6FQMV+fB636bmU=
|
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 h1:UFAqSbH6VqW5mEzQV2HVB7+p3k9JfTbidWJ/9F15yz0=
|
||||||
github.com/RomiChan/protobuf v0.0.0-20220318113238-d8a99598f896/go.mod h1:CKKOWC7mBxd36zxsCB1V8DTrwlTNRQvkSVbYqyUiGEE=
|
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=
|
github.com/RomiChan/syncx v0.0.0-20220320130821-c88644afda9c h1:zHWyqx7A71A/+mlzthPVcVrNGuTPyTpCW3meUPtaULU=
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@ -31,6 +32,7 @@ import (
|
|||||||
// HTTPServer HTTP通信相关配置
|
// HTTPServer HTTP通信相关配置
|
||||||
type HTTPServer struct {
|
type HTTPServer struct {
|
||||||
Disabled bool `yaml:"disabled"`
|
Disabled bool `yaml:"disabled"`
|
||||||
|
Address string `yaml:"address"`
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
Timeout int32 `yaml:"timeout"`
|
Timeout int32 `yaml:"timeout"`
|
||||||
@ -242,12 +244,20 @@ func runHTTP(bot *coolq.CQBot, node yaml.Node) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var addr string
|
network, addr := "tcp", ""
|
||||||
s := &httpServer{accessToken: conf.AccessToken}
|
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
|
goto client
|
||||||
}
|
}
|
||||||
addr = fmt.Sprintf("%s:%d", conf.Host, conf.Port)
|
|
||||||
s.api = api.NewCaller(bot)
|
s.api = api.NewCaller(bot)
|
||||||
if conf.RateLimit.Enabled {
|
if conf.RateLimit.Enabled {
|
||||||
s.api.Use(rateLimit(conf.RateLimit.Frequency, conf.RateLimit.Bucket))
|
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 {
|
if conf.LongPolling.Enabled {
|
||||||
s.api.Use(longPolling(bot, conf.LongPolling.MaxQueueSize))
|
s.api.Use(longPolling(bot, conf.LongPolling.MaxQueueSize))
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
log.Infof("CQ HTTP 服务器已启动: %v", addr)
|
listener, err := net.Listen(network, addr)
|
||||||
server := &http.Server{
|
if err != nil {
|
||||||
Addr: addr,
|
log.Infof("HTTP 服务启动失败, 请检查端口是否被占用: %v", err)
|
||||||
Handler: s,
|
|
||||||
}
|
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
||||||
log.Error(err)
|
|
||||||
log.Infof("HTTP 服务启动失败, 请检查端口是否被占用.")
|
|
||||||
log.Warnf("将在五秒后退出.")
|
log.Warnf("将在五秒后退出.")
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
log.Infof("CQ HTTP 服务器已启动: %v", listener.Addr())
|
||||||
|
log.Fatal(http.Serve(listener, s))
|
||||||
}()
|
}()
|
||||||
client:
|
client:
|
||||||
for _, c := range conf.Post {
|
for _, c := range conf.Post {
|
||||||
|
@ -4,7 +4,9 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -100,6 +102,7 @@ const wsReverseDefault = ` # 反向WS设置
|
|||||||
// WebsocketServer 正向WS相关配置
|
// WebsocketServer 正向WS相关配置
|
||||||
type WebsocketServer struct {
|
type WebsocketServer struct {
|
||||||
Disabled bool `yaml:"disabled"`
|
Disabled bool `yaml:"disabled"`
|
||||||
|
Address string `yaml:"address"`
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
|
|
||||||
@ -139,6 +142,17 @@ func runWSServer(b *coolq.CQBot, node yaml.Node) {
|
|||||||
return
|
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{
|
s := &webSocketServer{
|
||||||
bot: b,
|
bot: b,
|
||||||
conf: &conf,
|
conf: &conf,
|
||||||
@ -146,7 +160,6 @@ func runWSServer(b *coolq.CQBot, node yaml.Node) {
|
|||||||
filter: conf.Filter,
|
filter: conf.Filter,
|
||||||
}
|
}
|
||||||
filter.Add(s.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}`,
|
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.Client.Uin, time.Now().Unix())
|
||||||
b.OnEventPush(s.onBotPushEvent)
|
b.OnEventPush(s.onBotPushEvent)
|
||||||
@ -154,8 +167,12 @@ func runWSServer(b *coolq.CQBot, node yaml.Node) {
|
|||||||
mux.HandleFunc("/event", s.event)
|
mux.HandleFunc("/event", s.event)
|
||||||
mux.HandleFunc("/api", s.api)
|
mux.HandleFunc("/api", s.api)
|
||||||
mux.HandleFunc("/", s.any)
|
mux.HandleFunc("/", s.any)
|
||||||
log.Infof("CQ WebSocket 服务器已启动: %v", addr)
|
listener, err := net.Listen(network, address)
|
||||||
log.Fatal(http.ListenAndServe(addr, &mux))
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Infof("CQ WebSocket 服务器已启动: %v", listener.Addr())
|
||||||
|
log.Fatal(http.Serve(listener, &mux))
|
||||||
}
|
}
|
||||||
|
|
||||||
// runWSClient 运行一个反向向WS client
|
// runWSClient 运行一个反向向WS client
|
||||||
|
Loading…
x
Reference in New Issue
Block a user