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 Shellrent (#2126)
- Loading branch information
Showing
17 changed files
with
1,071 additions
and
8 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
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,69 @@ | ||
--- | ||
title: "Shellrent" | ||
date: 2019-03-03T16:39:46+01:00 | ||
draft: false | ||
slug: shellrent | ||
dnsprovider: | ||
since: "v4.16.0" | ||
code: "shellrent" | ||
url: "https://www.shellrent.com/" | ||
--- | ||
|
||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
<!-- providers/dns/shellrent/shellrent.toml --> | ||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
|
||
|
||
Configuration for [Shellrent](https://www.shellrent.com/). | ||
|
||
|
||
<!--more--> | ||
|
||
- Code: `shellrent` | ||
- Since: v4.16.0 | ||
|
||
|
||
Here is an example bash command using the Shellrent provider: | ||
|
||
```bash | ||
SHELLRENT_USERNAME=xxxx \ | ||
SHELLRENT_TOKEN=yyyy \ | ||
lego --email you@example.com --dns shellrent --domains my.example.org run | ||
``` | ||
|
||
|
||
|
||
|
||
## Credentials | ||
|
||
| Environment Variable Name | Description | | ||
|-----------------------|-------------| | ||
| `SHELLRENT_TOKEN` | Token | | ||
| `SHELLRENT_USERNAME` | Username | | ||
|
||
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 | | ||
|--------------------------------|-------------| | ||
| `SHELLRENT_HTTP_TIMEOUT` | API request timeout | | ||
| `SHELLRENT_POLLING_INTERVAL` | Time between DNS propagation check | | ||
| `SHELLRENT_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation | | ||
| `SHELLRENT_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://api.shellrent.com/section/api2) | ||
|
||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
<!-- providers/dns/shellrent/shellrent.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
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,245 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/go-acme/lego/v4/providers/dns/internal/errutils" | ||
) | ||
|
||
// DefaultBaseURL the default API endpoint. | ||
const defaultBaseURL = "https://manager.shellrent.com/api2" | ||
|
||
const authorizationHeader = "Authorization" | ||
|
||
// Client the Shellrent API client. | ||
type Client struct { | ||
username string | ||
token string | ||
|
||
baseURL *url.URL | ||
HTTPClient *http.Client | ||
} | ||
|
||
// NewClient Creates a new Client. | ||
func NewClient(username string, token string) *Client { | ||
baseURL, _ := url.Parse(defaultBaseURL) | ||
|
||
return &Client{ | ||
token: token, | ||
username: username, | ||
baseURL: baseURL, | ||
HTTPClient: &http.Client{Timeout: 10 * time.Second}, | ||
} | ||
} | ||
|
||
// ListServices lists service IDs. | ||
// https://api.shellrent.com/elenco-dei-servizi-acquistati | ||
func (c Client) ListServices(ctx context.Context) ([]int, error) { | ||
endpoint := c.baseURL.JoinPath("purchase") | ||
|
||
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := Response[[]IntOrString]{} | ||
|
||
err = c.do(req, &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if result.Code != 0 { | ||
return nil, result.Base | ||
} | ||
|
||
var ids []int | ||
|
||
for _, datum := range result.Data { | ||
ids = append(ids, datum.Value()) | ||
} | ||
|
||
return ids, nil | ||
} | ||
|
||
// GetServiceDetails gets service details. | ||
// https://api.shellrent.com/dettagli-servizio-acquistato | ||
func (c Client) GetServiceDetails(ctx context.Context, serviceID int) (*ServiceDetails, error) { | ||
endpoint := c.baseURL.JoinPath("purchase", "details", strconv.Itoa(serviceID)) | ||
|
||
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := Response[*ServiceDetails]{} | ||
|
||
err = c.do(req, &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if result.Code != 0 { | ||
return nil, result.Base | ||
} | ||
|
||
return result.Data, nil | ||
} | ||
|
||
// GetDomainDetails gets domain details. | ||
// https://api.shellrent.com/dettagli-dominio | ||
func (c Client) GetDomainDetails(ctx context.Context, domainID int) (*DomainDetails, error) { | ||
endpoint := c.baseURL.JoinPath("domain", "details", strconv.Itoa(domainID)) | ||
|
||
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := Response[*DomainDetails]{} | ||
|
||
err = c.do(req, &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if result.Code != 0 { | ||
return nil, result.Base | ||
} | ||
return result.Data, nil | ||
} | ||
|
||
// CreateRecord created a record. | ||
// https://api.shellrent.com/creazione-record-dns-di-un-dominio | ||
func (c Client) CreateRecord(ctx context.Context, domainID int, record Record) (int, error) { | ||
endpoint := c.baseURL.JoinPath("dns_record", "store", strconv.Itoa(domainID)) | ||
|
||
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
result := Response[*Record]{} | ||
|
||
err = c.do(req, &result) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if result.Code != 0 { | ||
return 0, result.Base | ||
} | ||
return result.Data.ID.Value(), nil | ||
} | ||
|
||
// DeleteRecord deletes a record. | ||
// https://api.shellrent.com/eliminazione-record-dns-di-un-dominio | ||
func (c Client) DeleteRecord(ctx context.Context, domainID int, recordID int) error { | ||
endpoint := c.baseURL.JoinPath("dns_record", "remove", strconv.Itoa(domainID), strconv.Itoa(recordID)) | ||
|
||
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result := Response[any]{} | ||
|
||
err = c.do(req, &result) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if result.Code != 0 { | ||
return result.Base | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c Client) do(req *http.Request, result any) error { | ||
req.Header.Set(authorizationHeader, c.username+"."+c.token) | ||
|
||
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) | ||
} | ||
|
||
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 response Base | ||
err := json.Unmarshal(raw, &response) | ||
if err != nil { | ||
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw) | ||
} | ||
|
||
return response | ||
} | ||
|
||
// TTLRounder rounds the given TTL in seconds to the next accepted value. | ||
// Accepted TTL values are: | ||
// - 3600 | ||
// - 14400 | ||
// - 28800 | ||
// - 57600 | ||
// - 86400 | ||
func TTLRounder(ttl int) int { | ||
for _, validTTL := range []int{3600, 14400, 28800, 57600, 86400} { | ||
if ttl <= validTTL { | ||
return validTTL | ||
} | ||
} | ||
|
||
return 3600 | ||
} |
Oops, something went wrong.