From e5d00cfa23d443fca526eedb495fecdc872b32c2 Mon Sep 17 00:00:00 2001 From: Lukas Werner Date: Wed, 29 Apr 2026 12:52:37 -0700 Subject: [PATCH] feat: add notification function --- config_example.lua | 5 +++++ dns.d.lua | 1 + lua-libraries/http.go | 44 +++++++++++++++++++++++++++++++++++++++++++ lua.go | 9 +++++++++ process_cloudflare.go | 1 + 5 files changed, 60 insertions(+) diff --git a/config_example.lua b/config_example.lua index 4e20db6..4589d4a 100644 --- a/config_example.lua +++ b/config_example.lua @@ -20,5 +20,10 @@ dns.get_ipv4 = function() return "" end +dns.notify = function(domain, old, new) + local message = domain .. "has new ip: " .. new + http.post("https://ntfy.sh/test-topic-dynamic-dns", message) +end + -- Interval to update the DNS records dns.interval = 4 * time.duration.hour diff --git a/dns.d.lua b/dns.d.lua index 0f06382..73c212f 100644 --- a/dns.d.lua +++ b/dns.d.lua @@ -4,6 +4,7 @@ ---@field cloudflare table ---@field interval number ---@field get_ipv4 function +---@field notify function dns = {} diff --git a/lua-libraries/http.go b/lua-libraries/http.go index 12d6fdd..7ad07eb 100644 --- a/lua-libraries/http.go +++ b/lua-libraries/http.go @@ -1,6 +1,7 @@ package lualibraries import ( + "bytes" "io" "net/http" @@ -47,6 +48,49 @@ var httpFunctions = []lua.RegistryFunction{ return 2 }}, + + {Name: "post", Function: func(l *lua.State) int { + url := lua.CheckString(l, 1) + body_content := lua.CheckString(l, 2) + + body := bytes.NewBufferString(body_content) + + req, err := http.NewRequest("POST", url, body) + if err != nil { + lua.Errorf(l, "unable to build new request: %s", err.Error()) + return 0 + } + + if !l.IsNil(3) { + headers, err := util.PullStringTable(l, 3) + if err != nil { + lua.Errorf(l, "unable to acces headers table: %s", err.Error()) + return 0 + } + for key, value := range headers { + req.Header.Set(key, value) + } + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + l.PushString("") + l.PushInteger(400) + return 2 + } + defer resp.Body.Close() + b, err := io.ReadAll(resp.Body) + if err != nil { + l.PushString("") + l.PushInteger(400) + return 2 + } + + l.PushString(string(b)) + l.PushInteger(resp.StatusCode) + return 2 + + }}, } func LoadHTTP(l *lua.State, global bool) { diff --git a/lua.go b/lua.go index c4137e8..e6da0f5 100644 --- a/lua.go +++ b/lua.go @@ -36,6 +36,15 @@ func FetchConfigIPv4(l *lua.State) (string, error) { return ip, nil } +func Notify(l *lua.State, domain string, oldIP string, newIP string) { + l.Global("dns") + l.Field(-1, "notify") + l.PushString(domain) + l.PushString(oldIP) + l.PushString(newIP) + l.Call(2, 0) +} + func GetConfigInterval(l *lua.State) time.Duration { l.Global("dns") l.Field(-1, "interval") diff --git a/process_cloudflare.go b/process_cloudflare.go index 5b8a9b8..bea2fc7 100644 --- a/process_cloudflare.go +++ b/process_cloudflare.go @@ -82,6 +82,7 @@ func startCloudflareConfig(ctx context.Context, l *lua.State, config map[string] log.Printf("error changing record for %s: %s\n", record.Name, err.Error()) continue } + Notify(l, record.Name, record.Content, ipv4Address) records[i].Content = res.Content } } -- 2.51.2