Compare commits
3 Commits
main
...
features/y
Author | SHA1 | Date | |
---|---|---|---|
b6b7b14388 | |||
ae06bb6c7e | |||
d676134ce3 |
@ -1,13 +1,10 @@
|
|||||||
FROM golang:1.19 as build
|
FROM golang:1.20 as build
|
||||||
|
|
||||||
COPY . /app
|
COPY . /app
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
RUN go build .
|
RUN go build .
|
||||||
|
|
||||||
FROM alpine:latest as app
|
FROM debian:latest as app
|
||||||
|
|
||||||
RUN apk --no-cache add bash libc6-compat && \
|
|
||||||
mkdir /app
|
|
||||||
|
|
||||||
COPY --from=build /app/ddns /app
|
COPY --from=build /app/ddns /app
|
||||||
|
|
||||||
|
68
config.go
68
config.go
@ -6,8 +6,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -17,6 +19,13 @@ const (
|
|||||||
ConfigHosts = "HOSTS"
|
ConfigHosts = "HOSTS"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ConfigModel struct {
|
||||||
|
Email string `yaml:"Email"`
|
||||||
|
Token string `yaml:"Token"`
|
||||||
|
Domain string `yaml:"Domain"`
|
||||||
|
Hosts []string `yaml:"Hosts"`
|
||||||
|
}
|
||||||
|
|
||||||
type ConfigClient struct{}
|
type ConfigClient struct{}
|
||||||
|
|
||||||
func NewConfigClient() ConfigClient {
|
func NewConfigClient() ConfigClient {
|
||||||
@ -26,6 +35,35 @@ func NewConfigClient() ConfigClient {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cc *ConfigClient) LoadConfig() ConfigModel {
|
||||||
|
// load yaml first
|
||||||
|
|
||||||
|
model, err := LoadYaml("config.yaml")
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// refresh env to make sure its current
|
||||||
|
cc.RefreshEnv()
|
||||||
|
|
||||||
|
// if no domains pulled from yaml, load from env
|
||||||
|
if len(model.Hosts) == 0 {
|
||||||
|
envHosts := cc.GetConfig(ConfigHosts)
|
||||||
|
model.Hosts = append(model.Hosts, strings.Split(envHosts, ",")...)
|
||||||
|
}
|
||||||
|
if model.Domain == "" {
|
||||||
|
model.Domain = cc.GetConfig(ConfigDomain)
|
||||||
|
}
|
||||||
|
if model.Email == "" {
|
||||||
|
model.Email = cc.GetConfig(ConfigEmail)
|
||||||
|
}
|
||||||
|
if model.Token == "" {
|
||||||
|
model.Token = cc.GetConfig(ConfigToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
return model
|
||||||
|
}
|
||||||
|
|
||||||
func (cc *ConfigClient) GetConfig(key string) string {
|
func (cc *ConfigClient) GetConfig(key string) string {
|
||||||
res, filled := os.LookupEnv(key)
|
res, filled := os.LookupEnv(key)
|
||||||
if !filled {
|
if !filled {
|
||||||
@ -70,3 +108,33 @@ func loadEnvFile() {
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cc *ConfigClient) IsConfigInCurrentDirectory(fileName string) error {
|
||||||
|
_, err := os.Stat(fileName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadYaml(yamlFile string) (ConfigModel, error) {
|
||||||
|
var results = ConfigModel{}
|
||||||
|
|
||||||
|
// check for the config in the current directory
|
||||||
|
_, err := os.Stat(yamlFile)
|
||||||
|
if err != nil {
|
||||||
|
return ConfigModel{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := os.ReadFile(yamlFile)
|
||||||
|
if err != nil {
|
||||||
|
return ConfigModel{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = yaml.Unmarshal(content, &results)
|
||||||
|
if err != nil {
|
||||||
|
return ConfigModel{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
7
cron.go
7
cron.go
@ -47,7 +47,8 @@ func (c cronClient) RunCloudflareCheck(ApiToken string, Email string, Domain str
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if dns.Result[0].Content != currentIp {
|
var result = dns.Result[0]
|
||||||
|
if result.Content != currentIp {
|
||||||
log.Println("IP Address no longer matches, sending an update")
|
log.Println("IP Address no longer matches, sending an update")
|
||||||
err = cf.UpdateDnsEntry(domainDetails.Result[0].ID, dns, currentIp)
|
err = cf.UpdateDnsEntry(domainDetails.Result[0].ID, dns, currentIp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,3 +58,7 @@ func (c cronClient) RunCloudflareCheck(ApiToken string, Email string, Domain str
|
|||||||
}
|
}
|
||||||
log.Println("Done!")
|
log.Println("Done!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c cronClient) HelloWorldJob() {
|
||||||
|
log.Print("Hello World")
|
||||||
|
}
|
||||||
|
2
go.mod
2
go.mod
@ -6,3 +6,5 @@ require (
|
|||||||
github.com/joho/godotenv v1.4.0
|
github.com/joho/godotenv v1.4.0
|
||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
3
go.sum
3
go.sum
@ -2,3 +2,6 @@ github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
|||||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
29
main.go
29
main.go
@ -5,7 +5,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -38,33 +37,36 @@ func GetCurrentIpAddress() (string, error) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config := NewConfigClient()
|
config := NewConfigClient()
|
||||||
email := config.GetConfig(ConfigEmail)
|
cfg := config.LoadConfig()
|
||||||
if email == "" {
|
|
||||||
|
if cfg.Email == "" {
|
||||||
log.Println("Unable to find 'EMAIL' env value.")
|
log.Println("Unable to find 'EMAIL' env value.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := config.GetConfig(ConfigToken)
|
if cfg.Token == "" {
|
||||||
if token == "" {
|
|
||||||
log.Println("Unable to find 'API_TOKEN' env value.")
|
log.Println("Unable to find 'API_TOKEN' env value.")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
domain := config.GetConfig(ConfigDomain)
|
if cfg.Domain == "" {
|
||||||
if token == "" {
|
|
||||||
log.Println("Unable to find 'DOMAIN' env value.")
|
log.Println("Unable to find 'DOMAIN' env value.")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hosts := config.GetConfig(ConfigHosts)
|
if len(cfg.Hosts) == 0 {
|
||||||
if token == "" {
|
|
||||||
log.Println("Unable to find 'HOSTS' env value.")
|
log.Println("Unable to find 'HOSTS' env value.")
|
||||||
}
|
}
|
||||||
hostsArray := strings.Split(hosts, ",")
|
|
||||||
log.Println("Env Check: OK")
|
log.Println("Config Check: OK")
|
||||||
|
|
||||||
cron := NewCron()
|
cron := NewCron()
|
||||||
log.Println("Cloudflare Check will run every 15 minutes.")
|
log.Println("Cloudflare Check will run every 15 minutes.")
|
||||||
cron.scheduler.AddFunc("0,15,30,45 * * * *", func() {
|
cron.scheduler.AddFunc("0/5 * * * *", func() {
|
||||||
cron.RunCloudflareCheck(token, email, domain, hostsArray)
|
cron.RunCloudflareCheck(cfg.Token, cfg.Email, cfg.Domain, cfg.Hosts)
|
||||||
|
})
|
||||||
|
cron.scheduler.AddFunc("0/1 * * * *", func() {
|
||||||
|
cron.HelloWorldJob()
|
||||||
})
|
})
|
||||||
cron.scheduler.Start()
|
cron.scheduler.Start()
|
||||||
|
|
||||||
@ -74,4 +76,3 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user