Skip to content

Commit

Permalink
Merge 68796e3 into f06bf84
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoshinonyaruko authored Nov 22, 2024
2 parents f06bf84 + 68796e3 commit bab6252
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 35 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func main() {
}
}

webhookHandler := server.NewWebhookHandler(100)
webhookHandler := server.NewWebhookHandler(5000)

// 启动消息处理协程
go webhookHandler.ListenAndProcessMessages()
Expand All @@ -458,7 +458,8 @@ func main() {
r.POST("/uploadpicv3", server.UploadBase64ImageHandlerV3(rateLimiter, api))
r.POST("/uploadrecord", server.UploadBase64RecordHandler(rateLimiter))
// 使用 CreateHandleValidation,传入 WebhookHandler 实例
r.POST("/"+conf.Settings.WebhookPath, server.CreateHandleValidation(conf.Settings.ClientSecret, webhookHandler))
server.InitPrivateKey(conf.Settings.ClientSecret)
r.POST("/"+conf.Settings.WebhookPath, server.CreateHandleValidation(webhookHandler))
r.Static("/channel_temp", "./channel_temp")
if config.GetFrpPort() == "0" && !config.GetDisableWebui() {
//webui和它的api
Expand Down
90 changes: 57 additions & 33 deletions server/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,26 @@ func NewWebhookHandler(queueSize int) *WebhookHandler {
}
}

// 在启动时生成私钥
var privateKey ed25519.PrivateKey

func InitPrivateKey(botSecret string) {
seed := botSecret
for len(seed) < ed25519.SeedSize {
seed = strings.Repeat(seed, 2)
}
seed = seed[:ed25519.SeedSize]
reader := strings.NewReader(seed)

_, key, err := ed25519.GenerateKey(reader)
if err != nil {
log.Fatalf("Failed to generate ed25519 private key: %v", err)
}
privateKey = key
}

// CreateHandleValidation 创建用于签名验证和消息入队的处理函数
func CreateHandleValidation(botSecret string, wh *WebhookHandler) gin.HandlerFunc {
func CreateHandleValidation(wh *WebhookHandler) gin.HandlerFunc {
return func(c *gin.Context) {
httpBody, err := io.ReadAll(c.Request.Body)
if err != nil {
Expand All @@ -60,46 +78,52 @@ func CreateHandleValidation(botSecret string, wh *WebhookHandler) gin.HandlerFun
return
}

// 解析请求数据
var payload Payload
if err := json.Unmarshal(httpBody, &payload); err != nil {
log.Println("Failed to parse HTTP payload:", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse payload"})
return
}

// 生成种子并创建私钥
seed := botSecret
for len(seed) < ed25519.SeedSize {
seed = strings.Repeat(seed, 2)
// 判断 Op 类型
switch payload.Op {
case 13:
// 签名验证逻辑
var msg bytes.Buffer
msg.WriteString(payload.D.EventTs)
msg.WriteString(payload.D.PlainToken)
signature := hex.EncodeToString(ed25519.Sign(privateKey, msg.Bytes()))

// 返回签名验证响应
c.JSON(http.StatusOK, gin.H{
"plain_token": payload.D.PlainToken,
"signature": signature,
})

default:
// 异步推送消息到队列
go func(httpBody []byte, payload Payload) {
webhookPayload := &WebhookPayload{
PlainToken: payload.D.PlainToken,
EventTs: payload.D.EventTs,
RawMessage: httpBody,
}

// 尝试写入队列
select {
case wh.messageQueue <- webhookPayload:
log.Println("Message enqueued successfully")
default:
log.Println("Message queue is full, dropping message")
}
}(httpBody, payload)

// 返回 HTTP Callback ACK 响应
c.JSON(http.StatusOK, gin.H{
"op": 12,
})
}
seed = seed[:ed25519.SeedSize]
reader := strings.NewReader(seed)
_, privateKey, err := ed25519.GenerateKey(reader)
if err != nil {
log.Println("Failed to generate ed25519 key:", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate ed25519 key"})
return
}

// 拼接消息并生成签名
var msg bytes.Buffer
msg.WriteString(payload.D.EventTs)
msg.WriteString(payload.D.PlainToken)
signature := hex.EncodeToString(ed25519.Sign(privateKey, msg.Bytes()))

// 推送验证成功消息到队列
webhookPayload := &WebhookPayload{
PlainToken: payload.D.PlainToken,
EventTs: payload.D.EventTs,
RawMessage: httpBody,
}
wh.messageQueue <- webhookPayload

// 返回签名验证响应
c.JSON(http.StatusOK, gin.H{
"plain_token": payload.D.PlainToken,
"signature": signature,
})
}
}

Expand Down

0 comments on commit bab6252

Please sign in to comment.