96b1edff45
iotlcm-upstream-chan:运营商上游通道适配服务(统一 /sim/* 协议)。 - 内联原私有依赖:yunze-public → internal/pub/(10 包 18 文件), yunze-pb/pb → internal/pb/(9 文件);Consul 注册改用官方 gogf/gf/contrib/registry/consul/v2(API 兼容) - Go module:yz-svr → git.5iot.com/iotlcm-public/iotlcm-upstream-chan - 修复 mingqian 包测试:实现重构后 `adapter.operations` 字段已移除, 测试断言按当前实现语义重写(不改产品行为) - 凭据与内部标识脱敏:MySQL/Redis/Consul 凭据、私网与生产 IP、 内网域名、真实手机号、注释里的真实 token/transid - 移除内部资产:CI 触发器、生产通道种子 SQL、真实凭据测试资源文档 - Dockerfile 改用公共基础镜像,移除写死的 registry 凭据 - 补 Apache-2.0 LICENSE,重写 README,LF 行尾 + .gitattributes
135 lines
4.3 KiB
Go
135 lines
4.3 KiB
Go
package upstreamimpl
|
|
|
|
import (
|
|
"context"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// henanXinjingClient is the direct HTTP client for the Henan Xinjing SIM
|
|
// platform. Authentication is userId+apikey+times+sign where
|
|
// sign = UPPERCASE(MD5("userId=<id>&apikey=<key>×=<unix_seconds>")).
|
|
type henanXinjingClient struct {
|
|
baseURL string
|
|
userID string
|
|
apiKey string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
// henanXinjingResponse matches the platform's standard envelope.
|
|
// Code can be either a JSON number or a JSON string (the platform
|
|
// returns both shapes for the same logical value), so keep it raw.
|
|
type henanXinjingResponse struct {
|
|
Code json.RawMessage `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data json.RawMessage `json:"data"`
|
|
}
|
|
|
|
func newHenanXinjingClient(baseURL, userID, apiKey string, timeout time.Duration) *henanXinjingClient {
|
|
if timeout <= 0 {
|
|
timeout = defaultUpstreamHTTPTimeout
|
|
}
|
|
return &henanXinjingClient{
|
|
baseURL: strings.TrimRight(strings.TrimSpace(baseURL), "/"),
|
|
userID: strings.TrimSpace(userID),
|
|
apiKey: strings.TrimSpace(apiKey),
|
|
httpClient: &http.Client{Timeout: timeout},
|
|
}
|
|
}
|
|
|
|
// buildSign returns UPPERCASE(MD5(...)) over the document-defined form:
|
|
//
|
|
// userId=<id>&apikey=<key>×=<unix_seconds>
|
|
func henanXinjingSign(userID, apiKey, times string) string {
|
|
raw := "userId=" + userID + "&apikey=" + apiKey + "×=" + times
|
|
sum := md5.Sum([]byte(raw))
|
|
return strings.ToUpper(hex.EncodeToString(sum[:]))
|
|
}
|
|
|
|
// decodeCode normalizes the response code. The platform sometimes returns
|
|
// `"code":0` and sometimes `"code":"-2"`. Both are valid; we treat any
|
|
// numeric zero (in either form) as success.
|
|
func henanXinjingDecodeCode(raw json.RawMessage) (int, error) {
|
|
if len(raw) == 0 {
|
|
return -1, fmt.Errorf("missing code field")
|
|
}
|
|
var n int
|
|
if err := json.Unmarshal(raw, &n); err == nil {
|
|
return n, nil
|
|
}
|
|
var s string
|
|
if err := json.Unmarshal(raw, &s); err != nil {
|
|
return -1, fmt.Errorf("code is neither number nor string: %s", string(raw))
|
|
}
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
return -1, fmt.Errorf("empty code string")
|
|
}
|
|
v, err := strconv.Atoi(s)
|
|
if err != nil {
|
|
return -1, fmt.Errorf("invalid code string %q: %w", s, err)
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// get performs a signed GET request and unmarshals the `data` field into out
|
|
// when out is non-nil. Business-layer errors (code != 0) become errors.
|
|
func (c *henanXinjingClient) get(ctx context.Context, path string, params map[string]string, out any) error {
|
|
if c == nil || c.baseURL == "" || c.userID == "" || c.apiKey == "" {
|
|
return fmt.Errorf("henan xinjing: baseURL, userID and apiKey are required")
|
|
}
|
|
values := url.Values{}
|
|
for k, v := range params {
|
|
if strings.TrimSpace(v) != "" {
|
|
values.Set(k, v)
|
|
}
|
|
}
|
|
times := strconv.FormatInt(time.Now().Unix(), 10)
|
|
values.Set("userId", c.userID)
|
|
values.Set("times", times)
|
|
values.Set("sign", henanXinjingSign(c.userID, c.apiKey, times))
|
|
|
|
endpoint := c.baseURL + "/" + strings.TrimLeft(path, "/") + "?" + values.Encode()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("henan xinjing: build request %s: %w", path, err)
|
|
}
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("henan xinjing: request %s: %w", path, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return fmt.Errorf("henan xinjing: read body %s: %w", path, err)
|
|
}
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
return fmt.Errorf("henan xinjing: HTTP %d from %s: %s", resp.StatusCode, path, string(body))
|
|
}
|
|
var envelope henanXinjingResponse
|
|
if err = json.Unmarshal(body, &envelope); err != nil {
|
|
return fmt.Errorf("henan xinjing: decode envelope %s: %w (body=%s)", path, err, string(body))
|
|
}
|
|
code, err := henanXinjingDecodeCode(envelope.Code)
|
|
if err != nil {
|
|
return fmt.Errorf("henan xinjing: %s: %w (body=%s)", path, err, string(body))
|
|
}
|
|
if code != 0 {
|
|
return fmt.Errorf("henan xinjing: %s failed: code=%d msg=%s", path, code, envelope.Msg)
|
|
}
|
|
if out != nil && len(envelope.Data) > 0 && string(envelope.Data) != "null" {
|
|
if err = json.Unmarshal(envelope.Data, out); err != nil {
|
|
return fmt.Errorf("henan xinjing: decode data %s: %w (data=%s)", path, err, string(envelope.Data))
|
|
}
|
|
}
|
|
return nil
|
|
}
|