From 9b172c35553d45fc30a7282845fd808150b0072e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AC=E6=98=8E?= <83812544+Ed1s0nZ@users.noreply.github.com> Date: Fri, 25 Apr 2025 13:32:18 +0800 Subject: [PATCH] Update tools.go --- tools.go | 93 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 20 deletions(-) diff --git a/tools.go b/tools.go index 2d07076..d8bc0aa 100644 --- a/tools.go +++ b/tools.go @@ -2,9 +2,9 @@ package main import ( "bytes" + "encoding/base64" "encoding/json" "fmt" - "net/url" "regexp" "strings" ) @@ -152,32 +152,22 @@ func generateHTTPRequest(input string) (string, error) { // 构建 Header var headerLines []string for key, values := range req.Header { + if strings.ToLower(key) == "content-length" { + continue // 跳过 Content-Length + } if len(values) > 0 { // 将多个值用逗号分隔 headerLines = append(headerLines, fmt.Sprintf("%s: %s", key, strings.Join(values, ","))) } } - // 特殊处理 Origin 和 Referer - if val, ok := req.Header["Origin"]; ok && len(val) > 0 { - origin := val[0] - parsedOrigin, err := url.Parse(origin) - if err == nil { - headerLines = append(headerLines, fmt.Sprintf("Origin: %s", parsedOrigin.String())) - } + decodedBytes, err := base64.StdEncoding.DecodeString(req.Body) + if err != nil { + fmt.Println("解码错误:", err) } - - if val, ok := req.Header["Referer"]; ok && len(val) > 0 { - referer := val[0] - parsedReferer, err := url.Parse(referer) - if err == nil { - headerLines = append(headerLines, fmt.Sprintf("Referer: %s", parsedReferer.String())) - } - } - // 如果 Body 不为空,计算 Content-Length - if req.Body != "" { - contentLength := len(req.Body) + if string(decodedBytes) != "" { + contentLength := len(string(decodedBytes)) headerLines = append(headerLines, fmt.Sprintf("Content-Length: %d", contentLength)) } @@ -188,9 +178,72 @@ func generateHTTPRequest(input string) (string, error) { buffer.WriteString(line + "\n") } if req.Body != "" { - buffer.WriteString("\n" + req.Body) + buffer.WriteString("\n" + string(decodedBytes)) } else { buffer.WriteString("\n\n") } return buffer.String(), nil } + +// Levenshtein 计算两个字符串的Levenshtein距离 +func Levenshtein(a, b string) int { + lenA := len(a) + lenB := len(b) + if lenA == 0 { + return lenB + } + if lenB == 0 { + return lenA + } + + // 创建距离矩阵 + dist := make([][]int, lenA+1) + for i := range dist { + dist[i] = make([]int, lenB+1) + dist[i][0] = i + } + for j := 0; j <= lenB; j++ { + dist[0][j] = j + } + + // 填充距离矩阵 + for i := 1; i <= lenA; i++ { + for j := 1; j <= lenB; j++ { + cost := 1 + if a[i-1] == b[j-1] { + cost = 0 + } + dist[i][j] = min( + dist[i-1][j]+1, // 删除 + dist[i][j-1]+1, // 插入 + dist[i-1][j-1]+cost, // 替换 + ) + } + } + + return dist[lenA][lenB] +} + +// min 返回三个整数中的最小值 +func min(a, b, c int) int { + if a < b { + if a < c { + return a + } + return c + } + if b < c { + return b + } + return c +} + +// StringSimilarity 计算两个字符串的相似度 (0-1之间) +func StringSimilarity(a, b string) float64 { + distance := Levenshtein(a, b) + maxLength := len(a) + if len(b) > maxLength { + maxLength = len(b) + } + return 1.0 - float64(distance)/float64(maxLength) +}