Skip to content

Commit

Permalink
feat(all): initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aerialls committed Jun 24, 2020
0 parents commit 3006faa
Show file tree
Hide file tree
Showing 19 changed files with 835 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
time: '04:00'
open-pull-requests-limit: 10
26 changes: 26 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Release
on:
push:
tags:
- '*'

jobs:
goreleaser:
name: GoReleaser
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.14
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test
on:
pull_request:
push:

jobs:
test:
name: Test
strategy:
matrix:
go-version: [1.13, 1.14]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Test
run: go test ./...
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Deploy
bin/
dist/

# Test binary, built with `go test -c`
*.test

# Local binary
/scaleway-ddns

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
34 changes: 34 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
before:
hooks:
- go mod tidy
builds:
- main: ./cmd/scaleway-ddns
env:
- CGO_ENABLED=0
goarch:
- amd64
goos:
- darwin
- linux
- windows
ldflags:
- -s -w
- -X main.version={{.Version}}
- -X main.commit={{.ShortCommit}}
- -X main.date={{.Date}}
checksum:
name_template: checksums.txt
snapshot:
name_template: "{{.Tag}}-{{.ShortCommit}}"
changelog:
sort: asc
filters:
exclude:
- ^(feat|fix)\(doc\)
- ^(feat|fix)\(test\)
- ^Merge pull request
archives:
- wrap_in_directory: true
files:
- LICENSE
- scaleway-ddns.yml
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @aerialls
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Julien Brochet

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# scaleway-ddns

[![Build status](https://github.com/aerialls/scaleway-ddns/workflows/Test/badge.svg)](https://github.com/aerialls/scaleway-ddns/actions?query=workflow%3ATest)
[![Go report card](https://goreportcard.com/badge/github.com/aerialls/scaleway-ddns)](https://goreportcard.com/report/github.com/aerialls/scaleway-ddns)
![Go version](https://img.shields.io/github/go-mod/go-version/aerialls/scaleway-ddns)

Dynamic DNS service based on [Scaleway DNS](https://console.scaleway.com/domains/external).

## Usage

```
scaleway-ddns --config /etc/scaleway-ddns/scaleway-ddns.yml
```

## Parameters

* `--config` - Specify the location of the configuration file (**required**)
* `--dry-run` - Do not perform update actions (default `false`)
* `--verbose` - Display debug messages (default `false`)
* `--help` - Display the help message block

## Configuration

```yaml
scaleway:
organization_id: __ORGANIZATION_ID__
access_key: __ACCESS_KEY__
secret_key: __SECRET_KEY__

domain:
name: contoso.com
record: public
ttl: 60

interval: 300

ipv4:
enabled: true
url: https://api-ipv4.ip.sb/ip

ipv6:
enabled: true
url: https://api-ipv6.ip.sb/ip
```
**Note**: IPv6 is disabled by default.
13 changes: 13 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Security Policy

## Supported Versions

The following versions are currently being supported with security updates.

| Version | Supported |
| ------- | ------------------ |
| 0.0.x | :white_check_mark: |

## Reporting a Vulnerability

To report a suspected vulnerability, please contact [email protected] and include the steps to produce the vulnerability.
63 changes: 63 additions & 0 deletions cmd/scaleway-ddns/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"github.com/aerialls/scaleway-ddns/config"
"github.com/aerialls/scaleway-ddns/ip"
"github.com/aerialls/scaleway-ddns/scaleway"
)

// UpdateDNSRecordFromCurrentIP updates the DNS record on Scaleway DNS based
// on the current IP for IPv4 or IPv6
func UpdateDNSRecordFromCurrentIP(
dns *scaleway.DNS,
domain config.DomainConfig,
cfg config.IPConfig,
recordType string,
dryRun bool,
) error {
if !cfg.Enabled {
logger.Debugf("skipping %s update, disabled in the configuration", recordType)
return nil
}

scalewayIP, err := dns.GetRecord(domain.Name, domain.Record, recordType)
if err != nil {
return err
}

currentIP, err := ip.GetPublicIP(cfg.URL)
if err != nil {
return err
}

logger.Debugf("current IP registered in Scaleway is %s", scalewayIP)
logger.Debugf("current public IP is %s", currentIP)

if scalewayIP == currentIP {
logger.Debug("both IPs are identical, nothing to do")
return nil
}

logger.Infof(
"updating %s record from %s to %s", recordType, scalewayIP, currentIP,
)

if dryRun {
logger.Info("running in dry-run mode, doing nothing")
return nil
}

err = dns.UpdateRecord(
domain.Name,
domain.Record,
domain.TTL,
currentIP,
recordType,
)

if err != nil {
return err
}

return nil
}
107 changes: 107 additions & 0 deletions cmd/scaleway-ddns/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"fmt"
"os"
"time"

"github.com/aerialls/scaleway-ddns/config"
"github.com/aerialls/scaleway-ddns/scaleway"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
cfgFile string
verbose bool
logger *logrus.Logger
dryRun bool
)

var rootCmd = &cobra.Command{
Use: "scaleway-ddns",
Short: "Dynamic DNS service based on Scaleway DNS",
Run: func(cmd *cobra.Command, args []string) {
logger.Info("starting dynamic records for Scaleway DNS")

cfg, err := config.NewConfig(cfgFile)
if err != nil {
logger.Fatal(err)
}

dns, err := scaleway.NewDNS(
logger,
cfg.ScalewayConfig.OrganizationID,
cfg.ScalewayConfig.AccessKey,
cfg.ScalewayConfig.SecretKey,
)

if err != nil {
logger.Fatal(err)
}

ticker := time.NewTicker(time.Duration(cfg.Interval) * time.Second)

for {
select {
case <-ticker.C:
logger.Debugf(
"updating A/AAAA records for %s.%s",
cfg.DomainConfig.Record,
cfg.DomainConfig.Name,
)

recordTypes := map[string]config.IPConfig{
"A": cfg.IPv4Config,
"AAAA": cfg.IPv6Config,
}

for recordType, recordCfg := range recordTypes {
err := UpdateDNSRecordFromCurrentIP(
dns,
cfg.DomainConfig,
recordCfg,
recordType,
dryRun,
)

if err != nil {
logger.WithError(err).Errorf(
"unable to update %s record", recordType,
)
}
}
}
}
},
}

func init() {
logger = logrus.New()

cobra.OnInitialize(initConfig)

rootCmd.Flags().StringVar(&cfgFile, "config", "", "config file")
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose logging")
rootCmd.Flags().BoolVarP(&dryRun, "dry-run", "d", false, "don't update DNS records")

rootCmd.MarkFlagRequired("config")
}

func initConfig() {
level := logrus.InfoLevel
if verbose {
level = logrus.DebugLevel
}

logger.SetLevel(level)
logger.SetFormatter(&logrus.TextFormatter{})
}

func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
25 changes: 25 additions & 0 deletions cmd/scaleway-ddns/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"

"github.com/spf13/cobra"
)

var (
version = "undefined"
commit = "dev"
date = "undefined"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(fmt.Sprintf("scaleway-ddns %s (commit %s, built at %s)", version, commit, date))
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}
Loading

0 comments on commit 3006faa

Please sign in to comment.