2019-04-05 11:39:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-02 18:15:52 +00:00
|
|
|
"bytes"
|
2019-04-05 11:39:35 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-04-05 12:49:57 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli"
|
2019-04-05 11:39:35 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-04-06 11:56:17 +00:00
|
|
|
"net/smtp"
|
2019-04-05 11:39:35 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Configuration struct {
|
2019-05-02 18:15:52 +00:00
|
|
|
Username string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
GandiKey string `json:"gandikey"`
|
|
|
|
Domains []string `json:"domains"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DnsResponse struct {
|
|
|
|
RecordType string `json:"rrset_type"`
|
|
|
|
Ttl int `json:"rrset_ttl"`
|
|
|
|
Name string `json:"rrset_name"`
|
|
|
|
Values []string `json:"rrset_values"`
|
2019-04-05 11:39:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 12:49:57 +00:00
|
|
|
var (
|
2019-06-12 06:53:29 +00:00
|
|
|
configPath string
|
2019-04-05 12:49:57 +00:00
|
|
|
)
|
|
|
|
|
2019-04-05 11:39:35 +00:00
|
|
|
func main() {
|
|
|
|
|
2019-04-05 12:49:57 +00:00
|
|
|
app := cli.NewApp()
|
2019-05-02 18:15:52 +00:00
|
|
|
app.Name = "dnsupdater"
|
2019-06-12 06:53:29 +00:00
|
|
|
app.Version = "0.0.1"
|
2019-05-02 18:15:52 +00:00
|
|
|
app.Usage = "Automatically update dns"
|
2019-04-05 12:49:57 +00:00
|
|
|
app.Action = start
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, C",
|
|
|
|
Usage: "path to config file",
|
|
|
|
Value: "/config/conf.json",
|
|
|
|
EnvVar: "CONFIG_PATH",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 06:53:29 +00:00
|
|
|
func start(c *cli.Context) {
|
|
|
|
if err := update(c); err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
SendStatusMail(fmt.Sprintf("Error during update process : %s", err))
|
2019-04-05 12:49:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(c *cli.Context) error {
|
|
|
|
configPath = c.String("config")
|
2019-04-05 11:39:35 +00:00
|
|
|
|
2019-04-05 12:49:57 +00:00
|
|
|
if confPathExists, _ := exists(configPath); !confPathExists {
|
2019-05-02 18:15:52 +00:00
|
|
|
configPath = "./data"
|
2019-04-05 11:39:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 12:49:57 +00:00
|
|
|
file, _ := os.Open(path.Join(configPath, "conf.json"))
|
2019-04-05 11:39:35 +00:00
|
|
|
defer file.Close()
|
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
configuration := Configuration{}
|
|
|
|
err := decoder.Decode(&configuration)
|
|
|
|
|
2019-04-05 12:49:57 +00:00
|
|
|
newIp, err := GetOutboundIP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-05 11:39:35 +00:00
|
|
|
|
2019-06-12 06:53:29 +00:00
|
|
|
updatedDomains := ""
|
|
|
|
for _, domain := range configuration.Domains {
|
|
|
|
currentIp, err := GetCurrentIp(configuration, domain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentIp == newIp {
|
|
|
|
continue
|
|
|
|
}
|
2019-04-05 11:39:35 +00:00
|
|
|
|
2019-06-12 06:53:29 +00:00
|
|
|
fmt.Printf("%s -> Ip has changed %s -> %s\n", domain, currentIp, newIp)
|
2019-04-05 11:39:35 +00:00
|
|
|
|
2019-06-01 17:31:30 +00:00
|
|
|
err = SetCurrentIp(newIp, domain, configuration)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-12 06:53:29 +00:00
|
|
|
updatedDomains += fmt.Sprintf("\t- %s\n", domain)
|
2019-04-05 12:49:57 +00:00
|
|
|
}
|
2019-04-06 11:56:17 +00:00
|
|
|
|
2019-06-12 06:53:29 +00:00
|
|
|
if updatedDomains != "" {
|
|
|
|
SendStatusMail(fmt.Sprintf(`Home IP has changed : %s
|
2019-06-14 04:46:00 +00:00
|
|
|
Dns updated successfully for domains
|
|
|
|
%s`, newIp, updatedDomains))
|
2019-06-12 06:53:29 +00:00
|
|
|
}
|
2019-04-06 11:56:17 +00:00
|
|
|
|
2019-04-05 12:49:57 +00:00
|
|
|
return nil
|
2019-04-05 11:39:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 11:56:17 +00:00
|
|
|
func SendStatusMail(messageText string) {
|
|
|
|
// user we are authorizing as
|
|
|
|
from := "laurent@lehouerou.net"
|
|
|
|
|
|
|
|
// use we are sending email to
|
|
|
|
to := "laurent@lehouerou.net"
|
|
|
|
|
|
|
|
// server we are authorized to send email through
|
|
|
|
host := "smtp.fastmail.com"
|
|
|
|
|
|
|
|
// Create the authentication for the SendMail()
|
|
|
|
// using PlainText, but other authentication methods are encouraged
|
|
|
|
auth := smtp.PlainAuth("", from, "c9bd8fb8l4bhs2f8", host)
|
|
|
|
|
|
|
|
// NOTE: Using the backtick here ` works like a heredoc, which is why all the
|
|
|
|
// rest of the lines are forced to the beginning of the line, otherwise the
|
|
|
|
// formatting is wrong for the RFC 822 style
|
|
|
|
message := fmt.Sprintf(`To: "Laurent Le Houerou" <laurent@lehouerou.net>
|
|
|
|
From: "Laurent Le Houerou" <laurent@lehouerou.net>
|
|
|
|
Subject: DnsUpdater Status
|
|
|
|
|
|
|
|
%s
|
2019-05-02 18:15:52 +00:00
|
|
|
`, messageText)
|
2019-04-06 11:56:17 +00:00
|
|
|
|
|
|
|
if err := smtp.SendMail(host+":587", auth, from, []string{to}, []byte(message)); err != nil {
|
|
|
|
fmt.Println("Error SendMail: ", err)
|
|
|
|
} else {
|
|
|
|
fmt.Println("Email Sent!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 06:53:29 +00:00
|
|
|
func GetCurrentIp(configuration Configuration, domain string) (string, error) {
|
|
|
|
url := fmt.Sprintf("https://dns.api.gandi.net/api/v5/domains/%s/records/@/A", domain)
|
2019-05-02 18:15:52 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
2019-06-01 17:31:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-05-02 18:15:52 +00:00
|
|
|
req.Header.Set("X-Api-Key", configuration.GandiKey)
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
2019-04-05 12:49:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-05-02 18:15:52 +00:00
|
|
|
defer resp.Body.Close()
|
2019-04-05 11:39:35 +00:00
|
|
|
|
2019-05-02 18:15:52 +00:00
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
dnsresponse := DnsResponse{}
|
|
|
|
err = decoder.Decode(&dnsresponse)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return dnsresponse.Values[0], nil
|
2019-04-05 11:39:35 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 18:15:52 +00:00
|
|
|
func SetCurrentIp(newip string, domain string, configuration Configuration) error {
|
|
|
|
url := fmt.Sprintf("https://dns.api.gandi.net/api/v5/domains/%s/records/@/A", domain)
|
|
|
|
fmt.Println("URL:>", url)
|
|
|
|
|
|
|
|
var str = fmt.Sprintf("{\"rrset_ttl\": %d,\"rrset_values\": [\"%s\"]}", 600, newip)
|
|
|
|
fmt.Println("json:", str)
|
|
|
|
var jsonStr = []byte(str)
|
|
|
|
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
|
2019-06-01 17:31:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-02 18:15:52 +00:00
|
|
|
req.Header.Set("X-Api-Key", configuration.GandiKey)
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
2019-04-05 12:49:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-02 18:15:52 +00:00
|
|
|
defer resp.Body.Close()
|
2019-04-05 11:39:35 +00:00
|
|
|
|
2019-05-02 18:15:52 +00:00
|
|
|
fmt.Println("response Status:", resp.Status)
|
|
|
|
fmt.Println("response Headers:", resp.Header)
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
fmt.Println("response Body:", string(body))
|
2019-04-05 12:49:57 +00:00
|
|
|
return nil
|
2019-04-05 11:39:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 12:49:57 +00:00
|
|
|
func GetOutboundIP() (string, error) {
|
2019-05-02 18:15:52 +00:00
|
|
|
url := "https://api.ipify.org"
|
2019-04-05 11:39:35 +00:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
2019-04-05 12:49:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-04-05 11:39:35 +00:00
|
|
|
res, err := http.DefaultClient.Do(req)
|
2019-04-05 12:49:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-04-05 11:39:35 +00:00
|
|
|
defer res.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
2019-04-05 12:49:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(body), nil
|
2019-04-05 11:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func exists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, err
|
|
|
|
}
|