feat: add baishan cdn deployer

This commit is contained in:
Fu Diwei
2025-02-17 20:19:31 +08:00
parent c72dc0d2c4
commit b2eb5d2754
34 changed files with 866 additions and 285 deletions

45
internal/pkg/vendors/baishan-sdk/api.go vendored Normal file
View File

@@ -0,0 +1,45 @@
package baishansdk
import (
"encoding/json"
"net/http"
)
func (c *Client) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := CreateCertificateResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/certificate", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *Client) GetDomainConfig(req *GetDomainConfigRequest) (*GetDomainConfigResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := GetDomainConfigResponse{}
err := c.sendRequestWithResult(http.MethodGet, "/v2/domain/config", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *Client) SetDomainConfig(req *SetDomainConfigRequest) (*SetDomainConfigResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := SetDomainConfigResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/config", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}

View File

@@ -0,0 +1,84 @@
package baishansdk
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/go-resty/resty/v2"
)
type Client struct {
apiToken string
client *resty.Client
}
func NewClient(apiToken string) *Client {
client := resty.New()
return &Client{
apiToken: apiToken,
client: client,
}
}
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
return c
}
func (c *Client) sendRequest(method string, path string, params map[string]any) (*resty.Response, error) {
url := "https://cdn.api.baishan.com" + path
req := c.client.R()
if method == http.MethodGet {
data := make(map[string]string)
for k, v := range params {
data[k] = fmt.Sprintf("%v", v)
}
req = req.
SetQueryParams(data).
SetQueryParam("token", c.apiToken)
} else if method == http.MethodPost {
req = req.
SetHeader("Content-Type", "application/json").
SetQueryParam("token", c.apiToken).
SetBody(params)
}
var resp *resty.Response
var err error
if method == http.MethodGet {
resp, err = req.Get(url)
} else if method == http.MethodPost {
resp, err = req.Post(url)
} else {
return nil, fmt.Errorf("baishan: unsupported method: %s", method)
}
if err != nil {
return nil, fmt.Errorf("baishan: failed to send request: %w", err)
} else if resp.IsError() {
return nil, fmt.Errorf("baishan: unexpected status code: %d, %s", resp.StatusCode(), resp.Body())
}
return resp, nil
}
func (c *Client) sendRequestWithResult(method string, path string, params map[string]any, result BaseResponse) error {
resp, err := c.sendRequest(method, path, params)
if err != nil {
return err
}
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("baishan: failed to parse response: %w", err)
}
if result.GetCode() != 0 {
return fmt.Errorf("baishan api error: %d, %s", result.GetCode(), result.GetMessage())
}
return nil
}

View File

@@ -0,0 +1,77 @@
package baishansdk
type BaseResponse interface {
GetCode() int
GetMessage() string
}
type baseResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (r *baseResponse) GetCode() int {
return r.Code
}
func (r *baseResponse) GetMessage() string {
return r.Message
}
type CreateCertificateRequest struct {
Certificate string `json:"certificate"`
Key string `json:"key"`
Name string `json:"name"`
}
type CreateCertificateResponse struct {
baseResponse
Data *DomainCertificate `json:"data"`
}
type GetDomainConfigRequest struct {
Domains string `json:"domains"`
Config string `json:"config"`
}
type GetDomainConfigResponse struct {
baseResponse
Data []*GetDomainConfigResponseData `json:"data"`
}
type GetDomainConfigResponseData struct {
Domain string `json:"domain"`
Config *DomainConfig `json:"config"`
}
type SetDomainConfigRequest struct {
Domains string `json:"domains"`
Config *DomainConfig `json:"config"`
}
type SetDomainConfigResponse struct {
baseResponse
Data *SetDomainConfigResponseData `json:"data"`
}
type SetDomainConfigResponseData struct {
Config *DomainConfig `json:"config"`
}
type DomainCertificate struct {
CertId int64 `json:"cert_id"`
Name string `json:"name"`
CertStartTime string `json:"cert_start_time"`
CertExpireTime string `json:"cert_expire_time"`
}
type DomainConfig struct {
Https *DomainConfigHttps `json:"https"`
}
type DomainConfigHttps struct {
CertId int64 `json:"cert_id"`
ForceHttps *string `json:"force_https,omitempty"`
EnableHttp2 *string `json:"http2,omitempty"`
EnableOcsp *string `json:"ocsp,omitempty"`
}

View File

@@ -1,48 +1,44 @@
package btpanelsdk
type BaseResponse interface {
GetStatus() *bool
GetMsg() *string
import (
"encoding/json"
)
func (c *Client) ConfigSavePanelSSL(req *ConfigSavePanelSSLRequest) (*ConfigSavePanelSSLResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := ConfigSavePanelSSLResponse{}
err := c.sendRequestWithResult("/config?action=SavePanelSSL", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
type baseResponse struct {
Status *bool `json:"status,omitempty"`
Msg *string `json:"msg,omitempty"`
func (c *Client) SiteSetSSL(req *SiteSetSSLRequest) (*SiteSetSSLResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := SiteSetSSLResponse{}
err := c.sendRequestWithResult("/site?action=SetSSL", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (r *baseResponse) GetStatus() *bool {
return r.Status
}
func (c *Client) SystemServiceAdmin(req *SystemServiceAdminRequest) (*SystemServiceAdminResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
func (r *baseResponse) GetMsg() *string {
return r.Msg
}
type ConfigSavePanelSSLRequest struct {
PrivateKey string `json:"privateKey"`
Certificate string `json:"certPem"`
}
type ConfigSavePanelSSLResponse struct {
baseResponse
}
type SiteSetSSLRequest struct {
Type string `json:"type"`
SiteName string `json:"siteName"`
PrivateKey string `json:"key"`
Certificate string `json:"csr"`
}
type SiteSetSSLResponse struct {
baseResponse
}
type SystemServiceAdminRequest struct {
Name string `json:"name"`
Type string `json:"type"`
}
type SystemServiceAdminResponse struct {
baseResponse
result := SystemServiceAdminResponse{}
err := c.sendRequestWithResult("/system?action=ServiceAdmin", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}

View File

@@ -11,67 +11,28 @@ import (
"github.com/go-resty/resty/v2"
)
type BaoTaPanelClient struct {
type Client struct {
apiHost string
apiKey string
client *resty.Client
}
func NewBaoTaPanelClient(apiHost, apiKey string) *BaoTaPanelClient {
func NewClient(apiHost, apiKey string) *Client {
client := resty.New()
return &BaoTaPanelClient{
return &Client{
apiHost: apiHost,
apiKey: apiKey,
client: client,
}
}
func (c *BaoTaPanelClient) WithTimeout(timeout time.Duration) *BaoTaPanelClient {
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
return c
}
func (c *BaoTaPanelClient) ConfigSavePanelSSL(req *ConfigSavePanelSSLRequest) (*ConfigSavePanelSSLResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := ConfigSavePanelSSLResponse{}
err := c.sendRequestWithResult("/config?action=SavePanelSSL", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *BaoTaPanelClient) SiteSetSSL(req *SiteSetSSLRequest) (*SiteSetSSLResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := SiteSetSSLResponse{}
err := c.sendRequestWithResult("/site?action=SetSSL", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *BaoTaPanelClient) SystemServiceAdmin(req *SystemServiceAdminRequest) (*SystemServiceAdminResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := SystemServiceAdminResponse{}
err := c.sendRequestWithResult("/system?action=ServiceAdmin", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *BaoTaPanelClient) generateSignature(timestamp string) string {
func (c *Client) generateSignature(timestamp string) string {
keyMd5 := md5.Sum([]byte(c.apiKey))
keyMd5Hex := strings.ToLower(hex.EncodeToString(keyMd5[:]))
@@ -80,7 +41,7 @@ func (c *BaoTaPanelClient) generateSignature(timestamp string) string {
return signMd5Hex
}
func (c *BaoTaPanelClient) sendRequest(path string, params map[string]any) (*resty.Response, error) {
func (c *Client) sendRequest(path string, params map[string]any) (*resty.Response, error) {
if params == nil {
params = make(map[string]any)
}
@@ -105,7 +66,7 @@ func (c *BaoTaPanelClient) sendRequest(path string, params map[string]any) (*res
return resp, nil
}
func (c *BaoTaPanelClient) sendRequestWithResult(path string, params map[string]any, result BaseResponse) error {
func (c *Client) sendRequestWithResult(path string, params map[string]any, result BaseResponse) error {
resp, err := c.sendRequest(path, params)
if err != nil {
return err

View File

@@ -0,0 +1,48 @@
package btpanelsdk
type BaseResponse interface {
GetStatus() *bool
GetMsg() *string
}
type baseResponse struct {
Status *bool `json:"status,omitempty"`
Msg *string `json:"msg,omitempty"`
}
func (r *baseResponse) GetStatus() *bool {
return r.Status
}
func (r *baseResponse) GetMsg() *string {
return r.Msg
}
type ConfigSavePanelSSLRequest struct {
PrivateKey string `json:"privateKey"`
Certificate string `json:"certPem"`
}
type ConfigSavePanelSSLResponse struct {
baseResponse
}
type SiteSetSSLRequest struct {
Type string `json:"type"`
SiteName string `json:"siteName"`
PrivateKey string `json:"key"`
Certificate string `json:"csr"`
}
type SiteSetSSLResponse struct {
baseResponse
}
type SystemServiceAdminRequest struct {
Name string `json:"name"`
Type string `json:"type"`
}
type SystemServiceAdminResponse struct {
baseResponse
}

View File

@@ -1,79 +1,57 @@
package gnamesdk
type BaseResponse interface {
GetCode() int
GetMsg() string
import (
"encoding/json"
)
func (c *Client) AddDomainResolution(req *AddDomainResolutionRequest) (*AddDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := AddDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/add", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
type baseResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
func (c *Client) ModifyDomainResolution(req *ModifyDomainResolutionRequest) (*ModifyDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := ModifyDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/edit", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (r *baseResponse) GetCode() int {
return r.Code
func (c *Client) DeleteDomainResolution(req *DeleteDomainResolutionRequest) (*DeleteDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := DeleteDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/delete", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (r *baseResponse) GetMsg() string {
return r.Msg
}
func (c *Client) ListDomainResolution(req *ListDomainResolutionRequest) (*ListDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
type AddDomainResolutionRequest struct {
ZoneName string `json:"ym"`
RecordType string `json:"lx"`
RecordName string `json:"zj"`
RecordValue string `json:"jlz"`
MX int `json:"mx"`
TTL int `json:"ttl"`
}
type AddDomainResolutionResponse struct {
baseResponse
Data int `json:"data"`
}
type ModifyDomainResolutionRequest struct {
ID string `json:"jxid"`
ZoneName string `json:"ym"`
RecordType string `json:"lx"`
RecordName string `json:"zj"`
RecordValue string `json:"jlz"`
MX int `json:"mx"`
TTL int `json:"ttl"`
}
type ModifyDomainResolutionResponse struct {
baseResponse
}
type DeleteDomainResolutionRequest struct {
ZoneName string `json:"ym"`
RecordID string `json:"jxid"`
}
type DeleteDomainResolutionResponse struct {
baseResponse
}
type ListDomainResolutionRequest struct {
ZoneName string `json:"ym"`
Page *int `json:"page,omitempty"`
PageSize *int `json:"limit,omitempty"`
}
type ListDomainResolutionResponse struct {
baseResponse
Count int `json:"count"`
Data []*ResolutionRecord `json:"data"`
Page int `json:"page"`
PageSize int `json:"pagesize"`
}
type ResolutionRecord struct {
ID string `json:"id"`
ZoneName string `json:"ym"`
RecordType string `json:"lx"`
RecordName string `json:"zjt"`
RecordValue string `json:"jxz"`
MX int `json:"mx"`
result := ListDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/list", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}

View File

@@ -12,80 +12,28 @@ import (
"github.com/go-resty/resty/v2"
)
type GnameClient struct {
type Client struct {
appId string
appKey string
client *resty.Client
}
func NewGnameClient(appId, appKey string) *GnameClient {
func NewClient(appId, appKey string) *Client {
client := resty.New()
return &GnameClient{
return &Client{
appId: appId,
appKey: appKey,
client: client,
}
}
func (c *GnameClient) WithTimeout(timeout time.Duration) *GnameClient {
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
return c
}
func (c *GnameClient) AddDomainResolution(req *AddDomainResolutionRequest) (*AddDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := AddDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/add", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *GnameClient) ModifyDomainResolution(req *ModifyDomainResolutionRequest) (*ModifyDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := ModifyDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/edit", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *GnameClient) DeleteDomainResolution(req *DeleteDomainResolutionRequest) (*DeleteDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := DeleteDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/delete", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *GnameClient) ListDomainResolution(req *ListDomainResolutionRequest) (*ListDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := ListDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/list", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *GnameClient) generateSignature(params map[string]string) string {
func (c *Client) generateSignature(params map[string]string) string {
// Step 1: Sort parameters by ASCII order
var keys []string
for k := range params {
@@ -109,7 +57,7 @@ func (c *GnameClient) generateSignature(params map[string]string) string {
return strings.ToUpper(fmt.Sprintf("%x", hash))
}
func (c *GnameClient) sendRequest(path string, params map[string]any) (*resty.Response, error) {
func (c *Client) sendRequest(path string, params map[string]any) (*resty.Response, error) {
if params == nil {
params = make(map[string]any)
}
@@ -136,7 +84,7 @@ func (c *GnameClient) sendRequest(path string, params map[string]any) (*resty.Re
return resp, nil
}
func (c *GnameClient) sendRequestWithResult(path string, params map[string]any, result BaseResponse) error {
func (c *Client) sendRequestWithResult(path string, params map[string]any, result BaseResponse) error {
resp, err := c.sendRequest(path, params)
if err != nil {
return err

View File

@@ -0,0 +1,79 @@
package gnamesdk
type BaseResponse interface {
GetCode() int
GetMsg() string
}
type baseResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
func (r *baseResponse) GetCode() int {
return r.Code
}
func (r *baseResponse) GetMsg() string {
return r.Msg
}
type AddDomainResolutionRequest struct {
ZoneName string `json:"ym"`
RecordType string `json:"lx"`
RecordName string `json:"zj"`
RecordValue string `json:"jlz"`
MX int `json:"mx"`
TTL int `json:"ttl"`
}
type AddDomainResolutionResponse struct {
baseResponse
Data int `json:"data"`
}
type ModifyDomainResolutionRequest struct {
ID string `json:"jxid"`
ZoneName string `json:"ym"`
RecordType string `json:"lx"`
RecordName string `json:"zj"`
RecordValue string `json:"jlz"`
MX int `json:"mx"`
TTL int `json:"ttl"`
}
type ModifyDomainResolutionResponse struct {
baseResponse
}
type DeleteDomainResolutionRequest struct {
ZoneName string `json:"ym"`
RecordID string `json:"jxid"`
}
type DeleteDomainResolutionResponse struct {
baseResponse
}
type ListDomainResolutionRequest struct {
ZoneName string `json:"ym"`
Page *int `json:"page,omitempty"`
PageSize *int `json:"limit,omitempty"`
}
type ListDomainResolutionResponse struct {
baseResponse
Count int `json:"count"`
Data []*ResolutionRecord `json:"data"`
Page int `json:"page"`
PageSize int `json:"pagesize"`
}
type ResolutionRecord struct {
ID string `json:"id"`
ZoneName string `json:"ym"`
RecordType string `json:"lx"`
RecordName string `json:"zjt"`
RecordValue string `json:"jxz"`
MX int `json:"mx"`
}

View File

@@ -1,34 +1,18 @@
package safelinesdk
type BaseResponse interface {
GetErrCode() *string
GetErrMsg() *string
}
import (
"encoding/json"
)
type baseResponse struct {
ErrCode *string `json:"err,omitempty"`
ErrMsg *string `json:"msg,omitempty"`
}
func (c *Client) UpdateCertificate(req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
func (r *baseResponse) GetErrCode() *string {
return r.ErrCode
}
func (r *baseResponse) GetErrMsg() *string {
return r.ErrMsg
}
type UpdateCertificateRequest struct {
Id int32 `json:"id"`
Type int32 `json:"type"`
Manual *UpdateCertificateRequestBodyManul `json:"manual"`
}
type UpdateCertificateRequestBodyManul struct {
Crt string `json:"crt"`
Key string `json:"key"`
}
type UpdateCertificateResponse struct {
baseResponse
result := UpdateCertificateResponse{}
err := c.sendRequestWithResult("/api/open/cert", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}

View File

@@ -9,41 +9,28 @@ import (
"github.com/go-resty/resty/v2"
)
type SafeLineClient struct {
type Client struct {
apiHost string
apiToken string
client *resty.Client
}
func NewSafeLineClient(apiHost, apiToken string) *SafeLineClient {
func NewClient(apiHost, apiToken string) *Client {
client := resty.New()
return &SafeLineClient{
return &Client{
apiHost: apiHost,
apiToken: apiToken,
client: client,
}
}
func (c *SafeLineClient) WithTimeout(timeout time.Duration) *SafeLineClient {
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
return c
}
func (c *SafeLineClient) UpdateCertificate(req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := UpdateCertificateResponse{}
err := c.sendRequestWithResult("/api/open/cert", params, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (c *SafeLineClient) sendRequest(path string, params map[string]any) (*resty.Response, error) {
func (c *Client) sendRequest(path string, params map[string]any) (*resty.Response, error) {
if params == nil {
params = make(map[string]any)
}
@@ -65,7 +52,7 @@ func (c *SafeLineClient) sendRequest(path string, params map[string]any) (*resty
return resp, nil
}
func (c *SafeLineClient) sendRequestWithResult(path string, params map[string]any, result BaseResponse) error {
func (c *Client) sendRequestWithResult(path string, params map[string]any, result BaseResponse) error {
resp, err := c.sendRequest(path, params)
if err != nil {
return err

View File

@@ -0,0 +1,34 @@
package safelinesdk
type BaseResponse interface {
GetErrCode() *string
GetErrMsg() *string
}
type baseResponse struct {
ErrCode *string `json:"err,omitempty"`
ErrMsg *string `json:"msg,omitempty"`
}
func (r *baseResponse) GetErrCode() *string {
return r.ErrCode
}
func (r *baseResponse) GetErrMsg() *string {
return r.ErrMsg
}
type UpdateCertificateRequest struct {
Id int32 `json:"id"`
Type int32 `json:"type"`
Manual *UpdateCertificateRequestBodyManul `json:"manual"`
}
type UpdateCertificateRequestBodyManul struct {
Crt string `json:"crt"`
Key string `json:"key"`
}
type UpdateCertificateResponse struct {
baseResponse
}