118 lines
3.0 KiB
Go
118 lines
3.0 KiB
Go
package fqapi
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"fanqie/internal/algos"
|
|
"fanqie/internal/common"
|
|
)
|
|
|
|
// CreateMD5 创建MD5哈希
|
|
func CreateMD5(s string) string {
|
|
hasher := md5.New()
|
|
hasher.Write([]byte(s))
|
|
return hex.EncodeToString(hasher.Sum(nil))
|
|
}
|
|
|
|
// GorgonRequest Gorgon请求(使用签名切换器)
|
|
func GorgonRequest(urlStr string, options map[string]interface{}) (*common.ClientResponse, error) {
|
|
headers, ok := options["headers"].(map[string]string)
|
|
if !ok {
|
|
headers = make(map[string]string)
|
|
options["headers"] = headers
|
|
}
|
|
|
|
dataMD5 := ""
|
|
if body, ok := options["body"].(string); ok && body != "" {
|
|
dataMD5 = CreateMD5(body)
|
|
} else if body, ok := options["body"].([]byte); ok && len(body) > 0 {
|
|
dataMD5 = CreateMD5(string(body))
|
|
}
|
|
|
|
if dataMD5 != "" {
|
|
headers["x-ss-stub"] = dataMD5
|
|
}
|
|
|
|
// 使用签名切换器(sign2 或 sign4)生成签名
|
|
signedHeaders := algos.GlobalSignatureSwitcher.Sign(urlStr, headers)
|
|
|
|
// 更新headers
|
|
for k, v := range signedHeaders {
|
|
headers[k] = v
|
|
}
|
|
|
|
// 调用实际的HTTP请求
|
|
client := common.NewHTTPClient()
|
|
return client.Request(urlStr, options)
|
|
}
|
|
|
|
// GorgonRequestSync Gorgon同步请求(使用签名切换器)
|
|
func GorgonRequestSync(urlStr string, options map[string]interface{}) (*common.ClientResponse, error) {
|
|
headers, ok := options["headers"].(map[string]string)
|
|
if !ok {
|
|
headers = make(map[string]string)
|
|
options["headers"] = headers
|
|
}
|
|
|
|
dataMD5 := ""
|
|
if body, ok := options["body"].(string); ok && body != "" {
|
|
dataMD5 = CreateMD5(body)
|
|
} else if body, ok := options["body"].([]byte); ok && len(body) > 0 {
|
|
dataMD5 = CreateMD5(string(body))
|
|
}
|
|
|
|
if dataMD5 != "" {
|
|
headers["x-ss-stub"] = dataMD5
|
|
}
|
|
|
|
// 使用签名切换器(sign2 或 sign4)生成签名
|
|
signedHeaders := algos.GlobalSignatureSwitcher.Sign(urlStr, headers)
|
|
|
|
// 删除x-ss-stub(如果存在)
|
|
if _, exists := signedHeaders["x-ss-stub"]; exists {
|
|
delete(signedHeaders, "x-ss-stub")
|
|
}
|
|
|
|
// 更新headers
|
|
for k, v := range signedHeaders {
|
|
headers[k] = v
|
|
}
|
|
|
|
// 调用实际的HTTP请求
|
|
client := common.NewHTTPClient()
|
|
return client.Request(urlStr, options)
|
|
}
|
|
|
|
// ContentRequest Content请求(硬编码使用Sign2,不受配置影响)
|
|
func ContentRequest(urlStr string, options map[string]interface{}) (*common.ClientResponse, error) {
|
|
headers, ok := options["headers"].(map[string]string)
|
|
if !ok {
|
|
headers = make(map[string]string)
|
|
options["headers"] = headers
|
|
}
|
|
|
|
dataMD5 := ""
|
|
if body, ok := options["body"].(string); ok && body != "" {
|
|
dataMD5 = CreateMD5(body)
|
|
} else if body, ok := options["body"].([]byte); ok && len(body) > 0 {
|
|
dataMD5 = CreateMD5(string(body))
|
|
}
|
|
|
|
if dataMD5 != "" {
|
|
headers["x-ss-stub"] = dataMD5
|
|
}
|
|
|
|
// 硬编码使用 Sign2(不受配置影响)
|
|
signedHeaders := algos.Sign2Wrapper(urlStr, headers)
|
|
|
|
// 更新headers
|
|
for k, v := range signedHeaders {
|
|
headers[k] = v
|
|
}
|
|
|
|
// 调用实际的HTTP请求
|
|
client := common.NewHTTPClient()
|
|
return client.Request(urlStr, options)
|
|
}
|
|
|