feat: add dns.la dns-01 applicant

This commit is contained in:
Fu Diwei
2025-02-21 17:21:39 +08:00
parent 316a3c950f
commit f81fa2eb63
21 changed files with 677 additions and 12 deletions

View File

@@ -12,7 +12,8 @@ import (
type Client struct {
apiToken string
client *resty.Client
client *resty.Client
}
func NewClient(apiToken string) *Client {

View File

@@ -12,7 +12,8 @@ import (
type Client struct {
apiToken string
client *resty.Client
client *resty.Client
}
func NewClient(apiToken string) *Client {

52
internal/pkg/vendors/dnsla-sdk/api.go vendored Normal file
View File

@@ -0,0 +1,52 @@
package dnslasdk
import (
"fmt"
"net/http"
"net/url"
)
func (c *Client) ListDomains(req *ListDomainsRequest) (*ListDomainsResponse, error) {
resp := ListDomainsResponse{}
err := c.sendRequestWithResult(http.MethodGet, "/domainList", req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (c *Client) ListRecords(req *ListRecordsRequest) (*ListRecordsResponse, error) {
resp := ListRecordsResponse{}
err := c.sendRequestWithResult(http.MethodGet, "/recordList", req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (c *Client) CreateRecord(req *CreateRecordRequest) (*CreateRecordResponse, error) {
resp := CreateRecordResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/record", req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (c *Client) UpdateRecord(req *UpdateRecordRequest) (*UpdateRecordResponse, error) {
resp := UpdateRecordResponse{}
err := c.sendRequestWithResult(http.MethodPut, "/record", req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (c *Client) DeleteRecord(req *DeleteRecordRequest) (*DeleteRecordResponse, error) {
resp := DeleteRecordResponse{}
err := c.sendRequestWithResult(http.MethodDelete, fmt.Sprintf("/record?id=%s", url.QueryEscape(req.Id)), req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}

View File

@@ -0,0 +1,80 @@
package dnslasdk
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/go-resty/resty/v2"
)
type Client struct {
apiId string
apiSecret string
client *resty.Client
}
func NewClient(apiId, apiSecret string) *Client {
client := resty.New()
return &Client{
apiId: apiId,
apiSecret: apiSecret,
client: client,
}
}
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
return c
}
func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R().SetBasicAuth(c.apiId, c.apiSecret)
req.Method = method
req.URL = "https://api.dns.la/api" + path
if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string)
if params != nil {
temp := make(map[string]any)
jsonData, _ := json.Marshal(params)
json.Unmarshal(jsonData, &temp)
for k, v := range temp {
qs[k] = fmt.Sprintf("%v", v)
}
}
req = req.SetQueryParams(qs)
} else {
req = req.
SetHeader("Content-Type", "application/json").
SetBody(params)
}
resp, err := req.Send()
if err != nil {
return nil, fmt.Errorf("dnsla api error: failed to send request: %w", err)
} else if resp.IsError() {
return nil, fmt.Errorf("dnsla api error: unexpected status code: %d, %s", resp.StatusCode(), resp.Body())
}
return resp, nil
}
func (c *Client) sendRequestWithResult(method string, path string, params interface{}, result BaseResponse) error {
resp, err := c.sendRequest(method, path, params)
if err != nil {
return err
}
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("dnsla api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode/100 != 2 {
return fmt.Errorf("dnsla api error: %d - %s", errcode, result.GetMessage())
}
return nil
}

125
internal/pkg/vendors/dnsla-sdk/models.go vendored Normal file
View File

@@ -0,0 +1,125 @@
package dnslasdk
type BaseResponse interface {
GetCode() int
GetMessage() string
}
type baseResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (r *baseResponse) GetCode() int {
return r.Code
}
func (r *baseResponse) GetMessage() string {
return r.Message
}
type DomainInfo struct {
Id string `json:"id"`
GroupId string `json:"groupId"`
GroupName string `json:"groupName"`
Domain string `json:"domain"`
DisplayDomain string `json:"displayDomain"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
}
type RecordInfo struct {
Id string `json:"id"`
DomainId string `json:"domainId"`
GroupId string `json:"groupId"`
GroupName string `json:"groupName"`
LineId string `json:"lineId"`
LineCode string `json:"lineCode"`
LineName string `json:"lineName"`
Type int32 `json:"type"`
Host string `json:"host"`
DisplayHost string `json:"displayHost"`
Data string `json:"data"`
DisplayData string `json:"displayData"`
Ttl int32 `json:"ttl"`
Weight int32 `json:"weight"`
Preference int32 `json:"preference"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
}
type ListDomainsRequest struct {
PageIndex int32 `json:"pageIndex"`
PageSize int32 `json:"pageSize"`
GroupId *string `json:"groupId,omitempty"`
}
type ListDomainsResponse struct {
baseResponse
Data *struct {
Total int32 `json:"total"`
Results []*DomainInfo `json:"results"`
} `json:"data,omitempty"`
}
type ListRecordsRequest struct {
PageIndex int32 `json:"pageIndex"`
PageSize int32 `json:"pageSize"`
DomainId string `json:"domainId"`
GroupId *string `json:"groupId,omitempty"`
LineId *string `json:"lineId,omitempty"`
Type *int32 `json:"type,omitempty"`
Host *string `json:"host,omitempty"`
Data *string `json:"data,omitempty"`
}
type ListRecordsResponse struct {
baseResponse
Data *struct {
Total int32 `json:"total"`
Results []*RecordInfo `json:"results"`
} `json:"data,omitempty"`
}
type CreateRecordRequest struct {
DomainId string `json:"domainId"`
GroupId *string `json:"groupId,omitempty"`
LineId *string `json:"lineId,omitempty"`
Type int32 `json:"type"`
Host string `json:"host"`
Data string `json:"data"`
Ttl int32 `json:"ttl"`
Weight *int32 `json:"weight,omitempty"`
Preference *int32 `json:"preference,omitempty"`
}
type CreateRecordResponse struct {
baseResponse
Data *struct {
Id string `json:"id"`
} `json:"data,omitempty"`
}
type UpdateRecordRequest struct {
Id string `json:"id"`
GroupId *string `json:"groupId,omitempty"`
LineId *string `json:"lineId,omitempty"`
Type *int32 `json:"type,omitempty"`
Host *string `json:"host,omitempty"`
Data *string `json:"data,omitempty"`
Ttl *int32 `json:"ttl,omitempty"`
Weight *int32 `json:"weight,omitempty"`
Preference *int32 `json:"preference,omitempty"`
}
type UpdateRecordResponse struct {
baseResponse
}
type DeleteRecordRequest struct {
Id string `json:"-"`
}
type DeleteRecordResponse struct {
baseResponse
}