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...)) }