1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| # main.go package main
import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" "os" "path" "strings" "time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" )
type ConfigurationModel struct { ACCESS_KEY_ID string ACCESS_KEY_SECRET string REGION_ID string SECURITY_GROUP_ID string IP_PROTOCOL string PORT_RANGE string DESCRIPTION string DURATION int }
type IpResponse struct { Origin string `json:"origin"` }
var configModel ConfigurationModel
const GetPublicIpUrl = "http://www.httpbin.org/ip"
func main() { loadConfig() fmt.Println("配置:", configModel.SECURITY_GROUP_ID)
filename := configModel.SECURITY_GROUP_ID + "_ip.txt" oldIp := readIp(filename) fmt.Printf("最后IP记录为: %s\n", oldIp)
ecsClient, err := ecs.NewClientWithAccessKey(configModel.REGION_ID, configModel.ACCESS_KEY_ID, configModel.ACCESS_KEY_SECRET) if err != nil { panic(err) }
for { newIp := getPublicIp() if oldIp != newIp { fmt.Printf("IP变更: %s => %s\n", oldIp, newIp)
delSecurityGroup(ecsClient, configModel, oldIp) addSecurityGroup(ecsClient, configModel, newIp)
oldIp = newIp writeIp(filename, newIp) }
time.Sleep(time.Duration(configModel.DURATION) * time.Second) }
}
func loadConfig() { var configFile string
dir, _ := os.Getwd() configFile = path.Join(dir, "settings.json")
f, err := os.Open(configFile) if err != nil { log.Fatalf("无法打开文件:%s", err) os.Exit(-1) } defer f.Close() data, _ := ioutil.ReadAll(f)
if err := json.Unmarshal(data, &configModel); err != nil { log.Fatalf("数据反序列化失败:%s", err) os.Exit(-1) } }
func getPublicIp() string { resp, err := http.Get(GetPublicIpUrl) if err != nil { log.Printf("获取公网 IP 出现错误,错误信息:%s", err) os.Exit(-1) } defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
var res IpResponse if err := json.Unmarshal(bytes, &res); err != nil { log.Fatalf("数据反序列化失败:%s", err) os.Exit(-1) }
return res.Origin }
func delSecurityGroup(client *ecs.Client, configModel ConfigurationModel, ip string) { request := ecs.CreateRevokeSecurityGroupRequest() request.SecurityGroupId = configModel.SECURITY_GROUP_ID request.PortRange = configModel.PORT_RANGE request.IpProtocol = configModel.IP_PROTOCOL request.SourceCidrIp = ip
response, err := client.RevokeSecurityGroup(request) if err != nil { panic(err) } fmt.Printf("删除安全组成功: (%d)! %s\n", response.GetHttpStatus(), ip) }
func addSecurityGroup(client *ecs.Client, configModel ConfigurationModel, ip string) { request := ecs.CreateAuthorizeSecurityGroupRequest() request.SecurityGroupId = configModel.SECURITY_GROUP_ID request.PortRange = configModel.PORT_RANGE request.IpProtocol = configModel.IP_PROTOCOL request.Description = configModel.DESCRIPTION request.SourceCidrIp = ip
response, err := client.AuthorizeSecurityGroup(request) if err != nil { panic(err) } fmt.Printf("添加安全组成功: (%d)! %s\n", response.GetHttpStatus(), ip) }
func readIp(filename string) string { ip := "127.0.0.1"
contents, err := ioutil.ReadFile(filename) if err == nil { ip = strings.Replace(string(contents), "\n", "", 1) }
return ip } func writeIp(filename string, content string) { data := []byte(content) ioutil.WriteFile(filename, data, 0644) }
|