mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 15:00:27 +08:00
支持阿里云DNS
This commit is contained in:
2
go.mod
2
go.mod
@@ -6,12 +6,12 @@ replace github.com/TeaOSLab/EdgeCommon => ../EdgeCommon
|
||||
|
||||
require (
|
||||
github.com/TeaOSLab/EdgeCommon v0.0.0-00010101000000-000000000000
|
||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.641
|
||||
github.com/go-sql-driver/mysql v1.5.0
|
||||
github.com/go-yaml/yaml v2.1.0+incompatible
|
||||
github.com/golang/protobuf v1.4.2
|
||||
github.com/iwind/TeaGo v0.0.0-20201020081413-7cf62d6f420f
|
||||
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
|
||||
github.com/mattn/go-isatty v0.0.12 // indirect
|
||||
github.com/mozillazg/go-pinyin v0.18.0
|
||||
github.com/pkg/sftp v1.12.0
|
||||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
|
||||
|
||||
20
go.sum
20
go.sum
@@ -4,6 +4,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/DataDog/sketches-go v0.0.0-20190923095040-43f19ad77ff7/go.mod h1:Q5DbzQ+3AkgGwymQO7aZFNP7ns2lZKGtvRBzRXfdi60=
|
||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.641 h1:X/Ik2DvrwICTd5hbRPjB7+s/61pk/b40HJ6XHAg2LSc=
|
||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.641/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA=
|
||||
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
@@ -26,6 +28,7 @@ github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gG
|
||||
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
|
||||
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
|
||||
github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
@@ -49,13 +52,20 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
||||
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
|
||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
|
||||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e h1:/xn7wUvlwaoA5IkdBUctv2OQbJSZ0/Dw8qRJmn55sJk=
|
||||
github.com/iwind/TeaGo v0.0.0-20200923021120-f5d76441fe9e/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||
github.com/iwind/TeaGo v0.0.0-20201020081413-7cf62d6f420f h1:6Ws2H+eorfVUoMO2jta6A9nIdh8oi5/5LXo/LkAxR+E=
|
||||
github.com/iwind/TeaGo v0.0.0-20201020081413-7cf62d6f420f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||
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/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
|
||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
|
||||
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
@@ -64,8 +74,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible h1:1qp9iks+69h7IGLazAplzS9Ca14HAxuD5c0rbFdPGy4=
|
||||
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible/go.mod h1:+ZBN7PBoh5gG6/y0ZQ85vJDBe21WnfbRrQQwTfliJJI=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
@@ -94,6 +102,10 @@ github.com/pkg/sftp v1.12.0/go.mod h1:fUqqXB5vEgVCZ131L+9say31RAri6aF6KDViawhxKK
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
|
||||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
|
||||
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a h1:pa8hGb/2YqsZKovtsgrwcDH1RZhVbTKCjLp47XpqCDs=
|
||||
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=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
@@ -144,7 +156,6 @@ golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4=
|
||||
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@@ -156,6 +167,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
@@ -193,6 +205,8 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
|
||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
|
||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/ini.v1 v1.42.0 h1:7N3gPTt50s8GuLortA00n8AqRTk75qOP98+mTPpgzRk=
|
||||
gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
||||
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
@@ -162,7 +163,7 @@ func (this *DNSDomainDAO) UpdateDomainRoutes(domainId int64, routesJSON []byte)
|
||||
}
|
||||
|
||||
// 查找域名线路
|
||||
func (this *DNSDomainDAO) FindDomainRoutes(domainId int64) ([]string, error) {
|
||||
func (this *DNSDomainDAO) FindDomainRoutes(domainId int64) ([]*dnsclients.Route, error) {
|
||||
routes, err := this.Query().
|
||||
Pk(domainId).
|
||||
Result("routes").
|
||||
@@ -173,10 +174,24 @@ func (this *DNSDomainDAO) FindDomainRoutes(domainId int64) ([]string, error) {
|
||||
if len(routes) == 0 || routes == "null" {
|
||||
return nil, nil
|
||||
}
|
||||
result := []string{}
|
||||
result := []*dnsclients.Route{}
|
||||
err = json.Unmarshal([]byte(routes), &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// 查找线路名称
|
||||
func (this *DNSDomainDAO) FindDomainRouteName(domainId int64, routeCode string) (string, error) {
|
||||
routes, err := this.FindDomainRoutes(domainId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, route := range routes {
|
||||
if route.Code == routeCode {
|
||||
return route.Name, nil
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
@@ -3,15 +3,14 @@ package models
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
)
|
||||
|
||||
// 获取所有的线路
|
||||
func (this *DNSDomain) DecodeRoutes() ([]string, error) {
|
||||
func (this *DNSDomain) DecodeRoutes() ([]*dnsclients.Route, error) {
|
||||
if len(this.Routes) == 0 || this.Routes == "null" {
|
||||
return nil, nil
|
||||
}
|
||||
result := []string{}
|
||||
result := []*dnsclients.Route{}
|
||||
err := json.Unmarshal([]byte(this.Routes), &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -25,7 +24,12 @@ func (this *DNSDomain) ContainsRoute(route string) (bool, error) {
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return lists.ContainsString(routes, route), nil
|
||||
for _, r := range routes {
|
||||
if r.Code == route {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// 获取所有的记录
|
||||
|
||||
1
internal/dnsclients/.gitignore
vendored
1
internal/dnsclients/.gitignore
vendored
@@ -1 +0,0 @@
|
||||
*_test.go
|
||||
156
internal/dnsclients/provider_alidns.go
Normal file
156
internal/dnsclients/provider_alidns.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package dnsclients
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 阿里云服务商
|
||||
type AliDNSProvider struct {
|
||||
accessKeyId string
|
||||
accessKeySecret string
|
||||
}
|
||||
|
||||
// 认证
|
||||
func (this *AliDNSProvider) Auth(params maps.Map) error {
|
||||
this.accessKeyId = params.GetString("accessKeyId")
|
||||
this.accessKeySecret = params.GetString("accessKeySecret")
|
||||
if len(this.accessKeyId) == 0 {
|
||||
return errors.New("'accessKeyId' should not be empty")
|
||||
}
|
||||
if len(this.accessKeySecret) == 0 {
|
||||
return errors.New("'accessKeySecret' should not be empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取域名列表
|
||||
func (this *AliDNSProvider) GetRecords(domain string) (records []*Record, err error) {
|
||||
pageNumber := 1
|
||||
size := 100
|
||||
|
||||
for {
|
||||
req := alidns.CreateDescribeDomainRecordsRequest()
|
||||
req.DomainName = domain
|
||||
req.PageNumber = requests.NewInteger(pageNumber)
|
||||
req.PageSize = requests.NewInteger(size)
|
||||
|
||||
resp := alidns.CreateDescribeDomainRecordsResponse()
|
||||
err = this.doAPI(req, resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, record := range resp.DomainRecords.Record {
|
||||
// 修正Record
|
||||
if record.Type == RecordTypeCName && !strings.HasSuffix(record.Value, ".") {
|
||||
record.Value += "."
|
||||
}
|
||||
|
||||
records = append(records, &Record{
|
||||
Id: record.RecordId,
|
||||
Name: record.RR,
|
||||
Type: record.Type,
|
||||
Value: record.Value,
|
||||
Route: record.Line,
|
||||
})
|
||||
}
|
||||
|
||||
pageNumber++
|
||||
if int64((pageNumber-1)*size) >= resp.TotalCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 读取域名支持的线路数据
|
||||
func (this *AliDNSProvider) GetRoutes(domain string) (routes []*Route, err error) {
|
||||
req := alidns.CreateDescribeSupportLinesRequest()
|
||||
req.DomainName = domain
|
||||
|
||||
resp := alidns.CreateDescribeSupportLinesResponse()
|
||||
err = this.doAPI(req, resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, line := range resp.RecordLines.RecordLine {
|
||||
routes = append(routes, &Route{
|
||||
Name: line.LineName,
|
||||
Code: line.LineCode,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 设置记录
|
||||
func (this *AliDNSProvider) AddRecord(domain string, newRecord *Record) error {
|
||||
req := alidns.CreateAddDomainRecordRequest()
|
||||
req.RR = newRecord.Name
|
||||
req.Type = newRecord.Type
|
||||
req.Value = newRecord.Value
|
||||
req.DomainName = domain
|
||||
req.Line = newRecord.Route
|
||||
|
||||
resp := alidns.CreateAddDomainRecordResponse()
|
||||
err := this.doAPI(req, resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.IsSuccess() {
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New(resp.GetHttpContentString())
|
||||
}
|
||||
|
||||
// 修改记录
|
||||
func (this *AliDNSProvider) UpdateRecord(domain string, record *Record, newRecord *Record) error {
|
||||
req := alidns.CreateUpdateDomainRecordRequest()
|
||||
req.RecordId = record.Id
|
||||
req.RR = newRecord.Name
|
||||
req.Type = newRecord.Type
|
||||
req.Value = newRecord.Value
|
||||
req.Line = newRecord.Route
|
||||
|
||||
resp := alidns.CreateUpdateDomainRecordResponse()
|
||||
err := this.doAPI(req, resp)
|
||||
return err
|
||||
}
|
||||
|
||||
// 删除记录
|
||||
func (this *AliDNSProvider) DeleteRecord(domain string, record *Record) error {
|
||||
req := alidns.CreateDeleteDomainRecordRequest()
|
||||
req.RecordId = record.Id
|
||||
|
||||
resp := alidns.CreateDeleteDomainRecordResponse()
|
||||
err := this.doAPI(req, resp)
|
||||
return err
|
||||
}
|
||||
|
||||
// 默认线路
|
||||
func (this *AliDNSProvider) DefaultRoute() string {
|
||||
return "default"
|
||||
}
|
||||
|
||||
// 执行请求
|
||||
func (this *AliDNSProvider) doAPI(req requests.AcsRequest, resp responses.AcsResponse) error {
|
||||
req.SetScheme("https")
|
||||
|
||||
client, err := alidns.NewClientWithAccessKey("cn-hangzhou", this.accessKeyId, this.accessKeySecret)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = client.DoAction(req, resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !resp.IsSuccess() {
|
||||
return errors.New(resp.GetHttpContentString())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
112
internal/dnsclients/provider_alidns_test.go
Normal file
112
internal/dnsclients/provider_alidns_test.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package dnsclients
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAliDNSProvider_GetRecords(t *testing.T) {
|
||||
provider, err := testAliDNSProvider()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
records, err := provider.GetRecords("meloy.cn")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logs.PrintAsJSON(records, t)
|
||||
}
|
||||
|
||||
func TestAliDNSProvider_DeleteRecord(t *testing.T) {
|
||||
provider, err := testAliDNSProvider()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = provider.DeleteRecord("meloy.cn", &Record{
|
||||
Id: "20746603318032384",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestAliDNSProvider_GetRoutes(t *testing.T) {
|
||||
provider, err := testAliDNSProvider()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
routes, err := provider.GetRoutes("meloy.cn")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logs.PrintAsJSON(routes, t)
|
||||
}
|
||||
|
||||
func TestAliDNSProvider_AddRecord(t *testing.T) {
|
||||
provider, err := testAliDNSProvider()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = provider.AddRecord("meloy.cn", &Record{
|
||||
Id: "",
|
||||
Name: "test",
|
||||
Type: RecordTypeA,
|
||||
Value: "192.168.1.100",
|
||||
Route: "unicom",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestAliDNSProvider_UpdateRecord(t *testing.T) {
|
||||
provider, err := testAliDNSProvider()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = provider.UpdateRecord("meloy.cn", &Record{Id: "20746664455255040"}, &Record{
|
||||
Id: "",
|
||||
Name: "test",
|
||||
Type: RecordTypeA,
|
||||
Value: "192.168.1.101",
|
||||
Route: "unicom",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func testAliDNSProvider() (ProviderInterface, error) {
|
||||
db, err := dbs.Default()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
one, err := db.FindOne("SELECT * FROM edgeDNSProviders WHERE type='alidns' ORDER BY id DESC")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
apiParams := maps.Map{}
|
||||
err = json.Unmarshal([]byte(one.GetString("apiParams")), &apiParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
provider := &AliDNSProvider{}
|
||||
err = provider.Auth(apiParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return provider, nil
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DNSPod服务商
|
||||
type DNSPodProvider struct {
|
||||
apiId string
|
||||
apiToken string
|
||||
@@ -70,7 +71,7 @@ func (this *DNSPodProvider) GetRecords(domain string) (records []*Record, err er
|
||||
}
|
||||
|
||||
// 读取线路数据
|
||||
func (this *DNSPodProvider) GetRoutes(domain string) ([]string, error) {
|
||||
func (this *DNSPodProvider) GetRoutes(domain string) (routes []*Route, err error) {
|
||||
infoResp, err := this.post("/Domain.info", map[string]string{
|
||||
"domain": domain,
|
||||
})
|
||||
@@ -92,12 +93,15 @@ func (this *DNSPodProvider) GetRoutes(domain string) ([]string, error) {
|
||||
if len(lines) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
lineStrings := []string{}
|
||||
for _, line := range lines {
|
||||
lineStrings = append(lineStrings, types.String(line))
|
||||
lineString := types.String(line)
|
||||
routes = append(routes, &Route{
|
||||
Name: lineString,
|
||||
Code: lineString,
|
||||
})
|
||||
}
|
||||
|
||||
return lineStrings, nil
|
||||
return routes, nil
|
||||
}
|
||||
|
||||
// 设置记录
|
||||
|
||||
110
internal/dnsclients/provider_dnspod_test.go
Normal file
110
internal/dnsclients/provider_dnspod_test.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package dnsclients
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDNSPodProvider_GetRoutes(t *testing.T) {
|
||||
provider, err := testDNSPodProvider()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
routes, err := provider.GetRoutes("yun4s.cn")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logs.PrintAsJSON(routes, t)
|
||||
}
|
||||
|
||||
func TestDNSPodProvider_GetRecords(t *testing.T) {
|
||||
provider, err := testDNSPodProvider()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
records, err := provider.GetRecords("yun4s.cn")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, record := range records {
|
||||
t.Log(record.Id, record.Type, record.Name, record.Value, record.Route)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSPodProvider_AddRecord(t *testing.T) {
|
||||
provider, err := testDNSPodProvider()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = provider.AddRecord("yun4s.cn", &Record{
|
||||
Type: RecordTypeCName,
|
||||
Name: "hello-forward",
|
||||
Value: "hello.yun4s.cn",
|
||||
Route: "联通",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestDNSPodProvider_UpdateRecord(t *testing.T) {
|
||||
provider, err := testDNSPodProvider()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = provider.UpdateRecord("yun4s.cn", &Record{
|
||||
Id: "697036856",
|
||||
}, &Record{
|
||||
Type: RecordTypeA,
|
||||
Name: "hello",
|
||||
Value: "192.168.1.102",
|
||||
Route: "联通",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestDNSPodProvider_DeleteRecord(t *testing.T) {
|
||||
provider, err := testDNSPodProvider()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = provider.DeleteRecord("yun4s.cn", &Record{
|
||||
Id: "697040986",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func testDNSPodProvider() (ProviderInterface, error) {
|
||||
db, err := dbs.Default()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
one, err := db.FindOne("SELECT * FROM edgeDNSProviders WHERE type='dnspod' ORDER BY id DESC")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
apiParams := maps.Map{}
|
||||
err = json.Unmarshal([]byte(one.GetString("apiParams")), &apiParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
provider := &DNSPodProvider{}
|
||||
err = provider.Auth(apiParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return provider, nil
|
||||
}
|
||||
@@ -11,7 +11,7 @@ type ProviderInterface interface {
|
||||
GetRecords(domain string) (records []*Record, err error)
|
||||
|
||||
// 读取域名支持的线路数据
|
||||
GetRoutes(domain string) (routes []string, err error)
|
||||
GetRoutes(domain string) (routes []*Route, err error)
|
||||
|
||||
// 设置记录
|
||||
AddRecord(domain string, newRecord *Record) error
|
||||
|
||||
@@ -5,7 +5,7 @@ type RecordType = string
|
||||
const (
|
||||
RecordTypeA RecordType = "A"
|
||||
RecordTypeCName RecordType = "CNAME"
|
||||
RecordTypeText RecordType = "TXT"
|
||||
RecordTypeTXT RecordType = "TXT"
|
||||
)
|
||||
|
||||
type Record struct {
|
||||
|
||||
7
internal/dnsclients/route.go
Normal file
7
internal/dnsclients/route.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package dnsclients
|
||||
|
||||
// 线路描述
|
||||
type Route struct {
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
@@ -7,24 +7,24 @@ type ProviderType = string
|
||||
// 服务商代号
|
||||
const (
|
||||
ProviderTypeDNSPod ProviderType = "dnspod"
|
||||
ProviderTypeAliyun ProviderType = "aliyun"
|
||||
ProviderTypeAliDNS ProviderType = "alidns"
|
||||
ProviderTypeDNSCom ProviderType = "dnscom"
|
||||
)
|
||||
|
||||
// 所有的服务商类型
|
||||
var AllProviderTypes = []maps.Map{
|
||||
{
|
||||
"name": "阿里云DNS",
|
||||
"code": ProviderTypeAliDNS,
|
||||
},
|
||||
{
|
||||
"name": "DNSPod",
|
||||
"code": ProviderTypeDNSPod,
|
||||
},
|
||||
{
|
||||
"name": "阿里云",
|
||||
"code": ProviderTypeAliyun,
|
||||
},
|
||||
{
|
||||
"name": "帝恩思",
|
||||
/**{
|
||||
"name": "帝恩思DNS.COM",
|
||||
"code": ProviderTypeDNSCom,
|
||||
},
|
||||
},**/
|
||||
}
|
||||
|
||||
// 查找服务商实例
|
||||
@@ -32,6 +32,8 @@ func FindProvider(providerType ProviderType) ProviderInterface {
|
||||
switch providerType {
|
||||
case ProviderTypeDNSPod:
|
||||
return &DNSPodProvider{}
|
||||
case ProviderTypeAliDNS:
|
||||
return &AliDNSProvider{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
@@ -294,7 +293,6 @@ func (this *DNSDomainService) SyncDNSDomainData(ctx context.Context, req *pb.Syn
|
||||
}
|
||||
allChanges = append(allChanges, changes...)
|
||||
}
|
||||
logs.Println("====")
|
||||
for _, change := range allChanges {
|
||||
action := change.GetString("action")
|
||||
record := change.Get("record").(*dnsclients.Record)
|
||||
@@ -316,7 +314,7 @@ func (this *DNSDomainService) SyncDNSDomainData(ctx context.Context, req *pb.Syn
|
||||
}
|
||||
}
|
||||
|
||||
logs.Println(action, record.Name, record.Type, record.Value, record.Route) // TODO 仅供调试
|
||||
//logs.Println(action, record.Name, record.Type, record.Value, record.Route)
|
||||
}
|
||||
|
||||
// 重新更新记录
|
||||
@@ -352,7 +350,16 @@ func (this *DNSDomainService) FindAllDNSDomainRoutes(ctx context.Context, req *p
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.FindAllDNSDomainRoutesResponse{Routes: routes}, nil
|
||||
|
||||
pbRoutes := []*pb.DNSRoute{}
|
||||
for _, route := range routes {
|
||||
pbRoutes = append(pbRoutes, &pb.DNSRoute{
|
||||
Name: route.Name,
|
||||
Code: route.Code,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.FindAllDNSDomainRoutesResponse{Routes: pbRoutes}, nil
|
||||
}
|
||||
|
||||
// 转换域名信息
|
||||
@@ -396,12 +403,16 @@ func (this *DNSDomainService) convertDomainToPB(domain *models.DNSDomain) (*pb.D
|
||||
}
|
||||
|
||||
// 线路
|
||||
routes := []string{}
|
||||
if len(domain.Routes) > 0 && domain.Routes != "null" {
|
||||
err := json.Unmarshal([]byte(domain.Routes), &routes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
routes, err := domain.DecodeRoutes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbRoutes := []*pb.DNSRoute{}
|
||||
for _, route := range routes {
|
||||
pbRoutes = append(pbRoutes, &pb.DNSRoute{
|
||||
Name: route.Name,
|
||||
Code: route.Code,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.DNSDomain{
|
||||
@@ -414,7 +425,7 @@ func (this *DNSDomainService) convertDomainToPB(domain *models.DNSDomain) (*pb.D
|
||||
NodesChanged: nodesChanged,
|
||||
CountServerRecords: int64(countServerRecords),
|
||||
ServersChanged: serversChanged,
|
||||
Routes: routes,
|
||||
Routes: pbRoutes,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -856,6 +856,11 @@ func (this *NodeService) FindAllEnabledNodesDNSWithClusterId(ctx context.Context
|
||||
}
|
||||
dnsDomainId := int64(clusterDNS.DnsDomainId)
|
||||
|
||||
routes, err := models.SharedDNSDomainDAO.FindDomainRoutes(dnsDomainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nodes, err := models.SharedNodeDAO.FindAllEnabledNodesDNSWithClusterId(req.NodeClusterId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -872,11 +877,21 @@ func (this *NodeService) FindAllEnabledNodesDNSWithClusterId(ctx context.Context
|
||||
return nil, err
|
||||
}
|
||||
|
||||
routeName := ""
|
||||
for _, r := range routes {
|
||||
if r.Code == route {
|
||||
routeName = r.Name
|
||||
}
|
||||
}
|
||||
|
||||
result = append(result, &pb.NodeDNSInfo{
|
||||
Id: int64(node.Id),
|
||||
Name: node.Name,
|
||||
IpAddr: ipAddr,
|
||||
Route: route,
|
||||
Route: &pb.DNSRoute{
|
||||
Name: routeName,
|
||||
Code: route,
|
||||
},
|
||||
})
|
||||
}
|
||||
return &pb.FindAllEnabledNodesDNSWithClusterIdResponse{Nodes: result}, nil
|
||||
@@ -915,11 +930,17 @@ func (this *NodeService) FindEnabledNodeDNS(ctx context.Context, req *pb.FindEna
|
||||
}
|
||||
|
||||
var route = ""
|
||||
var routeName = ""
|
||||
if dnsDomainId > 0 {
|
||||
route, err = node.DNSRoute(dnsDomainId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
routeName, err = models.SharedDNSDomainDAO.FindDomainRouteName(dnsDomainId, route)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
ipAddr, err := models.SharedNodeIPAddressDAO.FindFirstNodeIPAddress(int64(node.Id))
|
||||
@@ -929,10 +950,13 @@ func (this *NodeService) FindEnabledNodeDNS(ctx context.Context, req *pb.FindEna
|
||||
|
||||
return &pb.FindEnabledNodeDNSResponse{
|
||||
Node: &pb.NodeDNSInfo{
|
||||
Id: int64(node.Id),
|
||||
Name: node.Name,
|
||||
IpAddr: ipAddr,
|
||||
Route: route,
|
||||
Id: int64(node.Id),
|
||||
Name: node.Name,
|
||||
IpAddr: ipAddr,
|
||||
Route: &pb.DNSRoute{
|
||||
Name: routeName,
|
||||
Code: route,
|
||||
},
|
||||
ClusterId: clusterId,
|
||||
DnsDomainId: dnsDomainId,
|
||||
DnsDomainName: dnsDomainName,
|
||||
|
||||
@@ -515,5 +515,6 @@ func (this *NodeClusterService) CheckNodeClusterDNSChanges(ctx context.Context,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.CheckNodeClusterDNSChangesResponse{IsChanged: len(changes) > 0}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user