This commit is contained in:
2026-04-21 22:36:48 +08:00
commit 488c695fdf
748 changed files with 266838 additions and 0 deletions

View File

@@ -0,0 +1,160 @@
package westdex
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
)
const (
KEY_SIZE = 16 // AES-128, 16 bytes
)
// Encrypt encrypts the given data using AES encryption in ECB mode with PKCS5 padding
func Encrypt(data, secretKey string) (string, error) {
key := generateAESKey(KEY_SIZE*8, []byte(secretKey))
ciphertext, err := aesEncrypt([]byte(data), key)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(ciphertext), nil
}
// Decrypt decrypts the given base64-encoded string using AES encryption in ECB mode with PKCS5 padding
func Decrypt(encodedData, secretKey string) ([]byte, error) {
ciphertext, err := base64.StdEncoding.DecodeString(encodedData)
if err != nil {
return nil, err
}
key := generateAESKey(KEY_SIZE*8, []byte(secretKey))
plaintext, err := aesDecrypt(ciphertext, key)
if err != nil {
return nil, err
}
return plaintext, nil
}
// generateAESKey generates a key for AES encryption using a SHA-1 based PRNG
func generateAESKey(length int, password []byte) []byte {
h := sha1.New()
h.Write(password)
state := h.Sum(nil)
keyBytes := make([]byte, 0, length/8)
for len(keyBytes) < length/8 {
h := sha1.New()
h.Write(state)
state = h.Sum(nil)
keyBytes = append(keyBytes, state...)
}
return keyBytes[:length/8]
}
// aesEncrypt encrypts plaintext using AES in ECB mode with PKCS5 padding
func aesEncrypt(plaintext, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
paddedPlaintext := pkcs5Padding(plaintext, block.BlockSize())
ciphertext := make([]byte, len(paddedPlaintext))
mode := newECBEncrypter(block)
mode.CryptBlocks(ciphertext, paddedPlaintext)
return ciphertext, nil
}
// aesDecrypt decrypts ciphertext using AES in ECB mode with PKCS5 padding
func aesDecrypt(ciphertext, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
plaintext := make([]byte, len(ciphertext))
mode := newECBDecrypter(block)
mode.CryptBlocks(plaintext, ciphertext)
return pkcs5Unpadding(plaintext), nil
}
// pkcs5Padding pads the input to a multiple of the block size using PKCS5 padding
func pkcs5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
// pkcs5Unpadding removes PKCS5 padding from the input
func pkcs5Unpadding(src []byte) []byte {
length := len(src)
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}
// ECB mode encryption/decryption
type ecb struct {
b cipher.Block
blockSize int
}
func newECB(b cipher.Block) *ecb {
return &ecb{
b: b,
blockSize: b.BlockSize(),
}
}
type ecbEncrypter ecb
func newECBEncrypter(b cipher.Block) cipher.BlockMode {
return (*ecbEncrypter)(newECB(b))
}
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("crypto/cipher: output smaller than input")
}
for len(src) > 0 {
x.b.Encrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}
type ecbDecrypter ecb
func newECBDecrypter(b cipher.Block) cipher.BlockMode {
return (*ecbDecrypter)(newECB(b))
}
func (x *ecbDecrypter) BlockSize() int { return x.blockSize }
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
if len(src)%x.blockSize != 0 {
panic("crypto/cipher: input not full blocks")
}
if len(dst) < len(src) {
panic("crypto/cipher: output smaller than input")
}
for len(src) > 0 {
x.b.Decrypt(dst, src[:x.blockSize])
src = src[x.blockSize:]
dst = dst[x.blockSize:]
}
}
// Md5Encrypt 用于对传入的message进行MD5加密
func Md5Encrypt(message string) string {
hash := md5.New()
hash.Write([]byte(message)) // 将字符串转换为字节切片并写入
return hex.EncodeToString(hash.Sum(nil)) // 将哈希值转换为16进制字符串并返回
}

View File

@@ -0,0 +1,63 @@
package westdex
import (
"hyapi-server/internal/config"
"hyapi-server/internal/shared/external_logger"
)
// NewWestDexServiceWithConfig 使用配置创建西部数据服务
func NewWestDexServiceWithConfig(cfg *config.Config) (*WestDexService, error) {
// 将配置类型转换为通用外部服务日志配置
loggingConfig := external_logger.ExternalServiceLoggingConfig{
Enabled: cfg.WestDex.Logging.Enabled,
LogDir: cfg.WestDex.Logging.LogDir,
ServiceName: "westdex",
UseDaily: cfg.WestDex.Logging.UseDaily,
EnableLevelSeparation: cfg.WestDex.Logging.EnableLevelSeparation,
LevelConfigs: make(map[string]external_logger.ExternalServiceLevelFileConfig),
}
// 转换级别配置
for key, value := range cfg.WestDex.Logging.LevelConfigs {
loggingConfig.LevelConfigs[key] = external_logger.ExternalServiceLevelFileConfig{
MaxSize: value.MaxSize,
MaxBackups: value.MaxBackups,
MaxAge: value.MaxAge,
Compress: value.Compress,
}
}
// 创建通用外部服务日志器
logger, err := external_logger.NewExternalServiceLogger(loggingConfig)
if err != nil {
return nil, err
}
// 创建西部数据服务
service := NewWestDexService(
cfg.WestDex.URL,
cfg.WestDex.Key,
cfg.WestDex.SecretID,
cfg.WestDex.SecretSecondID,
logger,
)
return service, nil
}
// NewWestDexServiceWithLogging 使用自定义日志配置创建西部数据服务
func NewWestDexServiceWithLogging(url, key, secretID, secretSecondID string, loggingConfig external_logger.ExternalServiceLoggingConfig) (*WestDexService, error) {
// 设置服务名称
loggingConfig.ServiceName = "westdex"
// 创建通用外部服务日志器
logger, err := external_logger.NewExternalServiceLogger(loggingConfig)
if err != nil {
return nil, err
}
// 创建西部数据服务
service := NewWestDexService(url, key, secretID, secretSecondID, logger)
return service, nil
}

View File

@@ -0,0 +1,418 @@
package westdex
import (
"bytes"
"context"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"time"
"hyapi-server/internal/shared/crypto"
"hyapi-server/internal/shared/external_logger"
)
var (
ErrDatasource = errors.New("数据源异常")
ErrSystem = errors.New("系统异常")
ErrNotFound = errors.New("查询为空")
)
type WestResp struct {
Message string `json:"message"`
Code string `json:"code"`
Data string `json:"data"`
ID string `json:"id"`
ErrorCode *int `json:"error_code"`
Reason string `json:"reason"`
}
type G05HZ01WestResp struct {
Message string `json:"message"`
Code string `json:"code"`
Data json.RawMessage `json:"data"`
ID string `json:"id"`
ErrorCode *int `json:"error_code"`
Reason string `json:"reason"`
}
type WestConfig struct {
Url string
Key string
SecretID string
SecretSecondID string
}
type WestDexService struct {
config WestConfig
logger *external_logger.ExternalServiceLogger
}
// NewWestDexService 是一个构造函数,用于初始化 WestDexService
func NewWestDexService(url, key, secretID, secretSecondID string, logger *external_logger.ExternalServiceLogger) *WestDexService {
return &WestDexService{
config: WestConfig{
Url: url,
Key: key,
SecretID: secretID,
SecretSecondID: secretSecondID,
},
logger: logger,
}
}
// generateRequestID 生成请求ID
func (w *WestDexService) generateRequestID() string {
timestamp := time.Now().UnixNano()
hash := md5.Sum([]byte(fmt.Sprintf("%d_%s", timestamp, w.config.Key)))
return fmt.Sprintf("westdex_%x", hash[:8])
}
// buildRequestURL 构建请求URL
func (w *WestDexService) buildRequestURL(code string) string {
timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
return fmt.Sprintf("%s/%s/%s?timestamp=%s", w.config.Url, w.config.SecretID, code, timestamp)
}
// CallAPI 调用西部数据的 API
func (w *WestDexService) CallAPI(ctx context.Context, code string, reqData map[string]interface{}) (resp []byte, err error) {
startTime := time.Now()
requestID := w.generateRequestID()
// 从ctx中获取transactionId
var transactionID string
if ctxTransactionID, ok := ctx.Value("transaction_id").(string); ok {
transactionID = ctxTransactionID
}
// 构建请求URL
reqUrl := w.buildRequestURL(code)
// 记录请求日志
if w.logger != nil {
w.logger.LogRequest(requestID, transactionID, code, reqUrl)
}
jsonData, marshalErr := json.Marshal(reqData)
if marshalErr != nil {
err = errors.Join(ErrSystem, marshalErr)
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
}
return nil, err
}
// 创建HTTP POST请求
req, newRequestErr := http.NewRequestWithContext(ctx, "POST", reqUrl, bytes.NewBuffer(jsonData))
if newRequestErr != nil {
err = errors.Join(ErrSystem, newRequestErr)
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
}
return nil, err
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
// 发送请求超时时间设置为60秒
client := &http.Client{
Timeout: 60 * time.Second,
}
httpResp, clientDoErr := client.Do(req)
if clientDoErr != nil {
// 检查是否是超时错误
isTimeout := false
if ctx.Err() == context.DeadlineExceeded {
isTimeout = true
} else if netErr, ok := clientDoErr.(interface{ Timeout() bool }); ok && netErr.Timeout() {
isTimeout = true
} else if errStr := clientDoErr.Error(); errStr == "context deadline exceeded" ||
errStr == "timeout" ||
errStr == "Client.Timeout exceeded" ||
errStr == "net/http: request canceled" {
isTimeout = true
}
if isTimeout {
err = errors.Join(ErrDatasource, fmt.Errorf("API请求超时: %v", clientDoErr))
} else {
err = errors.Join(ErrSystem, clientDoErr)
}
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
}
return nil, err
}
defer func(Body io.ReadCloser) {
closeErr := Body.Close()
if closeErr != nil {
// 记录关闭错误
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, errors.Join(ErrSystem, fmt.Errorf("关闭响应体失败: %w", closeErr)), reqData)
}
}
}(httpResp.Body)
// 计算请求耗时
duration := time.Since(startTime)
// 检查请求是否成功
if httpResp.StatusCode == 200 {
// 读取响应体
bodyBytes, ReadErr := io.ReadAll(httpResp.Body)
if ReadErr != nil {
err = errors.Join(ErrSystem, ReadErr)
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
}
return nil, err
}
// 手动调用 json.Unmarshal 触发自定义的 UnmarshalJSON 方法
var westDexResp WestResp
UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp)
if UnmarshalErr != nil {
err = errors.Join(ErrSystem, UnmarshalErr)
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
}
return nil, err
}
// 记录响应日志(不记录具体响应数据)
if w.logger != nil {
w.logger.LogResponseWithID(requestID, transactionID, code, httpResp.StatusCode, duration, westDexResp.ID)
}
if westDexResp.Code != "00000" && westDexResp.Code != "200" && westDexResp.Code != "0" {
if westDexResp.Data == "" {
err = errors.Join(ErrSystem, fmt.Errorf(westDexResp.Message))
if w.logger != nil {
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
}
return nil, err
}
decryptedData, DecryptErr := crypto.WestDexDecrypt(westDexResp.Data, w.config.Key)
if DecryptErr != nil {
err = errors.Join(ErrSystem, DecryptErr)
if w.logger != nil {
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
}
return nil, err
}
// 记录业务错误日志包含响应ID
if w.logger != nil {
w.logger.LogErrorWithResponseID(requestID, transactionID, code, errors.Join(ErrDatasource, fmt.Errorf(westDexResp.Message)), reqData, westDexResp.ID)
}
// 记录性能日志(失败)
// 注意:通用日志系统不包含性能日志功能
return decryptedData, errors.Join(ErrDatasource, fmt.Errorf(westDexResp.Message))
}
if westDexResp.Data == "" {
err = errors.Join(ErrSystem, fmt.Errorf(westDexResp.Message))
if w.logger != nil {
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
}
return nil, err
}
decryptedData, DecryptErr := crypto.WestDexDecrypt(westDexResp.Data, w.config.Key)
if DecryptErr != nil {
err = errors.Join(ErrSystem, DecryptErr)
if w.logger != nil {
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
}
return nil, err
}
// 记录性能日志(成功)
// 注意:通用日志系统不包含性能日志功能
return decryptedData, nil
}
// 记录HTTP错误
err = errors.Join(ErrSystem, fmt.Errorf("西部请求失败Code: %d", httpResp.StatusCode))
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
// 注意:通用日志系统不包含性能日志功能
}
return nil, err
}
// G05HZ01CallAPI 调用西部数据的 G05HZ01 API
func (w *WestDexService) G05HZ01CallAPI(ctx context.Context, code string, reqData map[string]interface{}) (resp []byte, err error) {
startTime := time.Now()
requestID := w.generateRequestID()
// 从ctx中获取transactionId
var transactionID string
if ctxTransactionID, ok := ctx.Value("transaction_id").(string); ok {
transactionID = ctxTransactionID
}
// 构建请求URL
reqUrl := fmt.Sprintf("%s/%s/%s?timestamp=%d", w.config.Url, w.config.SecretSecondID, code, time.Now().UnixNano()/int64(time.Millisecond))
// 记录请求日志
if w.logger != nil {
w.logger.LogRequest(requestID, transactionID, code, reqUrl)
}
jsonData, marshalErr := json.Marshal(reqData)
if marshalErr != nil {
err = errors.Join(ErrSystem, marshalErr)
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
}
return nil, err
}
// 创建HTTP POST请求
req, newRequestErr := http.NewRequestWithContext(ctx, "POST", reqUrl, bytes.NewBuffer(jsonData))
if newRequestErr != nil {
err = errors.Join(ErrSystem, newRequestErr)
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
}
return nil, err
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
// 发送请求超时时间设置为60秒
client := &http.Client{
Timeout: 60 * time.Second,
}
httpResp, clientDoErr := client.Do(req)
if clientDoErr != nil {
// 检查是否是超时错误
isTimeout := false
if ctx.Err() == context.DeadlineExceeded {
isTimeout = true
} else if netErr, ok := clientDoErr.(interface{ Timeout() bool }); ok && netErr.Timeout() {
isTimeout = true
} else if errStr := clientDoErr.Error(); errStr == "context deadline exceeded" ||
errStr == "timeout" ||
errStr == "Client.Timeout exceeded" ||
errStr == "net/http: request canceled" {
isTimeout = true
}
if isTimeout {
err = errors.Join(ErrDatasource, fmt.Errorf("API请求超时: %v", clientDoErr))
} else {
err = errors.Join(ErrSystem, clientDoErr)
}
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
}
return nil, err
}
defer func(Body io.ReadCloser) {
closeErr := Body.Close()
if closeErr != nil {
// 记录关闭错误
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, errors.Join(ErrSystem, fmt.Errorf("关闭响应体失败: %w", closeErr)), reqData)
}
}
}(httpResp.Body)
// 计算请求耗时
duration := time.Since(startTime)
if httpResp.StatusCode == 200 {
bodyBytes, ReadErr := io.ReadAll(httpResp.Body)
if ReadErr != nil {
err = errors.Join(ErrSystem, ReadErr)
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
}
return nil, err
}
var westDexResp G05HZ01WestResp
UnmarshalErr := json.Unmarshal(bodyBytes, &westDexResp)
if UnmarshalErr != nil {
err = errors.Join(ErrSystem, UnmarshalErr)
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
}
return nil, err
}
// 记录响应日志(不记录具体响应数据)
if w.logger != nil {
w.logger.LogResponseWithID(requestID, transactionID, code, httpResp.StatusCode, duration, westDexResp.ID)
}
if westDexResp.Code != "0000" {
if westDexResp.Data == nil || westDexResp.Code == "1404" {
err = errors.Join(ErrNotFound, fmt.Errorf(westDexResp.Message))
if w.logger != nil {
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
}
return nil, err
} else {
// 记录业务错误日志包含响应ID
if w.logger != nil {
w.logger.LogErrorWithResponseID(requestID, transactionID, code, errors.Join(ErrSystem, fmt.Errorf(string(westDexResp.Data))), reqData, westDexResp.ID)
}
// 记录性能日志(失败)
// 注意:通用日志系统不包含性能日志功能
return westDexResp.Data, errors.Join(ErrSystem, fmt.Errorf(string(westDexResp.Data)))
}
}
if westDexResp.Data == nil {
err = errors.Join(ErrSystem, fmt.Errorf(westDexResp.Message))
if w.logger != nil {
w.logger.LogErrorWithResponseID(requestID, transactionID, code, err, reqData, westDexResp.ID)
}
return nil, err
}
// 记录性能日志(成功)
// 注意:通用日志系统不包含性能日志功能
return westDexResp.Data, nil
} else {
// 记录HTTP错误
err = errors.Join(ErrSystem, fmt.Errorf("西部请求失败Code: %d", httpResp.StatusCode))
if w.logger != nil {
w.logger.LogError(requestID, transactionID, code, err, reqData)
// 注意:通用日志系统不包含性能日志功能
}
return nil, err
}
}
func (w *WestDexService) Encrypt(data string) (string, error) {
encryptedValue, err := crypto.WestDexEncrypt(data, w.config.Key)
if err != nil {
return "", ErrSystem
}
return encryptedValue, nil
}
func (w *WestDexService) Md5Encrypt(data string) string {
result := Md5Encrypt(data)
return result
}
func (w *WestDexService) GetConfig() WestConfig {
return w.config
}