-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
millim
committed
Jan 6, 2020
0 parents
commit f5114fe
Showing
5 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
.idea | ||
dist | ||
*.iml | ||
public_ip | ||
*.yml | ||
*.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## 功能 | ||
阿里云上对服务器设置了白名单,但是部分IP不是固定IP,路由被重启后就不会被重新随机分配一个IP,因此弄了简单的内容对其进行更新。 | ||
|
||
|
||
```bash | ||
nohup esg -c config.yml >./log 2>&1 & | ||
|
||
# config.yml | ||
regionId: cn-hangzhou | ||
accessKeyId: 权限keyid | ||
accessKeySecret: 权限keysecret | ||
groups: | ||
- groupId: 安全组id | ||
port: 端口 | ||
info: 描述 | ||
- groupId: 安全组id | ||
port: 端口 | ||
info: 描述 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module ali-ecs-public-ip | ||
|
||
go 1.13 | ||
|
||
require ( | ||
github.com/aliyun/alibaba-cloud-sdk-go v1.60.315 | ||
gopkg.in/yaml.v2 v2.2.7 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
github.com/aliyun/alibaba-cloud-sdk-go v1.60.315 h1:Mf9pJyf4pOkVYXUSJstPt70TDy8vfzkK5ijN/sqYfHo= | ||
github.com/aliyun/alibaba-cloud-sdk-go v1.60.315/go.mod h1:mNZkuqaeM5UCiAdkV4r+lrheu8Q5fe/487bRFrGYZ8A= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A= | ||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= | ||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= | ||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= | ||
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE= | ||
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= | ||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= | ||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= | ||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= | ||
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/ini.v1 v1.42.0 h1:7N3gPTt50s8GuLortA00n8AqRTk75qOP98+mTPpgzRk= | ||
gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= | ||
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= | ||
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" | ||
"gopkg.in/yaml.v2" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
|
||
var file = "./public_ip" | ||
var config Config | ||
|
||
type Group struct { | ||
GroupId string `yaml:"groupId"` | ||
Port string `yaml:"port"` | ||
Info string `yaml:"info"` | ||
} | ||
|
||
type Config struct { | ||
RegionID string `yaml:"regionId"` | ||
AccessKeyID string `yaml:"accessKeyId"` | ||
AccessKeySecret string `yaml:"accessKeySecret"` | ||
Groups []Group `yaml:"groups"` | ||
|
||
} | ||
|
||
func main() { | ||
configAdd := "" | ||
flag.StringVar(&configAdd,"c","", "-c config.yml") | ||
flag.Parse() | ||
println(configAdd) | ||
|
||
if !fileExists(configAdd){ | ||
println("no search config file, use -c config.yml") | ||
return | ||
} | ||
|
||
|
||
ymlFile, err := ioutil.ReadFile(configAdd) | ||
if err != nil { | ||
println("read config err--->",err) | ||
return | ||
} | ||
|
||
err = yaml.UnmarshalStrict(ymlFile, &config) | ||
if err != nil { | ||
println("config load error!") | ||
println(err) | ||
return | ||
} | ||
|
||
for { | ||
inspect() | ||
time.Sleep(5 * time.Minute) | ||
} | ||
|
||
} | ||
|
||
func inspect(){ | ||
ip := getIP() | ||
if !fileExists(file) { | ||
createOriginalIP(ip) | ||
}else { | ||
oldIP, err := getOldIP() | ||
if err != nil { | ||
println(err.Error()) | ||
return | ||
} | ||
if oldIP == ip{ | ||
return | ||
} | ||
println(time.Now().String(),"----->","ip变了") | ||
updateAliyun(oldIP, ip) | ||
ioutil.WriteFile(file,[]byte(ip), 0644) | ||
} | ||
} | ||
|
||
func updateAliyun(oldIP, ip string){ | ||
client, _ = ecs.NewClientWithAccessKey(config.RegionID, config.AccessKeyID, config.AccessKeySecret) | ||
if len(config.Groups) > 0 { | ||
for _, v := range config.Groups{ | ||
updateGroup(v.GroupId, oldIP, ip, v.Port, v.Info) | ||
} | ||
} | ||
} | ||
|
||
|
||
func getOldIP()(string, error) { | ||
b, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
println(err.Error()) | ||
return "", err | ||
} | ||
return string(b), nil | ||
} | ||
|
||
func createOriginalIP(ip string){ | ||
f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, 0644) | ||
if err != nil { | ||
println(err.Error()) | ||
return | ||
} | ||
defer f.Close() | ||
_, err = f.WriteString(ip) | ||
if err != nil { | ||
println(err.Error()) | ||
return | ||
} | ||
} | ||
|
||
|
||
func fileExists(file string) bool{ | ||
_, err := os.Stat(file) | ||
if err != nil { | ||
if os.IsExist(err) { | ||
return true | ||
} | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
|
||
func getIP() string{ | ||
rep, err := http.Get("http://ifconfig.me") | ||
if err !=nil { | ||
return "" | ||
} | ||
|
||
defer rep.Body.Close() | ||
body, err := ioutil.ReadAll(rep.Body) | ||
if err !=nil { | ||
return "" | ||
} | ||
ip := string(body) | ||
return ip | ||
} | ||
|
||
var client *ecs.Client | ||
|
||
func updateGroup(group, oldIP, ip, port, info string){ | ||
removeGroup(group, oldIP, port) | ||
addGroup(group, ip, port, info) | ||
} | ||
|
||
func removeGroup(group, ip, port string){ | ||
request := ecs.CreateRevokeSecurityGroupRequest() | ||
request.SecurityGroupId = group | ||
request.RegionId = "cn-hangzhou" | ||
request.IpProtocol = "tcp" | ||
request.PortRange = fmt.Sprintf("%s/%s",port,port) | ||
request.SourceCidrIp = ip | ||
_, err := client.RevokeSecurityGroup(request) | ||
if err != nil { | ||
fmt.Print(err.Error()) | ||
} | ||
} | ||
func addGroup(group, ip, port, info string){ | ||
request := ecs.CreateAuthorizeSecurityGroupRequest() | ||
request.SecurityGroupId = group | ||
request.RegionId = "cn-hangzhou" | ||
request.IpProtocol = "tcp" | ||
request.PortRange = fmt.Sprintf("%s/%s",port,port) | ||
request.SourceCidrIp = ip | ||
request.Description = info | ||
_, err := client.AuthorizeSecurityGroup(request) | ||
if err != nil { | ||
fmt.Print(err.Error()) | ||
} | ||
|
||
} |