diff --git a/cli/main.go b/cli/main.go index 3e8a75f4..a17486a4 100644 --- a/cli/main.go +++ b/cli/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "io/ioutil" + "net" "os" "github.com/go-git/go-git/v5" @@ -16,10 +17,12 @@ const ( useCaseFlagName = "use-case" repoRootFlagName = "repo-root" printResultOnlyFlagName = "result-only" + dynamicDnsDomainDefault = "viktorbarzin.ddns.net" + publicDomainDefault = "viktorbarzin.me" ) var ( - validUseCases = []string{"setup-vpn", setupOpenWRTDNSFlagName} + validUseCases = []string{vpnUseCaseFlagName, setupOpenWRTDNSFlagName, addEmailAliasUseCase} ) func main() { @@ -46,6 +49,10 @@ func run() error { emailToForwardTo := flag.String(emailAliasFlagName, "", "Email which is used to forward emails to.") fromDomain := flag.String(fromEmailDomainFlagName, "@viktorbarzin.me", "Domain name which will receive emails. Example @viktorbarzin.me") + // settings for updating the main domain using the dyndns domain + dynDnsDomain := flag.String(dynDnsDomainFlagName, dynamicDnsDomainDefault, "Dynamic DNS domain to check against - used to update the main domain") + publicDomain := flag.String(publicDomainFlagName, publicDomainDefault, "Public domain to update") + // Flag definitions above! flag.Parse() @@ -149,6 +156,50 @@ func run() error { return errors.Wrapf(err, "failed to push changes") } glog.Infof("successfully added %s -> %s email aliasing", emailAlias, *emailToForwardTo) + case updatePublicIPUseCaseFlagName: + // Resolve the dynamic dns record + publicDNSIps, err := net.LookupIP(*publicDomain) + if err != nil { + return errors.Wrapf(err, "failed to resolve IP addresses") + } + if len(publicDNSIps) < 1 { + return fmt.Errorf("no ips found for %s", *dynDnsDomain) + } + + // Resolve the dynamic dns record + dynamicDNSIps, err := net.LookupIP(*dynDnsDomain) + if err != nil { + return errors.Wrap(err, "failed to resolve IP addresses") + } + if len(dynamicDNSIps) < 1 { + return fmt.Errorf("no ips found for %s", *dynDnsDomain) + } + + currIP, newIP := publicDNSIps[0], dynamicDNSIps[0] + if currIP.Equal(newIP) { + glog.Infof("IPs of dyndns and current ip match, nothing to do: current=%s, dyndns=%s", currIP, newIP) + // return nil // TODO: uncomment + } + // setup git repo + gitFs, err := NewGitFS(repository) + if err != nil { + return errors.Wrapf(err, "failed to initialize git fs") + } + worktree, err := gitFs.repo.Worktree() + if err != nil { + return errors.Wrapf(err, "failed to get worktree") + } + err = updatePublicIP(gitFs, currIP, newIP) + if err != nil { + return fmt.Errorf("failed to update public ip: %w", err) + } + // // commit changes + if _, err = worktree.Commit("Update public ip and ns records", &git.CommitOptions{All: true, Author: &object.Signature{Name: "Webhook Handler Bot"}}); err != nil { + return errors.Wrapf(err, "failed to commit") + } + if err = gitFs.Push(); err != nil { + return errors.Wrapf(err, "failed to push changes") + } default: err = errors.New(fmt.Sprintf("unsupported use case: %s", *useCase)) } diff --git a/cli/update_viktorbarzin_me.go b/cli/update_viktorbarzin_me.go new file mode 100644 index 00000000..5bf6e390 --- /dev/null +++ b/cli/update_viktorbarzin_me.go @@ -0,0 +1,32 @@ +package main + +import ( + "io/ioutil" + "net" + "os" + + "github.com/pkg/errors" +) + +const ( + dynDnsDomainFlagName = "dynamic-domain" + publicDomainFlagName = "public-domain" + updatePublicIPUseCaseFlagName = "kek" + + tfvarsFileRelative = "/terraform.tfvars" +) + +func updatePublicIP(gitFs *GitFS, currIp, newIp net.IP) error { + println(currIp.String()) + println(newIp.String()) + + f, err := (*gitFs.fs).OpenFile(tfvarsFileRelative, os.O_RDONLY, 0644) + defer f.Close() + if err != nil { + return errors.Wrapf(err, "failed to open tfvars file: %s", tfvarsFileRelative) + } + bytes, err := ioutil.ReadAll(f) + println(string(bytes)) + + return errors.New("test") +}