feat: add user-agent http header for thirdparty sdks

This commit is contained in:
Fu Diwei 2025-05-28 10:46:02 +08:00
parent ddb46f9dda
commit 829fa29cf1
30 changed files with 66 additions and 43 deletions

View File

@ -52,8 +52,9 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
// REF: https://discord.com/developers/docs/resources/message#create-message
req := n.httpClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("Authorization", "Bot "+n.config.BotToken).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetBody(map[string]any{
"content": subject + "\n" + message,
})

View File

@ -57,8 +57,9 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
// REF: https://gotify.net/api-docs#/message/createMessage
req := n.httpClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("Authorization", "Bearer "+n.config.Token).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetBody(map[string]any{
"title": subject,
"message": message,

View File

@ -60,6 +60,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
loginReq := n.httpClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetBody(map[string]any{
"login_id": n.config.Username,
"password": n.config.Password,
@ -76,8 +77,9 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
// REF: https://developers.mattermost.com/api-documentation/#/operations/CreatePost
postReq := n.httpClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("Authorization", "Bearer "+loginResp.Header().Get("Token")).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetBody(map[string]any{
"channel_id": n.config.ChannelId,
"props": map[string]interface{}{

View File

@ -53,6 +53,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
req := n.httpClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetBody(map[string]any{
"title": subject,
"message": message,

View File

@ -52,6 +52,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
req := n.httpClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetBody(map[string]any{
"title": subject,
"content": message,

View File

@ -51,6 +51,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
req := n.httpClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetBody(map[string]any{
"text": subject,
"desp": message,

View File

@ -52,8 +52,9 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
// REF: https://docs.slack.dev/messaging/sending-and-scheduling-messages#publishing
req := n.httpClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("Authorization", "Bearer "+n.config.BotToken).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetBody(map[string]any{
"token": n.config.BotToken,
"channel": n.config.ChannelId,

View File

@ -53,6 +53,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
req := n.httpClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetBody(map[string]any{
"chat_id": n.config.ChatId,
"text": subject + "\n" + message,

View File

@ -51,6 +51,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
req := n.httpClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetBody(map[string]any{
"msgtype": "text",
"text": map[string]string{

View File

@ -14,8 +14,6 @@ import (
)
type Client struct {
apiKey string
client *resty.Client
}
@ -25,7 +23,8 @@ func NewClient(serverUrl, apiVersion, apiKey string) *Client {
}
client := resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/") + "/api/" + apiVersion).
SetBaseURL(strings.TrimRight(serverUrl, "/")+"/api/"+apiVersion).
SetHeader("User-Agent", "certimate").
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
timestamp := fmt.Sprintf("%d", time.Now().Unix())
tokenMd5 := md5.Sum([]byte("1panel" + apiKey + timestamp))

View File

@ -19,7 +19,8 @@ type Client struct {
func NewClient(apiToken string) *Client {
client := resty.New().
SetBaseURL("https://cdn.api.baishan.com").
SetHeader("token", apiToken)
SetHeader("User-Agent", "certimate").
SetHeader("Token", apiToken)
return &Client{
client: client,

View File

@ -21,7 +21,9 @@ type Client struct {
func NewClient(serverUrl, apiKey string) *Client {
client := resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/"))
SetBaseURL(strings.TrimRight(serverUrl, "/")).
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetHeader("User-Agent", "certimate")
return &Client{
apiKey: apiKey,
@ -77,9 +79,7 @@ func (c *Client) sendRequest(path string, params interface{}) (*resty.Response,
data["request_time"] = fmt.Sprintf("%d", timestamp)
data["request_token"] = c.generateSignature(fmt.Sprintf("%d", timestamp))
req := c.client.R().
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetFormData(data)
req := c.client.R().SetFormData(data)
resp, err := req.Post(path)
if err != nil {
return resp, fmt.Errorf("baota api error: failed to send request: %w", err)

View File

@ -19,7 +19,9 @@ type Client struct {
func NewClient(serverUrl, apiKey string) *Client {
client := resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/") + "/api").
SetBaseURL(strings.TrimRight(serverUrl, "/")+"/api").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
timestamp := fmt.Sprintf("%d", time.Now().Unix())
keyMd5 := md5.Sum([]byte(apiKey))
@ -48,9 +50,7 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client {
}
func (c *Client) sendRequest(path string, params interface{}) (*resty.Response, error) {
req := c.client.R().
SetHeader("Content-Type", "application/json").
SetBody(params)
req := c.client.R().SetBody(params)
resp, err := req.Post(path)
if err != nil {
return resp, fmt.Errorf("baota api error: failed to send request: %w", err)

View File

@ -17,6 +17,7 @@ type Client struct {
func NewClient(apiToken string) *Client {
client := resty.New().
SetBaseURL("https://api.bunny.net").
SetHeader("User-Agent", "certimate").
SetHeader("AccessKey", apiToken)
return &Client{

View File

@ -17,7 +17,7 @@ type Client struct {
func NewClient(apiToken string) *Client {
client := resty.New().
SetBaseURL("https://api.cachefly.com/api/2.5").
SetHeader("x-cf-authorization", "Bearer "+apiToken)
SetHeader("X-CF-Authorization", "Bearer "+apiToken)
return &Client{
client: client,

View File

@ -18,8 +18,9 @@ type Client struct {
func NewClient(serverUrl, apiKey, apiSecret string) *Client {
client := resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/")).
SetHeader("api-key", apiKey).
SetHeader("api-secret", apiSecret)
SetHeader("User-Agent", "certimate").
SetHeader("API-Key", apiKey).
SetHeader("API-Secret", apiSecret)
return &Client{
client: client,

View File

@ -51,6 +51,7 @@ func NewClient(username, password string) *Client {
client.serverlessClient = resty.New()
client.apiClient = resty.New().
SetBaseURL("https://unicloud-api.dcloud.net.cn/unicloud/api").
SetHeader("User-Agent", "certimate").
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
if client.apiUserToken != "" {
req.Header.Set("Token", client.apiUserToken)
@ -173,9 +174,9 @@ func (c *Client) invokeServerless(endpoint, clientSecret, appId, spaceId, target
sign := c.generateSignature(payload, clientSecret)
req := c.serverlessClient.R().
SetHeader("Content-Type", "application/json").
SetHeader("Origin", "https://unicloud.dcloud.net.cn").
SetHeader("Referer", "https://unicloud.dcloud.net.cn").
SetHeader("Content-Type", "application/json").
SetHeader("X-Client-Info", string(clientInfoJsonb)).
SetHeader("X-Client-Token", c.serverlessJwtToken).
SetHeader("X-Serverless-Sign", sign).

View File

@ -17,7 +17,8 @@ type Client struct {
func NewClient(apiId, apiSecret string) *Client {
client := resty.New().
SetBaseURL("https://api.dns.la/api").
SetBasicAuth(apiId, apiSecret)
SetBasicAuth(apiId, apiSecret).
SetHeader("User-Agent", "certimate")
return &Client{
client: client,

View File

@ -32,6 +32,7 @@ func NewClient(serverUrl, apiRole, accessKeyId, accessKey string) *Client {
}
client.client = resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/")).
SetHeader("User-Agent", "certimate").
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
if client.accessToken != "" {
req.Header.Set("X-Cloud-Access-Token", client.accessToken)

View File

@ -20,7 +20,10 @@ type Client struct {
}
func NewClient(appId, appKey string) *Client {
client := resty.New()
client := resty.New().
SetBaseURL("http://api.gname.com").
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetHeader("User-Agent", "certimate")
return &Client{
appId: appId,
@ -74,11 +77,8 @@ func (c *Client) sendRequest(path string, params interface{}) (*resty.Response,
data["gntime"] = fmt.Sprintf("%d", time.Now().Unix())
data["gntoken"] = c.generateSignature(data)
url := "http://api.gname.com" + path
req := c.client.R().
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetFormData(data)
resp, err := req.Post(url)
req := c.client.R().SetFormData(data)
resp, err := req.Post(path)
if err != nil {
return resp, fmt.Errorf("gname api error: failed to send request: %w", err)
} else if resp.IsError() {

View File

@ -32,6 +32,7 @@ func NewClient(serverUrl, apiRole, accessKeyId, accessKey string) *Client {
}
client.client = resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/")).
SetHeader("User-Agent", "certimate").
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
if client.accessToken != "" {
req.Header.Set("X-Edge-Access-Token", client.accessToken)

View File

@ -28,7 +28,8 @@ func NewClient(serverUrl, username, password string) *Client {
password: password,
}
client.client = resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/") + "/prod-api").
SetBaseURL(strings.TrimRight(serverUrl, "/")+"/prod-api").
SetHeader("User-Agent", "certimate").
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
if client.accessToken != "" {
req.Header.Set("Authorization", "Bearer "+client.accessToken)

View File

@ -28,7 +28,8 @@ func NewClient(serverUrl, username, password string) *Client {
password: password,
}
client.client = resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/") + "/prod-api").
SetBaseURL(strings.TrimRight(serverUrl, "/")+"/prod-api").
SetHeader("User-Agent", "certimate").
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
if client.accessToken != "" {
req.Header.Set("Authorization", "Bearer "+client.accessToken)

View File

@ -17,7 +17,8 @@ type Client struct {
func NewClient(apiToken string) *Client {
client := resty.New().
SetBaseURL("https://api.netlify.com/api/v1").
SetHeader("Authorization", "Bearer "+apiToken)
SetHeader("Authorization", "Bearer "+apiToken).
SetHeader("User-Agent", "certimate")
return &Client{
client: client,

View File

@ -17,7 +17,8 @@ type Client struct {
func NewClient(apiKey string) *Client {
client := resty.New().
SetBaseURL("https://api.v2.rainyun.com").
SetHeader("x-api-key", apiKey)
SetHeader("User-Agent", "certimate").
SetHeader("X-API-Key", apiKey)
return &Client{
client: client,

View File

@ -25,6 +25,7 @@ func NewClient(serverUrl string, accessTokenId int32, accessToken string) *Clien
SetBaseURL(strings.TrimRight(serverUrl, "/")+"/api").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
var body []byte
var err error

View File

@ -17,6 +17,8 @@ type Client struct {
func NewClient(serverUrl, apiToken string) *Client {
client := resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/")).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetHeader("X-SLCE-API-TOKEN", apiToken)
return &Client{
@ -35,9 +37,7 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client {
}
func (c *Client) sendRequest(path string, params interface{}) (*resty.Response, error) {
req := c.client.R().
SetHeader("Content-Type", "application/json").
SetBody(params)
req := c.client.R().SetBody(params)
resp, err := req.Post(path)
if err != nil {
return resp, fmt.Errorf("safeline api error: failed to send request: %w", err)

View File

@ -26,6 +26,7 @@ func NewClient(username, password string) *Client {
}
client.client = resty.New().
SetBaseURL("https://console.upyun.com").
SetHeader("User-Agent", "certimate").
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
if client.loginCookie != "" {
req.Header.Set("Cookie", client.loginCookie)

View File

@ -11,7 +11,7 @@ import (
func (c *Client) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
resp := &CreateCertificateResponse{}
rres, err := c.client.SendRequestWithResult(http.MethodPost, "/cdn/certificates", req, resp, func(r *resty.Request) {
r.SetHeader("x-cnc-timestamp", fmt.Sprintf("%d", req.Timestamp))
r.SetHeader("X-CNC-Timestamp", fmt.Sprintf("%d", req.Timestamp))
})
if err != nil {
return resp, err
@ -28,7 +28,7 @@ func (c *Client) UpdateCertificate(certificateId string, req *UpdateCertificateR
resp := &UpdateCertificateResponse{}
rres, err := c.client.SendRequestWithResult(http.MethodPatch, fmt.Sprintf("/cdn/certificates/%s", url.PathEscape(certificateId)), req, resp, func(r *resty.Request) {
r.SetHeader("x-cnc-timestamp", fmt.Sprintf("%d", req.Timestamp))
r.SetHeader("X-CNC-Timestamp", fmt.Sprintf("%d", req.Timestamp))
})
if err != nil {
return resp, err

View File

@ -30,9 +30,10 @@ type Result interface {
func NewClient(accessKey, secretKey string) *Client {
client := resty.New().
SetBaseURL("https://open.chinanetcenter.com").
SetHeader("Host", "open.chinanetcenter.com").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("Host", "open.chinanetcenter.com").
SetHeader("User-Agent", "certimate").
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
// Step 1: Get request method
method := req.Method
@ -85,7 +86,7 @@ func NewClient(accessKey, secretKey string) *Client {
// Step 6: Get timestamp
var reqtime time.Time
timestampString := req.Header.Get("x-cnc-timestamp")
timestampString := req.Header.Get("X-CNC-Timestamp")
if timestampString == "" {
reqtime = time.Now().UTC()
timestampString = fmt.Sprintf("%d", reqtime.Unix())
@ -111,9 +112,9 @@ func NewClient(accessKey, secretKey string) *Client {
signHex := strings.ToLower(hex.EncodeToString(sign))
// Step 9: Add headers to request
req.Header.Set("x-cnc-accesskey", accessKey)
req.Header.Set("x-cnc-timestamp", timestampString)
req.Header.Set("x-cnc-auth-method", "AKSK")
req.Header.Set("X-CNC-AccessKey", accessKey)
req.Header.Set("X-CNC-Timestamp", timestampString)
req.Header.Set("X-CNC-Auth-Method", "AKSK")
req.Header.Set("Authorization", fmt.Sprintf("%s Credential=%s, SignedHeaders=%s, Signature=%s", SignAlgorithmHeader, accessKey, signedHeaders, signHex))
req.Header.Set("Date", reqtime.Format("Mon, 02 Jan 2006 15:04:05 GMT"))
@ -173,7 +174,7 @@ func (c *Client) SendRequestWithResult(method string, path string, params interf
if err != nil {
if resp != nil {
json.Unmarshal(resp.Body(), &result)
result.SetRequestId(resp.Header().Get("x-cnc-request-id"))
result.SetRequestId(resp.Header().Get("X-CNC-Request-Id"))
}
return resp, err
}
@ -185,6 +186,6 @@ func (c *Client) SendRequestWithResult(method string, path string, params interf
}
}
result.SetRequestId(resp.Header().Get("x-cnc-request-id"))
result.SetRequestId(resp.Header().Get("X-CNC-Request-Id"))
return resp, nil
}