Skip to content

Commit

Permalink
add name.com test
Browse files Browse the repository at this point in the history
  • Loading branch information
huyinghuan committed Sep 10, 2021
1 parent 9923217 commit c3f42c9
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 51 deletions.
48 changes: 34 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 阿里云 DNS 动态解析

家里的宽带开通了公网ip,想进行域名解析, 但是路由器有没有ddns功能,因此创建该项目, 利用阿里云注册的备案域名和域名解析API,进行动态域名解析
家里的宽带开通了公网ip,想进行域名解析,利用域名服务商提供的api,进行动态解析。

支持:阿里云域名, name.com


--------------------------

Expand All @@ -27,22 +30,42 @@ env GOOS=linux GOARCH=amd64 go build -o myddns -mod=vendor

## 使用

部署在家庭局域网内任意服务器上【树莓派或者路由器】
部署在家庭局域网内任意服务器上【树莓派或者路由器】。

查看帮助:

```
./myddns -h
```

阿里云域名:

```
./myddns --cloud aliyun --accessId xxx --accessKey xxxx --domain my.domain.com --refresh 30
```

启动时 指定`access id``access key`
name.com域名:

```
./myddns --accessId xxx --accessKey xxxx --domain my.domain.com --refresh 30
./myddns --cloud name.com --user xxx --token xxxx --domain my.domain.com --refresh 30
```

参数说明:

```
accessId: aliyun access id #注意创建的ram用户需要给aliyun dns 访问权限 必须
accessKey: aliyun access key #必须
cloud: 服务商 [可选]域名服务商,支持: aliyun name.com , 默认为aliyun
// aliyun 配置
accessId: aliyun access id #注意创建的ram用户需要给aliyun dns 访问权限 必须
accessKey: aliyun access key
// name.com 配置
user: 用户名
token: token 见: https://www.name.com/zh-cn/account/settings/api
domain: 需要解析的域名 #必须
refresh: 刷新检查ip间隔 30s #可选
```

可以使用`systemd`进行进程管理, 新建文件`/etc/systemd/system/myddns.service`
Expand All @@ -62,7 +85,9 @@ Restart=always
[Install]
WantedBy=multi-user.target
```

`systemd`启动服务

```
sudo systemctl enable myddns
sudo systemctl start myddns
Expand All @@ -71,12 +96,7 @@ sudo systemctl start myddns
journalctl -u myddns.service -f # 结束日志查看 ctrl+c
```




### 参考

https://help.aliyun.com/knowledge_detail/39863.html?spm=a2c4g.11186623.6.624.12f91550pQYrFU



- aliyun: https://help.aliyun.com/knowledge_detail/39863.html?spm=a2c4g.11186623.6.624.12f91550pQYrFU
- name.com: https://www.name.com/zh-cn/api-docs/
3 changes: 0 additions & 3 deletions cloud/aliyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ func (cloud *InsideAliyun) UpdateDomainRecord(id string, domain Domain) error {
// AddOrUpdateDomain
// @return isChange error
func (cloud *InsideAliyun) AddOrUpdateDomain(domain Domain) (bool, error) {
// if insideAliyun == nil {

// }
records, err := cloud.GetDomainRecords(domain.DomainName)
if err != nil {
return false, err
Expand Down
24 changes: 24 additions & 0 deletions cloud/aliyun_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cloud

import (
"testing"

"github.com/huyinghuan/ddns/config"
)

func TestAliyun(t *testing.T) {
server := CreateAliyun(config.AliyunConfig{
AccessKeyID: "",
AccessKeySecret: "",
})

hasChange, err := server.AddOrUpdateDomain(Domain{
RR: "",
IP: "",
DomainName: "",
})
if err != nil {
t.Fatal(err)
}
t.Log(hasChange)
}
21 changes: 0 additions & 21 deletions cloud/domainrecords_test.go.bak

This file was deleted.

6 changes: 3 additions & 3 deletions cloud/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

type Domain struct {
RecordId string
DomainName string
RR string
DomainName string // example.com
RR string // www
IP string
OriginDomain string
OriginDomain string // www.example.com
}

func ParseDomain(domain string) (Domain, error) {
Expand Down
17 changes: 9 additions & 8 deletions cloud/namecom.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type NameComItem struct {
ID int64 `json:"id,omitempty"`
Domain string `json:"domainName,omitempty"`
Host string `json:"host,omitempty"`
RR string `json:"fqdn,omitempty"`
Type string `json:"type,omitempty"`
IP string `json:"answer,omitempty"`
TTL int `json:"ttl,omitempty"`
Expand Down Expand Up @@ -65,7 +64,7 @@ func (cloud *InsideNameCom) GetDomainRecords(domain string) ([]Domain, error) {
for _, record := range items.Records {
queue = append(queue, Domain{
RecordId: strconv.FormatInt(record.ID, 10),
RR: record.RR,
RR: record.Host,
IP: record.IP,
DomainName: record.Domain,
})
Expand All @@ -75,8 +74,7 @@ func (cloud *InsideNameCom) GetDomainRecords(domain string) ([]Domain, error) {
func (cloud *InsideNameCom) AddDomainRecord(domain Domain) error {
api := fmt.Sprintf("%s/v4/domains/%s/records", cloud.API, domain.DomainName)
item := NameComItem{
Host: domain.DomainName,
RR: domain.RR,
Host: domain.RR,
Type: "A",
IP: domain.IP,
TTL: 300,
Expand All @@ -100,10 +98,9 @@ func (cloud *InsideNameCom) AddDomainRecord(domain Domain) error {
return nil
}
func (cloud *InsideNameCom) UpdateDomainRecord(id string, domain Domain) error {
api := fmt.Sprintf("%s/v4/domains/%s/records/%s", cloud.API, domain.DomainName, domain.RecordId)
api := fmt.Sprintf("%s/v4/domains/%s/records/%s", cloud.API, domain.DomainName, id)
item := NameComItem{
Host: domain.DomainName,
RR: domain.RR,
Host: domain.RR,
Type: "A",
IP: domain.IP,
TTL: 300,
Expand Down Expand Up @@ -145,8 +142,12 @@ func (cloud *InsideNameCom) AddOrUpdateDomain(domain Domain) (bool, error) {
}

func CreateNameCom(conf config.NameComConfig) *InsideNameCom {
api := "https://api.name.com"
if conf.API != "" {
api = conf.API
}
return &InsideNameCom{
API: "https://api.name.com",
API: api,
Username: conf.Username,
Token: conf.Token,
}
Expand Down
26 changes: 26 additions & 0 deletions cloud/namecome_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cloud

import (
"testing"

"github.com/huyinghuan/ddns/config"
)

func TestNameCome(t *testing.T) {
server := CreateNameCom(config.NameComConfig{
Username: "",
Token: "",
API: "https://api.name.com",
})

hasChange, err := server.AddOrUpdateDomain(Domain{
RR: "",
IP: "",
DomainName: "",
})
if err != nil {
t.Fatal(err)
}
t.Log(hasChange)

}
1 change: 1 addition & 0 deletions config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type AliyunConfig struct {
type NameComConfig struct {
Username string
Token string
API string
}

type Config struct {
Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {

var cloudName string

flag.StringVar(&cloudName, "cloud", "aliyun", "云服务商,支持: aliyun, name.com")
flag.StringVar(&cloudName, "cloud", "aliyun", "[可选]域名服务商,支持: aliyun name.com , 默认为aliyun")

flag.StringVar(&accessId, "accessId", "", "阿里云access id")
flag.StringVar(&accessKey, "accessKey", "", "阿里云access key")
Expand Down Expand Up @@ -82,7 +82,9 @@ func main() {
}

var cloudServer cloud.CloudServer

if cloudName == "" {
cloudName = "aliyun"
}
if cloudName == "aliyun" {
if conf.Aliyun.AccessKeyID == "" || conf.Aliyun.AccessKeySecret == "" {
log.Fatalln("关键参数不能为空: accessId, accessKey")
Expand All @@ -93,6 +95,8 @@ func main() {
log.Fatalln("关键参数不能为空: username, token")
}
cloudServer = cloud.CreateNameCom(conf.NameCom)
} else {
log.Fatalln("不支持该域名服务商")
}

log.Printf("服务商: %s 监控ip变动间隔: %ds 目标域名:\n -- %s \n", cloudName, conf.Refresh, strings.Join(targetDomainList, "\n -- "))
Expand Down

0 comments on commit c3f42c9

Please sign in to comment.