-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DNS provider for Rainyun/雨云 (#2354)
Co-authored-by: Fernandez Ludovic <[email protected]>
- Loading branch information
Showing
14 changed files
with
858 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
--- | ||
title: "Rain Yun/雨云" | ||
date: 2019-03-03T16:39:46+01:00 | ||
draft: false | ||
slug: rainyun | ||
dnsprovider: | ||
since: "v4.21.0" | ||
code: "rainyun" | ||
url: "https://www.rainyun.com" | ||
--- | ||
|
||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
<!-- providers/dns/rainyun/rainyun.toml --> | ||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
|
||
|
||
Configuration for [Rain Yun/雨云](https://www.rainyun.com). | ||
|
||
|
||
<!--more--> | ||
|
||
- Code: `rainyun` | ||
- Since: v4.21.0 | ||
|
||
|
||
Here is an example bash command using the Rain Yun/雨云 provider: | ||
|
||
```bash | ||
RAINYUN_API_KEY="xxxxxxxxxxxxxxxxxxxxx" \ | ||
lego --email [email protected] --dns rainyun -d '*.example.com' -d example.com run | ||
``` | ||
|
||
|
||
|
||
|
||
## Credentials | ||
|
||
| Environment Variable Name | Description | | ||
|-----------------------|-------------| | ||
| `RAINYUN_API_KEY` | API key | | ||
|
||
The environment variable names can be suffixed by `_FILE` to reference a file instead of a value. | ||
More information [here]({{% ref "dns#configuration-and-credentials" %}}). | ||
|
||
|
||
## Additional Configuration | ||
|
||
| Environment Variable Name | Description | | ||
|--------------------------------|-------------| | ||
| `RAINYUN_HTTP_TIMEOUT` | API request timeout | | ||
| `RAINYUN_POLLING_INTERVAL` | Time between DNS propagation check | | ||
| `RAINYUN_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation | | ||
| `RAINYUN_TTL` | The TTL of the TXT record used for the DNS challenge | | ||
|
||
The environment variable names can be suffixed by `_FILE` to reference a file instead of a value. | ||
More information [here]({{% ref "dns#configuration-and-credentials" %}}). | ||
|
||
|
||
|
||
|
||
## More information | ||
|
||
- [API documentation](https://www.apifox.cn/apidoc/shared-a4595cc8-44c5-4678-a2a3-eed7738dab03/api-151416609) | ||
|
||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
<!-- providers/dns/rainyun/rainyun.toml --> | ||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/go-acme/lego/v4/providers/dns/internal/errutils" | ||
querystring "github.com/google/go-querystring/query" | ||
) | ||
|
||
const defaultBaseURL = "https://api.v2.rainyun.com/product/" | ||
|
||
// Client the Rain Yun API client. | ||
type Client struct { | ||
apiKey string | ||
|
||
baseURL *url.URL | ||
HTTPClient *http.Client | ||
} | ||
|
||
// NewClient creates a new Client. | ||
func NewClient(apiKey string) (*Client, error) { | ||
if apiKey == "" { | ||
return nil, errors.New("credentials missing") | ||
} | ||
|
||
baseURL, _ := url.Parse(defaultBaseURL) | ||
|
||
return &Client{ | ||
apiKey: apiKey, | ||
baseURL: baseURL, | ||
HTTPClient: &http.Client{Timeout: 10 * time.Second}, | ||
}, nil | ||
} | ||
|
||
func (c *Client) AddRecord(ctx context.Context, domainID int, record Record) error { | ||
endpoint := c.baseURL.JoinPath("domain", strconv.Itoa(domainID), "dns") | ||
|
||
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.do(req, nil) | ||
} | ||
|
||
func (c *Client) DeleteRecord(ctx context.Context, domainID, recordID int) error { | ||
endpoint := c.baseURL.JoinPath("domain", strconv.Itoa(domainID), "dns") | ||
|
||
values, err := querystring.Values(Record{ID: recordID}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
endpoint.RawQuery = values.Encode() | ||
|
||
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.do(req, nil) | ||
} | ||
|
||
func (c *Client) ListRecords(ctx context.Context, domainID int) ([]Record, error) { | ||
endpoint := c.baseURL.JoinPath("domain", strconv.Itoa(domainID), "dns") | ||
|
||
query := endpoint.Query() | ||
query.Set("limit", "100") | ||
query.Set("page_no", "1") | ||
endpoint.RawQuery = query.Encode() | ||
|
||
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var recordData APIResponse[Record] | ||
err = c.do(req, &recordData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return recordData.Data.Records, nil | ||
} | ||
|
||
func (c *Client) ListDomains(ctx context.Context) ([]Domain, error) { | ||
endpoint := c.baseURL.JoinPath("domain") | ||
|
||
query := endpoint.Query() | ||
query.Set("options", `{"columnFilters":{"domains.Domain":""},"sort":[],"page":1,"perPage":100}`) | ||
endpoint.RawQuery = query.Encode() | ||
|
||
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var domainData APIResponse[Domain] | ||
|
||
err = c.do(req, &domainData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return domainData.Data.Records, nil | ||
} | ||
|
||
func (c *Client) do(req *http.Request, result any) error { | ||
req.Header.Add("x-api-key", c.apiKey) | ||
|
||
resp, err := c.HTTPClient.Do(req) | ||
if err != nil { | ||
return errutils.NewHTTPDoError(req, err) | ||
} | ||
|
||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if resp.StatusCode/100 != 2 { | ||
return parseError(req, resp) | ||
} | ||
|
||
if result == nil { | ||
return nil | ||
} | ||
|
||
raw, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return errutils.NewReadResponseError(req, resp.StatusCode, err) | ||
} | ||
|
||
err = json.Unmarshal(raw, result) | ||
if err != nil { | ||
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) { | ||
buf := new(bytes.Buffer) | ||
|
||
if payload != nil { | ||
err := json.NewEncoder(buf).Encode(payload) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create request JSON body: %w", err) | ||
} | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create request: %w", err) | ||
} | ||
|
||
req.Header.Set("Accept", "application/json") | ||
|
||
if payload != nil { | ||
req.Header.Set("Content-Type", "application/json") | ||
} | ||
|
||
return req, nil | ||
} | ||
|
||
func parseError(req *http.Request, resp *http.Response) error { | ||
raw, _ := io.ReadAll(resp.Body) | ||
|
||
var errAPI APIError | ||
err := json.Unmarshal(raw, &errAPI) | ||
if err != nil { | ||
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw) | ||
} | ||
|
||
return &errAPI | ||
} |
Oops, something went wrong.