package dnsprovider import ( "context" "encoding/base64" "encoding/json" "errors" "net/http" "net/url" "strconv" "strings" "time" ) type west struct { client *http.Client zone, baseURL string credential Credential now func() time.Time } func (p *west) call(ctx context.Context, action string, v url.Values, out any) error { base := p.baseURL if base == "" { base = "https://api.west.cn/API/v2" } endpoint := base + "/domain/" v.Set("act", action) if p.credential.DomainKey != "" { endpoint += "dns/" v.Set("apidomainkey", p.credential.DomainKey) } else { stamp := strconv.FormatInt(p.now().UnixMilli(), 10) v.Set("username", p.credential.Username) v.Set("time", stamp) v.Set("token", md5Hex(p.credential.Username+p.credential.APIKey+stamp)) } req, e := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(v.Encode())) if e != nil { return e } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") var response struct { Result int `json:"result"` Data json.RawMessage `json:"data"` } if e = doJSON(p.client, req, &response); e != nil { return e } if response.Result != 200 { return errors.New("西部数码拒绝请求,请检查 API 密码、单域名密钥或白名单") } if out == nil { return nil } return json.Unmarshal(response.Data, out) } func (p *west) Present(ctx context.Context, fqdn, value string) (string, error) { return p.CreateRecord(ctx, fqdn, "TXT", value) } type westRecord struct { ID json.RawMessage `json:"record_id"` Hostname string `json:"hostname"` Type string `json:"record_type"` Value string `json:"record_value"` } func (p *west) modernRecords(ctx context.Context, fqdn, kind string) ([]westRecord, error) { var data json.RawMessage if e := p.call(ctx, "dnsrec.list", url.Values{"domain": {p.zone}, "hostname": {fqdn}, "record_type": {kind}}, &data); e != nil { return nil, e } var list []westRecord if json.Unmarshal(data, &list) == nil { return list, nil } var wrapped struct { Items []westRecord `json:"items"` List []westRecord `json:"list"` Records []westRecord `json:"records"` } if json.Unmarshal(data, &wrapped) != nil { return nil, errors.New("西部数码记录响应无效") } if wrapped.Items != nil { return wrapped.Items, nil } if wrapped.List != nil { return wrapped.List, nil } if wrapped.Records != nil { return wrapped.Records, nil } return nil, errors.New("西部数码未返回解析记录列表") } func (p *west) CreateRecord(ctx context.Context, fqdn, kind, value string) (string, error) { if e := validateRecord(kind, value); e != nil { return "", e } rr, e := relativeRecordName(fqdn, p.zone) if e != nil { return "", e } if p.credential.DomainKey == "" { var out struct { ID json.RawMessage `json:"id"` } if e = p.call(ctx, "adddnsrecord", url.Values{"domain": {p.zone}, "host": {rr}, "type": {kind}, "value": {value}, "ttl": {"600"}, "line": {""}}, &out); e != nil { return "", e } id := rawID(out.ID) if !validIdentifier(id, 1, 128) { return "", errors.New("西部数码未返回新增记录编号") } return id, nil } before, e := p.modernRecords(ctx, fqdn, kind) if e != nil { return "", e } old := map[string]bool{} for _, r := range before { old[rawID(r.ID)] = true } if e = p.call(ctx, "dnsrec.add", url.Values{"domain": {p.zone}, "hostname": {fqdn}, "record_type": {kind}, "record_value": {value}, "ttl": {"600"}}, nil); e != nil { return "", e } after, e := p.modernRecords(ctx, fqdn, kind) if e != nil { return "", e } for _, r := range after { id := rawID(r.ID) if !old[id] && validIdentifier(id, 1, 128) && r.Hostname == fqdn && r.Type == kind && r.Value == value { return base64.RawURLEncoding.EncodeToString([]byte(fqdn + "|" + id)), nil } } return "", errors.New("西部数码已接受创建,但未能核对新增记录编号,请管理员核验") } func (p *west) Cleanup(ctx context.Context, id string) error { v := url.Values{"domain": {p.zone}} action := "deldnsrecord" if p.credential.DomainKey != "" { raw, e := base64.RawURLEncoding.DecodeString(id) if e != nil { return errors.New("西部数码记录标识无效") } parts := strings.Split(string(raw), "|") if len(parts) != 2 { return errors.New("西部数码记录标识无效") } if _, e = relativeRecordName(parts[0], p.zone); e != nil { return e } id = parts[1] action = "dnsrec.remove" v.Set("hostname", parts[0]) v.Set("record_id", id) } else { v.Set("id", id) } if !validIdentifier(id, 1, 128) { return errors.New("西部数码记录编号无效") } return p.call(ctx, action, v, nil) }