feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,184 @@
|
||||
package dnsprovider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type dnsmgr struct {
|
||||
client *http.Client
|
||||
zone string
|
||||
credential Credential
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func positiveID(s string) bool {
|
||||
id, e := strconv.ParseUint(s, 10, 64)
|
||||
return e == nil && id > 0 && strconv.FormatUint(id, 10) == s
|
||||
}
|
||||
func md5Hex(v string) string { sum := md5.Sum([]byte(v)); return hex.EncodeToString(sum[:]) }
|
||||
func (p *dnsmgr) call(ctx context.Context, path string, values url.Values, output any) error {
|
||||
stamp := strconv.FormatInt(p.now().Unix(), 10)
|
||||
values.Set("uid", p.credential.UserID)
|
||||
values.Set("timestamp", stamp)
|
||||
values.Set("sign", md5Hex(p.credential.UserID+stamp+p.credential.APIKey))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimSuffix(p.credential.Endpoint, "/")+"/api/"+path, strings.NewReader(values.Encode()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
// List APIs use {total,rows}; mutation/info APIs require an explicit successful code.
|
||||
var raw json.RawMessage
|
||||
if err = doJSON(p.client, req, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
var status struct {
|
||||
Code *int `json:"code"`
|
||||
}
|
||||
if json.Unmarshal(raw, &status) != nil {
|
||||
return errors.New("彩虹 DNS 响应无效")
|
||||
}
|
||||
if status.Code != nil && *status.Code != 0 {
|
||||
return errors.New("彩虹 DNS 拒绝请求,请检查 API 权限与票据")
|
||||
}
|
||||
if !strings.HasSuffix(path, "domain") && !strings.HasPrefix(path, "record/data/") && status.Code == nil {
|
||||
return errors.New("彩虹 DNS 未确认操作结果")
|
||||
}
|
||||
return json.Unmarshal(raw, output)
|
||||
}
|
||||
func (p *dnsmgr) Present(ctx context.Context, fqdn, value string) (string, error) {
|
||||
return p.CreateRecord(ctx, fqdn, "TXT", value)
|
||||
}
|
||||
|
||||
type mgrRecord struct {
|
||||
ID json.RawMessage `json:"RecordId"`
|
||||
Name string `json:"Name"`
|
||||
Type string `json:"Type"`
|
||||
Value json.RawMessage `json:"Value"`
|
||||
}
|
||||
|
||||
func rawID(raw json.RawMessage) string {
|
||||
var s string
|
||||
if json.Unmarshal(raw, &s) == nil {
|
||||
return s
|
||||
}
|
||||
var n json.Number
|
||||
if json.Unmarshal(raw, &n) == nil {
|
||||
return n.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func rawValueContains(raw json.RawMessage, value string) bool {
|
||||
var s string
|
||||
if json.Unmarshal(raw, &s) == nil {
|
||||
return s == value
|
||||
}
|
||||
var all []string
|
||||
if json.Unmarshal(raw, &all) == nil {
|
||||
for _, s := range all {
|
||||
if s == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (p *dnsmgr) records(ctx context.Context, rr, kind, value string) ([]mgrRecord, error) {
|
||||
var out struct {
|
||||
Total *int `json:"total"`
|
||||
Rows []mgrRecord `json:"rows"`
|
||||
}
|
||||
if err := p.call(ctx, "record/data/"+p.credential.ZoneID, url.Values{"subdomain": {rr}, "type": {kind}, "value": {value}, "offset": {"0"}, "limit": {"100"}}, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if out.Total == nil || *out.Total > 100 {
|
||||
return nil, errors.New("彩虹 DNS 记录响应不完整")
|
||||
}
|
||||
return out.Rows, nil
|
||||
}
|
||||
func (p *dnsmgr) CreateRecord(ctx context.Context, fqdn, kind, value string) (string, error) {
|
||||
if err := validateRecord(kind, value); err != nil {
|
||||
return "", err
|
||||
}
|
||||
rr, err := relativeRecordName(fqdn, p.zone)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !positiveID(p.credential.ZoneID) {
|
||||
return "", errors.New("彩虹 DNS 域名编号无效")
|
||||
}
|
||||
before, err := p.records(ctx, rr, kind, value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
old := map[string]bool{}
|
||||
for _, r := range before {
|
||||
old[rawID(r.ID)] = true
|
||||
}
|
||||
var info struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
RecordLine []struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
} `json:"recordLine"`
|
||||
MinTTL json.Number `json:"minTTL"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err = p.call(ctx, "domain/"+p.credential.ZoneID, url.Values{}, &info); err != nil {
|
||||
return "", err
|
||||
}
|
||||
minTTL := int64(0)
|
||||
if info.Data.MinTTL != "" {
|
||||
minTTL, err = info.Data.MinTTL.Int64()
|
||||
if err != nil || minTTL < 0 || minTTL > 2147483647 {
|
||||
return "", errors.New("彩虹 DNS 返回了无效的最小 TTL")
|
||||
}
|
||||
}
|
||||
line := ""
|
||||
found := false
|
||||
for _, l := range info.Data.RecordLine {
|
||||
if l.Name == "默认" || strings.EqualFold(l.Name, "default") {
|
||||
line = l.ID
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return "", errors.New("彩虹 DNS 未提供默认解析线路")
|
||||
}
|
||||
var response struct {
|
||||
Code int `json:"code"`
|
||||
}
|
||||
if err = p.call(ctx, "record/add/"+p.credential.ZoneID, url.Values{"name": {rr}, "type": {kind}, "value": {value}, "line": {line}, "ttl": {strconv.FormatInt(max(int64(600), minTTL), 10)}}, &response); err != nil {
|
||||
return "", err
|
||||
}
|
||||
after, err := p.records(ctx, rr, kind, value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, r := range after {
|
||||
id := rawID(r.ID)
|
||||
if validIdentifier(id, 1, 128) && !old[id] && (r.Name == rr || r.Name == fqdn) && r.Type == kind && rawValueContains(r.Value, value) {
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("彩虹 DNS 已接受创建,但未能核对新增记录编号,请管理员核验")
|
||||
}
|
||||
func (p *dnsmgr) Cleanup(ctx context.Context, id string) error {
|
||||
if !positiveID(p.credential.ZoneID) || !validIdentifier(id, 1, 128) {
|
||||
return errors.New("彩虹 DNS 记录编号无效")
|
||||
}
|
||||
var out struct {
|
||||
Code int `json:"code"`
|
||||
}
|
||||
return p.call(ctx, "record/delete/"+p.credential.ZoneID, url.Values{"recordid": {id}}, &out)
|
||||
}
|
||||
Reference in new issue
Block a user