Skip to content

Commit

Permalink
server: new config format for HTTP server and Websocket server
Browse files Browse the repository at this point in the history
For #1415
Fixes #1438
  • Loading branch information
wdvxdr1123 committed Mar 23, 2022
1 parent e4d10eb commit f63c59f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
30 changes: 18 additions & 12 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
Expand All @@ -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"`
Expand Down Expand Up @@ -242,33 +244,37 @@ 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))
}
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 {
Expand Down
23 changes: 20 additions & 3 deletions server/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"runtime/debug"
"strconv"
"strings"
Expand Down Expand Up @@ -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"`

Expand Down Expand Up @@ -139,23 +142,37 @@ 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,
token: conf.AccessToken,
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)
mux := http.ServeMux{}
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
Expand Down

0 comments on commit f63c59f

Please sign in to comment.