134 lines
3.9 KiB
Go
134 lines
3.9 KiB
Go
package easydns
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const maxResponse = 16 << 20
|
|
|
|
type Client struct {
|
|
base string
|
|
tokenFile string
|
|
useSocket bool
|
|
http *http.Client
|
|
}
|
|
|
|
func New(base, tokenFile, socket string) (*Client, error) {
|
|
tr := &http.Transport{MaxIdleConnsPerHost: 8, IdleConnTimeout: 30 * time.Second, ResponseHeaderTimeout: 30 * time.Second}
|
|
if socket != "" {
|
|
tr.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, "unix", socket)
|
|
}
|
|
base = "http://unix"
|
|
} else {
|
|
u, e := url.Parse(base)
|
|
if e != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" {
|
|
return nil, errors.New("invalid EasyDNS API URL")
|
|
}
|
|
base = strings.TrimSuffix(base, "/")
|
|
}
|
|
return &Client{base: base, tokenFile: tokenFile, useSocket: socket != "", http: &http.Client{Transport: tr, Timeout: 65 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}}, nil
|
|
}
|
|
|
|
type Error struct {
|
|
Status int
|
|
Message string
|
|
}
|
|
|
|
func (e *Error) Error() string {
|
|
if e.Status == 0 {
|
|
return e.Message
|
|
}
|
|
return fmt.Sprintf("EasyDNS HTTP %d: %s", e.Status, e.Message)
|
|
}
|
|
|
|
// Do uses ONLY paths explicitly built in the panel routes. It does not expose a generic API proxy.
|
|
func (c *Client) Do(ctx context.Context, method, path string, body any, etag string, out any) (string, error) {
|
|
if !strings.HasPrefix(path, "/api/v1/") && path != "/readyz" && path != "/healthz" {
|
|
return "", &Error{Message: "endpoint is not permitted"}
|
|
}
|
|
var src io.Reader
|
|
if body != nil {
|
|
b, e := json.Marshal(body)
|
|
if e != nil {
|
|
return "", e
|
|
}
|
|
src = bytes.NewReader(b)
|
|
}
|
|
req, e := http.NewRequestWithContext(ctx, method, c.base+path, src)
|
|
if e != nil {
|
|
return "", e
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
if etag != "" {
|
|
req.Header.Set("If-Match", etag)
|
|
}
|
|
if !c.useSocket && path != "/readyz" && path != "/healthz" && path != "/api/v1/health" {
|
|
secret, e := os.ReadFile(c.tokenFile)
|
|
if e != nil {
|
|
return "", &Error{Message: "Unable to read EasyDNS generated control token; check token file permissions or configure the Unix socket"}
|
|
}
|
|
token := strings.TrimSpace(string(secret))
|
|
if token == "" {
|
|
return "", &Error{Message: "EasyDNS control token file is empty"}
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
resp, e := c.http.Do(req)
|
|
if e != nil {
|
|
return "", &Error{Message: "EasyDNS API unavailable: " + e.Error()}
|
|
}
|
|
defer resp.Body.Close()
|
|
raw, e := io.ReadAll(io.LimitReader(resp.Body, maxResponse+1))
|
|
if e != nil {
|
|
return "", e
|
|
}
|
|
if len(raw) > maxResponse {
|
|
return "", &Error{Message: "EasyDNS API response exceeds 16 MiB"}
|
|
}
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
var envelope struct {
|
|
Error struct {
|
|
Status int `json:"status"`
|
|
Message string `json:"message"`
|
|
} `json:"error"`
|
|
}
|
|
msg := http.StatusText(resp.StatusCode)
|
|
if json.Unmarshal(raw, &envelope) == nil && envelope.Error.Message != "" {
|
|
msg = envelope.Error.Message
|
|
}
|
|
if msg == "" {
|
|
msg = "Unexpected EasyDNS response"
|
|
}
|
|
return resp.Header.Get("ETag"), &Error{Status: resp.StatusCode, Message: msg}
|
|
}
|
|
if out != nil && len(raw) > 0 {
|
|
if e := json.Unmarshal(raw, out); e != nil {
|
|
return "", fmt.Errorf("decode EasyDNS response: %w", e)
|
|
}
|
|
}
|
|
return resp.Header.Get("ETag"), nil
|
|
}
|
|
func (c *Client) Get(ctx context.Context, path string, out any) (string, error) {
|
|
return c.Do(ctx, http.MethodGet, path, nil, "", out)
|
|
}
|
|
func (c *Client) Write(ctx context.Context, method, path string, payload any, etag string, out any) error {
|
|
_, e := c.Do(ctx, method, path, payload, etag, out)
|
|
return e
|
|
}
|
|
func Escape(v string) string { return url.PathEscape(v) }
|