Skip to content

Commit 0566f85

Browse files
authored
feat/rewrite go (#1)
* feat: rewrite to go, support ipv6 * feat: add test github action * fix: github action hook * feat: add flags and config * feat: add makefile
1 parent 045b17d commit 0566f85

File tree

14 files changed

+595
-115
lines changed

14 files changed

+595
-115
lines changed

.env.sample

Lines changed: 0 additions & 5 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Test
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Install Go
14+
uses: actions/setup-go@v2
15+
with:
16+
go-version: 1.16
17+
18+
- name: Checkout
19+
uses: actions/checkout@v2
20+
21+
- name: Run tests
22+
run: go test -v

.gitignore

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,101 @@
1-
.DS_Store
21

3-
# local env files
2+
# Created by https://www.toptal.com/developers/gitignore/api/macos,linux,windows,go,dotenv
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,linux,windows,go,dotenv
4+
5+
### dotenv ###
46
.env
5-
.env.local
6-
.env.*.local
7-
8-
# Log files
9-
npm-debug.log*
10-
yarn-debug.log*
11-
yarn-error.log*
12-
13-
# Editor directories and files
14-
.idea
15-
.vscode
16-
*.suo
17-
*.ntvs*
18-
*.njsproj
19-
*.sln
20-
*.sw*
7+
8+
### Go ###
9+
# Binaries for programs and plugins
10+
*.exe
11+
*.exe~
12+
*.dll
13+
*.so
14+
*.dylib
15+
16+
# Test binary, built with `go test -c`
17+
*.test
18+
19+
# Output of the go coverage tool, specifically when used with LiteIDE
20+
*.out
21+
22+
# Dependency directories (remove the comment below to include it)
23+
# vendor/
24+
25+
### Go Patch ###
26+
/vendor/
27+
/Godeps/
28+
29+
### Linux ###
30+
*~
31+
32+
# temporary files which can be created if a process still has a handle open of a deleted file
33+
.fuse_hidden*
34+
35+
# KDE directory preferences
36+
.directory
37+
38+
# Linux trash folder which might appear on any partition or disk
39+
.Trash-*
40+
41+
# .nfs files are created when an open file is removed but is still being accessed
42+
.nfs*
43+
44+
### macOS ###
45+
# General
46+
.DS_Store
47+
.AppleDouble
48+
.LSOverride
49+
50+
# Icon must end with two \r
51+
Icon
52+
53+
54+
# Thumbnails
55+
._*
56+
57+
# Files that might appear in the root of a volume
58+
.DocumentRevisions-V100
59+
.fseventsd
60+
.Spotlight-V100
61+
.TemporaryItems
62+
.Trashes
63+
.VolumeIcon.icns
64+
.com.apple.timemachine.donotpresent
65+
66+
# Directories potentially created on remote AFP share
67+
.AppleDB
68+
.AppleDesktop
69+
Network Trash Folder
70+
Temporary Items
71+
.apdisk
72+
73+
### Windows ###
74+
# Windows thumbnail cache files
75+
Thumbs.db
76+
Thumbs.db:encryptable
77+
ehthumbs.db
78+
ehthumbs_vista.db
79+
80+
# Dump file
81+
*.stackdump
82+
83+
# Folder config file
84+
[Dd]esktop.ini
85+
86+
# Recycle Bin used on file shares
87+
$RECYCLE.BIN/
88+
89+
# Windows Installer files
90+
*.cab
91+
*.msi
92+
*.msix
93+
*.msm
94+
*.msp
95+
96+
# Windows shortcuts
97+
*.lnk
98+
99+
# End of https://www.toptal.com/developers/gitignore/api/macos,linux,windows,go,dotenv
100+
101+
bin/

.go-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.16.4

DIYDDNS.sh

Lines changed: 0 additions & 49 deletions
This file was deleted.

Makefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
_NAME=DIYDDNS
2+
3+
compile:
4+
for os in darwin linux; do \
5+
for arch in amd64 arm64; do \
6+
GOOS=$$os GOARCH=$$arch go build -o bin/$(_NAME) . ; \
7+
cd bin ; \
8+
tar czf $(_NAME)-$$os-$$arch.tar.gz $(_NAME) ; \
9+
sha256sum $(_NAME)-$$os-$$arch.tar.gz > $(_NAME)-$$os-$$arch.tar.gz.sha256 ; \
10+
cd - ; \
11+
done ; \
12+
done ; \
13+
for arm in 5 6 7; do \
14+
GOOS=linux GOARCH=arm GOARM=$$arm go build -o bin/$(_NAME) . ; \
15+
cd bin ; \
16+
tar czf $(_NAME)-linux-armv$$arm.tar.gz $(_NAME) ; \
17+
sha256sum $(_NAME)-linux-armv$$arm.tar.gz > $(_NAME)-linux-armv$$arm.tar.gz.sha256 ; \
18+
cd - ; \
19+
done
20+
21+
clean:
22+
rm bin/$(_NAME)*

README.md

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,19 @@
1-
[![Bash](https://img.shields.io/github/license/ctrlaltdev/DIYDDNS.svg?style=for-the-badge)](https:github.com/ctrlaltdev/DIYDDNS/blob/master/LICENSE)
2-
![Bash](https://img.shields.io/badge/_-SH-4EAA25.svg?style=for-the-badge)
3-
41
DIY Script for Dynamic DNS using Cloudflare APIs
52

63
## INIT
74

85
You'll need your Cloudflare API Key (in your profile, on cloudflare website)
96

10-
Copy `.env.sample` to `.env`:
11-
```bash
12-
$ cp .env.sample .env
13-
```
14-
15-
Edit .env and fill with the email you use on Clouflare and your API KEY.
16-
17-
### Get your DNS zone ID:
18-
19-
```bash
20-
$ . .env; \
21-
curl -X GET "https://api.cloudflare.com/client/v4/zones" \
22-
-H "X-Auth-Email: $EMAIL" \
23-
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
24-
-H "Content-Type: application/json" \
25-
| python -m json.tool
26-
```
27-
28-
Find the right DNS zone, and copy the id to the ZONE_ID var in `.env`.
29-
30-
### Get your DNS entry ID:
31-
32-
```bash
33-
$ . .env; \
34-
curl -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
35-
-H "X-Auth-Email: $EMAIL" \
36-
-H "X-Auth-Key: $CLOUDFLARE_API_KEY" \
37-
-H "Content-Type: application/json" \
38-
| python -m json.tool
7+
```sh
8+
DIYDDNS -init
399
```
40-
41-
Find the right DNS entry, and copy the id and the name to DNS_ID and DNS_NAME vars in `.env`.
42-
43-
BAM, you're good to go.
10+
and provide your cloudflare email and api key when prompted
4411

4512
## RUN IT LIKE YOU MEAN IT
4613

4714
Now that you're set, you have to make that run: `all hail the mighty cron`
4815

4916
You should be able to use something like that:
5017
```
51-
* * * * * /bin/bash /path/to/DIYDDNS.sh >> /dev/null
52-
```
53-
## Want a PTR with that?
54-
55-
If you want to create a PTR record too, just pass a flag to the cron:
56-
```
57-
* * * * * /bin/bash /path/to/DIYDDNS.sh --ptr >> /dev/null
18+
* * * * * DIYDDNS -fqdn sub.domain.tld >> /dev/null
5819
```

cloudflare.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"net"
6+
7+
"github.com/cloudflare/cloudflare-go"
8+
)
9+
10+
type CFConf struct {
11+
API_KEY string `yaml:"CF_API_KEY"`
12+
API_EMAIL string `yaml:"CF_API_EMAIL"`
13+
}
14+
15+
func GetRecords(domain string, name string, recordType string) ([]cloudflare.DNSRecord, error) {
16+
api, err := cloudflare.New(CONF.API_KEY, CONF.API_EMAIL)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
ctx := context.Background()
22+
23+
_, err = api.UserDetails(ctx)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
zoneID, err := api.ZoneIDByName(domain)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
host := cloudflare.DNSRecord{
34+
Name: name,
35+
Type: recordType,
36+
}
37+
38+
records, err := api.DNSRecords(ctx, zoneID, host)
39+
40+
return records, err
41+
}
42+
43+
func CreateRecord(domain string, name string, recordType string, dst net.IP) error {
44+
api, err := cloudflare.New(CONF.API_KEY, CONF.API_EMAIL)
45+
if err != nil {
46+
return err
47+
}
48+
49+
ctx := context.Background()
50+
51+
_, err = api.UserDetails(ctx)
52+
if err != nil {
53+
return err
54+
}
55+
56+
zoneID, err := api.ZoneIDByName(domain)
57+
if err != nil {
58+
return err
59+
}
60+
61+
rec := cloudflare.DNSRecord{
62+
Name: name,
63+
Type: recordType,
64+
Content: dst.String(),
65+
}
66+
67+
_, err = api.CreateDNSRecord(ctx, zoneID, rec)
68+
if err != nil {
69+
return err
70+
}
71+
72+
return nil
73+
}
74+
75+
func UpdateRecord(record cloudflare.DNSRecord, dst net.IP) error {
76+
api, err := cloudflare.New(CONF.API_KEY, CONF.API_EMAIL)
77+
if err != nil {
78+
return err
79+
}
80+
81+
ctx := context.Background()
82+
83+
_, err = api.UserDetails(ctx)
84+
if err != nil {
85+
return err
86+
}
87+
88+
rec := record
89+
rec.Content = dst.String()
90+
91+
err = api.UpdateDNSRecord(ctx, record.ZoneID, record.ID, rec)
92+
if err != nil {
93+
return err
94+
}
95+
96+
return nil
97+
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/ctrlaltdev/DIYDDNS
2+
3+
go 1.16
4+
5+
require (
6+
github.com/cloudflare/cloudflare-go v0.17.0
7+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
8+
)

0 commit comments

Comments
 (0)