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
146 lines
4.1 KiB
Go
146 lines
4.1 KiB
Go
package upstreamimpl
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
sharedtoken "git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/token"
|
|
)
|
|
|
|
const ziboOneLinkTokenPath = "/ec/get/token"
|
|
|
|
// ZiboTokenConfig contains the official OneLink token endpoint and the
|
|
// per-channel credentials stored in t_sim_channels.
|
|
type ZiboTokenConfig struct {
|
|
BaseURL string
|
|
Path string
|
|
AppID string
|
|
Password string
|
|
Timeout time.Duration
|
|
}
|
|
|
|
// ZiboTokenProvider fetches a token from the official OneLink endpoint.
|
|
type ZiboTokenProvider struct {
|
|
baseURL string
|
|
path string
|
|
appID string
|
|
password string
|
|
client *http.Client
|
|
sequence atomic.Uint64
|
|
}
|
|
|
|
type ziboTokenResponse struct {
|
|
Status string `json:"status"`
|
|
Message string `json:"message"`
|
|
Result []struct {
|
|
Token string `json:"token"`
|
|
TTL int `json:"ttl"`
|
|
} `json:"result"`
|
|
}
|
|
|
|
type ziboFetcher struct {
|
|
provider *ZiboTokenProvider
|
|
}
|
|
|
|
func NewZiboTokenProvider(config ZiboTokenConfig) *ZiboTokenProvider {
|
|
timeout := config.Timeout
|
|
if timeout <= 0 {
|
|
timeout = defaultUpstreamHTTPTimeout
|
|
}
|
|
return &ZiboTokenProvider{
|
|
baseURL: strings.TrimRight(strings.TrimSpace(config.BaseURL), "/"),
|
|
path: normalizeProviderPath(config.Path, ziboOneLinkTokenPath),
|
|
appID: strings.TrimSpace(config.AppID),
|
|
password: config.Password,
|
|
client: &http.Client{Timeout: timeout},
|
|
}
|
|
}
|
|
|
|
func (f ziboFetcher) FetchToken(ctx context.Context, _ string) (sharedtoken.OneLinkToken, error) {
|
|
token, err := f.provider.Fetch(ctx)
|
|
if err != nil {
|
|
return sharedtoken.OneLinkToken{}, err
|
|
}
|
|
return sharedtoken.OneLinkToken{
|
|
Value: token.Value,
|
|
CreatedAt: token.CreatedAt,
|
|
ExpiresAt: token.ExpiresAt,
|
|
}, nil
|
|
}
|
|
|
|
func (p *ZiboTokenProvider) Fetch(ctx context.Context) (OneLinkToken, error) {
|
|
if p == nil || p.client == nil {
|
|
return OneLinkToken{}, fmt.Errorf("zibo token provider is not initialized")
|
|
}
|
|
if p.baseURL == "" {
|
|
return OneLinkToken{}, fmt.Errorf("zibo token base URL is required")
|
|
}
|
|
if p.appID == "" || strings.TrimSpace(p.password) == "" {
|
|
return OneLinkToken{}, fmt.Errorf("zibo appid and password are required")
|
|
}
|
|
|
|
endpoint, err := url.Parse(joinProviderURL(p.baseURL, p.path))
|
|
if err != nil {
|
|
return OneLinkToken{}, fmt.Errorf("parse zibo token URL: %w", err)
|
|
}
|
|
query := endpoint.Query()
|
|
query.Set("appid", p.appID)
|
|
query.Set("password", p.password)
|
|
query.Set("transid", p.nextTransID())
|
|
query.Set("refresh", "0")
|
|
endpoint.RawQuery = query.Encode()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
|
|
if err != nil {
|
|
return OneLinkToken{}, fmt.Errorf("create zibo token request: %w", err)
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
resp, err := p.client.Do(req)
|
|
if err != nil {
|
|
return OneLinkToken{}, fmt.Errorf("request zibo token: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return OneLinkToken{}, fmt.Errorf("read zibo token response: %w", err)
|
|
}
|
|
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
|
return OneLinkToken{}, fmt.Errorf("zibo token HTTP status %d", resp.StatusCode)
|
|
}
|
|
|
|
var output ziboTokenResponse
|
|
if err := json.Unmarshal(body, &output); err != nil {
|
|
return OneLinkToken{}, fmt.Errorf("decode zibo token response: %w", err)
|
|
}
|
|
if strings.TrimSpace(output.Status) != "0" {
|
|
return OneLinkToken{}, fmt.Errorf("zibo token failed: status=%s, message=%s", output.Status, output.Message)
|
|
}
|
|
if len(output.Result) == 0 || strings.TrimSpace(output.Result[0].Token) == "" {
|
|
return OneLinkToken{}, fmt.Errorf("zibo token response is empty")
|
|
}
|
|
if output.Result[0].TTL <= 0 {
|
|
return OneLinkToken{}, fmt.Errorf("zibo token ttl must be positive")
|
|
}
|
|
|
|
now := time.Now()
|
|
return OneLinkToken{
|
|
Value: strings.TrimSpace(output.Result[0].Token),
|
|
CreatedAt: now,
|
|
ExpiresAt: now.Add(time.Duration(output.Result[0].TTL) * time.Second),
|
|
}, nil
|
|
}
|
|
|
|
func (p *ZiboTokenProvider) nextTransID() string {
|
|
return p.appID + time.Now().Format("20060102150405") +
|
|
fmt.Sprintf("%08d", p.sequence.Add(1)%100000000)
|
|
}
|