1 parent
8c4d463f1e
commit
de13d851a8
75 files changed
+4656
-442
No files matched your search
@@ -20,11 +20,18 @@ const maxResponse = 16 << 20
|
||||
type Client struct {
|
||||
base string
|
||||
tokenFile string
|
||||
token string
|
||||
useSocket bool
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func New(base, tokenFile, socket string) (*Client, error) {
|
||||
return NewWithToken(base, tokenFile, socket, "")
|
||||
}
|
||||
|
||||
// NewWithToken permits an explicitly configured in-memory token (e.g. from
|
||||
// Compose .env) without writing the secret into the container filesystem.
|
||||
func NewWithToken(base, tokenFile, socket, token 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) {
|
||||
@@ -38,7 +45,7 @@ func New(base, tokenFile, socket string) (*Client, error) {
|
||||
}
|
||||
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
|
||||
return &Client{base: base, tokenFile: tokenFile, token: token, 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 {
|
||||
@@ -78,11 +85,14 @@ func (c *Client) Do(ctx context.Context, method, path string, body any, etag str
|
||||
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 := c.token
|
||||
if token == "" {
|
||||
secret, e := os.ReadFile(c.tokenFile)
|
||||
if e != nil {
|
||||
return "", &Error{Message: "Unable to read EasyDNS generated control token; check token file permissions, EASYDNS_API_TOKEN, or configure the Unix socket"}
|
||||
}
|
||||
token = strings.TrimSpace(string(secret))
|
||||
}
|
||||
token := strings.TrimSpace(string(secret))
|
||||
if token == "" {
|
||||
return "", &Error{Message: "EasyDNS control token file is empty"}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user