-
-
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 Technitium (#2332)
- Loading branch information
Showing
14 changed files
with
760 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
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,74 @@ | ||
--- | ||
title: "Technitium" | ||
date: 2019-03-03T16:39:46+01:00 | ||
draft: false | ||
slug: technitium | ||
dnsprovider: | ||
since: "v4.20.0" | ||
code: "technitium" | ||
url: "https://technitium.com/" | ||
--- | ||
|
||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
<!-- providers/dns/technitium/technitium.toml --> | ||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
|
||
|
||
Configuration for [Technitium](https://technitium.com/). | ||
|
||
|
||
<!--more--> | ||
|
||
- Code: `technitium` | ||
- Since: v4.20.0 | ||
|
||
|
||
Here is an example bash command using the Technitium provider: | ||
|
||
```bash | ||
TECHNITIUM_SERVER_BASE_URL="https://localhost:5380" \ | ||
TECHNITIUM_API_TOKEN="xxxxxxxxxxxxxxxxxxxxx" \ | ||
lego --email [email protected] --dns technitium -d '*.example.com' -d example.com run | ||
``` | ||
|
||
|
||
|
||
|
||
## Credentials | ||
|
||
| Environment Variable Name | Description | | ||
|-----------------------|-------------| | ||
| `TECHNITIUM_API_TOKEN` | API token | | ||
| `TECHNITIUM_SERVER_BASE_URL` | Server base URL | | ||
|
||
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 | | ||
|--------------------------------|-------------| | ||
| `TECHNITIUM_HTTP_TIMEOUT` | API request timeout | | ||
| `TECHNITIUM_POLLING_INTERVAL` | Time between DNS propagation check | | ||
| `TECHNITIUM_PROPAGATION_TIMEOUT` | Maximum waiting time for DNS propagation | | ||
| `TECHNITIUM_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" %}}). | ||
|
||
Technitium DNS Server supports Dynamic Updates (RFC2136) for primary zones, | ||
so you can also use the [RFC2136 provider](https://go-acme.github.io/lego/dns/rfc2136/index.html). | ||
|
||
[RFC2136 provider](https://go-acme.github.io/lego/dns/rfc2136/index.html) is much better compared to the HTTP API option from security perspective. | ||
Technitium recommends to use it in production over the HTTP API. | ||
|
||
|
||
|
||
## More information | ||
|
||
- [API documentation](https://github.com/TechnitiumSoftware/DnsServer/blob/0f83d23e605956b66ac76921199e241d9cc061bd/APIDOCS.md) | ||
|
||
<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. --> | ||
<!-- providers/dns/technitium/technitium.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,158 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-acme/lego/v4/providers/dns/internal/errutils" | ||
querystring "github.com/google/go-querystring/query" | ||
) | ||
|
||
const statusSuccess = "ok" | ||
|
||
// Client the Technitium API client. | ||
type Client struct { | ||
apiToken string | ||
|
||
baseURL *url.URL | ||
HTTPClient *http.Client | ||
} | ||
|
||
// NewClient creates a new Client. | ||
func NewClient(baseURL, apiToken string) (*Client, error) { | ||
if apiToken == "" { | ||
return nil, errors.New("missing credentials") | ||
} | ||
|
||
if baseURL == "" { | ||
return nil, errors.New("missing server URL") | ||
} | ||
|
||
apiEndpoint, err := url.Parse(baseURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Client{ | ||
apiToken: apiToken, | ||
baseURL: apiEndpoint, | ||
HTTPClient: &http.Client{Timeout: 10 * time.Second}, | ||
}, nil | ||
} | ||
|
||
// AddRecord adds a resource record for an authoritative zone. | ||
// https://github.com/TechnitiumSoftware/DnsServer/blob/master/APIDOCS.md#add-record | ||
func (c *Client) AddRecord(ctx context.Context, record Record) (*Record, error) { | ||
endpoint := c.baseURL.JoinPath("api", "zones", "records", "add") | ||
|
||
req, err := c.newFormRequest(ctx, endpoint, record) | ||
if err != nil { | ||
return nil, fmt.Errorf("create request: %w", err) | ||
} | ||
|
||
result := &APIResponse[AddRecordResponse]{} | ||
|
||
err = c.do(req, result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if result.Status != statusSuccess { | ||
return nil, result | ||
} | ||
|
||
return result.Response.AddedRecord, nil | ||
} | ||
|
||
// DeleteRecord deletes a record from an authoritative zone. | ||
// https://github.com/TechnitiumSoftware/DnsServer/blob/master/APIDOCS.md#delete-record | ||
func (c *Client) DeleteRecord(ctx context.Context, record Record) error { | ||
endpoint := c.baseURL.JoinPath("api", "zones", "records", "delete") | ||
|
||
req, err := c.newFormRequest(ctx, endpoint, record) | ||
if err != nil { | ||
return fmt.Errorf("create request: %w", err) | ||
} | ||
|
||
result := &APIResponse[any]{} | ||
|
||
err = c.do(req, result) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if result.Status != statusSuccess { | ||
return result | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) do(req *http.Request, result any) error { | ||
resp, err := c.HTTPClient.Do(req) | ||
if err != nil { | ||
return errutils.NewHTTPDoError(req, err) | ||
} | ||
|
||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if resp.StatusCode > http.StatusBadRequest { | ||
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 (c *Client) newFormRequest(ctx context.Context, endpoint *url.URL, payload any) (*http.Request, error) { | ||
values := url.Values{} | ||
|
||
if payload != nil { | ||
var err error | ||
values, err = querystring.Values(payload) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create request body: %w", err) | ||
} | ||
} | ||
|
||
values.Set("token", c.apiToken) | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(values.Encode())) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create request: %w", err) | ||
} | ||
|
||
if payload != nil { | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
} | ||
|
||
return req, nil | ||
} | ||
|
||
func parseError(req *http.Request, resp *http.Response) error { | ||
raw, _ := io.ReadAll(resp.Body) | ||
|
||
var errAPI APIResponse[any] | ||
err := json.Unmarshal(raw, &errAPI) | ||
if err != nil { | ||
return errutils.NewUnexpectedStatusCodeError(req, resp.StatusCode, raw) | ||
} | ||
|
||
return &errAPI | ||
} |
Oops, something went wrong.