sync_ali_ip/main.go

237 lines
4.9 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io"
"jiujunet.com/alipay"
"net/http"
"time"
)
type (
IpRes struct {
Ret string `json:"ret"`
Data *IpInfo `json:"data"`
}
IpInfo struct {
Ip string `json:"ip"`
Location []string `json:"location"`
}
GroupRole struct {
Desc string `json:"desc"`
GroupId string `json:"groupId"`
RuleId string `json:"ruleId"`
}
)
var (
lastIp = ""
groupRoleList = []*GroupRole{
{
Desc: "收银系统正式-宝塔",
GroupId: "sg-bp1csfr2wcn7ujwoabkk",
RuleId: "sgr-bp1adiq57mnoe3asdpoh",
},
{
Desc: "收银系统正式-22",
GroupId: "sg-bp1csfr2wcn7ujwoabkk",
RuleId: "sgr-bp10yy3lg5lw9g6qhv8o",
},
{
Desc: "支付系统-22",
GroupId: "sg-bp1hnd3bgw6jq8kmn1ju",
RuleId: "sgr-bp1dwhn7mi8mlf60xcgo",
},
{
Desc: "支付系统-支付宝塔",
GroupId: "sg-bp1hnd3bgw6jq8kmn1ju",
RuleId: "sgr-bp1653xk85wnusza1nvh",
},
{
Desc: "支付系统-预发布宝塔",
GroupId: "sg-bp1hnd3bgw6jq8kmn1ju",
RuleId: "sgr-bp1idleb69yzsb69yluz",
},
{
Desc: "支付系统-预发布宝塔",
GroupId: "sg-bp1hnd3bgw6jq8kmn1ju",
RuleId: "sgr-bp1idleb69yzsb69yluz",
},
{
Desc: "银收客-22",
GroupId: "sg-bp1hnylmydz8bi7gqtgj",
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() {
//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(isCzg bool) {
fmt.Println("updateServerSgIp start")
fmt.Println("lastIp: ", lastIp)
fmt.Println("start time: ", time.Now().Format("2006-01-02 15:04:05"))
ipInfo := getSelfIp()
if ipInfo == nil {
fmt.Println("get self ip err")
return
}
fmt.Println("ipInfo:", ipInfo)
if !StrArrIsContain(ipInfo.Location, "西安") {
fmt.Println("not in xian")
return
}
if lastIp == ipInfo.Ip {
fmt.Println("ip not change")
return
}
lastIp = ipInfo.Ip
fmt.Println("ip change, go to update aliyun")
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.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)
}
}
}
}
func getSelfIp() *IpInfo {
url := "https://myip.ipip.net/json"
resp, err := http.Get(url)
if err != nil {
fmt.Println("http.Get err:", err)
return nil
}
defer func(Body io.ReadCloser) {
e := Body.Close()
if e != nil {
}
}(resp.Body)
// 读取内容
buf := make([]byte, 1024)
n, _ := resp.Body.Read(buf)
fmt.Println(string(buf[:n]))
result := &IpRes{}
err = json.Unmarshal(buf[:n], result)
if err != nil {
fmt.Println("json.Unmarshal err:", err)
return nil
}
if result.Ret != "ok" {
fmt.Println("result.Ret err:", result.Ret)
return nil
}
return result.Data
}
func StrArrIsContain(arr []string, str string) bool {
for _, v := range arr {
if v == str {
return true
}
}
return false
}