commit 3157d714f22e866924ddbd4dd838653c595d7187
Author: Paco Esteban <paco@onna.be>
Date: Thu, 1 Jun 2017 16:57:46 +0200
first commit
Diffstat:
4 files changed, 101 insertions(+), 0 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2017 Paco Esteban
+
+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.
diff --git a/README.md b/README.md
@@ -0,0 +1,10 @@
+duckdnsrefresh
+==============
+
+Utility to refresh dyn domain names from duckdns.org
+
+It is pretty simple, and could be improved ... I might do that one day.
+
+It only accepts `-v` to be a little more verbose. All the other configs need to be on a config file (YAML or JSON, it uses [viper](https://github.com/spf13/viper)). File has to be located in `$HOME/.duckdns/config.yaml` or in `.`
+
+There's a config file example in the repo.
diff --git a/config-example.yaml b/config-example.yaml
@@ -0,0 +1,4 @@
+token: mytoken
+domains:
+ - "domain1"
+ - "domain2"
diff --git a/main.go b/main.go
@@ -0,0 +1,66 @@
+//
+// main.go
+// Copyright (C) 2017 Paco Esteban <paco@onna.be>
+//
+// Distributed under terms of the MIT license.
+//
+
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "net/http"
+
+ "github.com/spf13/viper"
+)
+
+var (
+ token string
+ domains []string
+)
+
+func main() {
+ var v = flag.Bool("v", false, "Enable verbose output")
+ flag.Parse()
+ viper.SetConfigName("config") // name of config file (without extension)
+ viper.AddConfigPath("/etc/duckdns/") // path to look for the config file in
+ viper.AddConfigPath("$HOME/.duckdns") // call multiple times to add many search paths
+ viper.AddConfigPath(".") // optionally look for config in the working directory
+ err := viper.ReadInConfig() // Find and read the config file
+ if err != nil { // Handle errors reading the config file
+ panic(fmt.Errorf("Fatal error config file: %s \n", err))
+ }
+
+ token = viper.GetString("token")
+ domains = viper.GetStringSlice("domains")
+
+ for _, d := range domains {
+ if *v {
+ log.Printf("Trying to update domain: %s", d)
+ }
+ req := fmt.Sprintf("https://www.duckdns.org/update?domains=%s&token=%s", d, token)
+ r, err := http.Get(req)
+ if err != nil {
+ log.Fatalf("cannot get url: %s : %q", req, err)
+ }
+ if r.StatusCode != http.StatusOK {
+ log.Fatalf("incorrect response from server: %s ", r.Status)
+ }
+ defer r.Body.Close()
+
+ body, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ log.Fatalf("could not read the body: %q", err)
+ }
+ if string(body) != "OK" {
+ log.Fatalf("error while refreshing domain: %s", string(body))
+ }
+
+ if *v {
+ log.Printf("All good. got response '%s' from DuckDNS.", string(body))
+ }
+ }
+}