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:
@@ -0,0 +1,164 @@
|
||||
package upstreamimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
tengyuOneLinkTokenPath = "/prod-api/open/api/oneLinkToken"
|
||||
tengyuTokenTTL = time.Hour
|
||||
)
|
||||
|
||||
// OneLinkToken is the value returned by a shared-token fetcher.
|
||||
type OneLinkToken struct {
|
||||
Value string
|
||||
CreatedAt time.Time
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// TokenFetcher mirrors the token core fetch boundary. chanKey is the
|
||||
// protocol's c_no; it is never inferred from unrelated channel fields.
|
||||
type TokenFetcher interface {
|
||||
FetchToken(context.Context, string) (OneLinkToken, error)
|
||||
}
|
||||
|
||||
// TokenProvider is the consumer-facing token core boundary.
|
||||
type TokenProvider interface {
|
||||
GetToken(context.Context, string) (string, error)
|
||||
}
|
||||
|
||||
// TengyuTokenConfig contains all values required by the Tengyu protocol.
|
||||
// The provider intentionally does not infer these values from channel config.
|
||||
type TengyuTokenConfig struct {
|
||||
BaseURL string
|
||||
Path string
|
||||
PwdKey string
|
||||
ChannelNo string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// TengyuTokenProvider fetches a Tengyu shared OneLink token.
|
||||
type TengyuTokenProvider struct {
|
||||
baseURL string
|
||||
path string
|
||||
pwdKey string
|
||||
channelNo string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
type tengyuTokenResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
func NewTengyuTokenProvider(config TengyuTokenConfig) *TengyuTokenProvider {
|
||||
return &TengyuTokenProvider{
|
||||
baseURL: strings.TrimRight(strings.TrimSpace(config.BaseURL), "/"),
|
||||
path: normalizeProviderPath(config.Path, tengyuOneLinkTokenPath),
|
||||
pwdKey: config.PwdKey,
|
||||
channelNo: config.ChannelNo,
|
||||
client: newTengyuHTTPClient(config.Timeout),
|
||||
}
|
||||
}
|
||||
|
||||
// Historical in-package names are retained as a small adapter for callers
|
||||
// that constructed the provider before the token-core interfaces were named.
|
||||
type tengyuTokenConfig = TengyuTokenConfig
|
||||
|
||||
func newTengyuTokenProvider(config tengyuTokenConfig) *TengyuTokenProvider {
|
||||
return NewTengyuTokenProvider(config)
|
||||
}
|
||||
|
||||
func newTengyuHTTPClient(timeout time.Duration) *http.Client {
|
||||
if timeout <= 0 {
|
||||
timeout = defaultUpstreamHTTPTimeout
|
||||
}
|
||||
return &http.Client{Timeout: timeout}
|
||||
}
|
||||
|
||||
func (p *TengyuTokenProvider) Fetch(ctx context.Context) (OneLinkToken, error) {
|
||||
return p.fetch(ctx, p.channelNo)
|
||||
}
|
||||
|
||||
func (p *TengyuTokenProvider) FetchToken(ctx context.Context, channelNo string) (OneLinkToken, error) {
|
||||
if strings.TrimSpace(channelNo) == "" {
|
||||
channelNo = p.channelNo
|
||||
}
|
||||
return p.fetch(ctx, channelNo)
|
||||
}
|
||||
|
||||
func (p *TengyuTokenProvider) GetToken(ctx context.Context, channelNo string) (string, error) {
|
||||
token, err := p.FetchToken(ctx, channelNo)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token.Value, nil
|
||||
}
|
||||
|
||||
func (p *TengyuTokenProvider) fetch(ctx context.Context, channelNo string) (OneLinkToken, error) {
|
||||
if p == nil || p.client == nil {
|
||||
return OneLinkToken{}, fmt.Errorf("tengyu token provider is not initialized")
|
||||
}
|
||||
if p.baseURL == "" {
|
||||
return OneLinkToken{}, fmt.Errorf("tengyu token base URL is required")
|
||||
}
|
||||
if strings.TrimSpace(p.pwdKey) == "" {
|
||||
return OneLinkToken{}, fmt.Errorf("tengyu pwdkey is required")
|
||||
}
|
||||
if strings.TrimSpace(channelNo) == "" {
|
||||
return OneLinkToken{}, fmt.Errorf("tengyu channel number is required")
|
||||
}
|
||||
|
||||
endpoint, err := url.Parse(joinProviderURL(p.baseURL, p.path))
|
||||
if err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("parse Tengyu token URL: %w", err)
|
||||
}
|
||||
query := endpoint.Query()
|
||||
query.Set("pwdkey", p.pwdKey)
|
||||
query.Set("c_no", channelNo)
|
||||
endpoint.RawQuery = query.Encode()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
|
||||
if err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("create Tengyu token request: %w", err)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
resp, err := p.client.Do(req)
|
||||
if err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("request Tengyu token: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("read Tengyu token response: %w", err)
|
||||
}
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
||||
return OneLinkToken{}, fmt.Errorf("Tengyu token HTTP status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
|
||||
}
|
||||
|
||||
var output tengyuTokenResponse
|
||||
if err := json.Unmarshal(body, &output); err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("decode Tengyu token response: %w", err)
|
||||
}
|
||||
if output.Code != http.StatusOK {
|
||||
return OneLinkToken{}, fmt.Errorf("Tengyu token failed: code=%d, message=%s", output.Code, output.Msg)
|
||||
}
|
||||
var value string
|
||||
if err := json.Unmarshal(output.Data, &value); err != nil {
|
||||
return OneLinkToken{}, fmt.Errorf("decode Tengyu shared token: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return OneLinkToken{}, fmt.Errorf("Tengyu shared token is empty")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
return OneLinkToken{Value: value, CreatedAt: now, ExpiresAt: now.Add(tengyuTokenTTL)}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user