From 955b0406997a7c62bea47a520f484f6137164b3f Mon Sep 17 00:00:00 2001 From: Laurent Le Houerou Date: Fri, 5 Apr 2019 15:39:35 +0400 Subject: [PATCH] Initial commit --- .gitignore | 14 +++++ main.go | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 .gitignore create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2cc188 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +.idea/ diff --git a/main.go b/main.go new file mode 100644 index 0000000..59fe270 --- /dev/null +++ b/main.go @@ -0,0 +1,146 @@ +package main + +import ( + "encoding/json" + "fmt" + "gopkg.in/src-d/go-git.v4" + "gopkg.in/src-d/go-git.v4/plumbing/object" + githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http" + "io/ioutil" + "net/http" + "os" + "path" + "regexp" + "strings" + "time" +) + +type Configuration struct { + RepositoryUrl string `json:"url"` + Username string `json:"name"` + Email string `json:"email"` + GitLogin string `json:"login"` + GitPassword string `json:"password"` +} + +var configPath string + +func main() { + + Info("Running dnsupdater...") + + configPath = os.Args[1] + + if confPathExists, _ := exists(configPath); !confPathExists{ + configPath = "./data"; + } + + file, _ := os.Open(path.Join(configPath,"conf.json")) + defer file.Close() + decoder := json.NewDecoder(file) + configuration := Configuration{} + err := decoder.Decode(&configuration) + + err = os.RemoveAll(path.Join(configPath,"data")) + CheckIfError(err) + + _, err = git.PlainClone(path.Join(configPath,"data"), false, &git.CloneOptions{ + URL: configuration.RepositoryUrl, + Progress: os.Stdout, + }) + CheckIfError(err) + + currentIp := GetCurrentIp() + newIp := GetOutboundIP() + + if currentIp == newIp { + Info("Ip has not changed %s", currentIp) + os.Exit(0) + } + + Info("Ip has changed %s -> %s", currentIp, newIp) + SetCurrentIp(currentIp, newIp) + + repo, err := git.PlainOpen(path.Join(configPath,"data")) + CheckIfError(err) + w, err := repo.Worktree() + CheckIfError(err) + _, err = w.Add("templates/home_a.lua") + CheckIfError(err) + + status, err := w.Status() + CheckIfError(err) + + fmt.Println(status) + _, err = w.Commit(fmt.Sprintf("Changement d'IP maison : %s -> %s", currentIp, newIp), &git.CommitOptions{ + Author: &object.Signature{ + Name: configuration.Username, + Email: configuration.Email, + When: time.Now().Local(), + }, + }) + CheckIfError(err) + + err = repo.Push(&git.PushOptions{ + Auth: &githttp.BasicAuth{ + Username: configuration.GitLogin, + Password: configuration.GitPassword, + }, + }) + CheckIfError(err) + +} + +func GetCurrentIp() string { + b, err := ioutil.ReadFile(path.Join(configPath,"data/templates/home_a.lua")) + CheckIfError(err) + + re := regexp.MustCompile("\"(.*?)\"") + match := re.FindStringSubmatch(string(b)) + return match[1] +} + +func SetCurrentIp(oldip string, newip string) { + b, err := ioutil.ReadFile(path.Join(configPath,"data/templates/home_a.lua")) + CheckIfError(err) + + s := string(b) + s = strings.Replace(s, oldip, newip, 1) + ioutil.WriteFile(path.Join(configPath,"data/templates/home_a.lua"), []byte(s), 0644) +} + +func CheckIfError(err error) { + if err == nil { + return + } + + fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err)) + os.Exit(1) +} + +func GetOutboundIP() string { + url := "https://diagnostic.opendns.com/myip" + req, err := http.NewRequest("GET", url, nil) + CheckIfError(err) + res, err := http.DefaultClient.Do(req) + CheckIfError(err) + defer res.Body.Close() + body, err := ioutil.ReadAll(res.Body) + CheckIfError(err) + return string(body) +} + +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 +} + +func Info(format string, args ...interface{}) { + fmt.Printf("\x1b[34;1m%s\x1b[0m\n", fmt.Sprintf(format, args...)) +}