20250306_new

This commit is contained in:
李超 2025-03-06 18:53:00 +08:00
parent 10910035ed
commit c3b814bd68
41 changed files with 5415 additions and 13 deletions

View File

@ -1,4 +1,4 @@
cookie: vindex==3d=18=0AB00=0R;supportLang=cn%2Cen;lang=cn;sessionid=200001a5a07a5f7b4b6a746593a1b6e292fc;loginid=8416edbc52c22382289563de3ed94dab;200001a5a07a5f7b4b6a746593a1b6e292fc=true;abcd1234=true;login=false;
cookie: vindex==3e=10=0AB00=0R;supportLang=cn%2Cen;lang=cn;sessionid=2000011e9f293bf23a1bee459ac4083c17cf;loginid=5d67c39dc9b75343831b85d599f3d06e;2000011e9f293bf23a1bee459ac4083c17cf=true;abcd1234=true;login=false;
mysql:
port: 3306
url: 127.0.0.1

View File

@ -69,7 +69,7 @@ func DT_GET(urls string, headers map[string]string, bytess io.Reader) string {
}
// // 设置请求头
// req.Header.Set("Content-Type", "application/json")
// r.Header.Set("Content-Type", "application/json")
// 使用 map 动态设置请求头 // 设置请求头 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
for key, value := range headers {

Binary file not shown.

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 186 KiB

After

Width:  |  Height:  |  Size: 182 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 248 KiB

After

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 197 KiB

After

Width:  |  Height:  |  Size: 198 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 269 KiB

After

Width:  |  Height:  |  Size: 270 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 253 KiB

After

Width:  |  Height:  |  Size: 254 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 258 KiB

After

Width:  |  Height:  |  Size: 242 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 224 KiB

After

Width:  |  Height:  |  Size: 223 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 KiB

After

Width:  |  Height:  |  Size: 105 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 145 KiB

After

Width:  |  Height:  |  Size: 1.0 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 177 KiB

After

Width:  |  Height:  |  Size: 176 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 290 KiB

After

Width:  |  Height:  |  Size: 290 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 383 KiB

After

Width:  |  Height:  |  Size: 385 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 294 KiB

After

Width:  |  Height:  |  Size: 229 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 190 KiB

After

Width:  |  Height:  |  Size: 193 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 259 KiB

After

Width:  |  Height:  |  Size: 259 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 252 KiB

After

Width:  |  Height:  |  Size: 252 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 260 KiB

After

Width:  |  Height:  |  Size: 259 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 228 KiB

After

Width:  |  Height:  |  Size: 228 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 141 KiB

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 119 KiB

After

Width:  |  Height:  |  Size: 570 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 176 KiB

After

Width:  |  Height:  |  Size: 657 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 289 KiB

After

Width:  |  Height:  |  Size: 288 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 185 KiB

After

Width:  |  Height:  |  Size: 230 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 163 KiB

After

Width:  |  Height:  |  Size: 158 KiB

21
main.go
View File

@ -1,8 +1,8 @@
package main
import (
"dt_automate/openapi"
"dt_automate/tool"
"dt_automate/wps"
"flag"
"fmt"
"log"
@ -20,7 +20,7 @@ var (
func main() {
//SetFlags函数用来设置标准logger的输出配置
log.SetPrefix("DT_AUTOMATE: ")
log.SetPrefix("[DT_AUTOMATE]: ")
log.SetFlags(log.Llongfile | log.Lmicroseconds | log.Ldate)
// key.Keyprin("iPoTwF953OJExi8uLGOALs7cwpdiHyfLYDLXfno9nY7KLsLyOu5wDxN424pLFyIymNAzpNEdVQCkOBQM7Tj3uTkSQ7tOLPSzUXrLgMZj5Ft0s1Iv3Ls9Aq6W0NxBPNSY3r1NuXY3L0378YKoopsc68untFqlrXhjq5PIDah90Tlq/sQ3onL+/qd9xNn4Lj+PcDY8R0edNpOormOduN2p8JpS0H0WaZW5SV0iCYET9jRFcfXY3qZutAD9W3UGO2EjzkyZMfyN8lG4YgUGBTghkw4Ihoe9GQZF5NgYJumhvFzE1S8PvW0gBqQK9K1LpESyN1bcvXIALUEUzT+4vC6UBA==")
// 定义标志 -url 并设置其默认值为空字符串
@ -67,10 +67,10 @@ func main() {
} else {
log.Printf("授权未到期,剩余时间:%d天 %d小时 %d分钟 %d秒", days, hours, minutes, secod)
//可执行程序位置
// 互联网区截图
// // 互联网区截图
// method.HW_FAC1() //截取流量清洗的图片
// method.HW_FW1() //截取防火墙的图片
// method.HW_TSGZ() //截取态势感知的图片
// method.HW_TSGZ() //截取态势感知的图片
// method.HW_DTCLOUD() //截取运维中心的图片
// method.HW_BAOLJ() //截取堡垒机的图片
@ -79,18 +79,21 @@ func main() {
// method.ZWW_TSGZ() //截取态势感知的图片
// method.ZWW_DTCLOUD() //截取运维中心的图片
// method.ZWWW_BAOLJ() //截取堡垒机的图片
//互联网区运维巡检文档生成
// 互联网区运维巡检文档生成
// wps.HW_SYS_Word() //运维平台word文档生成
// // // //政务网区运维巡检文档生成
// //政务网区运维巡检文档生成
// wps.ZWW_SYS_Word() //运维平台word文档生成
// // // //安全巡检文档生成
// //安全巡检文档生成
// wps.SAFET_Word() //安全巡检文档生成
// //安全事件表格生成(需要先执行互联网区防火墙截图)
//安全事件表格生成(需要先执行互联网区防火墙截图)
// attackevent.Fw_event(attackevent.HW_fw1_cookie()) //将防火墙安全事件存放到xlsx文件中
//nessus的csv文件生成docx报告
wps.CSV_damo()
// wps.CSV_damo()
//翻译模块调用腾讯xt翻译月500万字限制
// log.Println(wps.Translate("hello"))
// wps.Weekly_word()
//调用云平台接口
openapi.Git_sign()
}
} else {
log.Println("没有授权")

View File

@ -121,7 +121,7 @@ func HW_DTCLOUD() {
page.WaitForTimeout(500)
page.Locator(`.arco-input-tag-input.arco-input-tag-input-size-default.arco-input-tag-input-autowidth`).Click()
page.Locator(`.arco-select-option-wrapper`).Nth(0).Locator(`.arco-checkbox-mask`).Click()
// page.Locator(`.arco-btn.arco-btn-primary.arco-btn-size-mini.arco-btn-shape-square`).Click()
page.Locator(`.arco-btn.arco-btn-primary.arco-btn-size-mini.arco-btn-shape-square`).Click()
page.WaitForTimeout(1000)
page.Screenshot(playwright.PageScreenshotOptions{
Path: playwright.String("img/hw_dtcloud_06.png"),

View File

@ -123,7 +123,7 @@ func ZWW_DTCLOUD() {
page.WaitForTimeout(1000)
page.Locator(`.arco-input-tag-input.arco-input-tag-input-size-default.arco-input-tag-input-autowidth`).Click()
page.Locator(`.arco-select-option-wrapper`).Nth(0).Locator(`.arco-checkbox-mask`).Click()
// page.Locator(`.arco-btn.arco-btn-primary.arco-btn-size-mini.arco-btn-shape-square`).Click()
page.Locator(`.arco-btn.arco-btn-primary.arco-btn-size-mini.arco-btn-shape-square`).Click()
page.WaitForTimeout(1000)
page.Screenshot(playwright.PageScreenshotOptions{
Path: playwright.String("img/zww_dtcloud_06.png"),

26
openapi/conn_api.go Normal file
View File

@ -0,0 +1,26 @@
package openapi
import (
"dt_automate/conn"
"dt_automate/openapi/sdk"
"log"
"net/url"
)
func Git_sign() {
values := url.Values{}
values.Set("pageSize", "10")
values.Set("pageNum", "1")
values.Set("id", "1933362020262871124")
parseURL, err := url.Parse("https://openapi.user.zww.dtcloud.com/api/account-tenant/v1/tenant/get")
if err != nil {
log.Println("err")
}
parseURL.RawQuery = values.Encode()
urlPathWithParams := parseURL.String()
log.Println(urlPathWithParams)
// 调用sign方法
str := conn.DT_GET(urlPathWithParams, sdk.Sign(), nil)
log.Println(str)
}

1056
openapi/cpu.json Normal file

File diff suppressed because it is too large Load Diff

2921
openapi/data.json.go Normal file

File diff suppressed because it is too large Load Diff

800
openapi/memory.json Normal file
View File

@ -0,0 +1,800 @@
{
"requestId": "784e9c55-221d-4c77-9d27-61484eac0211",
"code": "Success",
"message": "调用成功",
"data": {
"status": "success",
"errorType": "",
"error": "",
"warnings": null,
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"__name__": "vm_memory_usage",
"name": "ecs-l7bokoh5lang31"
},
"values": [
[
1741254033.985,
"100"
],
[
1741254047.985,
"100"
],
[
1741254061.985,
"69.39183782341958"
],
[
1741254075.985,
"69.39183782341958"
],
[
1741254089.985,
"84.19661909842625"
],
[
1741254103.985,
"84.19661909842625"
],
[
1741254117.985,
"91.60359429181115"
],
[
1741254131.985,
"91.60359429181115"
],
[
1741254145.985,
"92.57468658308883"
],
[
1741254159.985,
"92.57468658308883"
],
[
1741254173.985,
"92.57468658308883"
],
[
1741254187.985,
"92.26960522806081"
],
[
1741254201.985,
"92.26960522806081"
],
[
1741254215.985,
"93.06481728460922"
],
[
1741254229.985,
"93.06481728460922"
],
[
1741254243.985,
"92.84059082421979"
],
[
1741254257.985,
"92.84059082421979"
],
[
1741254271.985,
"93.02480661509736"
],
[
1741254285.985,
"93.02480661509736"
],
[
1741254299.985,
"92.98813016804482"
],
[
1741254313.985,
"92.98813016804482"
],
[
1741254327.985,
"93.07982128567618"
],
[
1741254341.985,
"93.07982128567618"
],
[
1741254355.985,
"93.0614830621499"
],
[
1741254369.985,
"93.0614830621499"
],
[
1741254383.985,
"93.0614830621499"
],
[
1741254397.985,
"93.68081488396905"
],
[
1741254411.985,
"93.68081488396905"
],
[
1741254425.985,
"93.46825820218724"
],
[
1741254439.985,
"93.46825820218724"
],
[
1741254453.985,
"93.58745665510803"
],
[
1741254467.985,
"93.58745665510803"
],
[
1741254481.985,
"93.59995998933049"
],
[
1741254495.985,
"93.59995998933049"
],
[
1741254509.985,
"93.38406908508937"
],
[
1741254523.985,
"93.38406908508937"
],
[
1741254537.985,
"93.55244731928515"
],
[
1741254551.985,
"93.55244731928515"
],
[
1741254565.985,
"93.05148039477193"
],
[
1741254579.985,
"93.05148039477193"
],
[
1741254593.985,
"93.05148039477193"
],
[
1741254607.985,
"93.38573619631902"
],
[
1741254621.985,
"93.38573619631902"
],
[
1741254635.985,
"92.88893704987997"
],
[
1741254649.985,
"92.88893704987997"
],
[
1741254663.985,
"93.89003734329154"
],
[
1741254677.985,
"93.89003734329154"
],
[
1741254691.985,
"93.51827153907709"
],
[
1741254705.985,
"93.51827153907709"
],
[
1741254719.985,
"94.86779807948787"
],
[
1741254733.985,
"94.86779807948787"
],
[
1741254747.985,
"93.12316617764738"
],
[
1741254761.985,
"93.12316617764738"
],
[
1741254775.985,
"93.89753934382502"
],
[
1741254789.985,
"93.89753934382502"
],
[
1741254803.985,
"93.89753934382502"
],
[
1741254817.985,
"93.55161376367032"
],
[
1741254831.985,
"93.55161376367032"
],
[
1741254845.985,
"93.66414377167244"
],
[
1741254859.985,
"93.66414377167244"
],
[
1741254873.985,
"94.11926513736996"
],
[
1741254887.985,
"94.11926513736996"
],
[
1741254901.985,
"93.92838090157375"
],
[
1741254915.985,
"93.92838090157375"
],
[
1741254929.985,
"93.4232461989864"
],
[
1741254943.985,
"93.4232461989864"
],
[
1741254957.985,
"94.35766204321152"
],
[
1741254971.985,
"94.35766204321152"
],
[
1741254985.985,
"94.02257268604961"
],
[
1741254999.985,
"94.02257268604961"
],
[
1741255013.985,
"94.02257268604961"
],
[
1741255027.985,
"93.59579221125634"
],
[
1741255041.985,
"93.59579221125634"
],
[
1741255055.985,
"93.23069485196052"
],
[
1741255069.985,
"93.23069485196052"
],
[
1741255083.985,
"93.89253801013604"
],
[
1741255097.985,
"93.89253801013604"
],
[
1741255111.985,
"93.2356961856495"
],
[
1741255125.985,
"93.2356961856495"
],
[
1741255139.985,
"93.92588023472926"
],
[
1741255153.985,
"93.92588023472926"
],
[
1741255167.985,
"93.5024339823953"
],
[
1741255181.985,
"93.5024339823953"
],
[
1741255195.985,
"95.37543344891971"
],
[
1741255209.985,
"95.37543344891971"
],
[
1741255223.985,
"95.37543344891971"
],
[
1741255237.985,
"93.42908108829022"
],
[
1741255251.985,
"93.42908108829022"
],
[
1741255293.985,
"94.22512670045346"
],
[
1741255307.985,
"94.22512670045346"
],
[
1741255321.985,
"94.83278874366498"
],
[
1741255335.985,
"94.83278874366498"
],
[
1741255349.985,
"95.52463990397439"
],
[
1741255363.985,
"95.52463990397439"
],
[
1741255475.985,
"97.36846492397973"
],
[
1741255489.985,
"97.36846492397973"
],
[
1741255503.985,
"95.75303414243798"
],
[
1741255517.985,
"95.75303414243798"
],
[
1741255531.985,
"94.30348092824752"
],
[
1741255545.985,
"94.30348092824752"
],
[
1741255559.985,
"94.88863696985862"
],
[
1741255573.985,
"94.88863696985862"
],
[
1741255587.985,
"94.16261002934115"
],
[
1741255601.985,
"94.16261002934115"
],
[
1741255615.985,
"94.71192317951453"
],
[
1741255629.985,
"94.71192317951453"
],
[
1741255643.985,
"94.71192317951453"
],
[
1741255657.985,
"95.64800613496932"
],
[
1741255671.985,
"95.64800613496932"
],
[
1741255685.985,
"95.68551613763671"
],
[
1741255699.985,
"95.68551613763671"
],
[
1741255713.985,
"98.051146972526"
],
[
1741255727.985,
"98.051146972526"
],
[
1741255741.985,
"94.61439717257936"
],
[
1741255755.985,
"94.61439717257936"
],
[
1741255769.985,
"93.58745665510803"
],
[
1741255783.985,
"93.58745665510803"
],
[
1741255797.985,
"93.8917044545212"
],
[
1741255811.985,
"93.8917044545212"
],
[
1741255825.985,
"94.09509202453987"
],
[
1741255839.985,
"94.09509202453987"
],
[
1741255853.985,
"94.09509202453987"
],
[
1741255867.985,
"95.2712389970659"
],
[
1741255881.985,
"95.2712389970659"
],
[
1741255895.985,
"95.87139903974393"
],
[
1741255909.985,
"95.87139903974393"
],
[
1741255923.985,
"94.33098826353694"
],
[
1741255937.985,
"94.33098826353694"
],
[
1741255979.985,
"96.59325820218724"
],
[
1741255993.985,
"96.59325820218724"
],
[
1741256007.985,
"96.35986263003467"
],
[
1741256021.985,
"96.35986263003467"
],
[
1741256035.985,
"95.03117497999467"
],
[
1741256049.985,
"95.03117497999467"
],
[
1741256063.985,
"95.03117497999467"
],
[
1741256077.985,
"97.71522405974926"
],
[
1741256091.985,
"97.71522405974926"
],
[
1741256217.985,
"98.2386969858629"
],
[
1741256231.985,
"98.2386969858629"
],
[
1741256245.985,
"98.2386969858629"
],
[
1741256259.985,
"98.2386969858629"
],
[
1741256273.985,
"98.2386969858629"
],
[
1741256315.985,
"98.73049479861297"
],
[
1741256329.985,
"98.73049479861297"
],
[
1741256343.985,
"93.58245532141905"
],
[
1741256357.985,
"93.58245532141905"
],
[
1741256371.985,
"94.20678847692719"
],
[
1741256385.985,
"94.20678847692719"
],
[
1741256399.985,
"94.07508668978394"
],
[
1741256413.985,
"94.07508668978394"
],
[
1741256427.985,
"94.07508668978394"
],
[
1741256441.985,
"94.07508668978394"
],
[
1741256455.985,
"96.466557748733"
],
[
1741256469.985,
"96.466557748733"
],
[
1741256483.985,
"96.466557748733"
],
[
1741256497.985,
"96.48156174979995"
],
[
1741256511.985,
"96.48156174979995"
],
[
1741256609.985,
"97.52433982395306"
],
[
1741256623.985,
"97.52433982395306"
],
[
1741256707.985,
"97.63020138703654"
],
[
1741256721.985,
"97.63020138703654"
],
[
1741256735.985,
"96.69245132035209"
],
[
1741256749.985,
"96.69245132035209"
],
[
1741256847.985,
"97.61102960789543"
],
[
1741256861.985,
"97.61102960789543"
],
[
1741256875.985,
"97.61102960789543"
],
[
1741256889.985,
"97.61102960789543"
],
[
1741256903.985,
"97.61102960789543"
],
[
1741256917.985,
"95.31708455588156"
],
[
1741256931.985,
"95.31708455588156"
],
[
1741256945.985,
"96.93418244865298"
],
[
1741256959.985,
"96.93418244865298"
],
[
1741256973.985,
"97.59769271805816"
],
[
1741256987.985,
"97.59769271805816"
],
[
1741257029.985,
"97.05421445718858"
],
[
1741257043.985,
"97.05421445718858"
],
[
1741257085.985,
"98.17784742598026"
],
[
1741257099.985,
"98.17784742598026"
],
[
1741257113.985,
"98.17784742598026"
],
[
1741257155.985,
"95.9030741531075"
],
[
1741257169.985,
"95.9030741531075"
],
[
1741257211.985,
"96.91251000266737"
],
[
1741257225.985,
"96.91251000266737"
],
[
1741257239.985,
"97.36679781275008"
],
[
1741257253.985,
"97.36679781275008"
],
[
1741257295.985,
"99.12810082688716"
],
[
1741257309.985,
"99.12810082688716"
],
[
1741257323.985,
"99.12810082688716"
],
[
1741257365.985,
"98.9247132568685"
],
[
1741257379.985,
"98.9247132568685"
],
[
1741257421.985,
"98.6854827954121"
],
[
1741257435.985,
"98.6854827954121"
],
[
1741257449.985,
"99.9891637770072"
],
[
1741257463.985,
"99.9891637770072"
]
]
}
]
}
}
}

295
openapi/pid.json Normal file

File diff suppressed because one or more lines are too long

141
openapi/sdk/params.go Normal file
View File

@ -0,0 +1,141 @@
package sdk
import (
"strconv"
"time"
)
const (
signatureHeader string = "x-signature"
signatureMethodHeader string = "x-signature-method"
signatureVersionHeader string = "x-signature-version"
timestampHeader string = "x-timestamp"
nonceHeader string = "x-nonce"
contentTypeHeader string = "Content-Type"
accessKeyIdHeader string = "x-accesskeyid"
regionIdHeader string = "x-regionid"
timezoneHeader string = "x-timezone"
apiVersionHeader string = "x-api-version"
securityTokenHeader string = "x-security-token"
localeHeader string = "x-locale"
hostHeader string = "Host"
)
type Params struct {
headers map[string]string
requestMethod string
uri string
requestBody string
secretKey string
}
func NewParams() *Params {
return &Params{headers: map[string]string{
signatureMethodHeader: "HMAC-SHA256",
signatureVersionHeader: "1.0",
timestampHeader: time.Now().Format("2006-01-02T15:04:05Z"),
nonceHeader: strconv.Itoa(int(time.Now().UnixNano())),
contentTypeHeader: "application/json",
}}
}
func (p *Params) Headers() map[string]string {
return p.headers
}
func (p *Params) RequestMethod(requestMethod string) *Params {
p.requestMethod = requestMethod
return p
}
func (p *Params) Uri(uri string) *Params {
p.uri = uri
return p
}
func (p *Params) RequestBody(requestBody string) *Params {
p.requestBody = requestBody
return p
}
func (p *Params) SecretKey(secretKey string) *Params {
p.secretKey = secretKey
return p
}
func (p *Params) Signature(signature string) *Params {
p.headers[signatureHeader] = signature
return p
}
func (p *Params) RegionId(regionId string) *Params {
p.headers[regionIdHeader] = regionId
return p
}
func (p *Params) AccessKeyId(accesskeyId string) *Params {
p.headers[accessKeyIdHeader] = accesskeyId
return p
}
func (p *Params) SignatureMethod(signatureMethod string) *Params {
p.headers[signatureMethodHeader] = signatureMethod
return p
}
func (p *Params) SignatureVersion(signatureVersion string) *Params {
p.headers[signatureVersionHeader] = signatureVersion
return p
}
func (p *Params) Nonce(nonce string) *Params {
p.headers[nonceHeader] = nonce
return p
}
func (p *Params) Timestamp(timestamp string) *Params {
p.headers[timestampHeader] = timestamp
return p
}
func (p *Params) Timezone(timezone string) *Params {
p.headers[timezoneHeader] = timezone
return p
}
func (p *Params) ApiVersion(apiVersion string) *Params {
p.headers[apiVersionHeader] = apiVersion
return p
}
func (p *Params) SecurityToken(securityToken string) *Params {
p.headers[securityTokenHeader] = securityToken
return p
}
func (p *Params) Locale(locale string) *Params {
p.headers[localeHeader] = locale
return p
}
func (p *Params) ContentType(contentType string) *Params {
p.headers[contentTypeHeader] = contentType
return p
}
func (p *Params) Host(host string) *Params {
p.headers[hostHeader] = host
return p
}
// Deprecated
func (p *Params) PutParams(params map[string]string) *Params {
return p.PutQueryParams(params)
}
func (p *Params) PutQueryParams(params map[string]string) *Params {
for k, v := range params {
p.headers[k] = v
}
return p
}

71
openapi/sdk/sign.go Normal file
View File

@ -0,0 +1,71 @@
package sdk
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"sort"
"strings"
)
func GenSignature(param *Params) string {
if param.Headers() == nil {
return ""
}
if param.secretKey == "" {
panic("secretKey不能为空")
}
var sortedKeys []string
for key, _ := range param.Headers() {
sortedKeys = append(sortedKeys, key)
}
sort.Strings(sortedKeys)
var params []string
for _, key := range sortedKeys {
if key != signatureHeader {
params = append(params, key+"="+strings.Trim(param.Headers()[key], " "))
}
}
params = append(params, "RequestMethod="+strings.Trim(param.requestMethod, " "))
params = append(params, "Uri="+strings.Trim(param.uri, " "))
if len(param.requestBody) <= 0 {
params = append(params, "RequestBody=")
} else {
body := param.requestBody
// 去除空格
body = strings.Replace(body, " ", "", -1)
// 去除换行符
body = strings.Replace(body, "\n", "", -1)
// 去除制表符
body = strings.Replace(body, "\t", "", -1)
// 去除回车符
body = strings.Replace(body, "\r", "", -1)
params = append(params, "RequestBody="+body)
}
return HmacSha256ToHex(param.secretKey+"&", strings.Trim(strings.Join(params, "&"), " "))
}
// HmacSha256 计算HmacSha256
// key 是加密所使用的key
// data 是加密的内容
func HmacSha256(key string, data string) []byte {
mac := hmac.New(sha256.New, []byte(key))
_, _ = mac.Write([]byte(data))
return mac.Sum(nil)
}
// HmacSha256ToHex 将加密后的二进制转16进制字符串
func HmacSha256ToHex(key string, data string) string {
return hex.EncodeToString(HmacSha256(key, data))
}
func RequestHeaders(params *Params) map[string]string {
genSignature := GenSignature(params)
params.Signature(genSignature)
return params.Headers()
}

52
openapi/sdk/sign_test.go Normal file
View File

@ -0,0 +1,52 @@
package sdk
import (
"fmt"
"strings"
"testing"
"time"
)
func Test_Sign(t *testing.T) {
p :=
NewParams().RegionId("region1").
AccessKeyId("sccdsfnioasn8s7dcbsjkdc").
SignatureMethod("HMAC-SHA256").
SignatureVersion("1.0").
Nonce("asc8-asd32-23sdfcsdc-sdc").
Timestamp(time.Now().Format("2006-01-02T15:04:05Z")).
Timezone("").
ApiVersion("1.0").
SecurityToken("").
Locale("").
ContentType("application/json").
Host("api.cecloudcs.com").
RequestMethod("GET").
Uri("/om/opsdemo/instance").
RequestBody("").
SecretKey("123abc").
// 扩展参数
PutQueryParams(map[string]string{
"size": "10",
})
headers := RequestHeaders(p)
sign := GenSignature(p)
fmt.Println(sign == headers[signatureHeader])
}
func Test_string(t *testing.T) {
body := "a b asdfaslkdfj" +
"sdfa dsf"
fmt.Println(body)
// 去除空格
body = strings.Replace(body, " ", "", -1)
// 去除换行符
body = strings.Replace(body, "\n", "", -1)
// 去除制表符
body = strings.Replace(body, "\t", "", -1)
// 去除回车符
body = strings.Replace(body, "\r", "", -1)
fmt.Println(body)
}

37
openapi/sdk/util.go Normal file
View File

@ -0,0 +1,37 @@
package sdk
import (
"log"
"time"
)
func Sign() map[string]string {
p := NewParams().
RegionId("dtcloud2").
AccessKeyId("3eZJAmU6MvdnCadr6MQV").
SignatureMethod("HMAC-SHA256").
SignatureVersion("1.0").
Nonce("dsfg-eebc-4ac2-9587-e8d258d871ce").
Timestamp(time.Now().Format("2006-01-02T15:04:05Z")).
Timezone("").
ApiVersion("1.0").
SecurityToken("").
Locale("").
ContentType("application/json").
Host("openapi.user.zww.dtcloud.com").
RequestMethod("GET").
Uri("/api/account-tenant/v1/tenant/get").
RequestBody("").
SecretKey("iQmbqVNvzxsTPWWiaAbN3010mSjSrRA8FhZ8hxJq").
// 扩展参数
PutQueryParams(map[string]string{
"pageSize": "10",
"pageNum": "1",
})
headers := RequestHeaders(p)
sign := GenSignature(p)
log.Println(sign == headers[signatureHeader])
log.Println(sign)
log.Println(headers)
return headers
}