git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/cloudflare/hostnames.go (about)

     1  package cloudflare
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"time"
     8  )
     9  
    10  // CustomHostnameStatus is the enumeration of valid state values in the CustomHostnameSSL.
    11  type CustomHostnameStatus string
    12  
    13  const (
    14  	// PENDING status represents state of CustomHostname is pending.
    15  	PENDING CustomHostnameStatus = "pending"
    16  	// ACTIVE status represents state of CustomHostname is active.
    17  	ACTIVE CustomHostnameStatus = "active"
    18  	// MOVED status represents state of CustomHostname is moved.
    19  	MOVED CustomHostnameStatus = "moved"
    20  	// DELETED status represents state of CustomHostname is removed.
    21  	DELETED CustomHostnameStatus = "deleted"
    22  )
    23  
    24  // CustomHostname represents a custom hostname in a zone.
    25  type CustomHostname struct {
    26  	ID                        string                                  `json:"id,omitempty"`
    27  	Hostname                  string                                  `json:"hostname,omitempty"`
    28  	CustomOriginServer        string                                  `json:"custom_origin_server,omitempty"`
    29  	CustomOriginSNI           string                                  `json:"custom_origin_sni,omitempty"`
    30  	SSL                       *CustomHostnameSSL                      `json:"ssl,omitempty"`
    31  	CustomMetadata            CustomMetadata                          `json:"custom_metadata,omitempty"`
    32  	Status                    CustomHostnameStatus                    `json:"status,omitempty"`
    33  	VerificationErrors        []string                                `json:"verification_errors,omitempty"`
    34  	OwnershipVerification     CustomHostnameOwnershipVerification     `json:"ownership_verification,omitempty"`
    35  	OwnershipVerificationHTTP CustomHostnameOwnershipVerificationHTTP `json:"ownership_verification_http,omitempty"`
    36  	CreatedAt                 *time.Time                              `json:"created_at,omitempty"`
    37  }
    38  
    39  // CustomHostnameSSL represents the SSL section in a given custom hostname.
    40  type CustomHostnameSSL struct {
    41  	ID                   string                          `json:"id,omitempty"`
    42  	Status               string                          `json:"status,omitempty"`
    43  	Method               string                          `json:"method,omitempty"`
    44  	Type                 string                          `json:"type,omitempty"`
    45  	Wildcard             *bool                           `json:"wildcard,omitempty"`
    46  	CustomCertificate    string                          `json:"custom_certificate,omitempty"`
    47  	CustomKey            string                          `json:"custom_key,omitempty"`
    48  	CertificateAuthority string                          `json:"certificate_authority,omitempty"`
    49  	Issuer               string                          `json:"issuer,omitempty"`
    50  	SerialNumber         string                          `json:"serial_number,omitempty"`
    51  	Settings             CustomHostnameSSLSettings       `json:"settings,omitempty"`
    52  	Certificates         []CustomHostnameSSLCertificates `json:"certificates,omitempty"`
    53  	// Deprecated: use ValidationRecords.
    54  	// If there a single validation record, this will equal ValidationRecords[0] for backwards compatibility.
    55  	SSLValidationRecord
    56  	ValidationRecords []SSLValidationRecord `json:"validation_records,omitempty"`
    57  	ValidationErrors  []SSLValidationError  `json:"validation_errors,omitempty"`
    58  }
    59  
    60  // CustomHostnameSSLSettings represents the SSL settings for a custom hostname.
    61  type CustomHostnameSSLSettings struct {
    62  	HTTP2         string   `json:"http2,omitempty"`
    63  	HTTP3         string   `json:"http3,omitempty"`
    64  	TLS13         string   `json:"tls_1_3,omitempty"`
    65  	MinTLSVersion string   `json:"min_tls_version,omitempty"`
    66  	Ciphers       []string `json:"ciphers,omitempty"`
    67  	EarlyHints    string   `json:"early_hints,omitempty"`
    68  }
    69  
    70  // CustomHostnameOwnershipVerification represents ownership verification status of a given custom hostname.
    71  type CustomHostnameOwnershipVerification struct {
    72  	Type  string `json:"type,omitempty"`
    73  	Name  string `json:"name,omitempty"`
    74  	Value string `json:"value,omitempty"`
    75  }
    76  
    77  // CustomHostnameSSLCertificates represent certificate properties like issuer, expires date and etc.
    78  type CustomHostnameSSLCertificates struct {
    79  	Issuer            string     `json:"issuer"`
    80  	SerialNumber      string     `json:"serial_number"`
    81  	Signature         string     `json:"signature"`
    82  	ExpiresOn         *time.Time `json:"expires_on"`
    83  	IssuedOn          *time.Time `json:"issued_on"`
    84  	FingerprintSha256 string     `json:"fingerprint_sha256"`
    85  	ID                string     `json:"id"`
    86  }
    87  
    88  // CustomHostnameOwnershipVerificationHTTP represents a response from the Custom Hostnames endpoints.
    89  type CustomHostnameOwnershipVerificationHTTP struct {
    90  	HTTPUrl  string `json:"http_url,omitempty"`
    91  	HTTPBody string `json:"http_body,omitempty"`
    92  }
    93  
    94  // CustomMetadata defines custom metadata for the hostname. This requires logic to be implemented by Cloudflare to act on the data provided.
    95  type CustomMetadata map[string]interface{}
    96  
    97  // SSLValidationError represents errors that occurred during SSL validation.
    98  type SSLValidationError struct {
    99  	Message string `json:"message,omitempty"`
   100  }
   101  
   102  // SSLValidationRecord displays Domain Control Validation tokens.
   103  type SSLValidationRecord struct {
   104  	CnameTarget string `json:"cname_target,omitempty"`
   105  	CnameName   string `json:"cname,omitempty"`
   106  
   107  	TxtName  string `json:"txt_name,omitempty"`
   108  	TxtValue string `json:"txt_value,omitempty"`
   109  
   110  	HTTPUrl  string `json:"http_url,omitempty"`
   111  	HTTPBody string `json:"http_body,omitempty"`
   112  
   113  	Emails []string `json:"emails,omitempty"`
   114  }
   115  
   116  func (client *Client) AddCustomHostname(ctx context.Context, zone, hostname string) (hostnameID string, err error) {
   117  	var res CustomHostname
   118  	input := CustomHostname{
   119  		Hostname: hostname,
   120  		SSL: &CustomHostnameSSL{
   121  			Method: "http",
   122  			Type:   "dv",
   123  			Settings: CustomHostnameSSLSettings{
   124  				HTTP2:         "on",
   125  				HTTP3:         "on",
   126  				MinTLSVersion: "1.2",
   127  				TLS13:         "on",
   128  			},
   129  		},
   130  	}
   131  
   132  	err = client.request(ctx, requestParams{
   133  		Payload: input,
   134  		Method:  http.MethodPost,
   135  		URL:     fmt.Sprintf("/client/v4/zones/%s/custom_hostnames", zone),
   136  	}, &res)
   137  	if err != nil {
   138  		return
   139  	}
   140  
   141  	hostnameID = res.ID
   142  
   143  	return
   144  }
   145  
   146  func (client *Client) RemoveCustomHostname(ctx context.Context, zone, hostnameID string) (err error) {
   147  	err = client.request(ctx, requestParams{
   148  		Method: http.MethodDelete,
   149  		URL:    fmt.Sprintf("/client/v4/zones/%s/custom_hostnames/%s", zone, hostnameID),
   150  	}, nil)
   151  
   152  	return
   153  }
   154  
   155  func (client *Client) GetCustomHostnameDetails(ctx context.Context, zone, hostnameID string) (res CustomHostname, err error) {
   156  	err = client.request(ctx, requestParams{
   157  		Method: http.MethodGet,
   158  		URL:    fmt.Sprintf("/client/v4/zones/%s/custom_hostnames/%s", zone, hostnameID),
   159  	}, &res)
   160  	return
   161  }