chore: 初始开源发布

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
This commit is contained in:
MiFaZhan
2026-10-22 15:42:29 +08:00
commit 96b1edff45
191 changed files with 30805 additions and 0 deletions
@@ -0,0 +1,153 @@
package upstreamimpl
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const xuzhouMobileTokenPath = "/admin/api/get/token"
// XuzhouMobileTokenConfig contains the public provider endpoint and the
// carrier type configured by the template. AppID is the per-channel
// credential and is deliberately not read from the template.
type XuzhouMobileTokenConfig struct {
BaseURL string
Path string
AppID string
CarrierType int
TTL time.Duration
Timeout time.Duration
}
// XuzhouMobileTokenProvider fetches the shared OneLink token issued by the
// Xuzhou Mobile provider.
type XuzhouMobileTokenProvider struct {
baseURL string
path string
appID string
carrierType int
ttl time.Duration
client *http.Client
}
type xuzhouMobileTokenResponse struct {
Success bool `json:"success"`
ErrorCode json.RawMessage `json:"errorCode"`
Body struct {
Token string `json:"token"`
} `json:"body"`
}
func parseXuzhouErrorCode(raw json.RawMessage) (int, error) {
var code int
if err := json.Unmarshal(raw, &code); err == nil {
return code, nil
}
var value string
if err := json.Unmarshal(raw, &value); err != nil {
return 0, err
}
return strconv.Atoi(strings.TrimSpace(value))
}
func NewXuzhouMobileTokenProvider(config XuzhouMobileTokenConfig) *XuzhouMobileTokenProvider {
timeout := config.Timeout
if timeout <= 0 {
timeout = defaultUpstreamHTTPTimeout
}
return &XuzhouMobileTokenProvider{
baseURL: strings.TrimRight(strings.TrimSpace(config.BaseURL), "/"),
path: normalizeProviderPath(config.Path, xuzhouMobileTokenPath),
appID: strings.TrimSpace(config.AppID),
carrierType: config.CarrierType,
ttl: config.TTL,
client: &http.Client{Timeout: timeout},
}
}
func (p *XuzhouMobileTokenProvider) Fetch(ctx context.Context) (OneLinkToken, error) {
if p == nil || p.client == nil {
return OneLinkToken{}, fmt.Errorf("xuzhou mobile token provider is not initialized")
}
if p.baseURL == "" {
return OneLinkToken{}, fmt.Errorf("xuzhou mobile token base URL is required")
}
if p.appID == "" {
return OneLinkToken{}, fmt.Errorf("xuzhou mobile appId is required")
}
endpoint, err := url.Parse(joinProviderURL(p.baseURL, p.path))
if err != nil {
return OneLinkToken{}, fmt.Errorf("parse Xuzhou Mobile token URL: %w", err)
}
query := endpoint.Query()
query.Set("appid", p.appID)
query.Set("carrierType", strconv.Itoa(p.carrierType))
endpoint.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return OneLinkToken{}, fmt.Errorf("create Xuzhou Mobile token request: %w", err)
}
req.Header.Set("Accept", "application/json")
resp, err := p.client.Do(req)
if err != nil {
return OneLinkToken{}, fmt.Errorf("request Xuzhou Mobile token: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return OneLinkToken{}, fmt.Errorf("read Xuzhou Mobile token response: %w", err)
}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return OneLinkToken{}, fmt.Errorf("Xuzhou Mobile token HTTP status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
}
var output xuzhouMobileTokenResponse
if err := json.Unmarshal(body, &output); err != nil {
return OneLinkToken{}, fmt.Errorf("decode Xuzhou Mobile token response: %w", err)
}
errorCode, err := parseXuzhouErrorCode(output.ErrorCode)
if err != nil {
return OneLinkToken{}, fmt.Errorf("decode Xuzhou Mobile errorCode: %w", err)
}
if !output.Success || errorCode != 0 {
return OneLinkToken{}, fmt.Errorf("Xuzhou Mobile token failed: success=%t, errorCode=%d", output.Success, errorCode)
}
value := strings.TrimSpace(output.Body.Token)
if value == "" {
return OneLinkToken{}, fmt.Errorf("Xuzhou Mobile shared token is empty")
}
now := time.Now()
expiresAt := time.Time{}
if p.ttl > 0 {
expiresAt = now.Add(p.ttl)
}
return OneLinkToken{Value: value, CreatedAt: now, ExpiresAt: expiresAt}, nil
}
func (p *XuzhouMobileTokenProvider) FetchToken(ctx context.Context, _ string) (OneLinkToken, error) {
return p.Fetch(ctx)
}
func normalizeProviderPath(path, fallback string) string {
path = strings.TrimSpace(path)
if path == "" {
path = fallback
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return path
}
func joinProviderURL(baseURL, path string) string {
return strings.TrimRight(strings.TrimSpace(baseURL), "/") + normalizeProviderPath(path, "/")
}