From 3c4411b325dc0564914c643021ced33db4825d7a Mon Sep 17 00:00:00 2001 From: GYJ <1157756119@qq.com> Date: Tue, 14 Jan 2025 10:28:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=AF=E8=80=80=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E7=BB=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/vcs.xml | 6 +++ alipay/sy_update_ali_ip.go | 103 +++++++++++++++++++++++++++++++++++ alipay/update_ali_ip.go | 3 +- go.mod | 2 +- main.go | 106 ++++++++++++++++++++++++++++++------- 5 files changed, 198 insertions(+), 22 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 alipay/sy_update_ali_ip.go diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/alipay/sy_update_ali_ip.go b/alipay/sy_update_ali_ip.go new file mode 100644 index 0000000..4e772a4 --- /dev/null +++ b/alipay/sy_update_ali_ip.go @@ -0,0 +1,103 @@ +package alipay + +import ( + "encoding/json" + "errors" + "fmt" + openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" + ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v4/client" + util "github.com/alibabacloud-go/tea-utils/v2/service" + "github.com/alibabacloud-go/tea/tea" + "github.com/google/uuid" + "strings" +) + +// 用户登录名称 ecs-ip@1882833282563718.onaliyun.com +// 登录密码 ETVuwaiCHBKP36$jvdxkp7&!)Zt4JcWL +// AccessKey ID LTAI5tQCWkqiinoXLpC5q5ZG +// AccessKey Secret bp6V7H7pkB0bJygJkVVxhlm2i7RiqD +var ( + _syClient *ecs20140526.Client + SyRegionId = "cn-nanjing" + SyAccessKeyId = "LTAI5tQCWkqiinoXLpC5q5ZG" + SyAccessKeySecret = "bp6V7H7pkB0bJygJkVVxhlm2i7RiqD" + SyEndPoint = "ecs.cn-nanjing.aliyuncs.com" + //SyEndPoint = "ecs-cn-hangzhou.aliyuncs.com" +) + +func CreateSyClient() (_result *ecs20140526.Client, _err error) { + // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 + // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。 + config := &openapi.Config{ + // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 + AccessKeyId: tea.String(SyAccessKeyId), + // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 + AccessKeySecret: tea.String(SyAccessKeySecret), + } + // Endpoint 请参考 https://api.aliyun.com/product/Ecs + config.Endpoint = tea.String(SyEndPoint) + _result = &ecs20140526.Client{} + _result, _err = ecs20140526.NewClient(config) + return _result, _err +} + +func UpdateSyAliIp(groupId, ruleId, ip string) (_err error) { + if _syClient == nil { + client, e := CreateSyClient() + if e != nil { + return e + } + _syClient = client + } + + newUUID, _ := uuid.NewUUID() + modifySecurityGroupRuleRequest := &ecs20140526.ModifySecurityGroupRuleRequest{ + RegionId: tea.String(SyRegionId), + ClientToken: tea.String(newUUID.String()), + SecurityGroupId: tea.String(groupId), + SecurityGroupRuleId: tea.String(ruleId), + SourceCidrIp: tea.String(ip), + } + runtime := &util.RuntimeOptions{} + tryErr := func() (_e error) { + defer func() { + if r := tea.Recover(recover()); r != nil { + _e = r + } + }() + // 复制代码运行请自行打印 API 的返回值 + _, _err = _syClient.ModifySecurityGroupRuleWithOptions(modifySecurityGroupRuleRequest, runtime) + if _err != nil { + return _err + } + + return nil + }() + + if tryErr != nil { + var err = &tea.SDKError{} + var _t *tea.SDKError + if errors.As(tryErr, &_t) { + err = _t + } + // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 + // 错误 message + fmt.Println(tea.StringValue(err.Message)) + // 诊断地址 + var data interface{} + d := json.NewDecoder(strings.NewReader(tea.StringValue(err.Data))) + e := d.Decode(&data) + if e != nil { + return e + } + if m, ok := data.(map[string]interface{}); ok { + recommend, _ := m["Recommend"] + fmt.Println(recommend) + } + _, _err = util.AssertAsString(err.Message) + if _err != nil { + return _err + } + } + return _err +} diff --git a/alipay/update_ali_ip.go b/alipay/update_ali_ip.go index cd78ed4..08d1464 100644 --- a/alipay/update_ali_ip.go +++ b/alipay/update_ali_ip.go @@ -17,6 +17,7 @@ var ( RegionId = "cn-hangzhou" AccessKeyId = "LTAI5tS5pSkUwm6VURuaetxs" AccessKeySecret = "iQFIO4MFa7tZ7HbHs0OXOaMDvMozM5" + EndPoint = "ecs-cn-hangzhou.aliyuncs.com" ) func CreateClient() (_result *ecs20140526.Client, _err error) { @@ -29,7 +30,7 @@ func CreateClient() (_result *ecs20140526.Client, _err error) { AccessKeySecret: tea.String(AccessKeySecret), } // Endpoint 请参考 https://api.aliyun.com/product/Ecs - config.Endpoint = tea.String("ecs-cn-hangzhou.aliyuncs.com") + config.Endpoint = tea.String(EndPoint) _result = &ecs20140526.Client{} _result, _err = ecs20140526.NewClient(config) return _result, _err diff --git a/go.mod b/go.mod index a3b2d2c..b851a08 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/alibabacloud-go/ecs-20140526/v4 v4.26.0 github.com/alibabacloud-go/tea v1.2.2 github.com/alibabacloud-go/tea-utils/v2 v2.0.7 + github.com/google/uuid v1.6.0 ) require ( @@ -24,7 +25,6 @@ require ( github.com/alibabacloud-go/tea-xml v1.1.3 // indirect github.com/aliyun/credentials-go v1.3.10 // indirect github.com/clbanning/mxj/v2 v2.5.5 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect diff --git a/main.go b/main.go index 4d33346..388c62e 100644 --- a/main.go +++ b/main.go @@ -66,22 +66,76 @@ var ( RuleId: "sgr-bp1flbqxvwq6f7ffu2qm", }, } + + syGroupRoleList = []*GroupRole{ + { + Desc: "2881", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc7i4mfwl67i3w4au7vi", + }, { + Desc: "8889", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc7djax9mkuc2vdb4tl2", + }, { + Desc: "3306", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc7ajavhz3quy6bqj84n", + }, { + Desc: "8680", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc7djax9mkuc2tea0rlx", + }, { + Desc: "3000", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc7bugau2qp748f0h56h", + }, { + Desc: "3307", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc76hu8bzkckm2l0geun", + }, { + Desc: "9200", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc7dagt483s8m2nyz4s1", + }, { + Desc: "8089", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc73tx7hw41tmksce625", + }, { + Desc: "5601", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc76hu8bzkcj33l0hd4g", + }, { + Desc: "28893", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc79b1igyb7t1u69o4lg", + }, { + Desc: "雷池-9443", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc7dqmoucm29ij296ue6", + }, { + Desc: "20884", + GroupId: "sg-gc7huspbax0pywqu423w", + RuleId: "sgr-gc7882ccgyc006rs1i4j", + }, + } ) func main() { - for { - updateServerSgIp() - - duration := time.Hour - //duration := 5 * time.Second - ticker := time.NewTicker(duration) - <-ticker.C - fmt.Println("定时结束!") - ticker.Stop() - } + //updateServerSgIp(true) + updateServerSgIp(false) + //for { + // updateServerSgIp() + // + // duration := time.Hour + // //duration := 5 * time.Second + // ticker := time.NewTicker(duration) + // <-ticker.C + // fmt.Println("定时结束!") + // ticker.Stop() + //} } -func updateServerSgIp() { +func updateServerSgIp(isCzg bool) { fmt.Println("updateServerSgIp start") fmt.Println("lastIp: ", lastIp) fmt.Println("start time: ", time.Now().Format("2006-01-02 15:04:05")) @@ -108,19 +162,31 @@ func updateServerSgIp() { fmt.Println("ip change, go to update aliyun") - for _, groupRole := range groupRoleList { - err := alipay.UpdateAliIp(groupRole.GroupId, groupRole.RuleId, ipInfo.Ip) + if isCzg { + for _, groupRole := range groupRoleList { + err := alipay.UpdateAliIp(groupRole.GroupId, groupRole.RuleId, ipInfo.Ip) + if err != nil { + fmt.Printf("UpdateAliIp err: %s, Desc: %s\n", err.Error(), groupRole.Desc) + } else { + fmt.Println("UpdateAliIp success: ", groupRole.Desc) + } + } + + err := alipay.UpdateAliIp("sg-bp1csfr2wcn7ujwoabkk", "sgr-bp1adiq57mnoe3asdpoh", ipInfo.Ip) if err != nil { - fmt.Printf("UpdateAliIp err: %s, Desc: %s\n", err.Error(), groupRole.Desc) - } else { - fmt.Println("UpdateAliIp success: ", groupRole.Desc) + fmt.Println("UpdateAliIp err:", err) + } + } else { + for _, groupRole := range syGroupRoleList { + err := alipay.UpdateSyAliIp(groupRole.GroupId, groupRole.RuleId, ipInfo.Ip) + if err != nil { + fmt.Printf("UpdateAliIp err: %s, Desc: %s\n", err.Error(), groupRole.Desc) + } else { + fmt.Println("UpdateAliIp success: ", groupRole.Desc) + } } } - err := alipay.UpdateAliIp("sg-bp1csfr2wcn7ujwoabkk", "sgr-bp1adiq57mnoe3asdpoh", ipInfo.Ip) - if err != nil { - fmt.Println("UpdateAliIp err:", err) - } } func getSelfIp() *IpInfo {