From df79d4ae20ca8c3e4fff51463baca97328e4f16c Mon Sep 17 00:00:00 2001 From: Bluefissure Date: Tue, 27 Jun 2023 19:32:01 +0000 Subject: [PATCH 1/4] feat: add sign-server-bearer --- cmd/gocq/login.go | 16 ++++++++++++++-- cmd/gocq/main.go | 3 +++ internal/base/flag.go | 2 ++ modules/config/config.go | 1 + modules/config/default_config.yml | 3 +++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/gocq/login.go b/cmd/gocq/login.go index 5909af0d1..d178ebc9f 100644 --- a/cmd/gocq/login.go +++ b/cmd/gocq/login.go @@ -14,13 +14,14 @@ import ( "github.com/Mrs4s/MiraiGo/client" "github.com/Mrs4s/MiraiGo/utils" - "github.com/Mrs4s/go-cqhttp/internal/base" "github.com/mattn/go-colorable" "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" "gopkg.ilharper.com/x/isatty" + "github.com/Mrs4s/go-cqhttp/internal/base" + "github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/internal/download" ) @@ -270,8 +271,14 @@ func energy(uin uint64, id string, appVersion string, salt []byte) ([]byte, erro if !strings.HasSuffix(signServer, "/") { signServer += "/" } + headers := make(map[string]string) + signServerBearer := base.SignServerBearer + if signServerBearer != "-" && signServerBearer != "" { + headers["Authorization"] = "Bearer " + strings.TrimPrefix(signServerBearer, "Bearer ") + } response, err := download.Request{ Method: http.MethodGet, + Header: headers, URL: signServer + "custom_energy" + fmt.Sprintf("?data=%v&salt=%v", id, hex.EncodeToString(salt)), }.Bytes() if err != nil { @@ -295,10 +302,15 @@ func sign(seq uint64, uin string, cmd string, qua string, buff []byte) (sign []b if !strings.HasSuffix(signServer, "/") { signServer += "/" } + headers := map[string]string{"Content-Type": "application/x-www-form-urlencoded"} + signServerBearer := base.SignServerBearer + if signServerBearer != "-" && signServerBearer != "" { + headers["Authorization"] = "Bearer " + strings.TrimPrefix(signServerBearer, "Bearer ") + } response, err := download.Request{ Method: http.MethodPost, URL: signServer + "sign", - Header: map[string]string{"Content-Type": "application/x-www-form-urlencoded"}, + Header: headers, Body: bytes.NewReader([]byte(fmt.Sprintf("uin=%v&qua=%s&cmd=%s&seq=%v&buffer=%v", uin, qua, cmd, seq, hex.EncodeToString(buff)))), }.Bytes() if err != nil { diff --git a/cmd/gocq/main.go b/cmd/gocq/main.go index 10ab254f2..0ba322c39 100644 --- a/cmd/gocq/main.go +++ b/cmd/gocq/main.go @@ -166,6 +166,9 @@ func LoginInteract() { if base.SignServer != "-" && base.SignServer != "" { log.Infof("使用服务器 %s 进行数据包签名", base.SignServer) + if base.SignServerBearer != "-" && base.SignServerBearer != "" { + log.Infof("使用 Bearer %s 认证服务器 %s ", base.SignServerBearer, base.SignServer) + } wrapper.DandelionEnergy = energy wrapper.FekitGetSign = sign } else { diff --git a/internal/base/flag.go b/internal/base/flag.go index e622ecaea..814e4003e 100644 --- a/internal/base/flag.go +++ b/internal/base/flag.go @@ -39,6 +39,7 @@ var ( AllowTempSession bool // 是否允许发送临时会话信息 UpdateProtocol bool // 是否更新协议 SignServer string // 使用特定的服务器进行签名 + SignServerBearer string // 签名服务器的 Bearer 认证 Token HTTPTimeout int PostFormat string // 上报格式 string or array @@ -89,6 +90,7 @@ func Init() { UseSSOAddress = conf.Account.UseSSOAddress AllowTempSession = conf.Account.AllowTempSession SignServer = conf.Account.SignServer + SignServerBearer = conf.Account.SignServerBearer HTTPTimeout = conf.Message.HTTPTimeout } { // others diff --git a/modules/config/config.go b/modules/config/config.go index b438359a5..8aa747298 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -36,6 +36,7 @@ type Account struct { UseSSOAddress bool `yaml:"use-sso-address"` AllowTempSession bool `yaml:"allow-temp-session"` SignServer string `yaml:"sign-server"` + SignServerBearer string `yaml:"sign-server-bearer"` } // Config 总配置文件 diff --git a/modules/config/default_config.yml b/modules/config/default_config.yml index 6873052f8..06d9b995b 100644 --- a/modules/config/default_config.yml +++ b/modules/config/default_config.yml @@ -24,6 +24,9 @@ account: # 账号相关 # sign-server: 'https://signserver.example.com' # 线上签名服务器 # 服务器可使用docker在本地搭建或者使用他人开放的服务 sign-server: '-' + # 签名服务器认证 Bearer Token + # 使用开放的服务可能需要提供此 Token 进行认证 + sign-server-bearer: '-' heartbeat: # 心跳频率, 单位秒 From 9d19e02fc1c5eee1d38795bdfcd4e7b0d19cc02c Mon Sep 17 00:00:00 2001 From: Bluefissure Date: Tue, 27 Jun 2023 19:32:59 +0000 Subject: [PATCH 2/4] fix: golint --- cmd/gocq/login.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gocq/login.go b/cmd/gocq/login.go index d178ebc9f..c7b51615c 100644 --- a/cmd/gocq/login.go +++ b/cmd/gocq/login.go @@ -266,7 +266,7 @@ func fetchCaptcha(id string) string { return "" } -func energy(uin uint64, id string, appVersion string, salt []byte) ([]byte, error) { +func energy(_ uint64, id string, appVersion string, salt []byte) ([]byte, error) { signServer := base.SignServer if !strings.HasSuffix(signServer, "/") { signServer += "/" From cf7ff4dcc0587a5580b32be4b54df2916e73590e Mon Sep 17 00:00:00 2001 From: Bluefissure Date: Tue, 27 Jun 2023 19:41:46 +0000 Subject: [PATCH 3/4] fix: golint --- cmd/gocq/login.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gocq/login.go b/cmd/gocq/login.go index c7b51615c..daca6729b 100644 --- a/cmd/gocq/login.go +++ b/cmd/gocq/login.go @@ -266,7 +266,7 @@ func fetchCaptcha(id string) string { return "" } -func energy(_ uint64, id string, appVersion string, salt []byte) ([]byte, error) { +func energy(_ uint64, id string, _ string, salt []byte) ([]byte, error) { signServer := base.SignServer if !strings.HasSuffix(signServer, "/") { signServer += "/" From ce1225c0ddd01f85ba6c2a0d6b1c1702f7fdf6c7 Mon Sep 17 00:00:00 2001 From: Bluefissure Date: Tue, 27 Jun 2023 19:45:29 +0000 Subject: [PATCH 4/4] fix: remove trimprefix --- cmd/gocq/login.go | 4 ++-- cmd/gocq/main.go | 2 +- internal/base/flag.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/gocq/login.go b/cmd/gocq/login.go index daca6729b..42c50e31c 100644 --- a/cmd/gocq/login.go +++ b/cmd/gocq/login.go @@ -274,7 +274,7 @@ func energy(_ uint64, id string, _ string, salt []byte) ([]byte, error) { headers := make(map[string]string) signServerBearer := base.SignServerBearer if signServerBearer != "-" && signServerBearer != "" { - headers["Authorization"] = "Bearer " + strings.TrimPrefix(signServerBearer, "Bearer ") + headers["Authorization"] = "Bearer " + signServerBearer } response, err := download.Request{ Method: http.MethodGet, @@ -305,7 +305,7 @@ func sign(seq uint64, uin string, cmd string, qua string, buff []byte) (sign []b headers := map[string]string{"Content-Type": "application/x-www-form-urlencoded"} signServerBearer := base.SignServerBearer if signServerBearer != "-" && signServerBearer != "" { - headers["Authorization"] = "Bearer " + strings.TrimPrefix(signServerBearer, "Bearer ") + headers["Authorization"] = "Bearer " + signServerBearer } response, err := download.Request{ Method: http.MethodPost, diff --git a/cmd/gocq/main.go b/cmd/gocq/main.go index 0ba322c39..63323c2ec 100644 --- a/cmd/gocq/main.go +++ b/cmd/gocq/main.go @@ -167,7 +167,7 @@ func LoginInteract() { if base.SignServer != "-" && base.SignServer != "" { log.Infof("使用服务器 %s 进行数据包签名", base.SignServer) if base.SignServerBearer != "-" && base.SignServerBearer != "" { - log.Infof("使用 Bearer %s 认证服务器 %s ", base.SignServerBearer, base.SignServer) + log.Infof("使用 Bearer %s 认证签名服务器 %s ", base.SignServerBearer, base.SignServer) } wrapper.DandelionEnergy = energy wrapper.FekitGetSign = sign diff --git a/internal/base/flag.go b/internal/base/flag.go index 814e4003e..9e304649e 100644 --- a/internal/base/flag.go +++ b/internal/base/flag.go @@ -39,7 +39,7 @@ var ( AllowTempSession bool // 是否允许发送临时会话信息 UpdateProtocol bool // 是否更新协议 SignServer string // 使用特定的服务器进行签名 - SignServerBearer string // 签名服务器的 Bearer 认证 Token + SignServerBearer string // 认证签名服务器的 Bearer Token HTTPTimeout int PostFormat string // 上报格式 string or array