|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/go-acme/lego/v4/providers/dns/internal/errutils" |
| 14 | + "github.com/go-acme/lego/v4/providers/dns/internal/useragent" |
| 15 | + "golang.org/x/oauth2" |
| 16 | +) |
| 17 | + |
| 18 | +const defaultBaseURL = "https://api.noip.com" |
| 19 | + |
| 20 | +// Client the No-IP API client. |
| 21 | +type Client struct { |
| 22 | + BaseURL *url.URL |
| 23 | + httpClient *http.Client |
| 24 | +} |
| 25 | + |
| 26 | +// NewClient creates a new Client. |
| 27 | +func NewClient(hc *http.Client) (*Client, error) { |
| 28 | + baseURL, _ := url.Parse(defaultBaseURL) |
| 29 | + |
| 30 | + if hc == nil { |
| 31 | + hc = &http.Client{Timeout: 10 * time.Second} |
| 32 | + } |
| 33 | + |
| 34 | + return &Client{ |
| 35 | + BaseURL: baseURL, |
| 36 | + httpClient: hc, |
| 37 | + }, nil |
| 38 | +} |
| 39 | + |
| 40 | +// CreateRData creates a record. |
| 41 | +// https://developer.noip.com/reference/v1-dns-records-create-rdata |
| 42 | +func (c *Client) CreateRData(ctx context.Context, zone, name, dnsType string, data RData) error { |
| 43 | + endpoint := c.BaseURL.JoinPath("v1", "dns", "records", zone, name, "rrsets", dnsType, "rdata") |
| 44 | + |
| 45 | + req, err := newJSONRequest(ctx, http.MethodPost, endpoint, []RData{data}) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + return c.do(req, nil) |
| 51 | +} |
| 52 | + |
| 53 | +// DeleteRDataByLabel deletes a record by label. |
| 54 | +// https://developer.noip.com/reference/v1-dns-records-delete-rdata-by-label |
| 55 | +func (c *Client) DeleteRDataByLabel(ctx context.Context, zone, name, dnsType, label string) error { |
| 56 | + endpoint := c.BaseURL.JoinPath("v1", "dns", "records", zone, name, "rrsets", dnsType, "rdata", label) |
| 57 | + |
| 58 | + req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + return c.do(req, nil) |
| 64 | +} |
| 65 | + |
| 66 | +func (c *Client) do(req *http.Request, result any) error { |
| 67 | + useragent.SetHeader(req.Header) |
| 68 | + |
| 69 | + resp, err := c.httpClient.Do(req) |
| 70 | + if err != nil { |
| 71 | + return errutils.NewHTTPDoError(req, err) |
| 72 | + } |
| 73 | + |
| 74 | + defer func() { _ = resp.Body.Close() }() |
| 75 | + |
| 76 | + if resp.StatusCode/100 != 2 { |
| 77 | + return parseError(req, resp) |
| 78 | + } |
| 79 | + |
| 80 | + if result == nil { |
| 81 | + return nil |
| 82 | + } |
| 83 | + |
| 84 | + raw, err := io.ReadAll(resp.Body) |
| 85 | + if err != nil { |
| 86 | + return errutils.NewReadResponseError(req, resp.StatusCode, err) |
| 87 | + } |
| 88 | + |
| 89 | + err = json.Unmarshal(raw, result) |
| 90 | + if err != nil { |
| 91 | + return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) { |
| 98 | + buf := new(bytes.Buffer) |
| 99 | + |
| 100 | + if payload != nil { |
| 101 | + err := json.NewEncoder(buf).Encode(payload) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("failed to create request JSON body: %w", err) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("unable to create request: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + req.Header.Set("Accept", "application/json") |
| 113 | + |
| 114 | + if payload != nil { |
| 115 | + req.Header.Set("Content-Type", "application/json") |
| 116 | + } |
| 117 | + |
| 118 | + return req, nil |
| 119 | +} |
| 120 | + |
| 121 | +func parseError(req *http.Request, resp *http.Response) error { |
| 122 | + raw, _ := io.ReadAll(resp.Body) |
| 123 | + |
| 124 | + var apiResp APIResponse[any] |
| 125 | + |
| 126 | + err := json.Unmarshal(raw, &apiResp) |
| 127 | + if err != nil { |
| 128 | + return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw) |
| 129 | + } |
| 130 | + |
| 131 | + return fmt.Errorf("[status code %d] %w", resp.StatusCode, apiResp.Errors) |
| 132 | +} |
| 133 | + |
| 134 | +func OAuthStaticAccessToken(client *http.Client, accessToken string) *http.Client { |
| 135 | + if client == nil { |
| 136 | + client = &http.Client{Timeout: 5 * time.Second} |
| 137 | + } |
| 138 | + |
| 139 | + client.Transport = &oauth2.Transport{ |
| 140 | + Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}), |
| 141 | + Base: client.Transport, |
| 142 | + } |
| 143 | + |
| 144 | + return client |
| 145 | +} |
0 commit comments