Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Bearer authentication to sign server requests #2247

Merged
merged 5 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions cmd/gocq/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"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/base"
"github.com/Mrs4s/go-cqhttp/internal/download"
Expand Down Expand Up @@ -269,13 +271,19 @@ func fetchCaptcha(id string) string {
return ""
}

func energy(uin uint64, id string, _ string, salt []byte) ([]byte, error) {
func energy(_ uint64, id string, _ string, salt []byte) ([]byte, error) {
signServer := base.SignServer
if !strings.HasSuffix(signServer, "/") {
signServer += "/"
}
headers := make(map[string]string)
signServerBearer := base.SignServerBearer
if signServerBearer != "-" && signServerBearer != "" {
headers["Authorization"] = "Bearer " + signServerBearer
}
req := download.Request{
Method: http.MethodGet,
Header: headers,
URL: signServer + "custom_energy" + fmt.Sprintf("?data=%v&salt=%v&uin=%v&android_id=%v&guid=%v",
id, hex.EncodeToString(salt), uin, utils.B2S(device.AndroidId), hex.EncodeToString(device.Guid)),
}.WithTimeout(time.Duration(base.SignServerTimeout) * time.Second)
Expand Down Expand Up @@ -337,10 +345,15 @@ func signRequset(seq uint64, uin string, cmd string, qua string, buff []byte) (s
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 " + signServerBearer
}
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&android_id=%v&guid=%v",
uin, qua, cmd, seq, hex.EncodeToString(buff), utils.B2S(device.AndroidId), hex.EncodeToString(device.Guid)))),
}.WithTimeout(time.Duration(base.SignServerTimeout) * time.Second).Bytes()
Expand Down
3 changes: 3 additions & 0 deletions cmd/gocq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
// 等待签名服务器直到连接成功
if !signWaitServer() {
log.Fatalf("连接签名服务器失败")
Expand Down
2 changes: 2 additions & 0 deletions internal/base/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
AllowTempSession bool // 是否允许发送临时会话信息
UpdateProtocol bool // 是否更新协议
SignServer string // 使用特定的服务器进行签名
SignServerBearer string // 认证签名服务器的 Bearer Token
Key string // 签名服务器密钥
IsBelow110 bool // 签名服务器版本是否低于1.1.0及以下
HTTPTimeout int // download 超时时间
Expand Down Expand Up @@ -92,6 +93,7 @@ func Init() {
UseSSOAddress = conf.Account.UseSSOAddress
AllowTempSession = conf.Account.AllowTempSession
SignServer = conf.Account.SignServer
SignServerBearer = conf.Account.SignServerBearer
Key = conf.Account.Key
IsBelow110 = conf.Account.IsBelow110
HTTPTimeout = conf.Message.HTTPTimeout
Expand Down
1 change: 1 addition & 0 deletions modules/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Key string `yaml:"key"`
IsBelow110 bool `yaml:"is-below-110"`
AutoRegister bool `yaml:"auto-register"`
Expand Down
3 changes: 3 additions & 0 deletions modules/config/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ account: # 账号相关
# sign-server: 'https://signserver.example.com' # 线上签名服务器
# 服务器可使用docker在本地搭建或者使用他人开放的服务
sign-server: '-'
# 签名服务器认证 Bearer Token
# 使用开放的服务可能需要提供此 Token 进行认证
sign-server-bearer: '-'
# 如果签名服务器的版本在1.1.0及以下, 请将下面的参数改成true
is-below-110: false
# 签名服务器所需要的apikey, 如果签名服务器的版本在1.1.0及以下则此项无效
Expand Down